forked from robertswiecki/intrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.c
165 lines (142 loc) · 4.24 KB
/
threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* intrace
*
* Threads
*
* author: Robert Swiecki <robert@swiecki,net>
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
*/
#include <config.h>
#include <pthread.h>
#include <netdb.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <intrace.h>
// For setuid case only
static int threads_dropPrivs(void)
{
if (setuid(getuid()) == -1)
return errPrivs;
return errNone;
}
extern int h_errno;
static char *thread_err2asc(int err)
{
switch (err) {
case NETDB_INTERNAL:
return strerror(errno);
break;
case NETDB_SUCCESS:
return "No problem";
break;
case HOST_NOT_FOUND:
return "Authoritative Answer. Host not found";
break;
case TRY_AGAIN:
return "Non-Authoritative. Host not found, or SERVERFAIL";
break;
case NO_RECOVERY:
return "Non recoverable errors, FORMERR, REFUSED, NOTIMP";
break;
case NO_DATA:
return "No valid name, no data record of requested type";
break;
default:
return "Strange, shouldn't happen";
break;
}
return "?";
}
bool threads_resolveIP(intrace_t * intrace, const char *hostname)
{
struct hostent he;
struct hostent *hep;
char workspace[4096];
int err;
if (intrace->familyMode == ANY || intrace->familyMode == IPV6) {
gethostbyname2_r(hostname, AF_INET6, &he, workspace, sizeof(workspace), &hep, &err);
if (hep != NULL) {
memcpy(intrace->rip6.s6_addr, hep->h_addr, hep->h_length);
intrace->isIPv6 = true;
return true;
}
}
if (intrace->familyMode == ANY || intrace->familyMode == IPV4) {
gethostbyname2_r(hostname, AF_INET, &he, workspace, sizeof(workspace), &hep, &err);
if (hep != NULL) {
memcpy(&intrace->rip.s_addr, hep->h_addr, hep->h_length);
intrace->isIPv6 = false;
return true;
}
}
debug_printf(dlError, "Couldn't resolve '%s': '%s'\n", intrace->hostname,
thread_err2asc(h_errno));
return false;
}
int threads_process(intrace_t * intrace)
{
int err;
pthread_attr_t attr;
pthread_t t;
if (pthread_mutex_init(&intrace->mutex, NULL) != 0) {
debug_printf(dlFatal, "threads: Mutex initialization failed\n");
return errMutex;
}
debug_printf(dlDebug, "Resolving '%s'\n", intrace->hostname);
if (!threads_resolveIP(intrace, intrace->hostname)) {
debug_printf(dlFatal, "Resolving '%s' failed\n", intrace->hostname);
return errResolve;
}
char haddr[INET6_ADDRSTRLEN];
if (!inet_ntop(_IT_AF(intrace), _IT_RIP(intrace), haddr, sizeof(haddr))) {
debug_printf(dlFatal, "Cannot convert IP addr to a text form\n");
return errResolve;
}
debug_printf(dlDebug, "%s for '%s' resolved='%s'\n", _IT_IPSTR(intrace), intrace->hostname,
haddr);
if ((err = listener_init(intrace)) != errNone) {
debug_printf(dlFatal, "threads: Listener initialization failed, err=%d'\n", err);
return err;
}
if ((err = sender_init(intrace)) != errNone) {
debug_printf(dlFatal, "threads: Packet sender initialization failed, err=%d\n",
err);
return err;
}
if ((err = threads_dropPrivs()) != errNone) {
debug_printf(dlFatal, "threads: Couldn't drop privileges, err=%d\n", err);
return err;
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&t, &attr, listener_thr, (void *)intrace) < 0) {
debug_printf(dlFatal, "threads: Cannot create listener thread\n");
return errThread;
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&t, &attr, sender_thr, (void *)intrace) < 0) {
debug_printf(dlFatal, "threads: Cannot create sender thread\n");
return errThread;
}
display_process(intrace);
return errNone;
}