File: sockd_rdroute.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 (144 lines) | stat: -rw-r--r-- 3,248 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
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
/* sockd_rdroute */


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <syslog.h>
#include "socks.h"

int sockd_rdroute(filename, rtAddrPtr, NrtPtr, useSyslog)
char *filename;
struct config **rtAddrPtr;
int *NrtPtr;
int useSyslog;
{
	FILE	*fd;
	static char	buf[1024];
	char	*bp;
	int	linenum = 0, permit;
	char	*argv[10];
	int	argc;
	int	next_arg;
	long	p;
	int Nrt = 0, maxrt = 0;
	struct config *rtAddr, *cp;
	int	has_error = 0;
	int	i;


	if ((fd = fopen(filename, "r")) == NULL) {
		if (useSyslog)
			syslog(LOG_HIGH, "Cannot open route file %s: %m\n", filename);
		else
			fprintf(stderr, "Cannot open route file %s: %m\n", filename);
		exit(1);
	}

	for (i = 0, cp = *rtAddrPtr; i++ < *NrtPtr; cp++) {
		if (cp->ddomain != NULL)
			free(cp->ddomain);
	}
	if (*rtAddrPtr)
		free(*rtAddrPtr);

	maxrt = CONF_INCR;
	rtAddr = (struct config *) malloc( maxrt * sizeof(struct config));
	if (rtAddr == NULL) {
		goto out_of_memory;
	}
	Nrt = 0;
	cp = rtAddr;

	while (fgets(buf, sizeof(buf) - 1, fd) != NULL) {
		linenum++;
		bzero(cp, sizeof(struct config));
		/*
		**  Comments start with a '#' anywhere on the line
		*/
		if ((bp = index(buf, '\n')) != NULL)
			*bp = '\0';
		for (bp = buf; *bp != '\0'; bp++) {
			if (*bp == ':') {
				*bp++ = '\0';
				cp->cmdp = strdup(bp);
				if (cp->cmdp == NULL)
					goto out_of_memory;
				break;
			} else if (*bp == '#') {
				*bp = '\0';
				break;
			} else if (*bp == '\t')
				*bp = ' ';
		}
		if (strlen(buf) == 0) continue;
		socks_mkargs(buf, &argc, argv, 10);
		if (argc == 0) {
			continue;
		}
		if (argc != 3) {
			if (useSyslog)
				syslog(LOG_HIGH, "Invalid entry at line %d in file %s", linenum, filename);
			else
				fprintf(stderr, "Invalid entry at line %d in file %s\n", linenum, filename);
			exit(1);
		}
		
		if (socks_GetQuad(argv[0], &cp->saddr) == -1) {
			if (useSyslog)
				syslog(LOG_HIGH, "illegal interface address at line %d in file %s", linenum, filename);
			else
				fprintf(stderr, "illegal interface address at line %d in file %s\n", linenum, filename);
			has_error = 1;
			continue;
		}
		if (socks_GetAddr(argv[1], &cp->daddr, &cp->ddomain) == -1){
			goto out_of_memory;
		}
		if (socks_GetQuad(argv[2], &cp->dmask) == -1) {
			if (useSyslog)
				syslog(LOG_HIGH, "illegal destination mask at line %d in file %s", linenum, filename);
			else
				fprintf(stderr, "illegal destination mask at line %d in file %s\n", linenum, filename);
			has_error = 1;
			continue;
		}

update_count:
		if (++Nrt >= maxrt) {
			maxrt += CONF_INCR;
			rtAddr = (struct config *) realloc(rtAddr, maxrt * sizeof(struct config));
		}
		if (rtAddr == NULL) {
			goto out_of_memory;
		}
		cp = rtAddr + Nrt;
	}
	fclose(fd);

	if (Nrt == 0) {
		if (useSyslog)
			syslog(LOG_HIGH, "No valid entires in file %s", filename);
		else
			fprintf(stderr, "No valid entires in file %s\n", filename);
		exit(1);
	}
	if (has_error)
		exit(1);
	if (Nrt < maxrt)
		rtAddr = (struct config *) realloc(rtAddr, Nrt * sizeof(struct config));
	*NrtPtr = Nrt;
	*rtAddrPtr = rtAddr;
	return 0;

out_of_memory:
	if (useSyslog)
		syslog(LOG_HIGH, "Out of memory\n");
	else
		fprintf(stderr, "Out of memory\n");
	exit(1);

}