-
Notifications
You must be signed in to change notification settings - Fork 21
/
socket.c
283 lines (244 loc) · 6.26 KB
/
socket.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* These are socket routines for the main module of CNTLM
*
* CNTLM 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.
*
* CNTLM 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., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 David Kubicek
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <syslog.h>
#include "utils.h"
extern int debug;
/*
* gethostbyname() wrapper. Return 1 if OK, otherwise 0.
*/
int so_resolv(struct in_addr *host, const char *name) {
/*
struct hostent *resolv;
resolv = gethostbyname(name);
if (!resolv)
return 0;
memcpy(host, resolv->h_addr_list[0], resolv->h_length);
return 1;
*/
struct addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
int rc = getaddrinfo(name, NULL, &hints, &res);
if (rc != 0) {
if (debug)
printf("so_resolv: %s failed: %s (%d)\n", name, gai_strerror(rc), rc);
return 0;
}
if (debug)
printf("Resolve %s:\n", name);
int addr_set = 0;
for (p = res; p != NULL; p = p->ai_next) {
struct sockaddr_in *ad = (struct sockaddr_in*)(p->ai_addr);
if (ad == NULL) {
freeaddrinfo(res);
return 0;
}
if (!addr_set) {
memcpy(host, &ad->sin_addr, sizeof(ad->sin_addr));
addr_set = 1;
if (debug)
printf(" -> %s\n", inet_ntoa(ad->sin_addr));
} else
if (debug)
printf(" %s\n", inet_ntoa(ad->sin_addr));
}
freeaddrinfo(res);
return 1;
}
/*
* Connect to a host. Host is required to be resolved
* in the struct in_addr already.
* Returns: socket descriptor
*/
int so_connect(struct in_addr host, int port) {
int flags, fd, rc;
struct sockaddr_in saddr;
// struct timeval tv;
// fd_set fds;
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
if (debug)
printf("so_connect: create: %s\n", strerror(errno));
return -1;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr = host;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
if (debug)
printf("so_connect: get flags: %s\n", strerror(errno));
close(fd);
return -1;
}
/* NON-BLOCKING connect with timeout
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
if (debug)
printf("so_connect: set non-blocking: %s\n", strerror(errno));
close(fd);
return -1;
}
*/
rc = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
/*
printf("connect = %d\n", rc);
if (rc < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS)) {
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 10;
tv.tv_usec = 0;
printf("select!\n");
rc = select(fd+1, NULL, &fds, NULL, &tv) - 1;
printf("select = %d\n", rc);
}
*/
if (rc < 0) {
if (debug)
printf("so_connect: %s\n", strerror(errno));
close(fd);
return -1;
}
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
if (debug)
printf("so_connect: set blocking: %s\n", strerror(errno));
close(fd);
return -1;
}
return fd;
}
/*
* Bind the specified port and listen on it.
* Return socket descriptor if OK, otherwise 0.
*/
int so_listen(int port, struct in_addr source) {
struct sockaddr_in saddr;
int fd;
socklen_t clen;
fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
if (debug)
printf("so_listen: new socket: %s\n", strerror(errno));
close(fd);
return -1;
}
clen = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &clen, sizeof(clen));
memset((void *)&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = source.s_addr;
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr))) {
syslog(LOG_ERR, "Cannot bind port %d: %s!\n", port, strerror(errno));
close(fd);
return -1;
}
if (listen(fd, 5)) {
close(fd);
return -1;
}
return fd;
}
/*
* Return 1 if data is available on the socket,
* 0 if connection was closed
* -1 if error (errno is set)
*/
int so_recvtest(int fd) {
char buf;
int i;
#ifndef MSG_DONTWAIT
unsigned int flags;
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
i = recv(fd, &buf, 1, MSG_PEEK);
fcntl(fd, F_SETFL, flags);
#else
i = recv(fd, &buf, 1, MSG_DONTWAIT | MSG_PEEK);
#endif
return i;
}
/*
* Return true if there are some data on the socket
*/
int so_dataready(int fd) {
return so_recvtest(fd) > 0;
}
/*
* Reliable way of finding out whether a connection was closed
* on the remote end, without actually reading from it.
*/
int so_closed(int fd) {
int i;
if (fd == -1)
return 1;
i = so_recvtest(fd);
return (i == 0 || (i == -1 && errno != EAGAIN && errno != ENOENT)); /* ENOENT, you ask? Perhap AIX devels could explain! :-( */
}
/*
* Receive a single line from the socket. This is no super-efficient
* implementation, but more than we need to read in a few headers.
* What's more, the data is actually recv'd from a socket buffer.
*
* I had to time this in comparison to recv with block read :) and
* the performance was very similar. Given the fact that it keeps us
* from creating a whole buffering scheme around the socket (HTTP
* connection is both line and block oriented, switching back and forth),
* it is actually OK.
*/
int so_recvln(int fd, char **buf, int *size) {
int len = 0;
int r = 1;
char c = 0;
char *tmp;
while (len < *size-1 && c != '\n') {
r = read(fd, &c, 1);
if (r <= 0)
break;
(*buf)[len++] = c;
/*
* End of buffer, still no EOL? Resize the buffer
*/
if (len == *size-1 && c != '\n') {
if (debug)
printf("so_recvln(%d): realloc %d\n", fd, *size*2);
*size *= 2;
tmp = realloc(*buf, *size);
if (tmp == NULL)
return -1;
else
*buf = tmp;
}
}
(*buf)[len] = 0;
return r;
}