File: debian-ipv6-support.patch

package info (click to toggle)
netselect 0.3.ds1-31
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: ansic: 1,409; sh: 629; makefile: 38
file content (1191 lines) | stat: -rw-r--r-- 32,520 bytes parent folder | download
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
Description: Add IPv6 support to netselect
 Patch to add IPv6 support to netselect. The patch makes it possible to
 query IPv6 hosts and determine if they are elligible for testing. It
 avoids the errors that netselect finds when the DNS query returns both
 a IPv4 and an IPv6 address.

 The patch also introduced two new options to netselect: -4, to test only
 IPv4 hosts and -6 to test only IPv6 hosts.

 Patch is available in Github at https://github.com/rahcollabora/netselect/commit/0420717c3aebcb9a517fcfc7cd62cce95783ec58
Author: Bob Ham <bob.ham@collabora.com>
Origin: vendor
Bug: https://github.com/apenwarr/netselect/pull/5
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=407767
Forwarded: yes
Reviewed-by: Javier Fernández-Sanguino Peña <jfs@debian.org>
Last-Update: 2025-01-09 <YYYY-MM-DD, last update of the meta-information, optional>
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/netselect.c	(revision 26675)
+++ b/netselect.c	(working copy)
@@ -59,6 +59,8 @@
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
+#include <netinet/ip6.h>
+#include <netinet/icmp6.h>
 #include <netinet/udp.h>
 #include <arpa/inet.h>
 #include <netdb.h>
@@ -100,7 +102,26 @@
     struct timeval tv;		/* time packet left */
 }__attribute__((packed)) IPacket;
 
+/*
+ * format of a (headerless udp) probe packet.
+ */
+typedef struct
+{
+    struct udphdr udp;
+    u_char seq;			/* sequence number of this packet */
+    u_char ttl;			/* ttl packet left with */
+    struct timeval tv;		/* time packet left */
+} OPacket6;
 
+/*
+ * format of a (icmpv6) probe packet.
+ */
+typedef struct
+{
+    struct icmp6_hdr icmp6;	/* icmp6 contains seq (id) and ttl (seq)
+                                 * fields already */
+    struct timeval tv;		/* time packet left */
+}__attribute__((packed)) I6Packet;
 
 /* 
  * currently-known information about a host
@@ -114,7 +135,7 @@
     char *dnsname;		/* DNS name of the host, matches hostname
                                  * unless a host resolves to multiple IP
                                  * addresses */
-    struct sockaddr_in addr;	/* remote address */
+    struct sockaddr_storage addr;	/* remote address */
 
     int invalid;		/* !=0 if we discard this host */
     int done;			/* !=0 if host testing is done */
@@ -132,23 +153,30 @@
 
 /* prototypes for functions in this file */
 static HostData *add_host(HostData *host, int *numhosts,
-			  char *hostname, struct sockaddr_in *addr,
+			  char *hostname, struct sockaddr *addr,
                           char *dnsname, int max_ttl);
 static char *un_url(char *orig);
-static char *fix_url(char *orig, char *hostname);
+static char *fix_url(char *orig, struct sockaddr *addr);
 static HostData *name_resolver(int *numhosts, int numnames, char **names,
 			       int max_ttl);
 
 static void send_probe(int seq, int ttl, OPacket *op,
 		       HostData *host);
+static void send_probe6(int seq, int ttl, OPacket6 *op,
+		       HostData *host);
 static void send_icmp_probe(int seq, int ttl, IPacket *op,
 		       HostData *host);
+static void send_icmp6_probe(int seq, int ttl, I6Packet *op,
+		       HostData *host);
 static time_t deltaT(struct timeval *t1p, struct timeval *t2p);
 static HostData *wait_for_reply(HostData *hosts, int numhosts,
-				       int sock, int msec_timeout);
+				       int msec_timeout);
 static HostData *packet_ok(HostData *hosts, int numhosts,
 				  u_char *buf, int cc,
 				  struct sockaddr_in *from);
+static HostData *packet6_ok(HostData *hosts, int numhosts,
+				  u_char *buf, int cc,
+				  struct sockaddr_in6 *from);
 static int choose_ttl(HostData *host);
 static void usage();
 static int results(HostData *hosts, int numhosts, int num_score, int use_dns);
@@ -159,7 +187,9 @@
 
 /* global variables */
 static int rcvsock;		/* receive (icmp) socket file descriptor */
+static int rcvsock6;		/* IPv6 receive (icmp) socket file descriptor */
 static int sndsock;		/* send (udp) socket file descriptor */
+static int sndsock6;		/* IPv6 send socket file descriptor */
 
 static int verbose = 0;
 static u_short ident;
@@ -167,6 +197,8 @@
 
 static int validhosts;
 
