File: socks_wrfz.c

package info (click to toggle)
socks4-server 4.3.beta2-13
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,512 kB
  • ctags: 1,778
  • sloc: ansic: 19,305; makefile: 404; sh: 52
file content (114 lines) | stat: -rw-r--r-- 2,785 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
/* socks_wrfz */

#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_wrfz(fc, confAddr, Nconf, useSyslog)
char *fc;
struct config *confAddr;
int Nconf;
int useSyslog;
{
	char *stringarea, *p;
	char *startaddr;
	int stringsize = 0;
	int i;
	struct config *cp;
	int fd;
	
	for (i = 0, cp = confAddr; i++ < Nconf; cp++) {
		if (cp->userlist != NULL)
			stringsize += strlen(cp->userlist) + 1;
		if (cp->serverlist != NULL)
			stringsize += strlen(cp->serverlist) + 1;
		if (cp->sdomain != NULL)
			stringsize += strlen(cp->sdomain) + 1;
		if (cp->ddomain != NULL)
			stringsize += strlen(cp->ddomain) + 1;
		if (cp->cmdp != NULL)
			stringsize += strlen(cp->cmdp) + 1;
	}

	if (stringsize != 0) {
		if ((stringarea = (char *)malloc(stringsize)) == NULL) {
			if (useSyslog)
				syslog(LOG_HIGH, "OUt of memory\n");
			else
				perror("socks_writefc(): malloc()");
			exit(1);
		}
		startaddr = stringarea - 1;
		p = stringarea;	
		for (i = 0, cp = confAddr; i++ < Nconf; cp++) {
			if (cp->userlist != NULL) {
				strcpy(p, cp->userlist);
				cp->userlist = (char *)(p - startaddr);
				p += strlen(p) + 1;
			}
			if (cp->serverlist != NULL) {
				strcpy(p, cp->serverlist);
				cp->serverlist = (char *)(p - startaddr);
				p += strlen(p) + 1;
			}
			if (cp->sdomain != NULL) {
				strcpy(p, cp->sdomain);
				cp->sdomain = (char *)(p - startaddr);
				p += strlen(p) + 1;
			}
			if (cp->ddomain != NULL) {
				strcpy(p, cp->ddomain);
				cp->ddomain = (char *)(p - startaddr);
				p += strlen(p) + 1;
			}
			if (cp->cmdp != NULL) {
				strcpy(p, cp->cmdp);
				cp->cmdp = (char *)(p - startaddr);
				p += strlen(p) + 1;
			}
		}
	}
	fd = creat(fc, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
	if (fd < 0) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: creat() %s: %m\n");
		else
			perror("socks_writefc(): creat()");
		exit(1);
	}
	if (write(fd, &Nconf, sizeof(Nconf)) != sizeof(Nconf)) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: write to %s: %m\n", fc);
		else
			perror("socks_writefc(): write()");
		exit(1);
	}
	if (write(fd, &stringsize, sizeof(stringsize)) != sizeof(stringsize)) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: write to %s: %m\n", fc);
		else
			perror("socks_writefc(): write()");
		exit(1);
	}
	if (write(fd, confAddr, Nconf * sizeof(struct config)) != (Nconf * sizeof(struct config))) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: write to %s: %m\n", fc);
		else
			perror("socks_writefc(): write()");
		exit(1);
	}
	if ((stringsize != 0) && (write(fd, stringarea, stringsize) != stringsize)) {
		if (useSyslog)
			syslog(LOG_HIGH, "Error: write to %s: %m\n", fc);
		else
			perror("socks_writefc(): write()");
		exit(1);
	}
	return 0;
}