4 * (C)opyright 1995-1998 Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
16 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <netinet/in_systm.h>
21 #include <netinet/ip.h>
22 #include <netinet/tcp.h>
23 #include <netinet/udp.h>
24 #include <netinet/ip_icmp.h>
26 #include <netinet/ip_var.h>
27 #include <netinet/tcpip.h>
29 #include "ip_compat.h"
31 #include <linux/sockios.h>
37 static const char sccsid
[] = "@(#)ipsdr.c 1.3 12/3/95 (C)1995 Darren Reed";
38 static const char rcsid
[] = "@(#)Id: ipsdr.c,v 2.2 2001/06/09 17:09:25 darrenr Exp";
46 u_short defports
[NPORTS
] = {
47 7, 9, 20, 21, 23, 25, 53, 69, 79, 111,
48 123, 161, 162, 512, 513, 513, 515, 520, 540, 6000, 0
50 u_short pweights
[NPORTS
] = {
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
55 ipsd_t
*iphits
[NPORTS
];
62 return sh1
->sh_ip
.s_addr
- sh2
->sh_ip
.s_addr
;
69 return sh1
->ss_ip
.s_addr
- sh2
->ss_ip
.s_addr
;
78 for (i
= 1, j
= 0; i
; i
<<= 1)
86 * Check to see if we've already received a packet from this host for this
89 int findhit(ihp
, src
, dport
)
99 if (ihp
->sd_sz
== 4) {
100 for (i
= 0, sh
= ihp
->sd_hit
; i
< ihp
->sd_cnt
; i
++, sh
++)
101 if (src
.s_addr
== sh
->sh_ip
.s_addr
)
104 for (i
= ihp
->sd_cnt
/ 2, j
= (i
/ 2) - 1; j
>= 0; j
--) {
105 k
= ihp
->sd_hit
[i
].sh_ip
.s_addr
- src
.s_addr
;
119 * Search for port number amongst the sorted array of targets we're
122 int detect(srcip
, dport
, date
)
123 struct in_addr srcip
;
131 for (i
= 10, j
= 4; j
>= 0; j
--) {
132 k
= dport
- defports
[i
];
135 if (findhit(ihp
, srcip
, dport
))
137 sh
= ihp
->sd_hit
+ ihp
->sd_cnt
;
140 if (++ihp
->sd_cnt
== ihp
->sd_sz
)
143 sh
= realloc(sh
, ihp
->sd_sz
* sizeof(*sh
));
146 qsort(sh
, ihp
->sd_cnt
, sizeof(*sh
), ipcmp
);
159 * Allocate initial storage for hosts
165 for (i
= 0; i
< NPORTS
; i
++) {
167 if (iphits
[i
]->sd_hit
)
168 free(iphits
[i
]->sd_hit
);
171 iphits
[i
] = (ipsd_t
*)malloc(sizeof(ipsd_t
));
172 iphits
[i
]->sd_port
= defports
[i
];
173 iphits
[i
]->sd_cnt
= 0;
174 iphits
[i
]->sd_sz
= 4;
175 iphits
[i
]->sd_hit
= (sdhit_t
*)malloc(sizeof(sdhit_t
) * 4);
181 * Write statistics out to a file
186 ipsd_t ipsd
, *ips
= &ipsd
;
191 if ((fd
= open(file
, O_RDONLY
)) == -1) {
196 printf("opened %s\n", file
);
198 if (read(fd
, ips
, sizeof(*ips
)) != sizeof(*ips
))
200 sz
= ips
->sd_sz
* sizeof(*hp
);
201 hp
= (sdhit_t
*)malloc(sz
);
202 if (read(fd
, hp
, sz
) != sz
)
204 for (i
= 0; i
< ips
->sd_cnt
; i
++)
205 detect(hp
[i
].sh_ip
, ips
->sd_port
, hp
[i
].sh_date
);
218 i
= scandir(dir
, &d
, NULL
, NULL
);
220 for (j
= 0; j
< i
; j
++) {
221 if (strncmp(d
[j
]->d_name
, "ipsd-hits.", 10))
223 addfile(d
[j
]->d_name
);
228 void printreport(ss
, num
)
237 printf("Hosts detected: %d\n", num
);
240 for (i
= 0; i
< num
; i
++)
241 printf("%s %d %d\n", inet_ntoa(ss
[i
].ss_ip
), ss
[i
].ss_hits
,
242 countpbits(ss
[i
].ss_ports
));
244 printf("--------------------------\n");
245 for (mask
= 0xfffffffe, j
= 32; j
; j
--, mask
<<= 1) {
246 ip
.s_addr
= ss
[0].ss_ip
.s_addr
& mask
;
247 ports
= ss
[0].ss_ports
;
248 for (i
= 1; i
< num
; i
++) {
250 if (ip
.s_addr
!= (sp
->ss_ip
.s_addr
& mask
)) {
251 printf("Netmask: 0x%08x\n", mask
);
252 printf("%s %d\n", inet_ntoa(ip
),
254 ip
.s_addr
= sp
->ss_ip
.s_addr
& mask
;
257 ports
|= sp
->ss_ports
;
260 printf("Netmask: 0x%08x\n", mask
);
261 printf("%s %d\n", inet_ntoa(ip
), countpbits(ports
));
271 int i
, num
, nip
, in
, j
, k
;
273 for (i
= 0; i
< NPORTS
; i
++)
274 nip
+= iphits
[i
]->sd_cnt
;
276 ss
= (ipss_t
*)malloc(sizeof(ipss_t
) * nip
);
278 for (in
= 0, i
= 0, num
= 0; i
< NPORTS
; i
++) {
280 for (j
= 0; j
< ips
->sd_cnt
; j
++) {
281 for (k
= 0; k
< num
; k
++)
282 if (!bcmp(&ss
[k
].ss_ip
, &ips
->sd_hit
[j
].sh_ip
,
283 sizeof(struct in_addr
))) {
284 ss
[k
].ss_hits
+= pweights
[i
];
285 ss
[k
].ss_ports
|= (1 << i
);
289 ss
[num
].ss_ip
= ips
->sd_hit
[j
].sh_ip
;
290 ss
[num
].ss_hits
= pweights
[i
];
291 ss
[k
].ss_ports
|= (1 << i
);
297 qsort(ss
, num
, sizeof(*ss
), ssipcmp
);
299 printreport(ss
, num
);
307 char c
, *name
= argv
[0], *dir
= NULL
;
311 dir
= dir
? dir
: ".";