-
Notifications
You must be signed in to change notification settings - Fork 91
/
proxy.c
824 lines (668 loc) · 22 KB
/
proxy.c
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#include "common.h"
/******************************************************************************
*
* proxy_node_new()
*
* Inputs: The proxy string comming from the command line request.
* The type of proxy we are setting up.
*
* Outputs: A pointer to a new proxy_node.
*
* Purpose: Initialize a new proxy_node.
*
******************************************************************************/
struct proxy_node *proxy_node_new(char *proxy_string, int proxy_type){
struct proxy_node *new_node;
char *first = NULL;
char *second = NULL;
char *third = NULL;
char *fourth = NULL;
unsigned int count;
char *tmp;
// First, quick syntax check.
count = 0;
tmp = proxy_string;
while((tmp = strchr(tmp, ':')) != NULL){
count++;
tmp++;
}
if((proxy_type == PROXY_DYNAMIC && !(count == 0 || count == 1)) \
|| (proxy_type == PROXY_STATIC && !(count == 2 || count == 3))){
report_error("proxy_node_new(): Improper port forward syntax for proxy type '%d': %s", proxy_type, proxy_string);
return(NULL);
}
// Now let's start setting up the nodes.
if((new_node = proxy_node_create()) == NULL){
report_error("proxy_node_new(): proxy_node_create(): %s", strerror(errno));
return(NULL);
}
new_node->proxy_type = proxy_type;
if((new_node->orig_request = (char *) calloc(strlen(proxy_string) + 1, sizeof(char))) == NULL){
report_error("proxy_node_new(): calloc(%d, sizeof(char)): %s", (int) strlen(proxy_string), strerror(errno));
free(new_node);
return(NULL);
}
memcpy(new_node->orig_request, proxy_string, strlen(proxy_string));
if((new_node->mem_ptr = (char *) calloc(strlen(proxy_string) + 1, sizeof(char))) == NULL){
report_error("proxy_node_new(): calloc(%d, sizeof(char)): %s", (int) strlen(proxy_string), strerror(errno));
free(new_node);
return(NULL);
}
first = new_node->mem_ptr;
// Proxy strings can have a lot of different formats. This block is where we figure out which format we're dealing with.
strcpy(first, proxy_string);
if((second = strchr(first, ':')) != NULL){
*(second++) = '\0';
}
if(proxy_type == PROXY_DYNAMIC){
if(second){
new_node->lhost = first;
new_node->lport = second;
}else{
new_node->lhost = DEFAULT_PROXY_ADDR;
new_node->lport = first;
}
} else if(proxy_type == PROXY_STATIC) {
if(!second || (third = strchr(second, ':')) == NULL){
report_error("proxy_node_new(): Malformed proxy string: %s", proxy_string);
goto CLEANUP;
}
if((fourth = strchr((third + 1) , ':')) != NULL){
new_node->lhost = first;
new_node->lport = second;
*(third++) = '\0';
new_node->rhost_rport = third;
}else{
new_node->lhost = DEFAULT_PROXY_ADDR;
new_node->lport = first;
new_node->rhost_rport = second;
}
} else {
goto CLEANUP;
}
if(proxy_listen(new_node) == -1){
report_error("proxy_node_new(): proxy_listen() failed. Skipping this proxy.");
goto CLEANUP;
}
new_node->origin = io->target;
new_node->id = new_node->fd;
return(new_node);
CLEANUP:
proxy_node_delete(new_node);
return(NULL);
}
/******************************************************************************
*
* proxy_listen()
*
* Inputs: A pointer to a proxy_node that we want to activate.
*
* Outputs: 0 on success, -1 otherwise.
*
* Purpose: Setup a new proxy listener.
*
******************************************************************************/
int proxy_listen(struct proxy_node *cur_proxy_node){
int yes = 1;
int rv, listener;
struct addrinfo hints, *ai, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((rv = getaddrinfo(cur_proxy_node->lhost, cur_proxy_node->lport, &hints, &ai)) != 0) {
report_error("proxy_listen(): getaddrinfo(%s, %s, %lx, %lx): %s", \
cur_proxy_node->lhost, cur_proxy_node->lport, (unsigned long) &hints, (unsigned long) &ai, gai_strerror(rv));
return(-1);
}
for(p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) {
continue;
}
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(bind(listener, p->ai_addr, p->ai_addrlen) < 0){
close(listener);
continue;
}
break;
}
if(p == NULL){
report_error("proxy_listen(): Failed to bind() to %s:%s", cur_proxy_node->lhost, cur_proxy_node->lport);
return(-1);
}
freeaddrinfo(ai);
if(listen(listener, 10) == -1) {
report_error("proxy_listen(): listen(%d, 10): %s", listener, strerror(errno));
return(-1);
}
fcntl(listener, F_SETFL, O_NONBLOCK);
cur_proxy_node->fd = listener;
return(0);
}
/******************************************************************************
*
* proxy_connect()
*
* Inputs: The string representing where to connect to.
*
* Outputs: 0 for success. -1 for fatal errors. -2 for non-fatal errors.
*
* Purpose: Complete the outbound connection of a proxy request.
*
******************************************************************************/
int proxy_connect(char *rhost_rport){
int count;
int yes = 1;
int rv, connector = -2;
struct addrinfo hints, *ai, *p;
char *rhost, *rport, *tmp_ptr;
tmp_ptr = rhost_rport;
if(*tmp_ptr == '['){
tmp_ptr++;
}
count = strlen(tmp_ptr);
if((rhost = (char *) calloc(count + 1, sizeof(char))) == NULL){
report_error("proxy_connect(): calloc(%d, %d): %s", count + 1, (int) sizeof(char), strerror(errno));
return(-1);
}
memcpy(rhost, tmp_ptr, count);
if(tmp_ptr != rhost_rport){
if((tmp_ptr = strchr(rhost, ']')) == NULL){
free(rhost);
return(-2);
}
*(tmp_ptr) = '\0';
}
if((rport = strrchr(rhost, ':')) == NULL){
free(rhost);
return(-2);
}
*(rport++) = '\0';
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((rv = getaddrinfo(rhost, rport, &hints, &ai)) != 0) {
report_error("proxy_connect(): getaddrinfo(%s, %s, %lx, %lx): %s", rhost, rport, (unsigned long) &hints, (unsigned long) &ai, gai_strerror(rv));
free(rhost);
return(-2);
}
for(p = ai; p != NULL; p = p->ai_next) {
connector = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (connector < 0) {
continue;
}
setsockopt(connector, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
fcntl(connector, F_SETFL, O_NONBLOCK);
errno = 0;
if(connect(connector, p->ai_addr, p->ai_addrlen) < 0){
if (errno != EINPROGRESS){
close(connector);
continue;
}
}
break;
}
free(rhost);
if(p == NULL){
return(-2);
}
return(connector);
}
/******************************************************************************
*
* proxy_node_create()
*
* Inputs: None, though we will heavily utilize the global io struct.
*
* Outputs: A pointer to the new proxy_node.
*
* Purpose: Initialize a new proxy node, and put it into it's place in the linked list.
*
******************************************************************************/
struct proxy_node *proxy_node_create(){
struct proxy_node *cur_proxy_node, *tmp_proxy_node;
if((cur_proxy_node = (struct proxy_node *) calloc(1, sizeof(struct proxy_node))) == NULL){
report_error("proxy_node_create(): calloc(1, %d): %s", (int) sizeof(struct proxy_node), strerror(errno));
return(NULL);
}
tmp_proxy_node = io->proxy_tail;
if(!tmp_proxy_node){
io->proxy_head = cur_proxy_node;
io->proxy_tail = cur_proxy_node;
}else{
tmp_proxy_node->next = cur_proxy_node;
cur_proxy_node->prev = tmp_proxy_node;
io->proxy_tail = cur_proxy_node;
}
io->fd_count++;
return(cur_proxy_node);
}
/******************************************************************************
*
* proxy_node_delete()
*
* Inputs: A pointer to the proxy_node we wish to destroy.
*
* Outputs: None.
*
* Purpose: Destroy a proxy node and remove it froms the linked list.
*
******************************************************************************/
void proxy_node_delete(struct proxy_node *cur_proxy_node){
if(cur_proxy_node == io->proxy_head){
io->proxy_head = cur_proxy_node->next;
}
if(cur_proxy_node == io->proxy_tail){
io->proxy_tail = cur_proxy_node->prev;
}
if(cur_proxy_node->prev){
cur_proxy_node->prev->next = cur_proxy_node->next;
}
if(cur_proxy_node->next){
cur_proxy_node->next->prev = cur_proxy_node->prev;
}
if(cur_proxy_node->origin == io->target){
if(cur_proxy_node->fd){
close(cur_proxy_node->fd);
}
io->fd_count--;
if(cur_proxy_node->orig_request){
free(cur_proxy_node->orig_request);
}
if(cur_proxy_node->mem_ptr){
free(cur_proxy_node->mem_ptr);
}
}else{
free(cur_proxy_node->orig_request);
}
if(cur_proxy_node){
free(cur_proxy_node);
}
}
/******************************************************************************
*
* proxy_node_find()
*
* Inputs: The tuple of (origin, id) that represents the unique id of a proxy.
*
* Outputs: The pointer to the matching proxy_node.
*
* Purpose: Find the matching proxy_node.
*
******************************************************************************/
struct proxy_node *proxy_node_find(unsigned short origin, unsigned short id){
struct proxy_node *tmp_proxy_node;
tmp_proxy_node = io->proxy_head;
while(tmp_proxy_node){
if((tmp_proxy_node->origin == origin) && (tmp_proxy_node->id == id)){
return(tmp_proxy_node);
}
tmp_proxy_node = tmp_proxy_node->next;
}
return(NULL);
}
/******************************************************************************
*
* connection_node_create()
*
* Inputs: None, though we will heavily utilize the global io struct.
*
* Outputs: A pointer to the new connection_node.
*
* Purpose: Initialize a new connection node, and put it into it's place in the linked list.
*
******************************************************************************/
struct connection_node *connection_node_create(){
struct connection_node *cur_connection_node, *tmp_connection_node;
if((cur_connection_node = (struct connection_node *) calloc(1, sizeof(struct connection_node))) == NULL){
report_error("connection_node_create(): calloc(1, %d): %s", (int) sizeof(struct connection_node), strerror(errno));
return(NULL);
}
tmp_connection_node = io->connection_tail;
if(!tmp_connection_node){
io->connection_head = cur_connection_node;
io->connection_tail = cur_connection_node;
}else{
tmp_connection_node->next = cur_connection_node;
cur_connection_node->prev = tmp_connection_node;
io->connection_tail = cur_connection_node;
}
io->fd_count++;
return(cur_connection_node);
}
/******************************************************************************
*
* connection_node_delete()
*
* Inputs: A pointer to the connection_node we wish to destroy.
*
* Outputs: None.
*
* Purpose: Destroy a connection node and remove it froms the linked list.
*
******************************************************************************/
void connection_node_delete(struct connection_node *cur_connection_node){
if(cur_connection_node == io->connection_head){
io->connection_head = cur_connection_node->next;
}
if(cur_connection_node == io->connection_tail){
io->connection_tail = cur_connection_node->prev;
}
if(cur_connection_node->prev){
cur_connection_node->prev->next = cur_connection_node->next;
}
if(cur_connection_node->next){
cur_connection_node->next->prev = cur_connection_node->prev;
}
if(cur_connection_node->fd){
close(cur_connection_node->fd);
}
if(cur_connection_node->rhost_rport){
free(cur_connection_node->rhost_rport);
}
if(cur_connection_node->buffer_head){
free(cur_connection_node->buffer_head);
}
if(cur_connection_node){
free(cur_connection_node);
}
io->fd_count--;
}
/******************************************************************************
*
* connection_node_find()
*
* Inputs: The tuple of (origin, id) that represents the unique id of a connection.
*
* Outputs: The pointer to the matching connection_node.
*
* Purpose: Find the matching connection_node.
*
******************************************************************************/
struct connection_node *connection_node_find(unsigned short origin, unsigned short id){
struct connection_node *tmp_connection_node;
tmp_connection_node = io->connection_head;
while(tmp_connection_node){
if((tmp_connection_node->origin == origin) && (tmp_connection_node->id == id)){
return(tmp_connection_node);
}
tmp_connection_node = tmp_connection_node->next;
}
return(NULL);
}
/******************************************************************************
*
* connection_node_queue()
*
* Inputs: The pointer to the connection node that needs to be requeued.
*
* Outputs: None.
*
* Purpose: Simplistic round-robin scheduling for the connections.
* Takes a node and moves it to the end of the list.
*
******************************************************************************/
void connection_node_queue(struct connection_node *cur_connection_node){
if(cur_connection_node == io->connection_tail){
return;
}
if(cur_connection_node == io->connection_head){
io->connection_head = cur_connection_node->next;
}
if(cur_connection_node->prev){
cur_connection_node->prev->next = cur_connection_node->next;
}
if(cur_connection_node->next){
cur_connection_node->next->prev = cur_connection_node->prev;
}
io->connection_tail->next = cur_connection_node;
cur_connection_node->prev = io->connection_tail;
cur_connection_node->next = NULL;
io->connection_tail = cur_connection_node;
}
/******************************************************************************
*
* parse_socks_request()
*
* Inputs: The pointer to the connection node that represents a socks proxy
* connection in its opening phase.
*
* Outputs: The current state of the socks request. -1 on error.
*
* Purpose: Socks requests involve a handshake. This is the function that
* parses that handshake.
*
******************************************************************************/
int parse_socks_request(struct connection_node *cur_connection_node){
int index, size;
int nmethods, i;
int len = 0;
char *domain_name;
char *head;
char *dst_port_ptr = NULL;
char *dst_addr_ptr = NULL;
int atype = 0x01;
int noauth_found;
head = cur_connection_node->buffer_ptr;
size = cur_connection_node->buffer_tail - cur_connection_node->buffer_ptr;
index = 0;
if(!size){
return(CON_SOCKS_INIT);
}
// Socks 4 or 4a.
if(head[index] == 4){
/*
* +----+----+----+----+----+----+----+----+----+----+....+----+
* | VN | CD | DSTPORT | DSTIP | USERID |NULL|
* +----+----+----+----+----+----+----+----+----+----+....+----+
* 1 1 2 4 variable 1
*
* 1 + 1 + 2 + 4 + 1
* = 9 Minimum number of bytes to ensure we have at least 1 char of USERID.
*/
if(size < 9){
return(CON_SOCKS_INIT);
}
index += 1;
// Only TCP Connect is supported.
if(head[index] != 1){
report_error("parse_socks_request(): Socks 4: Unsupported CD. Only CD \"Connect\" is supported.");
return(-1);
}
index += 1;
dst_port_ptr = head + index;
index += 2;
dst_addr_ptr = head + index;
index += 4;
// We don't accept connections with a USERID field.
if(head[index]){
report_error("parse_socks_request(): Socks 4: USERID found, but not supported by this implementation.");
return(-1);
}
// Socks 4a. Lets step through and grab the "domain".
// Note: In the modern era, most clients use socks 4a. Even if the request is a normal ipv4
// address and socks 4 would work fine... instead, they use socks 4a and send the ipv4 address
// represented as a string. So here the "domain" may be something we pass to dns to resolve.
// It *might* just be an ascii string representation of an ip address (v4 or v6).
index++;
if( \
!dst_addr_ptr[0] && \
!dst_addr_ptr[1] && \
!dst_addr_ptr[2] && \
dst_addr_ptr[3] \
){
if(!(index < size)){
return(CON_SOCKS_INIT);
}
domain_name = head + index;
while(head[index]){
index++;
if(index == size){
return(CON_SOCKS_INIT);
}
}
atype = 0x03;
if((cur_connection_node->rhost_rport = addr_to_string(atype, domain_name, dst_port_ptr, strlen(domain_name))) == NULL){
report_error("parse_socks_request(): addr_to_string(%d, %lx, %lx, %d): %s", \
atype, (unsigned long) dst_addr_ptr, (unsigned long) dst_port_ptr, (int) strlen(domain_name), strerror(errno));
return(-1);
}
index++;
cur_connection_node->buffer_ptr = head + index;
return(CON_ACTIVE);
}
if((cur_connection_node->rhost_rport = addr_to_string(atype, dst_addr_ptr, dst_port_ptr, 0)) == NULL){
report_error("parse_socks_request(): addr_to_string(%d, %lx, %lx, 0): %s", \
atype, (unsigned long) dst_addr_ptr, (unsigned long) dst_port_ptr, strerror(errno));
return(-1);
}
cur_connection_node->buffer_ptr = head + index;
return(CON_ACTIVE);
// SOCKS 5
}else if(head[index] == 5){
if(cur_connection_node->state == CON_SOCKS_INIT){
index += 1;
if(!(index < size)){
return(CON_SOCKS_INIT);
}
noauth_found = 0;
nmethods = head[index++];
for(i = 0; i < nmethods; i++){
if(!(index < size)){
return(CON_SOCKS_INIT);
}
// Someday we might implement more than the "NO AUTHENTICATION REQUIRED" method...
if(head[index] == 0x00){
noauth_found = 1;
}
index++;
}
cur_connection_node->buffer_ptr = head + index;
if(noauth_found){
return(CON_SOCKS_V5_AUTH);
}else{
report_error("parse_socks_request(): Socks 5: No supported auth type found. Only \"No Authentication Required\" is supported.");
return(-1);
}
}else if(cur_connection_node->state == CON_SOCKS_V5_AUTH){
/*
* +----+-----+-------+------+----------+----------+
* |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
* +----+-----+-------+------+----------+----------+
* | 1 | 1 | X'00' | 1 | Variable | 2 |
* +----+-----+-------+------+----------+----------+
* (First byte of "Variable" is the strlen of the string that follows in the DOMAINNAME case. No '\0' terminator.)
*
* 1 + 1 + 1 + 1 + 1 + V + 2
* = 7 + V... So 7 is the minimum number of bytes before we can do anything interesting.
*/
if(size < 7){
return(CON_SOCKS_INIT);
}
index += 1;
if(head[index] != 1){
report_error("parse_socks_request(): Socks 5: Unsupported CMD. Only CMD \"Connect\" is supported.");
return(-1);
}
index += 2;
atype = head[index];
index += 1;
if(atype == 0x01){
len = 4;
// From the diagram above. 4 + Variable + 2, where Variable is 4 in the ipv4 case.
if(size < (4 + len + 2)){
return(CON_SOCKS_INIT);
}
dst_addr_ptr = head + index;
index += len;
dst_port_ptr = head + index;
}else if(atype == 0x03){
len = head[index];
// From the diagram above. 4 + Variable + 2, where Variable length is defined in the first octet.
if(size < (4 + 1 + len + 2)){
return(CON_SOCKS_INIT);
}
index++; // Move past the length variable.
dst_addr_ptr = head + index;
index += len;
dst_port_ptr = head + index;
}else if(atype == 0x04){
len = 16;
// From the diagram above. 4 + Variable + 2, where Variable is 16 in the ipv6 case.
if(size < (4 + len + 2)){
return(CON_SOCKS_INIT);
}
dst_addr_ptr = head + index;
index += len;
dst_port_ptr = head + index;
}else{
report_error("parse_socks_request(): atype 0x%02x unknown.", atype);
return(-1);
}
index += 2;
if((cur_connection_node->rhost_rport = addr_to_string(atype, dst_addr_ptr, dst_port_ptr, len)) == NULL){
report_error("parse_socks_request(): addr_to_string(%d, %lx, %lx, 0): %s", \
atype, (unsigned long) dst_addr_ptr, (unsigned long) dst_port_ptr, strerror(errno));
return(-1);
}
cur_connection_node->buffer_ptr = head + index;
return(CON_ACTIVE);
}
}
return(-1);
}
/******************************************************************************
*
* addr_to_string()
*
* Inputs: The type of the socks request.
* The address or domain name of the socks request.
* The port of the socks request.
* The length of the domain name in the above address.
*
* Outputs: A pointer to a string representation of the address.
*
* Purpose: Convert the address:port information into a string.
*
* Note: If atype is 0x03, then len is the length of the addr string.
* If atype isn't 0x03, len is ignored.
*
******************************************************************************/
char *addr_to_string(int atype, char *addr, char *port, int len){
char *ptr;
unsigned short int port_num = ntohs(*((unsigned short int *)(port)));
// strlen("255.255.255.255:65535") -> 21
int string_len = 21;
// ntohs returns a uint16_t. Max size of that number is 6 bytes. Plus one for the leading ':'.
// Note that the largest value *should* be 65535, which is 6 not 7 characters long. Doing so will make
// the compiler grumpy.
int port_len = 7;
if(atype == 0x03){
string_len = len;
string_len += port_len;
}else if(atype == 0x04){
// strlen("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535") -> 47
string_len = 47;
}
if((ptr = (char *) calloc(string_len + 1, sizeof(char))) == NULL){
report_error("addr_to_string(): calloc(%d, %d): %s", string_len + 1, (int) sizeof(char), strerror(errno));
return(NULL);
}
string_len++;
if(atype == 0x03){
memcpy(ptr, addr, len);
snprintf(ptr + len, port_len, ":%d", port_num);
}else if(atype == 0x04){
snprintf(ptr, string_len, "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d", \
(unsigned char) addr[0], (unsigned char) addr[1], (unsigned char) addr[2], (unsigned char) addr[3], \
(unsigned char) addr[4], (unsigned char) addr[5], (unsigned char) addr[6], (unsigned char) addr[7], \
(unsigned char) addr[8], (unsigned char) addr[9], (unsigned char) addr[10], (unsigned char) addr[11], \
(unsigned char) addr[12], (unsigned char) addr[13], (unsigned char) addr[14], (unsigned char) addr[15], \
port_num);
}else{
snprintf(ptr, string_len, "%d.%d.%d.%d:%d", (unsigned char) addr[0], (unsigned char) addr[1], (unsigned char) addr[2], (unsigned char) addr[3], port_num);
}
return(ptr);
}