File: core.c

package info (click to toggle)
proxychains 3.1-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,668 kB
  • ctags: 253
  • sloc: sh: 10,464; ansic: 1,012; makefile: 35
file content (733 lines) | stat: -rw-r--r-- 17,561 bytes parent folder | download | duplicates (3)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/***************************************************************************
                          core.c  -  description
                             -------------------
    begin                : Tue May 14 2002
    copyright          :  netcreature (C) 2002
    email                 : netcreature@users.sourceforge.net
 ***************************************************************************/
/*     GPL */
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
#include <netdb.h>

#include <sys/utsname.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>
#include "core.h"

extern int tcp_read_time_out;
extern int tcp_connect_time_out;
extern int proxychains_quiet_mode;

static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static void encode_base_64(char* src,char* dest,int max_len)
{
	int n,l,i;
 	l=strlen(src);
	max_len=(max_len-1)/4;
	for ( i=0;i<max_len;i++,src+=3,l-=3)
	{
		switch (l) {
		case 0:
			break;
		case 1:
			n=src[0] << 16;
			*dest++=base64[(n >> 18) & 077];
			*dest++=base64[(n >> 12) & 077];
			*dest++='=';
			*dest++='=';
			break;
		case 2:
			n=src[0] << 16 | src[1] << 8;
			*dest++=base64[(n >> 18) & 077];
			*dest++=base64[(n >> 12) & 077];
			*dest++=base64[(n >> 6) & 077];
			*dest++='=';
			break;
		default:
			n=src[0] << 16 | src[1] << 8 | src[2];
			*dest++=base64[(n >> 18) & 077];
			*dest++=base64[(n >> 12) & 077];
			*dest++=base64[(n >> 6) & 077];
			*dest++=base64[n & 077];
		}
		if (l<3) break;
	}
	*dest++=0;
}

#define LOG_BUFF 1024*20

int proxychains_write_log(char *str,...)
{
    char buff[LOG_BUFF];
    va_list arglist;
    FILE * log_file;
    log_file=stderr;
    if (!proxychains_quiet_mode)
    {
        va_start(arglist,str);
        vsprintf(buff,str,arglist);
        va_end(arglist);
        fprintf(log_file,"%s",buff);
        fflush(log_file);
    }

        return EXIT_SUCCESS;
}

static int write_n_bytes(int fd,char *buff,size_t size)
{
  int i=0,wrote=0;
  for(;;)
  {
    i=write(fd,&buff[wrote],size-wrote);
    if(i<=0)
         return i;
    wrote+=i;
    if(wrote==size)
         return wrote;
  }
}

static int read_line(int fd, char *buff, size_t size)
{
  int i,ready;
  struct pollfd pfd[1];

  pfd[0].fd=fd;
  pfd[0].events=POLLIN;
  for(i=0;i<size-1;i++)
  {
    pfd[0].revents=0;
    ready=poll(pfd,1,tcp_read_time_out);
    if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1))
      return -1;
    else if(buff[i]=='\n')
    {
        buff[i+1]=0;
        return (i+1);
    }
  }
  return -1;
}

static int read_n_bytes(int fd,char *buff, size_t size)
{
  int i,ready;
  struct pollfd pfd[1];

  pfd[0].fd=fd;
  pfd[0].events=POLLIN;
  for(i=0;i<size;i++)
  {
    pfd[0].revents=0;
    ready=poll(pfd,1,tcp_read_time_out);
    if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1))
      return -1;
  }
  return size;
}

static int timed_connect(int sock, const struct sockaddr *addr, unsigned int len)
{
	int ret,value,value_len;
 	struct pollfd pfd[1];

	pfd[0].fd=sock;
	pfd[0].events=POLLOUT;	
	fcntl(sock, F_SETFL, O_NONBLOCK);
  	ret=true_connect(sock, addr,  len);
//	printf("\nconnect ret=%d\n",ret);fflush(stdout);
  	if(ret==-1 && errno==EINPROGRESS)
   	{
    		ret=poll(pfd,1,tcp_connect_time_out);
//      		printf("\npoll ret=%d\n",ret);fflush(stdout);
      		if(ret==1)
        	{
           		value_len=sizeof(int);
             		getsockopt(sock,SOL_SOCKET,SO_ERROR,&value,&value_len) ;
//             		printf("\nvalue=%d\n",value);fflush(stdout);
               	if(!value)
               		ret=0;
                 	else
                  		ret=-1;
           	}
              else
              	ret=-1;
       }
       else if (ret==0)
			;		
		else
	       	ret=-1;
       	

       fcntl(sock, F_SETFL, !O_NONBLOCK);
       return ret;
}

