-
Notifications
You must be signed in to change notification settings - Fork 243
/
netcat.c
executable file
·2234 lines (2070 loc) · 72.3 KB
/
netcat.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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// for license see license.txt
/* Netcat 1.00 951010
A damn useful little "backend" utility begun 950915 or thereabouts,
as *Hobbit*'s first real stab at some sockets programming. Something that
should have and indeed may have existed ten years ago, but never became a
standard Unix utility. IMHO, "nc" could take its place right next to cat,
cp, rm, mv, dd, ls, and all those other cryptic and Unix-like things.
Read the README for the whole story, doc, applications, etc.
Layout:
conditional includes:
includes:
handy defines:
globals:
malloced globals:
cmd-flag globals:
support routines:
main:
todo:
more of the portability swamp, and an updated generic.h
frontend progs to generate various packets, raw or otherwise...
char-mode [cbreak, fcntl-unbuffered, etc...]
connect-to-all-A-records hack
bluesky:
RAW mode!
backend progs to grab a pty and look like a real telnetd?!
*/
#ifdef __MINGW32__
#include <_mingw.h>
#include <winsock2.h>
#endif
#include "generic.h" /* same as with L5, skey, etc */
#ifdef WIN32
#pragma comment (lib, "ws2_32") /* winsock support */
#endif
/* conditional includes -- a very messy section: */
// #undef _POSIX_SOURCE /* might need this for something? */
#define HAVE_BIND /* XXX -- for now, see below... */
#define HAVE_HELP /* undefine if you dont want the help text */
// #define ANAL /* if you want case-sensitive DNS matching */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
#include <malloc.h> /* xxx: or does it live in sys/ ?? */
#endif
/* have to do this *before* including types.h. xxx: Linux still has it wrong */
#ifdef FD_SETSIZE /* should be in types.h, butcha never know. */
#undef FD_SETSIZE /* if we ever need more than 16 active */
#endif /* fd's, something is horribly wrong! */
#ifdef WIN32
#define FD_SETSIZE 64 /* WIN32 does this as an array not a bitfield and it likes 64 */
#else
#define FD_SETSIZE 16 /* <-- this'll give us a long anyways, wtf */
#endif
#include <sys/types.h> /* *now* do it. Sigh, this is broken */
#ifdef WIN32
#undef HAVE_RANDOM
#undef IP_OPTIONS
#undef SO_REUSEPORT
#include <windows.h>
#endif
#ifdef HAVE_RANDOM
#define SRAND srandom
#define RAND random
#else
#define SRAND srand
#define RAND rand
#endif /* HAVE_RANDOM */
/* xxx: these are rsh leftovers, move to new generic.h */
/* will we even need any nonblocking shit? Doubt it. */
/* get FIONBIO from sys/filio.h, so what if it is a compatibility feature */
/* #include <sys/filio.h> */
/*
#include <sys/ioctl.h>
#include <sys/file.h>
*/
/* includes: */
#ifdef WIN32
# include <time.h>
# include <fcntl.h>
# include <io.h>
# include <conio.h>
# include "getopt.h"
# define sleep(_x) Sleep((_x)*1000)
# define EADDRINUSE WSAEADDRINUSE
# define ETIMEDOUT WSAETIMEDOUT
# define ECONNREFUSED WSAECONNREFUSED
#else
# include <sys/time.h> /* timeval, time_t */
# include <setjmp.h> /* jmp_buf et al */
# include <sys/socket.h> /* basics, SO_ and AF_ defs, sockaddr, ... */
# include <netinet/in.h> /* sockaddr_in, htons, in_addr */
# include <netinet/in_systm.h> /* misc crud that netinet/ip.h references */
# include <netinet/ip.h> /* IPOPT_LSRR, header stuff */
# include <netdb.h> /* hostent, gethostby*, getservby* */
# include <arpa/inet.h> /* inet_ntoa */
#endif
#include <stdio.h>
#include <string.h> /* strcpy, strchr, yadda yadda */
#include <errno.h>
#include <signal.h>
/* handy stuff: */
#define SA struct sockaddr /* socket overgeneralization braindeath */
#define SAI struct sockaddr_in /* ... whoever came up with this model */
#define IA struct in_addr /* ... should be taken out and shot, */
/* ... not that TLI is any better. sigh.. */
#define SLEAZE_PORT 31337 /* for UDP-scan RTT trick, change if ya want */
#define USHORT unsigned short /* use these for options an' stuff */
#define BIGSIZ 8192 /* big buffers */
#define SMALLSIZ 256 /* small buffers, hostnames, etc */
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
#ifdef MAXHOSTNAMELEN
#undef MAXHOSTNAMELEN /* might be too small on aix, so fix it */
#endif
#define MAXHOSTNAMELEN 256
struct host_poop {
char name[MAXHOSTNAMELEN]; /* dns name */
char addrs[8][24]; /* ascii-format IP addresses */
struct in_addr iaddrs[8]; /* real addresses: in_addr.s_addr: ulong */
};
#define HINF struct host_poop
struct port_poop {
char name [64]; /* name in /etc/services */
char anum [8]; /* ascii-format number */
USHORT num; /* real host-order number */
};
#define PINF struct port_poop
/* globals: */
static int netfd = -1;
static int ofd = 0; /* hexdump output fd */
static char unknown[] = "(UNKNOWN)";
static char p_tcp[] = "tcp"; /* for getservby* */
static char p_udp[] = "udp";
#ifndef WIN32
#ifdef HAVE_BIND
extern int h_errno;
#endif
#endif
static int gatesidx = 0; /* LSRR hop count */
static int gatesptr = 4; /* initial LSRR pointer, settable */
static USHORT Single = 1; /* zero if scanning */
static unsigned int insaved = 0; /* stdin-buffer size for multi-mode */
static unsigned int wrote_out = 0; /* total stdout bytes */
static unsigned int wrote_net = 0; /* total net bytes */
static char wrote_txt[] = " sent %d, rcvd %d";
static char hexnibs[20] = "0123456789abcdef ";
/* will malloc up the following globals: */
static struct timeval * timer1 = NULL;
static struct timeval * timer2 = NULL;
static SAI * lclend = NULL; /* sockaddr_in structs */
static SAI * remend = NULL;
static HINF ** gates = NULL; /* LSRR hop hostpoop */
static char * optbuf = NULL; /* LSRR or sockopts */
static char * bigbuf_in; /* data buffers */
static char * bigbuf_net;
static fd_set * ding1; /* for select loop */
static fd_set * ding2;
static PINF * portpoop = NULL; /* for getportpoop / getservby* */
static unsigned char * stage = NULL; /* hexdump line buffer */
#ifdef WIN32
static char * setsockopt_c;
static int nnetfd;
static HANDLE w32_stdout;
static CONSOLE_SCREEN_BUFFER_INFO csbi;
#endif
/* global cmd flags: */
static USHORT o_alla = 0;
static unsigned int o_interval = 0;
static USHORT o_listen = 0;
static USHORT o_nflag = 0;
static USHORT o_wfile = 0;
static USHORT o_random = 0;
static USHORT o_udpmode = 0;
static USHORT o_verbose = 0;
static unsigned int o_wait = 0;
static USHORT o_zero = 0;
static USHORT o_esco = 0;
/* Debug macro: squirt whatever to stderr and sleep a bit so we can see it go
by. need to call like Debug ((stuff)) [with no ; ] so macro args match!
Beware: writes to stdOUT... */
#ifdef DEBUG
#define Debug(x) printf x; printf ("\n"); fflush (stdout); sleep (1);
#else
#define Debug(x) /* nil... */
#endif
/* support routines -- the bulk of this thing. Placed in such an order that
we don't have to forward-declare anything: */
static int helpme(); /* oop */
#ifdef WIN32
/* res_init
winsock needs to be initialized. Might as well do it as the res_init
call for Win32 */
static void res_init()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
return;
/* Confirm that the Windows Sockets DLL supports 1.1.*/
/* Note that if the DLL supports versions greater */
/* than 1.1 in addition to 1.1, it will still return */
/* 1.1 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
WSACleanup();
return;
}
}
/* winsockstr
Windows Sockets cannot report errors through perror() so we need to define
our own error strings to print. Someday all the string should be prettied up.
Prettied the errors I usually get */
static char * winsockstr(error)
int error;
{
switch (error)
{
case WSAEINTR : return("INTR");
case WSAEBADF : return("BADF");
case WSAEACCES : return("ACCES");
case WSAEFAULT : return("FAULT");
case WSAEINVAL : return("INVAL");
case WSAEMFILE : return("MFILE");
case WSAEWOULDBLOCK : return("WOULDBLOCK");
case WSAEINPROGRESS : return("INPROGRESS");
case WSAEALREADY : return("ALREADY");
case WSAENOTSOCK : return("NOTSOCK");
case WSAEDESTADDRREQ : return("DESTADDRREQ");
case WSAEMSGSIZE : return("MSGSIZE");
case WSAEPROTOTYPE : return("PROTOTYPE");
case WSAENOPROTOOPT : return("NOPROTOOPT");
case WSAEPROTONOSUPPORT: return("PROTONOSUPPORT");
case WSAESOCKTNOSUPPORT: return("SOCKTNOSUPPORT");
case WSAEOPNOTSUPP : return("OPNOTSUPP");
case WSAEPFNOSUPPORT : return("PFNOSUPPORT");
case WSAEAFNOSUPPORT : return("AFNOSUPPORT");
case WSAEADDRINUSE : return("ADDRINUSE");
case WSAEADDRNOTAVAIL : return("ADDRNOTAVAIL");
case WSAENETDOWN : return("NETDOWN");
case WSAENETUNREACH : return("NETUNREACH");
case WSAENETRESET : return("NETRESET");
case WSAECONNABORTED : return("CONNABORTED");
case WSAECONNRESET : return("CONNRESET");
case WSAENOBUFS : return("NOBUFS");
case WSAEISCONN : return("ISCONN");
case WSAENOTCONN : return("NOTCONN");
case WSAESHUTDOWN : return("SHUTDOWN");
case WSAETOOMANYREFS : return("TOOMANYREFS");
case WSAETIMEDOUT : return("TIMEDOUT");
case WSAECONNREFUSED : return("connection refused");
case WSAELOOP : return("LOOP");
case WSAENAMETOOLONG : return("NAMETOOLONG");
case WSAEHOSTDOWN : return("HOSTDOWN");
case WSAEHOSTUNREACH : return("HOSTUNREACH");
case WSAENOTEMPTY : return("NOTEMPTY");
case WSAEPROCLIM : return("PROCLIM");
case WSAEUSERS : return("USERS ");
case WSAEDQUOT : return("DQUOT");
case WSAESTALE : return("STALE");
case WSAEREMOTE : return("REMOTE ");
case WSAEDISCON : return("DISCON");
case WSASYSNOTREADY : return("SYSNOTREADY");
case WSAVERNOTSUPPORTED: return("VERNOTSUPPORTED");
case WSANOTINITIALISED : return("NOTINITIALISED");
case WSAHOST_NOT_FOUND : return("HOST_NOT_FOUND");
case WSATRY_AGAIN : return("TRY_AGAIN");
case WSANO_RECOVERY : return("NO_RECOVERY");
case WSANO_DATA : return("NO_DATA");
default : return("unknown socket error");
}
}
#endif
/* holler :
fake varargs -- need to do this way because we wind up calling through
more levels of indirection than vanilla varargs can handle, and not all
machines have vfprintf/vsyslog/whatever! 6 params oughta be enough. */
static void holler (str, p1, p2, p3, p4, p5, p6)
char * str;
char * p1, * p2, * p3, * p4, * p5, * p6;
{
if (o_verbose) {
fprintf (stderr, str, p1, p2, p3, p4, p5, p6);
#ifdef WIN32
if (h_errno)
fprintf (stderr, ": %s (%d:%ld)\n",winsockstr(h_errno), h_errno, GetLastError());
#else
if (errno) { /* this gives funny-looking messages, but */
perror (" "); /* it's more portable than sys_errlist[]... */
} /* xxx: do something better. */
#endif
else
fprintf (stderr, "\n");
fflush (stderr);
}
} /* holler */
/* bail :
error-exit handler, callable from anywhere */
static void bail (str, p1, p2, p3, p4, p5, p6)
char * str;
char * p1, * p2, * p3, * p4, * p5, * p6;
{
o_verbose = 1;
holler (str, p1, p2, p3, p4, p5, p6);
#ifdef WIN32
shutdown(netfd, 0x02); /* Kirby */
closesocket (netfd);
#else
close (netfd);
#endif
sleep (1);
exit (1);
} /* bail */
/* catch :
no-brainer interrupt handler */
#ifdef NTFIXTHIS
static void catch ()
{
errno = 0;
if (o_verbose > 1) /* normally we don't care */
bail (wrote_txt, wrote_net, wrote_out);
bail (" punt!");
}
#endif
/* timeout and other signal handling cruft */
#ifndef WIN32
static jmp_buf jbuf; /* timer crud */
static int jval = 0; /* timer crud */
static void tmtravel ()
{
#ifdef NTFIXTHIS
signal (SIGALRM, SIG_IGN);
alarm (0);
#endif
if (jval == 0)
bail ("spurious timer interrupt!");
longjmp (jbuf, jval);
}
#else
# undef setjmp
# define setjmp(x) 0
#endif
/* arm :
set the timer. Zero secs arg means unarm */
#if defined(WIN32) && !defined(DEBUG)
# define arm(x,y) ((void)0)
#else
void arm (num, secs)
unsigned int num;
unsigned int secs;
{
#ifdef WIN32
#ifdef DEBUG
HANDLE stdhnd;
stdhnd = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdhnd != INVALID_HANDLE_VALUE)
printf("handle is %ld\n", stdhnd);
else
printf("failed to get stdhndl\n");
#endif
#else
if (secs == 0) { /* reset */
signal (SIGALRM, SIG_IGN);
alarm (0);
jval = 0;
} else { /* set */
signal (SIGALRM, tmtravel);
alarm (secs);
jval = num;
} /* if secs */
#endif /* WIN32 */
} /* arm */
#endif
/* Hmalloc :
malloc up what I want, rounded up to *4, and pre-zeroed. Either succeeds
or bails out on its own, so that callers don't have to worry about it. */
static char * Hmalloc (size)
unsigned int size;
{
unsigned int s = (size + 4) & 0xfffffffc; /* 4GB?! */
char * p = malloc (s);
if (p != NULL)
memset (p, 0, s);
else
bail ("Hmalloc %d failed", s);
return (p);
} /* Hmalloc */
/* findline :
find the next newline in a buffer; return inclusive size of that "line",
or the entire buffer size, so the caller knows how much to then write().
Not distinguishing \n vs \r\n for the nonce; it just works as is... */
static unsigned int findline (buf, siz)
char * buf;
unsigned int siz;
{
register char * p;
register int x;
if (! buf) /* various sanity checks... */
return (0);
if (siz > BIGSIZ)
return (0);
x = siz;
for (p = buf; x > 0; x--) {
if (*p == '\n') {
x = (int) (p - buf);
x++; /* 'sokay if it points just past the end! */
Debug (("findline returning %d", x))
return (x);
}
p++;
} /* for */
Debug (("findline returning whole thing: %d", siz))
return (siz);
} /* findline */
/* comparehosts :
cross-check the host_poop we have so far against new gethostby*() info,
and holler about mismatches. Perhaps gratuitous, but it can't hurt to
point out when someone's DNS is fukt. Returns 1 if mismatch, in case
someone else wants to do something about it. */
static int comparehosts (poop, hp)
HINF * poop;
struct hostent * hp;
{
errno = 0;
#ifndef WIN32
h_errno = 0;
#endif
/* The DNS spec is officially case-insensitive, but for those times when you
*really* wanna see any and all discrepancies, by all means define this. */
#ifdef ANAL
if (strcmp (poop->name, hp->h_name) != 0) { /* case-sensitive */
#else
if (strcasecmp (poop->name, hp->h_name) != 0) { /* normal */
#endif
holler ("DNS fwd/rev mismatch: %s != %s", poop->name, hp->h_name);
return (1);
}
return (0);
/* ... do we need to do anything over and above that?? */
} /* comparehosts */
/* gethostpoop :
resolve a host 8 ways from sunday; return a new host_poop struct with its
info. The argument can be a name or [ascii] IP address; it will try its
damndest to deal with it. "numeric" governs whether we do any DNS at all,
and we also check o_verbose for what's appropriate work to do. */
static HINF * gethostpoop (name, numeric)
char * name;
USHORT numeric;
{
struct hostent * hostent;
struct in_addr iaddr;
register HINF * poop = NULL;
register int x;
/* I really want to strangle the twit who dreamed up all these sockaddr and
hostent abstractions, and then forced them all to be incompatible with
each other so you *HAVE* to do all this ridiculous casting back and forth.
If that wasn't bad enough, all the doc insists on referring to local ports
and addresses as "names", which makes NO sense down at the bare metal.
What an absolutely horrid paradigm, and to think of all the people who
have been wasting significant amounts of time fighting with this stupid
deliberate obfuscation over the last 10 years... then again, I like
languages wherein a pointer is a pointer, what you put there is your own
business, the compiler stays out of your face, and sheep are nervous.
Maybe that's why my C code reads like assembler half the time... */
/* If we want to see all the DNS stuff, do the following hair --
if inet_addr, do reverse and forward with any warnings; otherwise try
to do forward and reverse with any warnings. In other words, as long
as we're here, do a complete DNS check on these clowns. Yes, it slows
things down a bit for a first run, but once it's cached, who cares? */
errno = 0;
#ifndef WIN32
h_errno = 0;
#endif
if (name)
poop = (HINF *) Hmalloc (sizeof (HINF));
if (! poop)
bail ("gethostpoop fuxored");
strcpy (poop->name, unknown); /* preload it */
/* see wzv:workarounds.c for dg/ux return-a-struct inet_addr lossage */
iaddr.s_addr = inet_addr (name);
if (iaddr.s_addr == INADDR_NONE) { /* here's the great split: names... */
if (numeric)
bail ("Can't parse %s as an IP address", name);
hostent = gethostbyname (name);
if (! hostent)
/* failure to look up a name is fatal, since we can't do anything with it */
/* XXX: h_errno only if BIND? look up how telnet deals with this */
bail ("%s: forward host lookup failed: h_errno %d", name, h_errno);
strncpy (poop->name, hostent->h_name, sizeof (poop->name));
for (x = 0; hostent->h_addr_list[x] && (x < 8); x++) {
memcpy (&poop->iaddrs[x], hostent->h_addr_list[x], sizeof (IA));
strncpy (poop->addrs[x], inet_ntoa (poop->iaddrs[x]),
sizeof (poop->addrs[0]));
} /* for x -> addrs, part A */
if (! o_verbose) /* if we didn't want to see the */
return (poop); /* inverse stuff, we're done. */
/* do inverse lookups in separate loop based on our collected forward addrs,
since gethostby* tends to crap into the same buffer over and over */
for (x = 0; poop->iaddrs[x].s_addr && (x < 8); x++) {
hostent = gethostbyaddr ((char *)&poop->iaddrs[x],
sizeof (IA), AF_INET);
if ((! hostent) || (! hostent-> h_name))
holler ("Warning: inverse host lookup failed for %s: h_errno %d",
poop->addrs[x], h_errno);
else
(void) comparehosts (poop, hostent);
} /* for x -> addrs, part B */
} else { /* not INADDR_NONE: numeric addresses... */
memcpy (poop->iaddrs, &iaddr, sizeof (IA));
strncpy (poop->addrs[0], inet_ntoa (iaddr), sizeof (poop->addrs));
if (numeric) /* if numeric-only, we're done */
return (poop);
if (! o_verbose) /* likewise if we don't want */
return (poop); /* the full DNS hair */
hostent = gethostbyaddr ((char *) &iaddr, sizeof (IA), AF_INET);
/* numeric or not, failure to look up a PTR is *not* considered fatal */
if (! hostent)
holler ("%s: inverse host lookup failed: h_errno %d", name, h_errno);
else {
strncpy (poop->name, hostent->h_name, MAXHOSTNAMELEN - 2);
hostent = gethostbyname (poop->name);
if ((! hostent) || (! hostent->h_addr_list[0]))
holler ("Warning: forward host lookup failed for %s: h_errno %d",
poop->name, h_errno);
else
(void) comparehosts (poop, hostent);
} /* if hostent */
} /* INADDR_NONE Great Split */
/* whatever-all went down previously, we should now have a host_poop struct
with at least one IP address in it. */
#ifndef WIN32
h_errno = 0;
#endif
return (poop);
} /* gethostpoop */
/* getportpoop :
Same general idea as gethostpoop -- look up a port in /etc/services, fill
in global port_poop, but return the actual port *number*. Pass ONE of:
pstring to resolve stuff like "23" or "exec";
pnum to reverse-resolve something that's already a number.
If o_nflag is on, fill in what we can but skip the getservby??? stuff.
Might as well have consistent behavior here... */
static USHORT getportpoop (pstring, pnum)
char * pstring;
unsigned int pnum;
{
struct servent * servent;
#ifndef WIN32
register int x;
register int y;
#else
u_short x;
u_short y;
#endif
char * whichp = p_tcp;
if (o_udpmode)
whichp = p_udp;
portpoop->name[0] = '?'; /* fast preload */
portpoop->name[1] = '\0';
/* case 1: reverse-lookup of a number; placed first since this case is much
more frequent if we're scanning */
if (pnum) {
if (pstring) /* one or the other, pleeze */
return (0);
x = pnum;
if (o_nflag) /* go faster, skip getservbyblah */
goto gp_finish;
y = htons (x); /* gotta do this -- see Fig.1 below */
servent = getservbyport (y, whichp);
if (servent) {
y = ntohs (servent->s_port);
if (x != y) /* "never happen" */
holler ("Warning: port-bynum mismatch, %d != %d", x, y);
strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name));
} /* if servent */
goto gp_finish;
} /* if pnum */
/* case 2: resolve a string, but we still give preference to numbers instead
of trying to resolve conflicts. None of the entries in *my* extensive
/etc/services begins with a digit, so this should "always work" unless
you're at 3com and have some company-internal services defined... */
if (pstring) {
if (pnum) /* one or the other, pleeze */
return (0);
x = atoi (pstring);
if (x)
return (getportpoop (NULL, x)); /* recurse for numeric-string-arg */
if (o_nflag) /* can't use names! */
return (0);
servent = getservbyname (pstring, whichp);
if (servent) {
strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name));
x = ntohs (servent->s_port);
goto gp_finish;
} /* if servent */
} /* if pstring */
return (0); /* catches any problems so far */
/* Obligatory netdb.h-inspired rant: servent.s_port is supposed to be an int.
Despite this, we still have to treat it as a short when copying it around.
Not only that, but we have to convert it *back* into net order for
getservbyport to work. Manpages generally aren't clear on all this, but
there are plenty of examples in which it is just quietly done. More BSD
lossage... since everything getserv* ever deals with is local to our own
host, why bother with all this network-order/host-order crap at all?!
That should be saved for when we want to actually plug the port[s] into
some real network calls -- and guess what, we have to *re*-convert at that
point as well. Fuckheads. */
gp_finish:
/* Fall here whether or not we have a valid servent at this point, with
x containing our [host-order and therefore useful, dammit] port number */
sprintf (portpoop->anum, "%d", x); /* always load any numeric specs! */
portpoop->num = (x & 0xffff); /* ushort, remember... */
return (portpoop->num);
} /* getportpoop */
/* nextport :
Come up with the next port to try, be it random or whatever. "block" is
a ptr to randports array, whose bytes [so far] carry these meanings:
0 ignore
1 to be tested
2 tested [which is set as we find them here]
returns a USHORT random port, or 0 if all the t-b-t ones are used up. */
static USHORT nextport (block)
char * block;
{
register unsigned int x;
register unsigned int y;
y = 70000; /* high safety count for rnd-tries */
while (y > 0) {
x = (RAND() & 0xffff);
if (block[x] == 1) { /* try to find a not-done one... */
block[x] = 2;
break;
}
x = 0; /* bummer. */
y--;
} /* while y */
if (x)
return (x);
y = 65535; /* no random one, try linear downsearch */
while (y > 0) { /* if they're all used, we *must* be sure! */
if (block[y] == 1) {
block[y] = 2;
break;
}
y--;
} /* while y */
if (y)
return (y); /* at least one left */
return (0); /* no more left! */
} /* nextport */
/* loadports :
set "to be tested" indications in BLOCK, from LO to HI. Almost too small
to be a separate routine, but makes main() a little cleaner... */
static void loadports (block, lo, hi)
char * block;
USHORT lo;
USHORT hi;
{
USHORT x;
if (! block)
bail ("loadports: no block?!");
if ((! lo) || (! hi))
bail ("loadports: bogus values %d, %d", lo, hi);
x = hi;
while (lo <= x) {
block[x] = 1;
x--;
}
} /* loadports */
#ifdef GAPING_SECURITY_HOLE
char * pr00gie = NULL; /* global ptr to -e arg */
#ifdef WIN32
BOOL doexec(SOCKET ClientSocket); // this is in doexec.c
#else
/* doexec :
fiddle all the file descriptors around, and hand off to another prog. Sort
of like a one-off "poor man's inetd". This is the only section of code
that would be security-critical, which is why it's ifdefed out by default.
Use at your own hairy risk; if you leave shells lying around behind open
listening ports you deserve to lose!! */
doexec (fd)
int fd;
{
register char * p;
dup2 (fd, 0); /* the precise order of fiddlage */
#ifdef WIN32
shutdown(fd, SD_BOTH); /* Kirby */
closesocket (fd);
#else
close (fd); /* is apparently crucial; this is */
#endif
dup2 (0, 1); /* swiped directly out of "inetd". */
dup2 (0, 2);
p = strrchr (pr00gie, '/'); /* shorter argv[0] */
if (p)
p++;
else
p = pr00gie;
Debug (("gonna exec %s as %s...", pr00gie, p))
execl (pr00gie, p, NULL);
bail ("exec %s failed", pr00gie); /* this gets sent out. Hmm... */
} /* doexec */
#endif
#endif /* GAPING_SECURITY_HOLE */
#ifdef WIN32
static const BYTE f_color[8] =
{
0,FOREGROUND_RED, FOREGROUND_GREEN,
FOREGROUND_RED | FOREGROUND_GREEN,
FOREGROUND_BLUE,
FOREGROUND_BLUE | FOREGROUND_RED,
FOREGROUND_BLUE | FOREGROUND_GREEN,
FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE
};
static const BYTE b_color[8] =
{
0, BACKGROUND_RED,BACKGROUND_GREEN,
BACKGROUND_RED | BACKGROUND_GREEN,
BACKGROUND_BLUE,
BACKGROUND_BLUE | BACKGROUND_RED,
BACKGROUND_BLUE | BACKGROUND_GREEN,
BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE
};
# ifndef COMMON_LVB_REVERSE_VIDEO
# define COMMON_LVB_REVERSE_VIDEO 0x4000
# endif
# ifndef COMMON_LVB_UNDERSCORE
# define COMMON_LVB_UNDERSCORE 0x8000
# endif
#endif
/* doconnect :
do all the socket stuff, and return an fd for one of
an open outbound TCP connection
a UDP stub-socket thingie
with appropriate socket options set up if we wanted source-routing, or
an unconnected TCP or UDP socket to listen on.
Examines various global o_blah flags to figure out what-all to do. */
static int doconnect (rad, rp, lad, lp)
IA * rad;
USHORT rp;
IA * lad;
USHORT lp;
{
#ifndef WIN32
register int nnetfd;
#endif
register int rr;
int x, y;
errno = 0;
#ifdef WIN32
WSASetLastError(0);
#endif
/* grab a socket; set opts */
if (o_udpmode)
nnetfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
else
nnetfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (nnetfd < 0)
bail ("Can't get socket");
if (nnetfd == 0) /* might *be* zero if stdin was closed! */
nnetfd = dup (nnetfd); /* so fix it. Leave the old 0 hanging. */
#ifdef WIN32
rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, (const char FAR *)setsockopt_c, sizeof (setsockopt_c));
#else
x = 1;
rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x));
#endif
if (rr == -1)
holler ("nnetfd reuseaddr failed"); /* ??? */
#ifdef SO_REUSEPORT /* doesnt exist everywhere... */
#ifdef WIN32
rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &c, sizeof (c));
#else
rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
#endif
if (rr == -1)
holler ("nnetfd reuseport failed"); /* ??? */
#endif
/* fill in all the right sockaddr crud */
lclend->sin_family = AF_INET;
remend->sin_family = AF_INET;
/* if lad/lp, do appropriate binding */
if (lad)
memcpy (&lclend->sin_addr.s_addr, lad, sizeof (IA));
if (lp)
lclend->sin_port = htons (lp);
rr = 0;
if (lad || lp) {
x = (int) lp;
/* try a few times for the local bind, a la ftp-data-port... */
for (y = 4; y > 0; y--) {
rr = bind (nnetfd, (SA *)lclend, sizeof (SA));
if (rr == 0)
break;
if (errno != EADDRINUSE)
break;
else {
holler ("retrying local %s:%d", inet_ntoa (lclend->sin_addr), lp);
sleep (1);
errno = 0; /* clear from sleep */
} /* if EADDRINUSE */
} /* for y counter */
} /* if lad or lp */
if (rr)
bail ("Can't grab %s:%d with bind",
inet_ntoa(lclend->sin_addr), lp);
if (o_listen)
return (nnetfd); /* thanks, that's all for today */
memcpy (&remend->sin_addr.s_addr, rad, sizeof (IA));
remend->sin_port = htons (rp);
/* rough format of LSRR option and explanation of weirdness.
-Option comes after IP-hdr dest addr in packet, padded to *4, and ihl > 5.
-IHL is multiples of 4, i.e. real len = ip_hl << 2.
- type 131 1 ; 0x83: copied, option class 0, number 3
- len 1 ; of *whole* option!
- pointer 1 ; nxt-hop-addr; 1-relative, not 0-relative
- addrlist... var ; 4 bytes per hop-addr
- pad-to-32 var ; ones, i.e. "NOP"
-
-If we want to route A -> B via hops C and D, we must add C, D, *and* B to the
-options list. Why? Because when we hand the kernel A -> B with list C, D, B
-the "send shuffle" inside the kernel changes it into A -> C with list D, B and
-the outbound packet gets sent to C. If B wasn't also in the hops list, the
-final destination would have been lost at this point.
-
-When C gets the packet, it changes it to A -> D with list C', B where C' is
-the interface address that C used to forward the packet. This "records" the
-route hop from B's point of view, i.e. which address points "toward" B. This
-is to make B better able to return the packets. The pointer gets bumped by 4,
-so that D does the right thing instead of trying to forward back to C.
-
-When B finally gets the packet, it sees that the pointer is at the end of the
-LSRR list and is thus "completed". B will then try to use the packet instead
-of forwarding it, i.e. deliver it up to some application.
-
-Note that by moving the pointer yourself, you could send the traffic directly
-to B but have it return via your preconstructed source-route. Playing with
-this and watching "tcpdump -v" is the best way to understand what's going on.
-
-Only works for TCP in BSD-flavor kernels. UDP is a loss; udp_input calls
-stripoptions() early on, and the code to save the srcrt is notdef'ed.
-Linux is also still a loss at 1.3.x it looks like; the lsrr code is { }...
-*/
/* if any -g arguments were given, set up source-routing. We hit this after
the gates are all looked up and ready to rock, any -G pointer is set,
and gatesidx is now the *number* of hops */
if (gatesidx) { /* if we wanted any srcrt hops ... */
/* don't even bother compiling if we can't do IP options here! */
/* #ifdef IP_OPTIONS */
#ifndef WIN32
if (! optbuf) { /* and don't already *have* a srcrt set */
char * opp; /* then do all this setup hair */
optbuf = Hmalloc (48);
opp = optbuf;
*opp++ = IPOPT_LSRR; /* option */
*opp++ = (char)
(((gatesidx + 1) * sizeof (IA)) + 3) & 0xff; /* length */
*opp++ = gatesptr; /* pointer */
/* opp now points at first hop addr -- insert the intermediate gateways */
for ( x = 0; x < gatesidx; x++) {
memcpy (opp, gates[x]->iaddrs, sizeof (IA));
opp += sizeof (IA);
}
/* and tack the final destination on the end [needed!] */
memcpy (opp, rad, sizeof (IA));
opp += sizeof (IA);
*opp = IPOPT_NOP; /* alignment filler */
} /* if empty optbuf */
/* calculate length of whole option mess, which is (3 + [hops] + [final] + 1),
and apply it [have to do this every time through, of course] */
x = ((gatesidx + 1) * sizeof (IA)) + 4;
rr = setsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, x);
if (rr == -1)
bail ("srcrt setsockopt fuxored");
#else /* IP_OPTIONS */
holler ("Warning: source routing unavailable on this machine, ignoring");
#endif /* IP_OPTIONS*/
} /* if gatesidx */
/* wrap connect inside a timer, and hit it */
arm (1, o_wait);
if (setjmp (jbuf) == 0) {
rr = connect (nnetfd, (SA *)remend, sizeof (SA));
} else { /* setjmp: connect failed... */
rr = -1;
#ifdef WIN32
WSASetLastError(WSAETIMEDOUT); /* fake it */
#else
errno = ETIMEDOUT; /* fake it */
#endif
}
arm (0, 0);
if (rr == 0)
return (nnetfd);
#ifdef WIN32
errno = h_errno;
shutdown(nnetfd, 0x02); /* Kirby */
closesocket (nnetfd);
WSASetLastError(errno); /* don't want to lose connect error */
#else
close (nnetfd); /* clean up junked socket FD!! */
#endif
return (-1);
} /* doconnect */