File: Rgethostbyname.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 (188 lines) | stat: -rw-r--r-- 3,980 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
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
/* Rgethostbynam.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#if defined(ISC)
#include <net/errno.h>
#endif /* #if defined(ISC) */
#include <stdio.h>
#include <netdb.h>
#include <syslog.h>
#if (defined(sun) && !defined(SOLARIS)) || defined(sgi)
#include <strings.h>
#else
#include <string.h>
#endif
#include "socks.h"

#if !defined(NULL)
#define NULL 0
#endif

extern int socks_useSyslog;

struct hostent socks_fakeIP[NUMFAKEIP];
static char *Fh_aliases[NUMFAKEIP], *Fh_addr_list[NUMFAKEIP*2];
static u_int32 F_iplist[NUMFAKEIP];

static struct hostent socks_Hostent[NUMHOSTENT];


static int initHostent()
{
	int i;
	char **faddr_list = Fh_addr_list;

	for (i = 0; i < NUMFAKEIP; i++) {
		socks_fakeIP[i].h_name = NULL;
		Fh_aliases[i] = NULL;
		socks_fakeIP[i].h_aliases = &Fh_aliases[i];
		socks_fakeIP[i].h_addrtype = AF_INET;
		socks_fakeIP[i].h_length = IPADDRLENG;
		socks_fakeIP[i].h_addr_list = faddr_list;
		*faddr_list++ = (char *)&F_iplist[i];
		F_iplist[i] = htonl(i+1);
		*faddr_list++ = 0;
		
	}

	for (i = 0; i < NUMHOSTENT; i++) {
		socks_Hostent[i].h_name = NULL;
		socks_Hostent[i].h_addrtype = AF_INET;
		socks_Hostent[i].h_length = IPADDRLENG;
	}

} /* initHostent */


struct hostent	*Rgethostbyname(name)
	char *name;
{
	static int initdone = 0;
	static int nextFIP = 0;
	static int currentFIP = 0;
	static int numFIP = 0;
	struct hostent *hr, *hp;
	int ipt, naliases, naddrs;
	char *iplist = NULL;
	char **iparray = NULL;
	char *halist = NULL;
	char **harray = NULL;
	char **p;
	char *q;
	int i, cl;
	static int nextHosetent = 0;
	static int numHostent = 0;
	static int currentHostent = 0;

	if (initdone == 0) {
		initHostent();
		initdone = 1;
	}

	for (ipt = currentHostent, i = 0; i < numHostent; i++) {
		hr = &socks_Hostent[ipt];
		if (strcasecmp(hr->h_name, name) == 0)
			return hr;
		if (--ipt < 0)
			ipt = NUMHOSTENT -1;
	}

	for (ipt = currentFIP, i = 0; i < numFIP; i++) {
		hr = &socks_fakeIP[ipt];
		if ( strcasecmp(hr->h_name, name) == 0)
			return hr;
		if (--ipt < 0)
			ipt = NUMFAKEIP -1;
	}

	if ((hp = gethostbyname(name)) != NULL)
		goto realIP;

	/* Return hostent address of a fake IP */
	if (++currentFIP >= NUMFAKEIP)
		currentFIP = 0;
	if (++numFIP >= NUMFAKEIP)
		numFIP = NUMFAKEIP;
	hr = &socks_fakeIP[currentFIP];
	if (hr->h_name != NULL) {
		free(hr->h_name);
	}
	if ((hr->h_name = strdup(name)) == NULL) {
		goto out_of_memory;
	}
	return hr;

realIP:
	/* Return hostent address of a real IP */
	if (++currentHostent >= NUMHOSTENT)
		currentHostent = 0;
	if (++numHostent >= NUMHOSTENT)
		numHostent = NUMHOSTENT;
	hr = &socks_Hostent[currentHostent];
	if (hr->h_name != NULL) {
		free(hr->h_name);
		harray = hr->h_aliases;
		if (*harray != NULL)
			free(*harray);
		free(harray);
		iparray = hr->h_addr_list;
		free(*iparray);
		free(iparray);
	}
	if ((hr->h_name = strdup(name)) == NULL) {
		goto out_of_memory;
	}
	naliases = 1;
	cl = 0;
	for (p = hp->h_aliases; *p != NULL; p++) {
		naliases++;
		cl += (strlen(*p) +1);
	}
	if (cl > 0) {
		if ((halist = (char *)malloc(cl)) == NULL) {
			goto out_of_memory;
		}
	}
	naddrs = 1;
	for (p = hp->h_addr_list; *p != NULL; p++) {
		naddrs++;
	}

	if (((harray = (char **)malloc(naliases * sizeof(iplist))) == NULL) ||
	    ((iparray = (char **)malloc(naddrs * sizeof(iplist))) == NULL) ||
	    ((iplist = (char *)malloc((naddrs - 1) * IPADDRLENG)) == NULL)) {
		goto out_of_memory;
	}
	
	hr->h_aliases = harray;
	for (p = hp->h_aliases; *p != NULL; p++) {
		*harray++ = halist;
		for (q = *p; *q != '\0'; )
			*halist++ = *q++;
		*halist++ = '\0';
	}
	*harray = NULL;

	hr->h_addr_list = iparray;
	for (p = hp->h_addr_list; *p != NULL; p++) {
		*iparray++ = iplist;
		q = *p;
		*iplist++ = *q++;
		*iplist++ = *q++;
		*iplist++ = *q++;
		*iplist++ = *q++;
	}
	*iparray = NULL;
	return hr;

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

}