static int tunnel_to(int sock, unsigned int ip, unsigned short port, proxy_type pt,char *user,char *pass)
{
        int len;
        char buff[BUFF_SIZE];
        bzero (buff,sizeof(buff));
        switch(pt)
        {
        	case HTTP_TYPE:
         		{
             		sprintf(buff,"CONNECT %s:%d HTTP/1.0\r\n",
			        inet_ntoa( * (struct in_addr *) &ip),
			        ntohs(port));
           			if (user[0])
                		{
					char src[256];
     					char dst[512];
					strcpy(src,user);
					strcat(src,":");
					strcat(src,pass);
					encode_base_64(src,dst,512);
					strcat(buff,"Proxy-Authorization: Basic ");
					strcat(buff,dst);
					strcat(buff,"\r\n\r\n");
				}
    				else
					strcat(buff,"\r\n");
			
           			len=strlen(buff);

			        if(len!=send(sock,buff,len,0))
			                return SOCKET_ERROR;
			
           			bzero(buff,sizeof(buff));
                        len=0 ;
      			 // read header byte by byte.
			       while(len<BUFF_SIZE)
			       {
			                if(1==read_n_bytes(sock,buff+len,1))
			                        len++;
			                else
			                        return SOCKET_ERROR;
			                if (    len > 4     &&
		                        	buff[len-1]=='\n'  &&
			                        buff[len-2]=='\r'  &&
			                        buff[len-3]=='\n'  &&
			                        buff[len-4]=='\r'  )
		                        break;
			       }

			       // if not ok (200) or response greather than BUFF_SIZE return BLOCKED;
			       if (     (len==BUFF_SIZE)  ||
			                ! (     buff[9] =='2'         &&
			                        buff[10]=='0'        &&
			                        buff[11]=='0'         ))
                                  return BLOCKED;
			       return SUCCESS;
           		}
            	break;
            case SOCKS4_TYPE:
            	{
               		memset(buff,0,sizeof(buff));
                 		buff[0]=4; // socks version
  				buff[1]=1; // connect command
				memcpy(&buff[2],&port,2); // dest port
				memcpy(&buff[4],&ip,4); // dest host
				len=strlen(user)+1; // username
    				if(len>1)	
         				strcpy(&buff[8],user);
				if((len+8)!=write_n_bytes(sock,buff,(8+len)))
					return SOCKET_ERROR;

 				if(8!=read_n_bytes(sock,buff,8))
					return SOCKET_ERROR;
            	
				if (buff[0]!=0||buff[1]!=90)
					return BLOCKED;
     				
         			return SUCCESS;
               	}
                	break;
            case SOCKS5_TYPE:
            	{
               		if(user)
                 		{
                 			buff[0]=5;   //version
					buff[1]=2;	//nomber of methods
					buff[2]=0;   // no auth method
	    				buff[3]=2;  /// auth method -> username / password
                              if(4!=write_n_bytes(sock,buff,4))
					 	return SOCKET_ERROR;
       			}
            		else
                		{
            			buff[0]=5;   //version
					buff[1]=1;	//nomber of methods
					buff[2]=0;   // no auth method
                              if(3!=write_n_bytes(sock,buff,3))
					 	return SOCKET_ERROR;
       			}

				memset(buff,0,sizeof(buff));

				if(2!=read_n_bytes(sock,buff,2))
			 		return SOCKET_ERROR;
			
      			if (buff[0]!=5||(buff[1]!=0&&buff[1]!=2))
         			{
        				if((buff[0]==0x05)&&(buff[1]==(char)0xFF))
             						return BLOCKED;
						else
							return SOCKET_ERROR;
          			}
          			
          			if (buff[1]==2)
               		{
					// authentication
					char in[2];
     					char out[515]; char* cur=out;
					int c;
     					*cur++=1; // version
					c=strlen(user);
					*cur++=c;
					strncpy(cur,user,c);
					cur+=c;
					c=strlen(pass);
					*cur++=c;
					strncpy(cur,pass,c);
					cur+=c;
					
     					if((cur-out)!=write_n_bytes(sock,out,cur-out))
					 	return SOCKET_ERROR;
     					
          				
					if(2!=read_n_bytes(sock,in,2))
			 			return SOCKET_ERROR;
					if(in[0]!=1||in[1]!=0)
       				{
						if(in[0]!=1)
      						return SOCKET_ERROR;
						else
      						return BLOCKED;
					}
				}	

     				buff[0]=5;       // version
				buff[1]=1;       // connect
				buff[2]=0;       // reserved
				buff[3]=1;       // ip v4

			 	memcpy(&buff[4],&ip,4); // dest host
				memcpy(&buff[8],&port,2); // dest port
				

			      if(10!=write_n_bytes(sock,buff,10))
					return SOCKET_ERROR;
		
			      if(4!=read_n_bytes(sock,buff,4))
					return SOCKET_ERROR;

				if (buff[0]!=5||buff[1]!=0)
			      	return SOCKET_ERROR;

			  	switch (buff[3])
			      {
					case 1: len=4;  break;
					case 4: len=16; break;
					case 3: len=0;
			  			if(1!=read_n_bytes(sock,(char*)&len,1))
			 				return SOCKET_ERROR;
        					break;
					default:
						return SOCKET_ERROR;
				}

     				if((len+2)!=read_n_bytes(sock,buff,(len+2)))
					return SOCKET_ERROR;

				return SUCCESS;
                	}
                	break;	

        }

return SOCKET_ERROR;
}

