summaryrefslogtreecommitdiff
path: root/src/sock.c
blob: bf0614e64cddf697ed6d01a5f0b937d58dd84cca (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* $Id: sock.c,v 1.2 2000-03-31 20:10:13 rjkaes Exp $
 *
 * Sockets are created and destroyed here. When a new connection comes in from
 * a client, we need to copy the socket and the create a second socket to the
 * remote server the client is trying to connect to. Also, the listening
 * socket is created and destroyed here. Sounds more impressive than it
 * actually is.
 *
 * Copyright (C) 1998  Steven Young
 * Copyright (C) 1999  Robert James Kaes (rjkaes@flarenet.com)
 * Copyright (C) 2000  Chris Lightfoot (chris@ex-parrot.com)
 *
 * 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, 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.
 */

#ifdef HAVE_CONFIG_H
#include <defines.h>
#endif

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <assert.h>

#include "tinyproxy.h"
#include "sock.h"
#include "log.h"
#include "utils.h"
#include "dnscache.h"

/* This routine is so old I can't even remember writing it.  But I do
 * remember that it was an .h file because I didn't know putting code in a
 * header was bad magic yet.  anyway, this routine opens a connection to a
 * system and returns the fd.
 */

/*
 * Cleaned up some of the code to use memory routines which are now the
 * default. Also, the routine first checks to see if the address is in
 * dotted-decimal form before it does a name lookup. Finally, the newly
 * created socket is made non-blocking.
 *      - rjkaes
 */
int opensock(char *ip_addr, int port)
{
	int sock_fd, flags;
	struct sockaddr_in port_info;

	assert(ip_addr);
	assert(port > 0);

	memset((struct sockaddr *) &port_info, 0, sizeof(port_info));

	port_info.sin_family = AF_INET;

	/* chris - Could block; neet to ensure that this is never called
	 * before a non-blocking DNS query happens for this address. Not
	 * relevant in the code as it stands.
	 */
	if (dnscache(&port_info.sin_addr, ip_addr) < 0) {
		log("ERROR opensock: Could not lookup address: %s", ip_addr);
		return -1;
	}

	port_info.sin_port = htons(port);

	if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		log("ERROR opensock: socket (%s)", strerror(errno));
		return -1;
	}

	flags = fcntl(sock_fd, F_GETFL, 0);
	fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);

	if (connect
	    (sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
		if (errno != EINPROGRESS) {
			log("ERROR opensock: connect (%s)", strerror(errno));
			return -1;
		}
	}

	return sock_fd;
}

/* chris - added this to open a socket given a struct in_addr */
int opensock_inaddr(struct in_addr *inaddr, int port)
{
	int sock_fd, flags;
	struct sockaddr_in port_info;

	assert(inaddr);
	assert(port > 0);

	memset((struct sockaddr *) &port_info, 0, sizeof(port_info));

	port_info.sin_family = AF_INET;

	memcpy(&port_info.sin_addr, inaddr, sizeof(struct in_addr));

	port_info.sin_port = htons(port);

	if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		log("ERROR opensock_inaddr: socket (%s)", strerror(errno));
		return -1;
	}

	flags = fcntl(sock_fd, F_GETFL, 0);
	fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);

	if (connect
	    (sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
		if (errno != EINPROGRESS) {
			log("ERROR opensock: connect (%s)", strerror(errno));
			return -1;
		}
	}

	return sock_fd;
}

int setup_fd;
static struct sockaddr listen_sock_addr;

/*
 * Start listening to a socket.
 */
