File: SendGetDst.c

package info (click to toggle)
socks4-server 4.3.beta2-20
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,512 kB
  • ctags: 1,778
  • sloc: ansic: 19,305; makefile: 399; sh: 52
file content (114 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (9)
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
/* SendGetDst */

#include <sys/types.h>
#if defined(ISC)
#include <sys/bsdtypes.h>
#endif /* #if defined(ISC) */
#include <sys/time.h>
#include <syslog.h>
#include <errno.h>
#if defined(ISC)
#include <net/errno.h>
#endif /* #if defined(ISC) */
#include "socks.h"

#ifndef NULL
#define NULL	0L
#endif

int socks_SendDst(s, dst)
int	s;
Socks_t	*dst;
{
	char c[sizeof(Socks_t)];
	char *p = c;
	int i = sizeof(Socks_t), n, ret;
	fd_set	fds;
	int	fdsbits = s + 1;
	struct	timeval timeout;

	c[0] = dst->version;
	c[1] = dst->cmd;
	bcopy(&dst->port, c+2, sizeof(dst->port));
	bcopy(&dst->host, c+2+sizeof(dst->port), sizeof(dst->host));

	while ( i > 0) {
		FD_ZERO(&fds);
		FD_SET(s, &fds);
		timeout.tv_sec = 15;
		timeout.tv_usec = 0;
		if ((ret = select(fdsbits, NULL, &fds, NULL, &timeout)) == 0)
			continue;
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			else {
/*
				syslog(LOG_LOW, "select() in socks_SendDst(): %m");
*/
				return -1;
			}
		}
		if((n = write(s, p, i)) > 0) {
			p += n;
			i -= n;
		} else if ((n < 0) && ((errno == EWOULDBLOCK) || (errno == EINTR)))
			continue;
		else {
/*
			syslog(LOG_LOW, "write() in socks_SendDst(): %m");
*/
			return (-2);
		}
	}
	return 0;
}

int socks_GetDst(s, dst)
int	s;
Socks_t	*dst;
{
	char	c[sizeof(Socks_t)];
	char	*p = c;
	int	i = sizeof(Socks_t), n, ret;
	fd_set	fds;
	int	fdsbits = s + 1;
	struct	timeval timeout;

	while ( i > 0) {
		FD_ZERO(&fds);
		FD_SET(s, &fds);
		timeout.tv_sec = 15;
		timeout.tv_usec = 0;
		if ((ret = select(fdsbits, &fds, NULL, NULL, &timeout)) == 0)
			continue;
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			else {	
/*
				syslog(LOG_LOW, "select() in socks_GetDst(): %m");
*/
				return -1;
			}
		}
		if((n = read(s, p, i)) > 0) {
			p += n;
			i -= n;
		} else if ((n < 0) && ((errno == EWOULDBLOCK) || (errno == EINTR)))
			continue;
		else {
/*
			syslog(LOG_LOW, "read() in socks_GetDst(): %m");
*/
			return -2;
		}
	}

	dst->version = c[0];
	dst->cmd = c[1];
	bcopy(c+2, &dst->port, sizeof(dst->port));
	bcopy(c+2+sizeof(dst->port), &dst->host, sizeof(dst->host));
	return 0;
}