forked from sipwise/sipsak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.c
978 lines (874 loc) · 22.3 KB
/
helper.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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
* $Id: helper.c 397 2006-01-28 21:11:50Z calrissian $
*
* Copyright (C) 2002-2004 Fhg Fokus
* Copyright (C) 2004-2005 Nils Ohlmeier
*
* This file belongs to sipsak, a free sip testing tool.
*
* sipsak 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.
*
* sipsak is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "sipsak.h"
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_UNISTD_H
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
#ifdef HAVE_CTYPE_H
# include <ctype.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_RULI_H
# include <ruli.h>
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_CARES_H
# ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h>
# endif
# include <ares.h>
# ifndef NS_RRFIXEDSZ
# define NS_RRFIXEDSZ 10
# define NS_QFIXEDSZ 4
# define NS_HFIXEDSZ 12
# endif
int caport;
ip_addr_t caadr;
char *ca_tmpname;
ares_channel channel;
#endif // HAVE_CARES_H
#include "helper.h"
#include "exit_code.h"
/* returns 1 if the address family is IPv4 or IPv6 */
int is_ip_af(int af)
{
return af == AF_INET || af == AF_INET6;
}
/* returns the address family for a numerical address */
int get_address_family(char *str)
{
struct addrinfo hint, *res=0 ;
int af;
char * buf;
int len;
len = strlen(str);
buf = (char*)malloc(len+1);
memset(buf, 0, len);
/* ipv6 */
if(str[0] == '[' && str[len-1] == ']') {
str++;
len -=2;
}
strncpy(buf, str, len);
buf[len] = 0;
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(buf, NULL, &hint, &res))
return AF_UNSPEC;
af = res->ai_family;
freeaddrinfo(res);
free(buf);
return af;
}
/* returns 1 if the string is an IP address otherwise zero */
int is_ip(char *str)
{
return is_ip_af(get_address_family(str));
}
/* returns the size of the in_addr structure for this address family or 0 if not supported */
int get_in_addr_sz(int af)
{
switch(af)
{
case AF_INET: return sizeof(struct in_addr);
case AF_INET6: return sizeof(struct in6_addr);
default: return 0;
}
}
/* returns an in_addr or in6_addr or NULL if af is not one of AF_INET or AF_INET6 */
void * get_in_addr(int af)
{
int sz;
sz = get_in_addr_sz(af);
if(!sz)
return NULL;
return malloc(get_in_addr_sz(af));
}
/* */
int set_ip_address(ip_addr_t * ip, struct addrinfo * addr)
{
if(ip == NULL || addr == NULL)
return -1;
switch(addr->ai_family)
{
case AF_INET: ip->v4 = ((struct sockaddr_in*)addr->ai_addr)->sin_addr; break;
case AF_INET6: ip->v6 = ((struct sockaddr_in6*)addr->ai_addr)->sin6_addr; break;
default: return -1;
}
ip->af = addr->ai_family;
return 0;
}
ip_addr_t parse_ip_address(char * host, int af)
{
ip_addr_t addr;
int ret;
memset(&addr, 0, sizeof(ip_addr_t));
if(!is_ip_af(af))
{
fprintf(stderr, "attempt to resolve '%s' from invalid address family %d at %s:%d\n", host, af, __FILE__, __LINE__);
return addr;
}
addr.af = af;
if(af == AF_INET6) {
if(host[0] != '[' || host[strlen(host)-1] != ']') {
fprintf("invalid ipv6 format: %s (missing brackets)\n", *host);
exit_code(2);
}
host++;
host[strlen(host)-1] = 0;
}
ret = inet_pton(af, host, &addr);
if(af == AF_INET6) {
host[strlen(host)] = ']';
}
if(ret == 0)
fprintf(stderr, "invalid address: '%s'\n", host);
else if(ret == -1)
fprintf(stderr, "invalid address family '%d'\n", af);
return addr;
}
/* take either a dot.decimal string of ip address or a
domain name and returns a NETWORK ordered long int containing
the address. i chose to internally represent the address as long for speedier
comparisions.
any changes to getaddress have to be patched back to the net library.
contact: [email protected]
returns zero if there is an error.
this is convenient as 0 means 'this' host and the traffic of
a badly behaving dns system remains inside (you send to 0.0.0.0)
*/
ip_addr_t getaddress_from_family(char *host, int af_dest) {
ip_addr_t addr;
struct addrinfo * res = NULL;
struct addrinfo hints;
int af, ret;
memset(&addr, 0, sizeof(ip_addr_t));
af = get_address_family(host);
if(is_ip_af(af))
{
#ifdef DEBUG
printf("got an ip address: '%s'\n", host);
#endif
addr = parse_ip_address(host, af);
if(!assigned(addr))
exit_code(2);
return addr;
}
#ifdef DEBUG
printf("looking up '%s' (af hint: %d)\n", host, af_dest);
#endif
/* try the system's own resolution mechanism for dns lookup:
required only for domain names.
inspite of what the rfc2543 :D Using SRV DNS Records recommends,
we are leaving it to the operating system to do the name caching.
this is an important implementational issue especially in the light
dynamic dns servers like dynip.com or dyndns.com where a dial
ip address is dynamically assigned a sub domain like farhan.dynip.com
although expensive, this is a must to allow OS to take
the decision to expire the DNS records as it deems fit.
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af_dest;
ret = getaddrinfo(host, NULL, &hints, &res);
if(ret) {
fprintf(stderr, "'%s' is unresolveable: %s\n", host, gai_strerror(ret));
exit_code(2);
}
set_ip_address(&addr, res);
freeaddrinfo(res);
return addr;
}
ip_addr_t getaddress(char * host)
{
return getaddress_from_family(host, AF_INET | AF_INET6);
}
#ifdef HAVE_CARES_H
static const unsigned char *parse_rr(const unsigned char *aptr, const unsigned char *abuf, int alen) {
char *name;
long len;
int status, type, dnsclass, dlen, af;
struct in_addr addr;
struct in6_addr addr6;
#ifdef DEBUG
printf("ca_tmpname: %s\n", ca_tmpname);
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len);
if (status != ARES_SUCCESS) {
printf("error: failed to expand query name\n");
exit_code(2);
}
aptr += len;
if (aptr + NS_RRFIXEDSZ > abuf + alen) {
printf("error: not enough data in DNS answer 1\n");
free(name);
return NULL;
}
type = DNS_RR_TYPE(aptr);
dnsclass = DNS_RR_CLASS(aptr);
dlen = DNS_RR_LEN(aptr);
aptr += NS_RRFIXEDSZ;
if (aptr + dlen > abuf + alen) {
printf("error: not enough data in DNS answer 2\n");
free(name);
return NULL;
}
if (dnsclass != CARES_CLASS_C_IN) {
printf("error: unsupported dnsclass (%i) in DNS answer\n", dnsclass);
free(name);
return NULL;
}
if ((ca_tmpname == NULL && type != CARES_TYPE_SRV) ||
(ca_tmpname != NULL &&
(type != CARES_TYPE_A && type != CARES_TYPE_CNAME))) {
printf("error: unsupported DNS response type (%i)\n", type);
free(name);
return NULL;
}
if (type == CARES_TYPE_SRV) {
free(name);
caport = DNS__16BIT(aptr + 4);
#ifdef DEBUG
printf("caport: %i\n", caport);
#endif
status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
if (status != ARES_SUCCESS) {
printf("error: failed to expand SRV name\n");
return NULL;
}
#ifdef DEBUG
printf("SRV name: %s\n", name);
#endif
af = get_address_family(name);
if (is_ip_af(af)) {
inet_pton(af, name, &caadr);
free(name);
}
else {
ca_tmpname = name;
}
}
else if (type == CARES_TYPE_CNAME) {
if (STRNCASECMP(ca_tmpname, name, strlen(ca_tmpname)) == 0) {
ca_tmpname = malloc(strlen(name));
if (ca_tmpname == NULL) {
printf("error: failed to allocate memory\n");
exit_code(2);
}
strcpy(ca_tmpname, name);
}
free(name);
}
else if (type == CARES_TYPE_A) {
if (dlen == 4 && STRNCASECMP(ca_tmpname, name, strlen(ca_tmpname)) == 0) {
memcpy(&addr, aptr, sizeof(struct in_addr));
caadr.v4.s_addr = addr.s_addr;
}
free(name);
}
else if (type == CARES_TYPE_AAAA) {
if (dlen == 4 && STRNCASECMP(ca_tmpname, name, strlen(ca_tmpname)) == 0) {
memcpy(&addr6, aptr, sizeof(struct in6_addr));
memcpy(&caadr, &addr6, sizeof(struct in6_addr));
}
free(name);
}
return aptr + dlen;
}
static const unsigned char *skip_rr(const unsigned char *aptr, const unsigned char *abuf, int alen) {
int status, dlen;
long len;
char *name;
#ifdef DEBUG
printf("skipping rr section...\n");
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len);
aptr += len;
dlen = DNS_RR_LEN(aptr);
aptr += NS_RRFIXEDSZ;
aptr += dlen;
free(name);
return aptr;
}
static const unsigned char *skip_query(const unsigned char *aptr, const unsigned char *abuf, int alen) {
int status;
long len;
char *name;
#ifdef DEBUG
printf("skipping query section...\n");
#endif
status = ares_expand_name(aptr, abuf, alen, &name, &len);
aptr += len;
aptr += NS_QFIXEDSZ;
free(name);
return aptr;
}
static void cares_callback(void *arg, int status, int timeouts, unsigned char *abuf, int alen) {
int i;
unsigned int ancount, nscount, arcount;
const unsigned char *aptr;
#ifdef DEBUG
printf("cares_callback: status=%i, alen=%i\n", status, alen);
#endif
if (status != ARES_SUCCESS) {
if (verbose > 1)
printf("ares failed: %s\n", ares_strerror(status));
return;
}
ancount = DNS_HEADER_ANCOUNT(abuf);
nscount = DNS_HEADER_NSCOUNT(abuf);
arcount = DNS_HEADER_ARCOUNT(abuf);
#ifdef DEBUG
printf("ancount: %i, nscount: %i, arcount: %i\n", ancount, nscount, arcount);
#endif
/* safety check */
if (alen < NS_HFIXEDSZ)
return;
aptr = abuf + NS_HFIXEDSZ;
aptr = skip_query(aptr, abuf, alen);
for (i = 0; i < ancount && !assigned(caadr); i++) {
if (ca_tmpname == NULL)
aptr = parse_rr(aptr, abuf, alen);
else
aptr = skip_rr(aptr, abuf, alen);
}
if (!assigned(caadr)) {
for (i = 0; i < nscount; i++) {
aptr = skip_rr(aptr, abuf, alen);
}
for (i = 0; i < arcount && !assigned(caadr); i++) {
aptr = parse_rr(aptr, abuf, alen);
}
}
}
inline ip_addr_t srv_ares(char *host, int *port, char *srv) {
int nfds, count, srvh_len;
char *srvh;
fd_set read_fds, write_fds;
struct timeval *tvp, tv;
caport = 0;
memset(&caadr, 0, sizeof(ip_addr_t));
ca_tmpname = NULL;
#ifdef DEBUG
printf("!!! ARES query !!!\n");
#endif
srvh_len = strlen(host) + strlen(srv) + 2;
srvh = malloc(srvh_len);
if (srvh == NULL) {
printf("error: failed to allocate memory (%i) for ares query\n", srvh_len);
exit_code(2);
}
memset(srvh, 0, srvh_len);
strncpy(srvh, srv, strlen(srv));
memcpy(srvh + strlen(srv), ".", 1);
strcpy(srvh + strlen(srv) + 1, host);
#ifdef DEBUG
printf("hostname: '%s', len: %i\n", srvh, srvh_len);
#endif
ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL);
#ifdef DEBUG
printf("after ares_query\n");
#endif
/* wait for query to complete */
while (1) {
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
nfds = ares_fds(channel, &read_fds, &write_fds);
if (nfds == 0)
break;
tvp = ares_timeout(channel, NULL, &tv);
count = select(nfds, &read_fds, &write_fds, NULL, tvp);
if (count < 0 && errno != EINVAL) {
perror("ares select");
exit_code(2);
}
ares_process(channel, &read_fds, &write_fds);
}
#ifdef DEBUG
printf("end of while\n");
#endif
*port = caport;
if (!assigned(caadr) && ca_tmpname != NULL) {
caadr = getaddress(ca_tmpname);
}
if (ca_tmpname != NULL)
free(ca_tmpname);
free(srvh);
return caadr;
}
#endif // HAVE_CARES_H
#ifdef HAVE_RULI_H
inline ip_addr_t srv_ruli(char *host, int *port, char *srv) {
int srv_code;
int ruli_opts = RULI_RES_OPT_SEARCH | RULI_RES_OPT_SRV_NOINET6 | RULI_RES_OPT_SRV_NOSORT6 | RULI_RES_OPT_SRV_NOFALL;
ip_addr_t ip;
#ifdef RULI_RES_OPT_SRV_CNAME
ruli_opts |= RULI_RES_OPT_SRV_CNAME;
#endif
memset(&ip, 0, sizeof(ip_addr_t));
ruli_sync_t *sync_query = ruli_sync_query(srv, host, *port, ruli_opts);
/* sync query failure? */
if (!sync_query) {
printf("DNS SRV lookup failed for: %s\n", host);
exit_code(2);
}
srv_code = ruli_sync_srv_code(sync_query);
/* timeout? */
if (srv_code == RULI_SRV_CODE_ALARM) {
printf("Timeout during DNS SRV lookup for: %s\n", host);
ruli_sync_delete(sync_query);
exit_code(2);
}
/* service provided? */
else if (srv_code == RULI_SRV_CODE_UNAVAILABLE) {
printf("SRV service not provided for: %s\n", host);
ruli_sync_delete(sync_query);
exit_code(2);
}
else if (srv_code) {
int rcode = ruli_sync_rcode(sync_query);
if (verbose > 1)
printf("SRV query failed for: %s, srv_code=%d, rcode=%d\n", host, srv_code, rcode);
ruli_sync_delete(sync_query);
return ip;
}
ruli_list_t *srv_list = ruli_sync_srv_list(sync_query);
int srv_list_size = ruli_list_size(srv_list);
if (srv_list_size < 1) {
if (verbose > 1)
printf("No SRV record: %s.%s\n", srv, host);
return ip;
}
ruli_srv_entry_t *entry = (ruli_srv_entry_t *) ruli_list_get(srv_list, 0);
ruli_list_t *addr_list = &entry->addr_list;
int addr_list_size = ruli_list_size(addr_list);
if (addr_list_size < 1) {
printf("missing addresses in SRV lookup for: %s\n", host);
ruli_sync_delete(sync_query);
exit_code(2);
}
*port = entry->port;
ruli_addr_t *addr = (ruli_addr_t *) ruli_list_get(addr_list, 0);
switch (ruli_addr_family(addr)) {
case PF_INET:
ip.v4 = ruli_addr_inet(addr);
break;
case PF_INET6:
ip.v6 = ruli_addr_inet6(addr);
break;
default:
break;
}
return ip;
}
#endif // HAVE_RULI_H
ip_addr_t getsrvaddress(char *host, int *port, char *srv) {
#ifdef HAVE_RULI_H
return srv_ruli(host, port, srv);
#else
# ifdef HAVE_CARES_H // HAVE_RULI_H
return srv_ares(host, port, srv);
# else // HAVE_CARES_H
ip_addr_t addr;
memset(&addr, 0, sizeof(addr));
return addr;
# endif
#endif
}
/* Finds the SRV records for the given host. It returns the target IP
* address and fills the port and transport if a suitable SRV record
* exists. Otherwise it returns 0. The function follows 3263: first
* TLS, then TCP and finally UDP. */
ip_addr_t getsrvadr(char *host, int *port, unsigned int *transport) {
ip_addr_t adr;
memset(&adr, 0, sizeof(ip_addr_t));
#ifdef HAVE_SRV
int srvport = 5060;
#ifdef HAVE_CARES_H
int status;
int optmask = ARES_OPT_FLAGS;
struct ares_options options;
options.flags = ARES_FLAG_NOCHECKRESP;
options.servers = NULL;
options.nservers = 0;
status = ares_init_options(&channel, &options, optmask);
if (status != ARES_SUCCESS) {
printf("error: failed to initialize ares\n");
exit_code(2);
}
#endif
#ifdef WITH_TLS_TRANSP
adr = getsrvaddress(host, &srvport, SRV_SIP_TLS);
if (assigned(adr)) {
*transport = SIP_TLS_TRANSPORT;
if (verbose > 1)
printf("using SRV record: %s.%s:%i\n", SRV_SIP_TLS, host, srvport);
printf("TLS transport not yet supported\n");
exit_code(2);
}
else {
#endif
adr = getsrvaddress(host, &srvport, SRV_SIP_TCP);
if (assigned(adr)) {
*transport = SIP_TCP_TRANSPORT;
if (verbose > 1)
printf("using SRV record: %s.%s:%i\n", SRV_SIP_TCP, host, srvport);
}
else {
adr = getsrvaddress(host, &srvport, SRV_SIP_UDP);
if (assigned(adr)) {
*transport = SIP_UDP_TRANSPORT;
if (verbose > 1)
printf("using SRV record: %s.%s:%i\n", SRV_SIP_UDP, host, srvport);
}
}
#ifdef WITH_TLS_TRANSP
}
#endif
#ifdef HAVE_CARES_H
ares_destroy(channel);
#endif
*port = srvport;
#endif // HAVE_SRV
return adr;
}
/* because the full qualified domain name is needed by many other
functions it will be determined by this function.
*/
void get_fqdn(){
char hname[100], dname[100], hlp[18];
size_t namelen=100, len;
ip_addr_t addr;
struct utsname un;
int n;
memset(&hname, 0, sizeof(hname));
memset(&dname, 0, sizeof(dname));
memset(&hlp, 0, sizeof(hlp));
if (hostname) {
strncpy(fqdn, hostname, FQDN_SIZE-1);
strncpy(hname, hostname, sizeof(hname)-1);
}
else {
if ((uname(&un))==0) {
strncpy(hname, un.nodename, sizeof(hname)-1);
}
else {
if (gethostname(&hname[0], namelen) < 0) {
fprintf(stderr, "error: cannot determine hostname\n");
exit_code(2);
}
}
#ifdef HAVE_GETDOMAINNAME
/* a hostname with dots should be a domainname */
if ((strchr(hname, '.'))==NULL) {
if (getdomainname(&dname[0], namelen) < 0) {
fprintf(stderr, "error: cannot determine domainname\n");
exit_code(2);
}
if (strcmp(&dname[0],"(none)")!=0)
snprintf(fqdn, FQDN_SIZE, "%s.%s", hname, dname);
}
else {
strncpy(fqdn, hname, FQDN_SIZE-1);
}
#endif
}
if (numeric == 1 && !is_ip(fqdn)) {
addr = getaddress_from_family(hname, address.af);
if (assigned(addr)) {
if (numeric == 1) {
inet_ntop(address.af, &addr, fqdn, FQDN_SIZE-1);
}
else {
strncpy(fqdn, hname, FQDN_SIZE-1);
}
}
else {
fprintf(stderr, "error: cannot resolve local hostname: %s\n", hname);
exit_code(2);
}
}
if (!is_ip(fqdn)) {
if (hostname) {
fprintf(stderr, "warning: %s is not resolvable... continouing anyway\n", fqdn);
strncpy(fqdn, hostname, FQDN_SIZE-1);
}
else {
fprintf(stderr, "error: this FQDN or IP is not valid: %s\n", fqdn);
exit_code(2);
}
}
else {
/* ipv6 formatting */
if(get_address_family(fqdn) == AF_INET6 && fqdn[0] != '[')
{
len = strlen(fqdn);
for(n=len; n; n--) {
fqdn[n] = fqdn[n-1];
}
fqdn[0] = '[';
fqdn[len+1] = ']';
fqdn[len+2] = 0;
}
}
if (verbose > 2)
printf("fqdnhostname: %s\n", fqdn);
}
/* this function searches for search in mess and replaces it with
replacement */
void replace_string(char *mess, char *search, char *replacement){
char *backup, *insert;
insert=STRCASESTR(mess, search);
if (insert==NULL){
if (verbose > 2)
fprintf(stderr, "warning: could not find this '%s' replacement string in "
"message\n", search);
}
else {
while (insert){
backup=str_alloc(strlen(insert)+1);
strcpy(backup, insert+strlen(search));
strcpy(insert, replacement);
strcpy(insert+strlen(replacement), backup);
free(backup);
insert=STRCASESTR(mess, search);
}
}
}
/* checks if the strings contains special double marks and then
* replace all occurences of this strings in the message */
void replace_strings(char *mes, char *strings) {
char *pos, *atr, *val, *repl, *end;
char sep;
pos=atr=val=repl = NULL;
#ifdef DEBUG
printf("replace_strings entered\nstrings: '%s'\n", strings);
#endif
if ((isalnum(*strings) != 0) &&
(isalnum(*(strings + strlen(strings) - 1)) != 0)) {
replace_string(req, "$replace$", replace_str);
}
else {
sep = *strings;
#ifdef DEBUG
printf("sep: '%c'\n", sep);
#endif
end = strings + strlen(strings);
pos = strings + 1;
while (pos < end) {
atr = pos;
pos = strchr(atr, sep);
if (pos != NULL) {
*pos = '\0';
val = pos + 1;
pos = strchr(val, sep);
if (pos != NULL) {
*pos = '\0';
pos++;
}
}
#ifdef DEBUG
printf("atr: '%s'\nval: '%s'\n", atr, val);
#endif
if ((atr != NULL) && (val != NULL)) {
repl = str_alloc(strlen(val) + 3);
if (repl == NULL) {
printf("failed to allocate memory\n");
exit_code(2);
}
sprintf(repl, "$%s$", atr);
replace_string(mes, repl, val);
free(repl);
}
#ifdef DEBUG
printf("pos: '%s'\n", pos);
#endif
}
}
#ifdef DEBUG
printf("mes:\n'%s'\n", mes);
#endif
}
/* insert \r in front of all \n if it is not present allready */
void insert_cr(char *mes){
char *lf, *pos, *backup;
pos = mes;
lf = strchr(pos, '\n');
while ((lf != NULL) && (*(--lf) != '\r')) {
backup=str_alloc(strlen(lf)+2);
strcpy(backup, lf+1);
*(lf+1) = '\r';
strcpy(lf+2, backup);
free(backup);
pos = lf+3;
lf = strchr(pos, '\n');
}
lf = STRCASESTR(mes, "\r\n\r\n");
if (lf == NULL) {
lf = mes + strlen(mes);
sprintf(lf, "\r\n");
}
}
/* sipmly swappes the content of the two buffers */
void swap_buffers(char *fst, char *snd) {
char *tmp;
if (fst == snd)
return;
tmp = str_alloc(strlen(fst)+1);
strcpy(tmp, fst);
strcpy(fst, snd);
strcpy(snd, tmp);
free(tmp);
}
void swap_ptr(char **fst, char **snd)
{
char *tmp;
tmp = *fst;
*fst = *snd;
*snd = tmp;
}
/* trashes one character in buff randomly */
void trash_random(char *message)
{
int r;
float t;
char *position;
t=(float)rand()/RAND_MAX;
r=(int)(t * (float)strlen(message));
position=message+r;
r=(int)(t*(float)255);
*position=(char)r;
if (verbose > 2)
printf("request:\n%s\n", message);
}
/* this function is taken from traceroute-1.4_p12
which is distributed under the GPL and it returns
the difference between to timeval structs */
double deltaT(struct timeval *t1p, struct timeval *t2p)
{
register double dt;
dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
(double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
return (dt);
}
/* returns one if the string contains only numbers otherwise zero */
int is_number(char *number)
{
int digit = 1;
while (digit && (*number != '\0')) {
digit = isdigit(*number);
number++;
}
return digit;
}
/* tries to convert the given string into an integer. it strips
* white-spaces and exits if an error happens */
int str_to_int(char *num)
{
int ret;
#ifdef HAVE_STRTOL
errno = 0;
ret = strtol(num, NULL, 10);
if (errno == EINVAL || errno == ERANGE) {
printf("%s\n", num);
perror("integer converting error");
exit_code(2);
}
#else
char backup;
int len = strlen(num);
char *start = num;
char *end = num + len;
while (!isdigit(*start) && isspace(*start) && start < end)
start++;
end = start;
end++;
while (end < num + len && *end != '\0' && !isspace(*end))
end++;
backup = *end;
*end = '\0';
if (!is_number(start)) {
fprintf(stderr, "error: string is not a number: %s\n", start);
exit_code(2);
}
ret = atoi(start);
*end = backup;
if (ret <= 0) {
fprintf(stderr, "error: failed to convert string to integer: %s\n", num);
exit_code(2);
}
#endif
return ret;
}
/* reads into the given buffer from standard input until the EOF
* character, LF character or the given size of the buffer is exceeded */
int read_stdin(char *buf, int size, int ret)
{
int i, j;
for(i = 0; i < size - 1; i++) {
j = getchar();
if (((ret == 0) && (j == EOF)) ||
((ret == 1) && (j == '\n'))) {
*(buf + i) = '\0';
return i;
}
else {
*(buf + i) = j;
}
}
*(buf + i) = '\0';
if (verbose)
fprintf(stderr, "warning: readin buffer size exceeded\n");
return i;
}
/* tries to allocate the given size of memory and sets it all to zero.
* if the allocation fails it exits */
void *str_alloc(size_t size)
{
char *ptr;
#ifdef HAVE_CALLOC
ptr = calloc(1, size);
#else
ptr = malloc(size);
#endif
if (ptr == NULL) {
fprintf(stderr, "error: memory allocation failed\n");
exit_code(255);
}
#ifndef HAVE_CALLOC
memset(ptr, 0, size);
#endif
return ptr;
}