static int start_chain(int *fd, proxy_data *pd, char* begin_mark)
{
	struct sockaddr_in addr;
	
	*fd=socket(PF_INET,SOCK_STREAM,0);
	if(*fd==-1)
		goto error;
	
	proxychains_write_log("%s-<>-%s:%d-",
				begin_mark,
				inet_ntoa(*(struct in_addr*)&pd->ip),
				htons(pd->port));
	pd->ps=PLAY_STATE;
	bzero(&addr,sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = pd->ip;
	addr.sin_port = pd->port;
	if (timed_connect (*fd ,(struct sockaddr*)&addr,sizeof(addr))) {
		pd->ps=DOWN_STATE;
		goto error1;
	}
	pd->ps=BUSY_STATE;
	return SUCCESS;
error1:
	proxychains_write_log("<--timeout\n");
error:
	if(*fd!=-1)
		close(*fd);
	return SOCKET_ERROR;
}

static proxy_data * select_proxy(select_type how, 
		proxy_data *pd, int proxy_count, int *offset)
{
	int i=0,k=0;
	if(*offset>=proxy_count)
		return NULL;
	switch(how) {
		case RANDOMLY:
			srand(time(NULL));
			do {
				k++;
				i = 0 + (int) (proxy_count*1.0*rand()/
						(RAND_MAX+1.0));
			} while (pd[i].ps!=PLAY_STATE && k<proxy_count*100 );
		break;
		case FIFOLY:
			for(i=*offset;i<proxy_count;i++) {
				if(pd[i].ps==PLAY_STATE) {
					*offset=i;
					break;
				}
			}
		default:
		break;
	}
	if (i>=proxy_count)
		i=0;
	return pd[i].ps==PLAY_STATE?&pd[i]:NULL;
}


static void release_all(proxy_data *pd, int proxy_count)
{
	int i;
	for(i=0;i<proxy_count;i++)
		pd[i].ps=PLAY_STATE;
	return;
}

static void release_busy(proxy_data *pd, int proxy_count)
{
	int i;
	for(i=0;i<proxy_count;i++)
		if(pd[i].ps==BUSY_STATE)
			pd[i].ps=PLAY_STATE;
	return;
}

static int calc_alive(proxy_data *pd, int proxy_count)
{
	int i;
	int alive_count=0;
	release_busy(pd,proxy_count);
	for(i=0;i<proxy_count;i++)
		if(pd[i].ps==PLAY_STATE)
			alive_count++;
	return alive_count;
}


static int chain_step(int ns, proxy_data *pfrom, proxy_data *pto)
{
	int retcode=-1;
	
	proxychains_write_log("<>-%s:%d-", 
			inet_ntoa(*(struct in_addr*)&pto->ip),
			htons(pto->port));
	retcode = 
		tunnel_to(ns, pto->ip, pto->port, pfrom->pt, pfrom->user, 
				pfrom->pass);
	switch(retcode) {
		case SUCCESS:
			pto->ps=BUSY_STATE;
			break;
		case BLOCKED:
			pto->ps=BLOCKED_STATE;
			proxychains_write_log("<--denied\n");
			close(ns);
			break;
		case SOCKET_ERROR:
			pto->ps=DOWN_STATE;
			proxychains_write_log("<--timeout\n");
			close(ns);
			break;
	}
	return retcode;
}

int connect_proxy_chain( int sock, unsigned int target_ip, 
		unsigned short target_port, proxy_data *pd, 
		unsigned int proxy_count, chain_type ct, int max_chain )
{
	proxy_data p4;
	proxy_data *p1,*p2,*p3;
	int ns=-1;
	int offset=0;
	int alive_count=0;
	int curr_len=0;

#define TP "<>"
#define DT "|D-chain|"
#define ST "|S-chain|"
#define RT "|R-chain|"
	
	p3=&p4;

again:
	switch(ct)  {
		case DYNAMIC_TYPE:
		alive_count=calc_alive(pd,proxy_count);
		offset=0;
		do {
			if(!(p1=select_proxy(FIFOLY,pd,proxy_count,&offset)))
				goto error_more;
		} while(SUCCESS!=start_chain(&ns,p1,DT) && offset<proxy_count);
		for(;;) {
			p2=select_proxy(FIFOLY,pd,proxy_count,&offset);
			if(!p2)
				break;
			if(SUCCESS!=chain_step(ns,p1,p2))
				goto again;
			p1=p2;
		}
		proxychains_write_log(TP);
		p3->ip=target_ip;
		p3->port=target_port;
		if(SUCCESS!=chain_step(ns,p1,p3))
			goto error;
		break;

	case STRICT_TYPE:
		alive_count=calc_alive(pd,proxy_count);
		offset=0;
		if(!(p1=select_proxy(FIFOLY,pd,proxy_count,&offset)))
			goto error_strict;
		if(SUCCESS!=start_chain(&ns,p1,ST))
			goto error_strict;
		while(offset<proxy_count) {
			if(!(p2=select_proxy(FIFOLY,pd,proxy_count,&offset)))
				break;
			if(SUCCESS!=chain_step(ns,p1,p2))
				goto error_strict;
			p1=p2;
		}
		proxychains_write_log(TP);
		p3->ip=target_ip;
		p3->port=target_port;
		if(SUCCESS!=chain_step(ns,p1,p3))
			goto error;
		break;
		
	case RANDOM_TYPE:
		alive_count=calc_alive(pd,proxy_count);
		if(alive_count<max_chain)
			goto error_more;
		curr_len=offset=0;
		do {
			if(!(p1=select_proxy(RANDOMLY,pd,proxy_count,&offset)))
				goto error_more;
		} while(SUCCESS!=start_chain(&ns,p1,RT) && offset<max_chain);
		while(++curr_len<max_chain) {
			if(!(p2=select_proxy(RANDOMLY,pd,proxy_count,&offset)))
				goto error_more;
			if(SUCCESS!=chain_step(ns,p1,p2))
				goto again;
			p1=p2;
		}
		proxychains_write_log(TP);
		p3->ip=target_ip;
		p3->port=target_port;
		if(SUCCESS!=chain_step(ns,p1,p3))
			goto error;
			
	}

done:
	proxychains_write_log("<><>-OK\n");
	dup2(ns,sock);
	close(ns);
	return 0;
error:
	if(ns!=-1)
		close(ns);
	errno = ECONNREFUSED;  // for nmap ;)
	return -1;
	
error_more:
	proxychains_write_log("\n!!!need more proxies!!!\n");
error_strict:
	release_all(pd,proxy_count);
	if(ns!=-1)
		close(ns);
	errno = ETIMEDOUT;
	return -1;
}

static struct hostent hostent_space;
static in_addr_t resolved_addr;
static char* resolved_addr_p;
static char addr_name[1024*8];
struct hostent* proxy_gethostbyname(const char *name)
{
	int pipe_fd[2];
	char buff[256];
	in_addr_t addr;
	pid_t pid;
	int status;
	struct hostent* hp;

	hostent_space.h_addr_list = &resolved_addr_p;
	*hostent_space.h_addr_list = (char*)&resolved_addr;
	resolved_addr = 0;
	
	gethostname(buff,sizeof(buff));
	if(!strcmp(buff,name))
		goto got_buff;

	bzero(buff,sizeof(buff));
	
	// TODO: this works only once, so cache it  ...
	// 	 later
	while (hp=gethostent())
		if (!strcmp(hp->h_name,name)) 
			return hp; 
	
	if(pipe(pipe_fd))
		goto err;
	pid = fork();
	switch(pid) {
	
		case 0: // child
			proxychains_write_log("|DNS-request| %s \n", name);
			dup2(pipe_fd[1],1);
			//dup2(pipe_fd[1],2);
		//	putenv("LD_PRELOAD=");
			execl("/usr/lib/proxychains3/proxyresolv","proxyresolv",name,NULL);
			perror("can't exec proxyresolv");
			exit(2);

		case -1: //error
			close(pipe_fd[0]);
			close(pipe_fd[1]);
			perror("can't fork");
			goto err;
		
		default:
			close(pipe_fd[1]);
			waitpid(pid, &status, 0);
			read(pipe_fd[0],&buff,sizeof(buff));
			close(pipe_fd[0]);
got_buff:
			addr = inet_addr(buff);
			if (addr == -1)
				goto err_dns;
			memcpy(*(hostent_space.h_addr_list),
						&addr ,sizeof(struct in_addr));
			hostent_space.h_name = addr_name;
			hostent_space.h_length = sizeof (in_addr_t);
	}
	proxychains_write_log("|DNS-response| %s is %s\n",
			name, inet_ntoa(*(struct in_addr*)&addr));
	return &hostent_space;
err_dns:
	proxychains_write_log("|DNS-response|: %s does not exist\n", name);
err:
	return NULL;
}
int proxy_getaddrinfo(const char *node, const char *service,
		const struct addrinfo *hints,
		struct addrinfo **res)
{
	struct servent *se = NULL;
	struct hostent *hp = NULL;
	struct sockaddr* sockaddr_space = NULL;
	struct addrinfo*  addrinfo_space = NULL;
	
//	printf("proxy_getaddrinfo node %s service %s\n",node,service);
	addrinfo_space = malloc(sizeof(struct addrinfo));
	if(!addrinfo_space)
		goto err1;
	sockaddr_space = malloc(sizeof(struct sockaddr));
	if(!sockaddr_space)
		goto err2;
	bzero(sockaddr_space, sizeof(*sockaddr_space));
	bzero(addrinfo_space, sizeof(*addrinfo_space));
	if (node &&
	    !inet_aton(node,&((struct sockaddr_in*)sockaddr_space)->sin_addr)) {
		hp = proxy_gethostbyname(node);
		if (hp) 
			memcpy(&((struct sockaddr_in*)sockaddr_space)->sin_addr,
				*(hp->h_addr_list),
				sizeof(in_addr_t));
		else
			goto err3;
	}
	if (service)
		se = getservbyname(service, NULL);
	
	if (!se) {
		((struct sockaddr_in*)sockaddr_space)->sin_port = 
			htons(atoi(service?:"0"));
	} else 
		((struct sockaddr_in*)sockaddr_space)->sin_port = se->s_port;

	*res = addrinfo_space;
	(*res)->ai_addr = sockaddr_space;
	if (node)
		strcpy(addr_name, node);
	(*res)->ai_canonname = addr_name;
	(*res)->ai_next = NULL;
	(*res)->ai_family = sockaddr_space->sa_family = AF_INET;
	(*res)->ai_socktype = hints->ai_socktype;
	(*res)->ai_flags = hints->ai_flags;
	(*res)->ai_protocol = hints->ai_protocol;
	(*res)->ai_addrlen = sizeof(*sockaddr_space);
	goto out;
err3:
	free(sockaddr_space);
err2:
	free(addrinfo_space);
err1:
	return 1;
out:
	return 0;
}