int init_listen_sock(int port)
{
	struct sockaddr_in laddr;
	int i = 1;

	assert(port > 0);

	if ((setup_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		log("ERROR init_listen_sock: socket (%s)", strerror(errno));
		return -1;
	}

	if (setsockopt(setup_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) {
		log("ERROR init_listen_sock: setsockopt (%s)",
		    strerror(errno));
		return -1;
	}

	memset(&listen_sock_addr, 0, sizeof(listen_sock_addr));
	memset(&laddr, 0, sizeof(laddr));
	laddr.sin_family = AF_INET;
	laddr.sin_port = htons(port);

	if (config.ipAddr) {
		laddr.sin_addr.s_addr = inet_addr(config.ipAddr);
	} else {
		laddr.sin_addr.s_addr = inet_addr("0.0.0.0");
	}
	if (bind(setup_fd, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) {
		log("ERROR init_listen_sock: bind (%s)", strerror(errno));
		return -1;
	}
	if ((listen(setup_fd, MAXLISTEN)) != 0) {
		log("ERROR init_listen_sock: listen (%s)", strerror(errno));
		return -1;
	}

	return 0;
}

/*
 * Grab a pending connection
 */
int listen_sock(void)
{
	static int sock;
	int sz = sizeof(listen_sock_addr);

	if ((sock = accept(setup_fd, &listen_sock_addr, &sz)) < 0) {
		if (errno != ECONNABORTED
#ifdef EPROTO
		    && errno != EPROTO
#endif
#ifdef EWOULDBLOCK
		    && errno != EWOULDBLOCK
#endif
		    && errno != EINTR)
			log("ERROR listen_sock: accept (%s)", strerror(errno));
		return -1;
	}
	stats.num_listens++;

	return sock;
}

/*
 * Stop listening on a socket.
 */
void de_init_listen_sock(void)
{
	close(setup_fd);
}

/*
 * Takes a socket descriptor and returns the string contain the peer's
 * IP address.
 */
char *getpeer_ip(int fd, char *ipaddr)
{
	struct sockaddr_in name;
	int namelen = sizeof(name);

	assert(fd >= 0);
	assert(ipaddr);

	memset(ipaddr, '\0', PEER_IP_LENGTH);

	if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
		log("ERROR Connect: 'could not get peer name'");
	} else {
		strncpy(ipaddr,
			inet_ntoa(*(struct in_addr *) &name.sin_addr.s_addr),
			PEER_IP_LENGTH - 1);
	}

	return ipaddr;
}

/*
 * Takes a socket descriptor and returns the string containing the peer's
 * address.
 */
char *getpeer_string(int fd, char *string)
{
	struct sockaddr_in name;
	int namelen = sizeof(name);
	struct hostent *peername;

	assert(fd >= 0);
	assert(string);

	memset(string, '\0', PEER_STRING_LENGTH);

	if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
		log("ERROR Connect: 'could not get peer name'");
	} else {
		if (
		    (peername =
		     gethostbyaddr((char *) &name.sin_addr.s_addr,
				   sizeof(name.sin_addr.s_addr),
				   AF_INET)) != NULL) {
			strncpy(string, peername->h_name,
				PEER_STRING_LENGTH - 1);
		}
	}

	return string;
}


/*
 * Okay, this is a wacked out function. The basic gist is that we read in one
 * line from the socket and return it in "line". However, if we can't pull in
 * one complete line (up to an including the '\n') then we need to store it in
 * the buffer's "working_string". Fun. :)
 *	-- rjkaes
 */
int readline(int fd, struct buffer_s *buffer, char **line)
{
	char inbuf[BUFFER];
	int bytesin;
	char *endline = NULL;
	char *newline;
	unsigned long length;

	assert(fd >= 0);
	assert(buffer);
	assert(line);

	*line = NULL;

	/* Inspect the queue. */
	if ((bytesin = recv(fd, inbuf, BUFFER - 1, MSG_PEEK)) <= 0) {
		goto CONN_ERROR;
	}

	if (buffer->working_length == 0) {
		/* There is no working line, so read in a line of text. */

		/* Okay, check to see if there is a '\n' in this. */
		endline = xstrstr(inbuf, "\n", bytesin, FALSE);
		
		if (endline) {
			/* Yes, we have a complete line. */
			*(++endline) = '\0';
			length = strlen(inbuf);
			
			/* Actually pull the data off the queue */
			if ((bytesin = recv(fd, inbuf, length, 0) <= 0)) {
				goto CONN_ERROR;
			}

			*line = xstrdup(inbuf);
			return strlen(*line);
		}

		/*
		 * Well, we don't have a complete line, so add it to the
		 * working_string.
		 */
		if (!(buffer->working_string = xmalloc(bytesin))) {
			return -1;
		}

		if ((bytesin = recv(fd, inbuf, bytesin, 0)) <= 0) {
			safefree(buffer->working_string);
			goto CONN_ERROR;
		}

		memcpy(buffer->working_string, inbuf, bytesin);
		buffer->working_length = bytesin;

		return 0;
	}

	/* 
	 * Alright, we do have a working line, so read in more data and see
	 * if there is a '\n' in it.
	 */
	endline = xstrstr(inbuf, "\n", bytesin, FALSE);

	if (endline) {
		/* 
		 * Great, there was a "\n" found, so combine with
		 * working_string.
		 */
		*(++endline) = '\0';
		length = strlen(inbuf);

		if (!(*line = xmalloc(bytesin + buffer->working_length + 1))) {
			return -1;
		}

		/* Pull the data off */
		if ((bytesin = recv(fd, inbuf, bytesin, 0)) <= 0) {
			goto CONN_ERROR;
		}

		/* Copy all the data into a new line */
		memcpy(*line, buffer->working_string, buffer->working_length);
		memcpy(*line + buffer->working_length, inbuf, bytesin);

		*(*line + buffer->working_length + bytesin + 1) = '\0';
		
		safefree(buffer->working_string);
		buffer->working_length = 0;

		return strlen(*line);
	}

	/*
	 * Well, we have a working line and still don't have a complete line.
	 * Add the new data to the working line and return.
	 */
	if (!(newline = xmalloc(buffer->working_length + bytesin))) {
		return -1;
	}

	if ((bytesin = recv(fd, inbuf, bytesin, 0)) <= 0) {
		goto CONN_ERROR;
	}
	
	memcpy(newline, buffer->working_string, buffer->working_length);
	memcpy(newline + buffer->working_length, inbuf, bytesin);

	safefree(buffer->working_string);
	buffer->working_string = newline;
	buffer->working_length += bytesin;

	return 0;

	/* Handle all the errors a socket could produce. */
      CONN_ERROR:
	if (bytesin == 0 || (errno != EAGAIN && errno != EINTR)) {
		return -1;
	}
	return 0;
}