-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdsock.c
2810 lines (2592 loc) · 66.8 KB
/
dsock.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
/*
* dsock.c - Linux socket processing functions for /proc-based lsof
*/
/*
* Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: dsock.c,v 1.34 2009/03/25 19:22:39 abe Exp $";
#endif
#include "lsof.h"
/*
* Local definitions
*/
#define INOBUCKS 128 /* inode hash bucket count -- must be
* a power of two */
#define INOHASH(ino) ((int)((ino * 31415) >> 3) & (INOBUCKS - 1))
#define TCPUDPHASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp_bucks - 1))
#define TCPUDP6HASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp6_bucks - 1))
/*
* Local structures
*/
struct ax25sin { /* AX25 socket information */
char *da; /* destination address */
char *dev_ch; /* device characters */
char *sa; /* source address */
INODETYPE inode;
unsigned long sq, rq; /* send and receive queue values */
unsigned char sqs, rqs; /* send and receive queue states */
int state;
struct ax25sin *next;
};
struct ipxsin { /* IPX socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
int state;
unsigned long txq, rxq; /* transmit and receive queue values */
struct ipxsin *next;
};
struct packin { /* packet information */
INODETYPE inode;
int ty; /* socket type */
int pr; /* protocol */
struct packin *next;
};
struct rawsin { /* raw socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
char *sp; /* state characters */
MALLOC_S lal; /* strlen(la) */
MALLOC_S ral; /* strlen(ra) */
MALLOC_S spl; /* strlen(sp) */
struct rawsin *next;
};
struct tcp_udp { /* IPv4 TCP and UDP socket
* information */
INODETYPE inode;
unsigned long faddr, laddr; /* foreign & local IPv6 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp *next;
};
#if defined(HASIPv6)
struct tcp_udp6 { /* IPv6 TCP and UDP socket
* information */
INODETYPE inode;
struct in6_addr faddr, laddr; /* foreign and local IPv6 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp6 *next;
};
#endif /* defined(HASIPv6) */
struct uxsin { /* UNIX socket information */
INODETYPE inode;
char *pcb;
char *path;
unsigned char sb_def;
dev_t sb_dev;
INODETYPE sb_ino;
dev_t sb_rdev;
struct uxsin *next;
};
/*
* Local static values
*/
static char *AX25path = (char *)NULL; /* path to AX25 /proc information */
static struct ax25sin **AX25sin = (struct ax25sin **)NULL;
/* AX25 socket info, hashed by inode */
static char *ax25st[] = {
"LISTENING", /* 0 */
"SABM SENT", /* 1 */
"DISC SENT", /* 2 */
"ESTABLISHED", /* 3 */
"RECOVERY" /* 4 */
};
#define NAX25ST (sizeof(ax25st) / sizeof(char *))
static char *Ipxpath = (char *)NULL; /* path to IPX /proc information */
static struct ipxsin **Ipxsin = (struct ipxsin **)NULL;
/* IPX socket info, hashed by inode */
static struct packin **Packin = (struct packin **)NULL;
/* packet info, hashed by inode */
static char *Packpath = (char *)NULL; /* path to packer /proc information */
struct packpr { /* packet protocol conversions */
int pr; /* protocol number */
char *nm; /* protocol name */
} static Packpr[] = {
#if defined(ETH_P_LOOP)
{ ETH_P_LOOP, "LOOP" },
#endif /* defined(ETH_P_LOOP) */
#if defined(ETH_P_PUP)
{ ETH_P_PUP, "PUP" },
#endif /* defined(ETH_P_PUP) */
#if defined(ETH_P_PUPAT)
{ ETH_P_PUPAT, "PUPAT" },
#endif /* defined(ETH_P_PUPAT) */
#if defined(ETH_P_IP)
{ ETH_P_IP, "IP" },
#endif /* defined(ETH_P_IP) */
#if defined(ETH_P_X25)
{ ETH_P_X25, "X25" },
#endif /* defined(ETH_P_X25) */
#if defined(ETH_P_ARP)
{ ETH_P_ARP, "ARP" },
#endif /* defined(ETH_P_ARP) */
#if defined(ETH_P_BPQ)
{ ETH_P_BPQ, "BPQ" },
#endif /* defined(ETH_P_BPQ) */
#if defined(ETH_P_IEEEPUP)
{ ETH_P_IEEEPUP, "I3EPUP" },
#endif /* defined(ETH_P_IEEEPUP) */
#if defined(ETH_P_IEEEPUPAT)
{ ETH_P_IEEEPUPAT, "I3EPUPA" },
#endif /* defined(ETH_P_IEEEPUPAT) */
#if defined(ETH_P_DEC)
{ ETH_P_DEC, "DEC" },
#endif /* defined(ETH_P_DEC) */
#if defined(ETH_P_DNA_DL)
{ ETH_P_DNA_DL, "DNA_DL" },
#endif /* defined(ETH_P_DNA_DL) */
#if defined(ETH_P_DNA_RC)
{ ETH_P_DNA_RC, "DNA_RC" },
#endif /* defined(ETH_P_DNA_RC) */
#if defined(ETH_P_DNA_RT)
{ ETH_P_DNA_RT, "DNA_RT" },
#endif /* defined(ETH_P_DNA_RT) */
#if defined(ETH_P_LAT)
{ ETH_P_LAT, "LAT" },
#endif /* defined(ETH_P_LAT) */
#if defined(ETH_P_DIAG)
{ ETH_P_DIAG, "DIAG" },
#endif /* defined(ETH_P_DIAG) */
#if defined(ETH_P_CUST)
{ ETH_P_CUST, "CUST" },
#endif /* defined(ETH_P_CUST) */
#if defined(ETH_P_SCA)
{ ETH_P_SCA, "SCA" },
#endif /* defined(ETH_P_SCA) */
#if defined(ETH_P_RARP)
{ ETH_P_RARP, "RARP" },
#endif /* defined(ETH_P_RARP) */
#if defined(ETH_P_ATALK)
{ ETH_P_ATALK, "ATALK" },
#endif /* defined(ETH_P_ATALK) */
#if defined(ETH_P_AARP)
{ ETH_P_AARP, "AARP" },
#endif /* defined(ETH_P_AARP) */
#if defined(ETH_P_8021Q)
{ ETH_P_8021Q, "8021Q" },
#endif /* defined(ETH_P_8021Q) */
#if defined(ETH_P_IPX)
{ ETH_P_IPX, "IPX" },
#endif /* defined(ETH_P_IPX) */
#if defined(ETH_P_IPV6)
{ ETH_P_IPV6, "IPV6" },
#endif /* defined(ETH_P_IPV6) */
#if defined(ETH_P_SLOW)
{ ETH_P_SLOW, "SLOW" },
#endif /* defined(ETH_P_SLOW) */
#if defined(ETH_P_WCCP)
{ ETH_P_WCCP, "WCCP" },
#endif /* defined(ETH_P_WCCP) */
#if defined(ETH_P_PPP_DISC)
{ ETH_P_PPP_DISC, "PPP_DIS" },
#endif /* defined(ETH_P_PPP_DISC) */
#if defined(ETH_P_PPP_SES)
{ ETH_P_PPP_SES, "PPP_SES" },
#endif /* defined(ETH_P_PPP_SES) */
#if defined(ETH_P_MPLS_UC)
{ ETH_P_MPLS_UC, "MPLS_UC" },
#endif /* defined(ETH_P_MPLS_UC) */
#if defined(ETH_P_MPLS_MC)
{ ETH_P_MPLS_MC, "MPLS_MC" },
#endif /* defined(ETH_P_MPLS_MC) */
#if defined(ETH_P_ATMMPOA)
{ ETH_P_ATMMPOA, "ATMMPOA" },
#endif /* defined(ETH_P_ATMMPOA) */
#if defined(ETH_P_MPLS_MC)
{ ETH_P_MPLS_MC, "MPLS_MC" },
#endif /* defined(ETH_P_MPLS_MC) */
#if defined(ETH_P_ATMFATE)
{ ETH_P_ATMFATE, "ATMFATE" },
#endif /* defined(ETH_P_ATMFATE) */
#if defined(ETH_P_AOE)
{ ETH_P_AOE, "AOE" },
#endif /* defined(ETH_P_AOE) */
#if defined(ETH_P_TIPC)
{ ETH_P_TIPC, "TIPC" },
#endif /* defined(ETH_P_TIPC) */
#if defined(ETH_P_802_3)
{ ETH_P_802_3, "802.3" },
#endif /* defined(ETH_P_802_3) */
#if defined(ETH_P_AX25)
{ ETH_P_AX25, "AX25" },
#endif /* defined(ETH_P_AX25) */
#if defined(ETH_P_ALL)
{ ETH_P_ALL, "ALL" },
#endif /* defined(ETH_P_ALL) */
#if defined(ETH_P_802_2)
{ ETH_P_802_2, "802.2" },
#endif /* defined(ETH_P_802_2) */
#if defined(ETH_P_SNAP)
{ ETH_P_SNAP, "SNAP" },
#endif /* defined(ETH_P_SNAP) */
#if defined(ETH_P_DDCMP)
{ ETH_P_DDCMP, "DDCMP" },
#endif /* defined(ETH_P_DDCMP) */
#if defined(ETH_P_WAN_PPP)
{ ETH_P_WAN_PPP, "WAN_PPP" },
#endif /* defined(ETH_P_WAN_PPP) */
#if defined(ETH_P_PPP_MP)
{ ETH_P_PPP_MP, "PPP MP" },
#endif /* defined(ETH_P_PPP_MP) */
#if defined(ETH_P_LOCALTALK)
{ ETH_P_LOCALTALK, "LCLTALK" },
#endif /* defined(ETH_P_LOCALTALK) */
#if defined(ETH_P_PPPTALK)
{ ETH_P_PPPTALK, "PPPTALK" },
#endif /* defined(ETH_P_PPPTALK) */
#if defined(ETH_P_TR_802_2)
{ ETH_P_TR_802_2, "802.2" },
#endif /* defined(ETH_P_TR_802_2) */
#if defined(ETH_P_MOBITEX)
{ ETH_P_MOBITEX, "MOBITEX" },
#endif /* defined(ETH_P_MOBITEX) */
#if defined(ETH_P_CONTROL)
{ ETH_P_CONTROL, "CONTROL" },
#endif /* defined(ETH_P_CONTROL) */
#if defined(ETH_P_IRDA)
{ ETH_P_IRDA, "IRDA" },
#endif /* defined(ETH_P_IRDA) */
#if defined(ETH_P_ECONET)
{ ETH_P_ECONET, "ECONET" },
#endif /* defined(ETH_P_ECONET) */
#if defined(ETH_P_HDLC)
{ ETH_P_HDLC, "HDLC" },
#endif /* defined(ETH_P_HDLC) */
#if defined(ETH_P_ARCNET)
{ ETH_P_ARCNET, "ARCNET" },
#endif /* defined(ETH_P_ARCNET) */
};
#define NPACKPR (sizeof(Packpr) / sizeof(struct packpr))
struct packty { /* packet socket type conversions */
int ty; /* type number */
char *nm; /* type name */
} static Packty[] = {
#if defined(SOCK_STREAM)
{ SOCK_STREAM, "STREAM" },
#endif /* defined(SOCK_STREAM) */
#if defined(SOCK_DGRAM)
{ SOCK_DGRAM, "DGRAM" },
#endif /* defined(SOCK_DGRAM) */
#if defined(SOCK_RAW)
{ SOCK_RAW, "RAW" },
#endif /* defined(SOCK_RAW) */
#if defined(SOCK_RDM)
{ SOCK_RDM, "RDM" },
#endif /* defined(SOCK_RDM) */
#if defined(SOCK_SEQPACKET)
{ SOCK_SEQPACKET, "SEQPACKET" },
#endif /* defined(SOCK_SEQPACKET) */
#if defined(SOCK_PACKET)
{ SOCK_PACKET, "PACKET" },
#endif /* defined(SOCK_PACKET) */
};
#define NPACKTY (sizeof(Packty) / sizeof(struct packty))
static char *Rawpath = (char *)NULL; /* path to raw socket /proc
* information */
static struct rawsin **Rawsin = (struct rawsin **)NULL;
/* raw socket info, hashed by inode */
static char *SockStatPath = (char *)NULL;
/* path to /proc/net socket status */
static char *TCPpath = (char *)NULL; /* path to TCP /proc information */
static struct tcp_udp **TcpUdp = (struct tcp_udp **)NULL;
/* IPv4 TCP & UDP info, hashed by
* inode */
static int TcpUdp_bucks = 0; /* dynamically sized hash bucket
* count for TCP and UDP -- will
* be a power of two */
#if defined(HASIPv6)
static char *Raw6path = (char *)NULL; /* path to raw IPv6 /proc information */
static struct rawsin **Rawsin6 = (struct rawsin **)NULL;
/* IPv6 raw socket info, hashed by
* inode */
static char *SockStatPath6 = (char *)NULL;
/* path to /proc/net IPv6 socket
* status */
static char *TCP6path = (char *)NULL; /* path to IPv6 TCP /proc information */
static struct tcp_udp6 **TcpUdp6 = (struct tcp_udp6 **)NULL;
/* IPv6 TCP & UDP info, hashed by
* inode */
static int TcpUdp6_bucks = 0; /* dynamically sized hash bucket
* count for IPv6 TCP and UDP -- will
* be a power of two */
static char *UDP6path = (char *)NULL; /* path to IPv6 UDP /proc information */
static char *UDP6LITEpath = (char *)NULL;
/* path to IPv6 UDPLITE /proc
* information */
#endif /* defined(HASIPv6) */
static char *UDPpath = (char *)NULL; /* path to UDP /proc information */
static char *UDPLITEpath = (char *)NULL;
/* path to UDPLITE /proc information */
static char *UNIXpath = (char *)NULL; /* path to UNIX /proc information */
static struct uxsin **Uxsin = (struct uxsin **)NULL;
/* UNIX socket info, hashed by inode */
/*
* Local function prototypes
*/
_PROTOTYPE(static struct ax25sin *check_ax25,(INODETYPE i));
_PROTOTYPE(static struct ipxsin *check_ipx,(INODETYPE i));
_PROTOTYPE(static struct packin *check_pack,(INODETYPE i));
_PROTOTYPE(static struct rawsin *check_raw,(INODETYPE i));
_PROTOTYPE(static struct tcp_udp *check_tcpudp,(INODETYPE i, char **p));
_PROTOTYPE(static struct uxsin *check_unix,(INODETYPE i));
_PROTOTYPE(static void get_ax25,(char *p));
_PROTOTYPE(static void get_ipx,(char *p));
_PROTOTYPE(static void get_pack,(char *p));
_PROTOTYPE(static void get_raw,(char *p));
_PROTOTYPE(static void get_tcpudp,(char *p, int pr, int clr));
_PROTOTYPE(static void get_unix,(char *p));
_PROTOTYPE(static void print_ax25info,(struct ax25sin *ap));
_PROTOTYPE(static void print_ipxinfo,(struct ipxsin *ip));
#if defined(HASIPv6)
_PROTOTYPE(static struct rawsin *check_raw6,(INODETYPE i));
_PROTOTYPE(static struct tcp_udp6 *check_tcpudp6,(INODETYPE i, char **p));
_PROTOTYPE(static void get_raw6,(char *p));
_PROTOTYPE(static void get_tcpudp6,(char *p, int pr, int clr));
_PROTOTYPE(static int net6a2in6,(char *as, struct in6_addr *ad));
#endif /* defined(HASIPv6) */
/*
* __BIONIC__ is stupid, so we need to specify these if building for it
*/
#if defined(__BIONIC__)
enum {
TCP_ESTABLISHED = 1,
TCP_SYN_SENT,
TCP_SYN_RECV,
TCP_FIN_WAIT1,
TCP_FIN_WAIT2,
TCP_TIME_WAIT,
TCP_CLOSE,
TCP_CLOSE_WAIT,
TCP_LAST_ACK,
TCP_LISTEN,
TCP_CLOSING, /* now a valid state */
};
#endif /* defined(__BIONIC__) */
/*
* build_IPstates() -- build the TCP and UDP state tables
*/
void
build_IPstates()
{
if (!TcpSt) {
(void) enter_IPstate("TCP", "ESTABLISHED", TCP_ESTABLISHED);
(void) enter_IPstate("TCP", "SYN_SENT", TCP_SYN_SENT);
(void) enter_IPstate("TCP", "SYN_RECV", TCP_SYN_RECV);
(void) enter_IPstate("TCP", "FIN_WAIT1", TCP_FIN_WAIT1);
(void) enter_IPstate("TCP", "FIN_WAIT2", TCP_FIN_WAIT2);
(void) enter_IPstate("TCP", "TIME_WAIT", TCP_TIME_WAIT);
(void) enter_IPstate("TCP", "CLOSE", TCP_CLOSE);
(void) enter_IPstate("TCP", "CLOSE_WAIT", TCP_CLOSE_WAIT);
(void) enter_IPstate("TCP", "LAST_ACK", TCP_LAST_ACK);
(void) enter_IPstate("TCP", "LISTEN", TCP_LISTEN);
(void) enter_IPstate("TCP", "CLOSING", TCP_CLOSING);
(void) enter_IPstate("TCP", "CLOSED", 0);
(void) enter_IPstate("TCP", (char *)NULL, 0);
}
}
/*
* check_ax25() - check for AX25 socket file
*/
static struct ax25sin *
check_ax25(i)
INODETYPE i; /* socket file's inode number */
{
struct ax25sin *ap;
int h;
h = INOHASH(i);
for (ap = AX25sin[h]; ap; ap = ap->next) {
if (i == ap->inode)
return(ap);
}
return((struct ax25sin *)NULL);
}
/*
* check_ipx() - check for IPX socket file
*/
static struct ipxsin *
check_ipx(i)
INODETYPE i; /* socket file's inode number */
{
int h;
struct ipxsin *ip;
h = INOHASH(i);
for (ip = Ipxsin[h]; ip; ip = ip->next) {
if (i == ip->inode)
return(ip);
}
return((struct ipxsin *)NULL);
}
/*
* check_pack() - check for packet file
*/
static struct packin *
check_pack(i)
INODETYPE i; /* packet file's inode number */
{
int h;
struct packin *pp;
h = INOHASH(i);
for (pp = Packin[h]; pp; pp = pp->next) {
if (i == pp->inode)
return(pp);
}
return((struct packin *)NULL);
}
/*
* check_raw() - check for raw socket file
*/
static struct rawsin *
check_raw(i)
INODETYPE i; /* socket file's inode number */
{
int h;
struct rawsin *rp;
h = INOHASH(i);
for (rp = Rawsin[h]; rp; rp = rp->next) {
if (i == rp->inode)
return(rp);
}
return((struct rawsin *)NULL);
}
/*
* check_tcpudp() - check for IPv4 TCP or UDP socket file
*/
static struct tcp_udp *
check_tcpudp(i, p)
INODETYPE i; /* socket file's inode number */
char **p; /* protocol return */
{
int h;
struct tcp_udp *tp;
h = TCPUDPHASH(i);
for (tp = TcpUdp[h]; tp; tp = tp->next) {
if (i == tp->inode) {
switch (tp->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
return(tp);
}
}
return((struct tcp_udp *)NULL);
}
#if defined(HASIPv6)
/*
* check_raw6() - check for raw IPv6 socket file
*/
static struct rawsin *
check_raw6(i)
INODETYPE i; /* socket file's inode number */
{
int h;
struct rawsin *rp;
h = INOHASH(i);
for (rp = Rawsin6[h]; rp; rp = rp->next) {
if (i == rp->inode)
return(rp);
}
return((struct rawsin *)NULL);
}
/*
* check_tcpudp6() - check for IPv6 TCP or UDP socket file
*/
static struct tcp_udp6 *
check_tcpudp6(i, p)
INODETYPE i; /* socket file's inode number */
char **p; /* protocol return */
{
int h;
struct tcp_udp6 *tp6;
h = TCPUDP6HASH(i);
for (tp6 = TcpUdp6[h]; tp6; tp6 = tp6->next) {
if (i == tp6->inode) {
switch (tp6->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
return(tp6);
}
}
return((struct tcp_udp6 *)NULL);
}
#endif /* defined(HASIPv6) */
/*
* check_unix() - check for UNIX domain socket
*/
static struct uxsin *
check_unix(i)
INODETYPE i; /* socket file's inode number */
{
int h;
struct uxsin *up;
h = INOHASH(i);
for (up = Uxsin[h]; up; up = up->next) {
if (i == up->inode)
return(up);
}
return((struct uxsin *)NULL);
}
/*
* get_ax25() - get /proc/net/ax25 info
*/
static void
get_ax25(p)
char *p; /* /proc/net/ipx path */
{
struct ax25sin *ap, *np;
FILE *as;
char buf[MAXPATHLEN], *da, *dev_ch, *ep, **fp, *sa;
int h, nf;
INODETYPE inode;
unsigned long rq, sq, state;
MALLOC_S len;
unsigned char rqs, sqs;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
/*
* Do second time cleanup or first time setup.
*/
if (AX25sin) {
for (h = 0; h < INOBUCKS; h++) {
for (ap = AX25sin[h]; ap; ap = np) {
np = ap->next;
if (ap->da)
(void) free((FREE_P *)ap->da);
if (ap->dev_ch)
(void) free((FREE_P *)ap->dev_ch);
if (ap->sa)
(void) free((FREE_P *)ap->sa);
(void) free((FREE_P *)ap);
}
AX25sin[h] = (struct ax25sin *)NULL;
}
} else {
AX25sin = (struct ax25sin **)calloc(INOBUCKS,
sizeof(struct ax25sin *));
if (!AX25sin) {
(void) fprintf(stderr,
"%s: can't allocate %d AX25 hash pointer bytes\n",
Pn, INOBUCKS * sizeof(struct ax25sin *));
Exit(1);
}
}
/*
* Open the /proc/net/ax25 file, assign a page size buffer to the stream,
* and read it. Store AX25 socket info in the AX25sin[] hash buckets.
*/
if (!(as = open_proc_stream(p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, as)) {
if ((nf = get_fields(buf, (char *)NULL, &fp, (int *)NULL, 0)) < 24)
continue;
/*
* /proc/next/ac25 has no title line, a very poor deficiency in its
* implementation.
*
* The ax25_get_info() function in kern mnodule .../net/ax25/af_ax25.c
* says the format of the lines in the file is:
*
* magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 \
* t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q \
* inode
*
* The code in this function is forced to assume that format is in
* effect..
*/
/*
* Assemble the inode number and see if it has already been recorded.
* If it has, skip this line.
*/
ep = (char *)NULL;
if (!fp[23] || !*fp[23]
|| (inode = strtoull(fp[23], &ep, 0)) == ULONG_MAX
|| !ep || *ep)
continue;
h = INOHASH((INODETYPE)inode);
for (ap = AX25sin[h]; ap; ap = ap->next) {
if (inode == ap->inode)
break;
}
if (ap)
continue;
/*
* Assemble the send and receive queue values and the state.
*/
rq = sq = (unsigned long)0;
rqs = sqs = (unsigned char)0;
ep = (char *)NULL;
if (!fp[21] || !*fp[21]
|| (sq = strtoul(fp[21], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
sqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[22] || !*fp[22]
|| (rq = strtoul(fp[22], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
rqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[4] || !*fp[4]
|| (state = strtoul(fp[4], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Allocate space for the destination address.
*/
if (!fp[3] || !*fp[3])
da = (char *)NULL;
else if ((len = strlen(fp[3]))) {
if (!(da = (char *)malloc(len + 1))) {
(void) fprintf(stderr,
"%s: can't allocate %d destination AX25 addr bytes: %s\n",
Pn, len + 1, fp[3]);
Exit(1);
}
(void) snpf(da, len + 1, "%s", fp[3]);
} else
da = (char *)NULL;
/*
* Allocate space for the source address.
*/
if (!fp[2] || !*fp[2])
sa = (char *)NULL;
else if ((len = strlen(fp[2]))) {
if (!(sa = (char *)malloc(len + 1))) {
(void) fprintf(stderr,
"%s: can't allocate %d source AX25 address bytes: %s\n",
Pn, len + 1, fp[2]);
Exit(1);
}
(void) snpf(sa, len + 1, "%s", fp[2]);
} else
sa = (char *)NULL;
/*
* Allocate space for the device characters.
*/
if (!fp[1] || !*fp[1])
dev_ch = (char *)NULL;
else if ((len = strlen(fp[1]))) {
if (!(dev_ch = (char *)malloc(len + 1))) {
(void) fprintf(stderr,
"%s: can't allocate %d destination AX25 dev bytes: %s\n",
Pn, len + 1, fp[1]);
Exit(1);
}
(void) snpf(dev_ch, len + 1, "%s", fp[1]);
} else
dev_ch = (char *)NULL;
/*
* Allocate space for an ax25sin entry, fill it, and link it to its
* hash bucket.
*/
if (!(ap = (struct ax25sin *)malloc(sizeof(struct ax25sin)))) {
(void) fprintf(stderr,
"%s: can't allocate %d byte ax25sin structure\n",
Pn, sizeof(struct ax25sin));
Exit(1);
}
ap->da = da;
ap->dev_ch = dev_ch;
ap->inode = inode;
ap->rq = rq;
ap->rqs = rqs;
ap->sa = sa;
ap->sq = sq;
ap->sqs = sqs;
ap->state = (int)state;
ap->next = AX25sin[h];
AX25sin[h] = ap;
}
(void) fclose(as);
}
/*
* get_ipx() - get /proc/net/ipx info
*/
static void
get_ipx(p)
char *p; /* /proc/net/ipx path */
{
char buf[MAXPATHLEN], *ep, **fp, *la, *ra;
int fl = 1;
int h;
INODETYPE inode;
unsigned long rxq, state, txq;
struct ipxsin *ip, *np;
MALLOC_S len;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Ipxsin) {
for (h = 0; h < INOBUCKS; h++) {
for (ip = Ipxsin[h]; ip; ip = np) {
np = ip->next;
if (ip->la)
(void) free((FREE_P *)ip->la);
if (ip->ra)
(void) free((FREE_P *)ip->ra);
(void) free((FREE_P *)ip);
}
Ipxsin[h] = (struct ipxsin *)NULL;
}
} else {
Ipxsin = (struct ipxsin **)calloc(INOBUCKS,
sizeof(struct ipxsin *));
if (!Ipxsin) {
(void) fprintf(stderr,
"%s: can't allocate %d IPX hash pointer bytes\n",
Pn, INOBUCKS * sizeof(struct ipxsin *));
Exit(1);
}
}
/*
* Open the /proc/net/ipx file, assign a page size buffer to the stream,
* and read it. Store IPX socket info in the Ipxsin[] hash buckets.
*/
if (!(xs = open_proc_stream(p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(buf, (char *)NULL, &fp, (int *)NULL, 0) < 7)
continue;
if (fl) {
/*
* Check the column labels in the first line.
*/
if (!fp[0] || strcmp(fp[0], "Local_Address")
|| !fp[1] || strcmp(fp[1], "Remote_Address")
|| !fp[2] || strcmp(fp[2], "Tx_Queue")
|| !fp[3] || strcmp(fp[3], "Rx_Queue")
|| !fp[4] || strcmp(fp[4], "State")
|| !fp[5] || strcmp(fp[5], "Uid")
|| !fp[6] || strcmp(fp[6], "Inode"))
{
if (!Fwarn) {
(void) fprintf(stderr,
"%s: WARNING: unsupported format: %s\n",
Pn, p);
}
break;
}
fl = 0;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[6] || !*fp[6]
|| (inode = strtoull(fp[6], &ep, 0)) == ULONG_MAX
|| !ep || *ep)
continue;
h = INOHASH(inode);
for (ip = Ipxsin[h]; ip; ip = ip->next) {
if (inode == ip->inode)
break;
}
if (ip)
continue;
/*
* Assemble the transmit and receive queue values and the state.
*/
ep = (char *)NULL;
if (!fp[2] || !*fp[2]
|| (txq = strtoul(fp[2], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[3] || !*fp[3]
|| (rxq = strtoul(fp[3], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[4] || !*fp[4]
|| (state = strtoul(fp[4], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Allocate space for the local address, unless it is "Not_Connected".
*/
if (!fp[0] || !*fp[0] || strcmp(fp[0], "Not_Connected") == 0)
la = (char *)NULL;
else if ((len = strlen(fp[0]))) {
if (!(la = (char *)malloc(len + 1))) {
(void) fprintf(stderr,
"%s: can't allocate %d local IPX address bytes: %s\n",
Pn, len + 1, fp[0]);
Exit(1);
}
(void) snpf(la, len + 1, "%s", fp[0]);
} else
la = (char *)NULL;
/*
* Allocate space for the remote address, unless it is "Not_Connected".
*/
if (!fp[1] || !*fp[1] || strcmp(fp[1], "Not_Connected") == 0)
ra = (char *)NULL;