File: socks_rdfz.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 (106 lines) | stat: -rw-r--r-- 2,411 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
/* socks_rdfz */

#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 socks_rdfz(fc, cfAddrPtr, NcfPtr, cfstrings, useSyslog)
char *fc;
struct config **cfAddrPtr;
int *NcfPtr;
char **cfstrings;
int useSyslog;
{
	int fd, i;
	char *stringarea, *startaddr;
	int stringsize;
	int Ncf;
	struct config *cfAddr, *cp;

	if ((fd = open(fc, O_RDONLY)) < 0) {
		if (useSyslog)
			syslog(LOG_HIGH, "Cannot open %s: %m\n", fc);
		else
			perror("socks_rdfz(): open()");
		exit(1);
	}
	if (*cfAddrPtr != NULL)
		free(*cfAddrPtr);
	if (*cfstrings != NULL)
		free(*cfstrings);

	if (read(fd, &Ncf, sizeof(Ncf)) != sizeof(Ncf)) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: read from %s: %m\n", fc);
		else
			perror("socks_rdfz(): read()");
		exit(1);
	}

	if (read(fd, &stringsize, sizeof(stringsize)) != sizeof(stringsize)) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: read from %s: %m\n", fc);
		else
			perror("socks_rdfz(): read()");
		exit(1);
	}

	cfAddr = (struct config *) malloc(Ncf * (sizeof(struct config)));
	if (cfAddr == NULL) {
		if (useSyslog)
			syslog(LOG_HIGH, "Out of memory\n");
		else
			perror("socks_rdfz(): malloc()");
		exit(1);
	}
	if (read(fd, cfAddr, Ncf * sizeof(struct config)) != (Ncf * sizeof(struct config))) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: read from %s: %m\n", fc);
		else
			perror("socks_rdfz(): read()");
		exit(1);
	}
	*cfAddrPtr = cfAddr;
	*NcfPtr = Ncf;
	if (stringsize == 0) {
		close(fd);
		return 0;
	}
	if ((stringarea = (char *)malloc(stringsize)) == NULL) {
		if (useSyslog)
			syslog(LOG_HIGH, "Out of memory\n");
		else
			perror("socks_rdfz(): malloc()");
		exit(1);
	}
	*cfstrings = stringarea;
	if (read(fd, stringarea, stringsize) != stringsize) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: read from %s: %m\n", fc);
		else
			perror("socks_rdfz(): read()");
		exit(1);
	}

	startaddr = stringarea - 1;

	for (i = 0, cp = cfAddr; i++ < Ncf; cp++) {
		if (cp->userlist != NULL)
			cp->userlist = startaddr + (int)cp->userlist;
		if (cp->serverlist != NULL)
			cp->serverlist = startaddr + (int)cp->serverlist;
		if (cp->sdomain != NULL)
			cp->sdomain = startaddr + (int)cp->sdomain;
		if (cp->ddomain != NULL)
			cp->ddomain = startaddr + (int)cp->ddomain;
		if (cp->cmdp != NULL)
			cp->cmdp = startaddr + (int)cp->cmdp;
	}
	return 0;
}