+static int addr_fam = AF_UNSPEC;
+
 int main(int argc, char **argv)
 {
     extern char *optarg;
@@ -173,15 +205,18 @@
     extern int optind;
     int hostcount, startcount, endcount = 0, sent_one, lag, min_lag = 100;
     int ch, seq, ttl, max_ttl = 30, num_score = 1;
-    int use_icmp, use_dns, found = 0;
+    int use_icmp = 0, use_dns = 0, found = 0;
+    int sock_v6_only = 1;
     unsigned int min_tries = 10;
     struct timeval now;
     struct timezone tz;
     OPacket udppacket;          /* last output (udp) packet */
-    IPacket icmppacket;          /* last output (udp) packet */
+    IPacket icmppacket;         /* last output (icmp) packet */
+    OPacket6 udppacket6;        /* last output (headerless udp) packet */
+    I6Packet icmp6packet;       /* last output (icmp6) packet */
     
     HostData *host, *hosts;
-    int numhosts, delay, must_continue, count;
+    int numhosts, delay, must_continue, count, port_unreachable, other_unreachable;
     int socket_errno = 0;
 
     /* we might have cap_net_raw on linux, instead print a tip if and when
@@ -190,8 +225,9 @@
         fprintf (stderr, "%s: root privileges required\n", argv[0]);
     */
 
-    if ((rcvsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0
-     || (sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW )) < 0)
+    if ((rcvsock  = socket(AF_INET,  SOCK_RAW, IPPROTO_ICMP)) < 0
+     || (sndsock  = socket(AF_INET,  SOCK_RAW, IPPROTO_RAW )) < 0
+     || (rcvsock6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
     {
 	/* Capture errno so that command-line options can be parsed.
 	   We delay reporting an error until this has happened. */
@@ -206,7 +242,7 @@
 #ifdef __EMX__
     _response(&argc,&argv);
 #endif
-    while ((ch = getopt(argc, argv, "s:t:m:IDv?")) != EOF)
+    while ((ch = getopt(argc, argv, "s:t:m:IDv?46")) != EOF)
     {
 	switch (ch)
 	{
@@ -240,6 +276,14 @@
             use_dns = 1;
 	    break;
 
+	case '4':
+            addr_fam = AF_INET;
+	    break;
+
+	case '6':
+            addr_fam = AF_INET6;
+	    break;
+
 	case 'v':
 	    verbose++;
 	    break;
@@ -272,6 +316,19 @@
 	return 5;
     }
 
+    if (addr_fam == AF_INET)
+    {
+	close(rcvsock6);
+	rcvsock6 = -1;
+    }
+    else if (addr_fam == AF_INET6)
+    {
+	close(rcvsock);
+	rcvsock = -1;
+    }
+
+    ident = (getpid() & 0xffff) | 0x8000;
+
     if ( use_icmp )  
     {
         memset(&icmppacket, 0, sizeof(IPacket));
@@ -278,6 +335,8 @@
         icmppacket.ip.ip_tos = 0;
         icmppacket.ip.ip_v = IPVERSION;
         icmppacket.ip.ip_id = 0;
+
+        sndsock6 = rcvsock6;
     } 
     else 
     {
@@ -285,10 +344,41 @@
         udppacket.ip.ip_tos = 0;
         udppacket.ip.ip_v = IPVERSION;
         udppacket.ip.ip_id = 0;
+
+	if (addr_fam != AF_INET)
+	{
+	    struct sockaddr_in6 source;
+
+	    if ((sndsock6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+	    {
+		perror("netselect: socket");
+		if (errno == EPERM) {
+		    fprintf(stderr, "You should be root to run netselect.\n");
+		    return 6;
+		}
+		return 5;
+	    }
+
+	    if (setsockopt(sndsock6, IPPROTO_IPV6, IPV6_V6ONLY,
+			   &sock_v6_only, sizeof(int)) < 0)
+	    {
+		perror("setsockopt with IPV6_V6ONLY");
+		return 1;
+	    }
+
+	    memset(&source, 0, sizeof(struct sockaddr_in6));
+	    source.sin6_family = AF_INET6;
+	    source.sin6_port = htons(ident);
+	    memcpy(&source.sin6_addr, &in6addr_any, sizeof(struct in6_addr));
+
+	    if (bind(sndsock6, (struct sockaddr *)&source, sizeof(source)) < 0)
+	    {
+		perror("bind");
+		return 1;
+	    }
+	}
     }
 
-    ident = (getpid() & 0xffff) | 0x8000;
-    
     validhosts = numhosts = 0;
     
     hosts = name_resolver(&numhosts, argc, argv, max_ttl);
@@ -350,12 +440,26 @@
 		    host->retries++;
 		    
 		    host->num_out++;
-                    if ( use_icmp ) 
-                        send_icmp_probe(host->seq, ttl, &icmppacket, host);
-                    else
-                        send_probe(host->seq, ttl, &udppacket, host);
-		    endcount = hostcount;
-		    sent_one = 1;
+		    if (host->addr.ss_family == AF_INET
+		     || host->addr.ss_family == AF_INET6)
+		    {
+			if (host->addr.ss_family == AF_INET)
+			{
+			    if ( use_icmp ) 
+				send_icmp_probe(host->seq, ttl, &icmppacket, host);
+			    else
+				send_probe(host->seq, ttl, &udppacket, host);
+			}
+			else
+			{
+			    if ( use_icmp ) 
+				send_icmp6_probe(host->seq, ttl, &icmp6packet, host);
+			    else
+				send_probe6(host->seq, ttl, &udppacket6, host);
+			}
+			endcount = hostcount;
+			sent_one = 1;
+		    }
 		}
 	    }
 	    else if (host->hops_less_than - host->hops_more_than > 2)
@@ -374,7 +478,7 @@
 	} while (hostcount != startcount);
 	
 	delay = min_lag/2; /* transmit time must be <= min_lag / 2 */
-	if ((host = wait_for_reply(hosts, numhosts, rcvsock, delay)) != NULL)
+	if ((host = wait_for_reply(hosts, numhosts, delay)) != NULL)
 	{
 	    gettimeofday(&now, &tz);
 	    delay = 0;
@@ -383,10 +487,11 @@
 		fprintf(stderr, "%-35s  %5u ms  %3d hops - ", host->shortname,
 		       (unsigned)deltaT(&host->send_time, &now),
 		       choose_ttl(host));
-	    
-	    switch (host->code - 1)
+
+	    port_unreachable = 0;
+	    other_unreachable = 0;
+	    if (host->code == -1)
 	    {
-	    case -2:
 		if (verbose >= 3)
 		    fprintf(stderr, "HIGHER");
 		else if (verbose >= 1)
@@ -396,9 +501,51 @@
 		host->hops_more_than = choose_ttl(host);
 		host->retries = 0;
 		host->num_out--;
-		break;
-		
-	    case ICMP_UNREACH_PORT:
+	    }
+	    else if (host->addr.ss_family == AF_INET)
+	    {
+		switch (host->code - 1)
+		{
+		case ICMP_UNREACH_PORT:
+		    port_unreachable = 1;
+		    break;
+
+		case ICMP_UNREACH_NET:
+		case ICMP_UNREACH_HOST:
+		case ICMP_UNREACH_PROTOCOL:
+		case ICMP_UNREACH_NEEDFRAG:
+		case ICMP_UNREACH_SRCFAIL:
+		case ICMP_UNREACH_FILTER_PROHIB:
+		case ICMP_UNREACH_NET_PROHIB:	/* misuse */
+		case ICMP_UNREACH_HOST_PROHIB:
+		case ICMP_UNREACH_NET_UNKNOWN:
+		case ICMP_UNREACH_HOST_UNKNOWN:
+		case ICMP_UNREACH_ISOLATED:
+		case ICMP_UNREACH_TOSNET:
+		case ICMP_UNREACH_TOSHOST:
+		    other_unreachable = 1;
+		    break;
+		}
+	    }
+	    else if (host->addr.ss_family == AF_INET6)
+	    {
+		switch (host->code - 1)
+		{
+		case ICMP6_DST_UNREACH_NOPORT:
+		    port_unreachable = 1;
+		    break;
+
+		case ICMP6_DST_UNREACH_NOROUTE:
+		case ICMP6_DST_UNREACH_ADMIN:
+		case ICMP6_DST_UNREACH_BEYONDSCOPE:
+		case ICMP6_DST_UNREACH_ADDR:
+		    other_unreachable = 1;
+		    break;
+		}
+	    }
+
+	    if (port_unreachable)
+	    {
 		if (verbose >= 3)
 		    fprintf(stderr, "OK");
 		else if (verbose >= 1)
@@ -414,27 +561,14 @@
 			fprintf(stderr, "\nmin_lag is now %d", min_lag);
 		}
 		host->total_lag += lag;
-		break;
-
-	    case ICMP_UNREACH_NET:
-	    case ICMP_UNREACH_HOST:
-	    case ICMP_UNREACH_PROTOCOL:
-	    case ICMP_UNREACH_NEEDFRAG:
-	    case ICMP_UNREACH_SRCFAIL:
-	    case ICMP_UNREACH_FILTER_PROHIB:
-	    case ICMP_UNREACH_NET_PROHIB:	/* misuse */
-	    case ICMP_UNREACH_HOST_PROHIB:
-	    case ICMP_UNREACH_NET_UNKNOWN:
-	    case ICMP_UNREACH_HOST_UNKNOWN:
-	    case ICMP_UNREACH_ISOLATED:
-	    case ICMP_UNREACH_TOSNET:
-	    case ICMP_UNREACH_TOSHOST:
+	    }
+	    else if (other_unreachable)
+	    {
 		if (verbose >= 3)
 		    fprintf(stderr, "unreachable! (blocking probes?)\n");
 		if (!host->invalid)
 		    validhosts--;
 		host->invalid = 1;
-		break;
 	    }
 
 	    if (verbose >= 3)
@@ -463,6 +597,10 @@
     return 0;
 }
 
+static inline int in6_addr_cmp(struct in6_addr *a, struct in6_addr *b)
+{
+    return memcmp(&a->s6_addr, &b->s6_addr, 16);
+}
 
 /* 
  * when the newly-created objects are freed, we try to free(hostname)...
@@ -470,7 +608,7 @@
  * function!
  */
 static HostData *add_host(HostData *hosts, int *numhosts,
-			  char *hostname, struct sockaddr_in *addr,
+			  char *hostname, struct sockaddr *addr,
 			  char *dnsname, int max_ttl)
 {
     HostData *host;
@@ -477,14 +615,51 @@
     
     if (addr)
     {
-      int hcount;
-      for (hcount = 0, host = hosts; hcount < *numhosts; hcount++, host++)
-	if (host->addr.sin_addr.s_addr == addr->sin_addr.s_addr)
+	int hcount;
+	sa_family_t family = ((struct sockaddr_storage *)addr)->ss_family;
+	struct in_addr *addr4, *haddr4;
+	struct in6_addr *addr6, *haddr6;
+	
+	
+	if ((family != AF_INET  && family   != AF_INET6)
+	 || (family == AF_INET  && addr_fam == AF_INET6)
+	 || (family == AF_INET6 && addr_fam == AF_INET ))
+        {
+	    if (verbose >= 1)
+		fprintf(stderr, "\nUnsupported address family %hu for host %s address\n",
+			family, hostname);
+	    return hosts;
+        }
+
+	if (family == AF_INET)
+	    addr4 = &((struct sockaddr_in  *)addr)->sin_addr;
+	else
+	    addr6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
+	
+	for (hcount = 0, host = hosts; hcount < *numhosts; hcount++, host++)
 	{
-	  if (verbose >= 1)
-	    fprintf(stderr, "\nDuplicate address %s (%s, %s); keeping only under first name.\n",
-		    inet_ntoa(addr->sin_addr), host->hostname, hostname);
-	  return hosts;
+	    if (host->invalid || host->addr.ss_family != family)
+		continue;
+	    
+	    if (family == AF_INET)
+		haddr4 = &((struct sockaddr_in  *)&host->addr)->sin_addr;
+	    else
+		haddr6 = &((struct sockaddr_in6 *)&host->addr)->sin6_addr;
+
+	    if ((family == AF_INET  && haddr4->s_addr == addr4->s_addr)
+	     || (family == AF_INET6 && in6_addr_cmp(haddr6, addr6) == 0))
+	    {
+		if (verbose >= 1)
+		{
+		    char txt[INET6_ADDRSTRLEN];
+		    fprintf(stderr, "\nDuplicate address %s (%s, %s); keeping only under first name.\n",
+			    inet_ntop(family,
+				      family == AF_INET ? (void *)addr4 : (void *)addr6,
+				      txt, sizeof(txt)),
+			    host->hostname, hostname);
+		}
+		return hosts;
+	    }
 	}
     }
     
@@ -502,7 +677,7 @@
     host->dnsname = dnsname;
     host->shortname = un_url(hostname);
     if (addr)
-	memcpy(&host->addr, addr, sizeof(*addr));
+	memcpy(&host->addr, addr, sizeof(struct sockaddr_storage));
     else
 	host->invalid = 1;
     host->hops_less_than = max_ttl;
@@ -513,62 +688,132 @@
 
 static char *un_url(char *orig)
 {
-    char *sptr, *eptr, *newbuf;
+    char *sptr, *eptr = NULL, *newbuf;
     
     if ((sptr = strstr(orig, "://")) != NULL)
     {
 	/* URL formatted, like: http://hostname:port/dir/file */
 	sptr += 3; /* skip :// */
-	eptr = strchr(sptr, ':');
+	if (*sptr == '[') /* v6 literal address */
+	{
+	    eptr = strchr(sptr, ']');
+	    if (eptr)
+		++sptr;
+	}
+	if (!eptr) eptr = strchr(sptr, ':');
 	if (!eptr) eptr = strchr(sptr, '/');
 	if (!eptr) eptr = strchr(sptr, 0);
-	
+    }
+    else
+    {
+	if (*orig == '[')
+	{
+	    /* quoted v6 literal address in non-URL format */
+	    eptr = strchr(orig, ']');
+	    if (eptr)
+		sptr = orig + 1;
+	}
+
+	if (!sptr && (eptr = strchr(orig, ':')) != NULL)
+	{
+	    /* Could be an IPv6 address */
+	    struct sockaddr_storage ss;
+	    if (inet_pton(AF_INET6, orig, &ss) != 1)
+	    {
+		/* FTP formatted, like: ftp.debian.org:/debian/foo */
+		sptr = orig;
+	    }
+	}
+    }
+
+    if (sptr)
+    {
 	newbuf = (char *)malloc(eptr-sptr+1);
 	strncpy(newbuf, sptr, eptr-sptr);
 	newbuf[eptr-sptr] = 0;
 	return newbuf;
     }
-    else if ((sptr = strchr(orig, ':')) != NULL)
-    {
-	/* FTP formatted, like: ftp.debian.org:/debian/foo */
-	newbuf = (char *)malloc(sptr-orig+1);
-	strncpy(newbuf, orig, sptr-orig);
-	newbuf[sptr-orig] = 0;
-	return newbuf;
-    }
     else /* just plain */
 	return strdup(orig);
 }
 
+static char *fix_url(char *orig, struct sockaddr *addr)
+{
+    char *pree = NULL, *posts = NULL;
+    char addrstr[INET6_ADDRSTRLEN + 2];
+    void *src;
 
-static char *fix_url(char *orig, char *hostname)
-{
-    char *sptr, *eptr, *newbuf;
-    
-    if ((sptr = strstr(orig, "://")) != NULL)
+    if ((pree = strstr(orig, "://")) != NULL)
     {
 	/* URL formatted, like: http://hostname:port/dir/file */
-	sptr += 3; /* skip :// */
-	eptr = strchr(sptr, ':');
-	if (!eptr) eptr = strchr(sptr, '/');
-	if (!eptr) eptr = strchr(sptr, 0);
+	pree += 3; /* skip :// */
+	if (*pree == '[') /* v6 literal address */
+	{
+	    posts = strchr(pree, ']');
+	    if (posts)
+		++posts;
+	}
+	if (!posts) posts = strchr(pree, ':');
+	if (!posts) posts = strchr(pree, '/');
+	if (!posts) posts = strchr(pree, 0);
+    }
+    else
+    {
+	pree = orig;
+	if (*pree == '[')
+	{
+	    /* quoted v6 literal address in non-URL format */
+	    posts = strchr(pree, ']');
+	    if (posts)
+		++posts;
+	}
 	
-	newbuf = (char *)malloc(strlen(orig) + strlen(hostname) + 1);
-	strncpy(newbuf, orig, sptr-orig);
-	strcpy(newbuf+(sptr-orig), hostname);
-	strcat(newbuf, eptr);
-	return newbuf;
+	if (!posts)
+	{
+	    struct sockaddr_storage ss;
+	    if (inet_pton(AF_INET6, orig, &ss) != 1)
+	    {
+		/* FTP formatted, like: ftp.debian.org:/debian/foo */
+		posts = strchr(orig, ':');
+	    }
+	}
     }
-    else if ((sptr = strchr(orig, ':')) != NULL)
+
+    if (addr->sa_family == AF_INET)
+	src = &((struct sockaddr_in  *)addr)->sin_addr;
+    else
+	src = &((struct sockaddr_in6 *)addr)->sin6_addr;
+
+    inet_ntop(addr->sa_family, src, addrstr, sizeof(addrstr));
+
+    if (posts)
     {
-	/* FTP formatted, like: ftp.debian.org:/debian/foo */
-	newbuf = (char *)malloc(strlen(orig) + strlen(hostname) + 1);
-	strcpy(newbuf, hostname);
-	strcat(newbuf, sptr);
+	char *newbuf;
+	size_t addrlen;
+	size_t prelen = pree - orig;
+	size_t postlen = strlen(posts);
+
+	/* We want to quote a v6 address in this case */
+	if (addr->sa_family == AF_INET6)
+	{
+	    char quoted_addr[sizeof(addrstr)];
+	    sprintf(quoted_addr, "[%s]", addrstr);
+	    strcpy(addrstr, quoted_addr);
+	}
+
+	addrlen = strlen(addrstr);
+
+	newbuf = (char *)malloc(prelen + addrlen + postlen + 1);
+
+	if (prelen > 0)
+	    strncpy(newbuf, orig, prelen);
+	strncpy(newbuf + prelen, addrstr, addrlen);
+	strncpy(newbuf + prelen + addrlen, posts, postlen);
+
 	return newbuf;
     }
     else /* just plain */
-	return strdup(hostname);
+	return strdup(addrstr);
 }
 
 
@@ -582,7 +827,7 @@
     struct {
 	char *hostname;
 	int broken, multi;
-	struct sockaddr_in addr;
+	struct sockaddr_storage addr;
     } result;
     
     HostData *hosts = NULL;
@@ -591,8 +836,9 @@
     time_t start = time(NULL);
     fd_set rfd, wfd, efd;
     struct timeval tv;
-    struct hostent *hp;
+    struct addrinfo *hp, hints;
     pid_t pid;
+    int err;
     
     if (pipe(pipes))
     {
@@ -605,6 +851,10 @@
     setmode(pipes[0],O_BINARY);
     setmode(pipes[1],O_BINARY);
 #endif
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = addr_fam;
+    hints.ai_flags  = AI_ADDRCONFIG;
     
     while (time(NULL) < start + 20)
     {
@@ -628,45 +878,52 @@
 		result.multi = result.broken = 0;
 		
 		/* child task -- actually do name lookup */
-		result.addr.sin_family = AF_INET;
-		result.addr.sin_addr.s_addr = inet_addr(names[count]);
+
+		/* try simple IPv4 dotted-quad conversion */
+		if (addr_fam != AF_INET6)
+		{
+		    struct sockaddr_in *addr4 = (struct sockaddr_in *) &result.addr;
+		    addr4->sin_family = AF_INET;
+		    addr4->sin_addr.s_addr = inet_addr(names[count]);
 		
-		if (result.addr.sin_addr.s_addr != INADDR_NONE)
-		{
-		    write(pipes[1], &result, sizeof(result));
+		    if (addr4->sin_addr.s_addr != INADDR_NONE)
+		    {
+			write(pipes[1], &result, sizeof(result));
+			/* Use this as a flag to not do a lookup */
+			result.multi = 1;
+		    }
 		}
-		else
+
+		if (result.multi == 0);
 		{
 		    /* must actually do the name lookup */
 		    char *simplename = un_url(result.hostname);
-		    hp = gethostbyname(simplename);
+		    err = getaddrinfo(simplename, NULL, &hints, &hp);
 		    free(simplename);
 		    
-		    if (hp)
+		    if (!err)
 		    {
-			if (hp->h_addrtype != AF_INET 
-			    || hp->h_length != sizeof(struct in_addr))
+			struct addrinfo *ptr;
+			int already_seen_one = 0;
+
+			result.broken = 0;
+			for (ptr = hp; ptr; ptr = ptr->ai_next)
 			{
-			    result.broken = 1;
+			    if ((ptr->ai_family != AF_INET  && ptr->ai_family != AF_INET6)
+			     || (ptr->ai_family == AF_INET  && addr_fam == AF_INET6)
+			     || (ptr->ai_family == AF_INET6 && addr_fam == AF_INET))
+				continue;
+
+			    if (already_seen_one)
+				result.multi = 1;
+
+			    memcpy(&result.addr, ptr->ai_addr, ptr->ai_addrlen);
 			    write(pipes[1], &result, sizeof(result));
+			    if (!already_seen_one)
+				already_seen_one = 1;
 			}
-			else
-			{
-			    char **ptr = hp->h_addr_list;
-			    
-			    result.broken = 0;
-			    if (ptr[1])
-				result.multi = 1;
-			    result.addr.sin_family = AF_INET;
-			    
-			    while (*ptr)
-			    {
-				memcpy(&result.addr.sin_addr,
-				       *ptr, hp->h_length);
-				write(pipes[1], &result, sizeof(result));
-				ptr++;
-			    }
-			}
+
+			freeaddrinfo(hp);
 		    }
 		    else
 		    {
@@ -747,12 +1004,12 @@
 		    
 		    if (result.multi)
 			newhostname = fix_url(result.hostname,
-					      inet_ntoa(result.addr.sin_addr));
+					      (struct sockaddr *)&result.addr);
 		    else
 			newhostname = strdup(result.hostname);
 		    
 		    hosts = add_host(hosts, numhosts, newhostname,
-				     &result.addr, dnsname, max_ttl);
+				     (struct sockaddr *)&result.addr, dnsname, max_ttl);
 		}
 	    }
 	}
@@ -779,6 +1036,35 @@
     return hosts;
 }
 
+static void do_sendto(int sockfd, const void *buf, size_t len, HostData *host)
+{
+    int i;
+    i = sendto(sockfd, buf, len, 0, (struct sockaddr *)&host->addr,
+	       sizeof(struct sockaddr_storage));
+    if (i < 0 || i != len)
+    {
+	if (i < 0)
+	{
+	    switch (errno)
+	    {
+	    case ENETDOWN:
+	    case ENETUNREACH:
+	    case EHOSTDOWN:
+	    case EHOSTUNREACH:
+		if (verbose >= 3)
+		    fprintf(stderr, "unreachable or down!\n");
+		if (!host->invalid)
+		    validhosts--;
+		host->invalid = 1;
+		break;
+		
+	    default:
+		perror("sendto");
+	    }
+	}
+	fflush(stdout);
+    }
+}
 
 static void send_probe(int seq, int ttl, OPacket *op, HostData *host)
 {
@@ -785,9 +1071,8 @@
     struct ip *ip = &op->ip;
     struct udphdr *up = &op->udp;
     struct timezone tz;
-    int i;
 
-    op->ip.ip_dst = host->addr.sin_addr;
+    op->ip.ip_dst = ((struct sockaddr_in *)&host->addr)->sin_addr;
     op->seq = seq;
     op->ttl = ttl;
     gettimeofday(&op->tv, &tz);
@@ -808,31 +1093,30 @@
     if (verbose >= 4)
     	fprintf(stderr, "%-35s(UDP)>>\n", host->shortname);
 
-    i = sendto(sndsock, op, sizeof(OPacket), 0,
-	       (struct sockaddr *)&host->addr, sizeof(host->addr));
-    if (i < 0 || i != sizeof(OPacket))
+    do_sendto(sndsock, op, sizeof(OPacket), host);
+}
+
+static void send_probe6(int seq, int ttl, OPacket6 *op, HostData *host)
+{
+    struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&host->addr;
+    struct timezone tz;
+
+    op->seq = seq;
+    op->ttl = ttl;
+    gettimeofday(&op->tv, &tz);
+
+    addr->sin6_port = htons(port + seq);
+    
+    if (verbose >= 4)
+    	fprintf(stderr, "%-35s(UDP6)>>\n", host->shortname);
+
+    if (setsockopt(sndsock6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
     {
-	if (i < 0)
-	{
-	    switch (errno)
-	    {
-	    case ENETDOWN:
-	    case ENETUNREACH:
-	    case EHOSTDOWN:
-	    case EHOSTUNREACH:
-		if (verbose >= 3)
-		    fprintf(stderr, "unreachable or down!\n");
-		if (!host->invalid)
-		    validhosts--;
-		host->invalid = 1;
-		break;
-		
-	    default:
-		perror("sendto");
-	    }
-	}
-	fflush(stdout);
+	perror("setsockopt with IPV6_UNICAST_HOPS");
+	return;
     }
+
+    do_sendto(sndsock6, op, sizeof(OPacket6), host);
 }
 
 uint16_t
@@ -852,9 +1136,8 @@
     struct ip *ip = &op->ip;
     struct icmp *icmp = &op->icmp;
     struct timezone tz;
-    int i;
 
-    op->ip.ip_dst = host->addr.sin_addr;
+    op->ip.ip_dst = ((struct sockaddr_in *)&host->addr)->sin_addr;
     op->seq = seq;
     op->ttl = ttl;
     gettimeofday(&op->tv, &tz);
@@ -879,31 +1162,34 @@
     if (verbose >= 5)
         fprintf(stderr, "ICMP sequence: %i, identifier: %i\n", icmp->icmp_seq, icmp->icmp_id);
 
-    i = sendto(sndsock, op, sizeof(IPacket), 0,
-	       (struct sockaddr *)&host->addr, sizeof(host->addr));
-    if (i < 0 || i != sizeof(IPacket))
+    do_sendto(sndsock, op, sizeof(IPacket), host);
+}
+
+static void send_icmp6_probe(int seq, int ttl, I6Packet *op, HostData *host)
+{
+    struct icmp6_hdr *icmp = &op->icmp6;
+    struct timezone tz;
+
+    gettimeofday(&op->tv, &tz);
+
+    icmp->icmp6_type = ICMP6_ECHO_REQUEST;
+    icmp->icmp6_code = 0;
+    icmp->icmp6_cksum = 0;
+    icmp->icmp6_id = htons (ident);
+    icmp->icmp6_seq = htons (seq);
+
+    if (verbose >= 4)
+    	fprintf(stderr, "%-35s(ICMP6)>>\n", host->shortname);
+    if (verbose >= 5)
+        fprintf(stderr, "ICMP6 sequence: %i, identifier: %i\n", icmp->icmp6_seq, icmp->icmp6_id);
+
+    if (setsockopt(sndsock6, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
     {
-	if (i < 0)
-	{
-	    switch (errno)
-	    {
-	    case ENETDOWN:
-	    case ENETUNREACH:
-	    case EHOSTDOWN:
-	    case EHOSTUNREACH:
-		if (verbose >= 3)
-		    fprintf(stderr, "unreachable or down!\n");
-		if (!host->invalid)
-		    validhosts--;
-		host->invalid = 1;
-		break;
-		
-	    default:
-		perror("sendto");
-	    }
-	}
-	fflush(stdout);
+	perror("setsockopt with IPV6_UNICAST_HOPS");
+	return;
     }
+
+    do_sendto(sndsock6, op, sizeof(I6Packet), host);
 }
 
 
@@ -913,27 +1199,59 @@
 	 + (t2p->tv_usec - t1p->tv_usec) / 1000;
 }
 
+static HostData * receive(HostData *hosts, int numhosts, int sock)
+{
+    u_char inpacket[INPACKET_SIZE];
+    struct sockaddr_storage from;
+    int cc = 0;
+    HostData *host = NULL;
 
+#if !defined(__GLIBC__)
+    int fromlen = sizeof(from);
+#else				/* __GLIBC__ */
+    socklen_t fromlen = sizeof(from);
+#endif				/* __GLIBC__ */
+
+    cc = recvfrom(sock, inpacket, INPACKET_SIZE, 0,
+		  (struct sockaddr *) &from, &fromlen);
+    if (cc > 0 &&
+	(addr_fam == AF_UNSPEC || from.ss_family == addr_fam))
+    {
+	if (from.ss_family == AF_INET)
+	    host = packet_ok(hosts, numhosts, inpacket, cc,
+			     (struct sockaddr_in *)&from);
+	else
+	    host = packet6_ok(hosts, numhosts, inpacket, cc,
+			      (struct sockaddr_in6 *)&from);
+    }
+
+    return host;
+}
+
 static HostData *wait_for_reply(HostData *hosts, int numhosts,
-				       int sock, int msec_timeout)
+				       int msec_timeout)
 {
     fd_set fds;
     struct timeval wait, start_time;
     struct timezone tz;
-    u_char inpacket[INPACKET_SIZE];
-    struct sockaddr_in from;
-    int cc = 0;
     time_t msec_used;
     HostData *host;
+    int nfds = -1;
+    int sock;
     
-#if !defined(__GLIBC__)
-    int fromlen = sizeof(from);
-#else				/* __GLIBC__ */
-    socklen_t fromlen = sizeof(from);
-#endif				/* __GLIBC__ */
-
     FD_ZERO(&fds);
-    FD_SET(sock, &fds);
+    if (addr_fam != AF_INET6)
+    {
+	FD_SET(rcvsock,  &fds);
+	nfds = rcvsock;
+    }
+    if (addr_fam != AF_INET)
+    {
+	FD_SET(rcvsock6, &fds);
+	if (nfds < rcvsock6)
+	    nfds = rcvsock6;
+    }
+    ++nfds;
     
     gettimeofday(&start_time, &tz);
 
@@ -947,16 +1265,19 @@
 	wait.tv_usec = (msec_timeout - msec_used) * 1000;
 	wait.tv_sec = 0;
 	
-	if (select(sock + 1, &fds, NULL, NULL, &wait) > 0)
+	if (select(nfds, &fds, NULL, NULL, &wait) > 0)
 	{
-	    cc = recvfrom(rcvsock, inpacket, INPACKET_SIZE, 0,
-			  (struct sockaddr *) &from, &fromlen);
-	    if (cc > 0)
-	    {
-		if ((host = packet_ok(hosts, numhosts, inpacket, cc, &from))
-		            != NULL)
-		    return host;
-	    }
+	    if (addr_fam == AF_INET)
+		sock = rcvsock;
+	    else if (addr_fam == AF_INET6)
+		sock = rcvsock6;
+	    else if (FD_ISSET(rcvsock, &fds))
+		sock = rcvsock;
+	    else
+		sock = rcvsock6;
+
+	    if ((host = receive(hosts, numhosts, sock)) != NULL)
+		return host;
 	}
     }
     
@@ -974,6 +1295,7 @@
     struct icmp *icp;
     HostData *host;
     int hcount;
+    struct sockaddr_in *haddr;
 
     ip = (struct ip *) buf;
     hlen = ip->ip_hl << 2;
@@ -1001,11 +1323,13 @@
 	
 	for (hcount = 0, host = hosts; hcount < numhosts; hcount++, host++)
 	{
-	    if (host->invalid) continue;
+	    if (host->invalid || host->addr.ss_family != AF_INET) continue;
 
+	    haddr = (struct sockaddr_in *)&host->addr;
+
             /* Valid ICMP echo reply packet */
             if ( type == ICMP_ECHOREPLY &&
-                 ip->ip_src.s_addr == host->addr.sin_addr.s_addr &&
+                 ip->ip_src.s_addr == haddr->sin_addr.s_addr &&
                  icp->icmp_id == htons(ident) && 
                  icp->icmp_seq == htons(host->seq) )
             {
@@ -1014,7 +1338,7 @@
             }
             /* Time exceeded reply to an ICMP echo request */
             if ( type == ICMP_TIMXCEED &&
-                 hip->ip_dst.s_addr == host->addr.sin_addr.s_addr &&
+                 hip->ip_dst.s_addr == haddr->sin_addr.s_addr &&
                  hip->ip_id == htons(ident+host->seq) )
             {
 		host->code = -1; 
@@ -1025,7 +1349,7 @@
 	    if (hlen + 12 <= cc && hip->ip_p == IPPROTO_UDP &&
 		up->uh_sport == htons(ident) &&
 		up->uh_dport == htons(port + host->seq) &&
-		hip->ip_dst.s_addr == host->addr.sin_addr.s_addr)
+		hip->ip_dst.s_addr == haddr->sin_addr.s_addr)
 	    {
 		host->code = (type == ICMP_TIMXCEED ? -1 : code + 1);
 		return host;
@@ -1038,7 +1362,106 @@
     return NULL;
 }
 
+static HostData *packet6_ok(HostData *hosts, int numhosts,
+				  u_char * buf, int cc,
+				  struct sockaddr_in6 *from)
+{
+    u_char type, code;
+    struct icmp6_hdr *icp;
+    HostData *host;
+    int hcount;
 
+    if (cc < sizeof(struct icmp6_hdr))
+	return NULL;
+
+    icp = (struct icmp6_hdr *) buf;
+
+    type = icp->icmp6_type;
+    code = icp->icmp6_code;
+
+    if (verbose >= 5)
+    {
+	char txt[INET6_ADDRSTRLEN];
+        fprintf(stderr, "Received ICMP6 type: %i, code: %i, from %s\n", type, code,
+		inet_ntop(AF_INET6, &from->sin6_addr, txt, sizeof(txt)) );
+    }
+
+    if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT)
+	|| type == ICMP6_DST_UNREACH || type == ICMP6_ECHO_REPLY )
+    {
+	struct ip6_hdr *hip;
+	int invoke_len;
+
+	hip = (struct ip6_hdr *) (icp + 1);
+
+	if (type != ICMP6_ECHO_REPLY)
+	{
+	    if (cc < (sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)))
+		return NULL;
+
+	    /* Check packet is as big as the following code assumes */
+	    switch (hip->ip6_nxt)
+	    {
+	    case IPPROTO_ICMPV6:
+		invoke_len = sizeof(struct icmp6_hdr);
+		break;
+	    case IPPROTO_UDP:
+		invoke_len = sizeof(struct udphdr);
+		break;
+	    default:
+		return NULL;
+	    };
+
+	    if (cc < (sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr) + invoke_len))
+		return NULL;
+	}
+
+	
+	for (hcount = 0, host = hosts; hcount < numhosts; hcount++, host++)
+	{
+	    struct sockaddr_in6 *haddr;
+
+	    if (host->invalid || host->addr.ss_family != AF_INET6) continue;
+
+	    haddr = (struct sockaddr_in6 *)&host->addr;
+
+            /* Valid ICMP echo reply packet */
+            if ( type == ICMP6_ECHO_REPLY &&
+		 in6_addr_cmp(&from->sin6_addr, &haddr->sin6_addr) == 0 &&
+                 icp->icmp6_id == htons(ident) && 
+                 icp->icmp6_seq == htons(host->seq) )
+            {
+		host->code = ICMP6_DST_UNREACH_NOPORT + 1; /* Behave like if a UDP packet was received even though we used ICMP */
+		return host;
+            }
+            /* Time exceeded reply to an ICMP echo request */
+            if ( type == ICMP6_TIME_EXCEEDED &&
+		 hip->ip6_nxt == IPPROTO_ICMPV6 &&
+                 in6_addr_cmp(&hip->ip6_dst, &haddr->sin6_addr) == 0 &&
+		 ((struct icmp6_hdr *)(hip + 1))->icmp6_id == htons(ident+host->seq) )
+	    {
+		host->code = -1;
+		return host;
+	    }
+	    
+            /* Valid reply to an UDP probe packet */
+	    if ( hip->ip6_nxt == IPPROTO_UDP &&
+		 in6_addr_cmp(&hip->ip6_dst, &haddr->sin6_addr) == 0 &&
+		 ((struct udphdr *)(hip + 1))->uh_sport == htons(ident) &&
+		 ((struct udphdr *)(hip + 1))->uh_dport == htons(port + host->seq) )
+	    {
+		host->code = (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
+		return host;
+	    }
+	}
+    }
+
+    if (verbose >= 3)
+        fprintf(stderr, "received an unknown v6 packet!\n"); 
+    return NULL;
+}
+
+
 static int choose_ttl(HostData *host)
 {
     if (!host || host->invalid)