"Fossies" - the Fresh Open Source Software Archive 
Member "ngrep-1_47/tcpkill.c" (7 Sep 2017, 2057 Bytes) of package /linux/misc/ngrep-1_47.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "tcpkill.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1_46_1_vs_1_47.
1 /*
2 * tcpkill.c
3 *
4 * Kill TCP connections already in progress.
5 *
6 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
7 *
8 * $Id: tcpkill.c,v 1.17 2001/03/17 08:10:43 dugsong Exp $
9 */
10
11 #include <sys/types.h>
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <libnet.h>
18 #include <pcap.h>
19
20 #include "tcpkill.h"
21
22 libnet_t *l;
23
24 void
25 tcpkill_kill(const struct pcap_pkthdr *pcap, const u_char *pkt,
26 uint32_t pcap_off, uint32_t kill_count)
27 {
28 struct libnet_ipv4_hdr *ip;
29 struct libnet_tcp_hdr *tcp;
30 u_char ctext[64];
31 uint32_t seq, win, i, len;
32
33 pkt += pcap_off;
34 len = pcap->caplen - pcap_off;
35
36 ip = (struct libnet_ipv4_hdr *)pkt;
37 if (ip->ip_p != IPPROTO_TCP)
38 return;
39
40 tcp = (struct libnet_tcp_hdr *)(pkt + (ip->ip_hl << 2));
41 if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST))
42 return;
43
44 seq = ntohl(tcp->th_ack);
45 win = ntohs(tcp->th_win);
46
47 snprintf(ctext, sizeof(ctext), "%s:%d > %s:%d:",
48 libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
49 ntohs(tcp->th_sport),
50 libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
51 ntohs(tcp->th_dport));
52
53 for (i = 0; i < kill_count; i++) {
54 seq += (i * win);
55
56 libnet_clear_packet(l);
57
58 libnet_build_tcp(ntohs(tcp->th_dport), ntohs(tcp->th_sport),
59 seq, 0, TH_RST, 0, 0, 0, LIBNET_TCP_H,
60 NULL, 0, l, 0);
61
62 libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H, 0,
63 libnet_get_prand(LIBNET_PRu16), 0, 64,
64 IPPROTO_TCP, 0, ip->ip_dst.s_addr,
65 ip->ip_src.s_addr, NULL, 0, l, 0);
66
67 if (libnet_write(l) < 0)
68 warn("write");
69
70 fprintf(stderr, "%s R %lu:%lu(0) win 0\n", ctext, seq, seq);
71 }
72 }
73
74 void
75 tcpkill_init(void)
76 {
77 char *intf, ebuf[PCAP_ERRBUF_SIZE];
78 char libnet_ebuf[LIBNET_ERRBUF_SIZE];
79
80 if ((intf = pcap_lookupdev(ebuf)) == NULL)
81 errx(1, "%s", ebuf);
82
83 if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL)
84 errx(1, "couldn't initialize sending");
85
86 libnet_seed_prand(l);
87 }