UbixOS V2
2.0
tcpdump.c
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2001, Swedish Institute of Computer Science.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the Institute nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*
29
* This file is part of the lwIP TCP/IP stack.
30
*
31
* Author: Adam Dunkels <adam@sics.se>
32
*
33
* $Id: tcpdump.c 54 2016-01-11 01:29:55Z reddawg $
34
*/
35
36
#include <
vfs/file.h
>
37
#include <
ubixos/kpanic.h
>
38
39
#include "netif/tcpdump.h"
40
#include "net/ipv4/ip.h"
41
#include "
net/tcp.h
"
42
#include "
net/udp.h
"
43
#include "net/ipv4/inet.h"
44
45
fileDescriptor
*
file
=
NULL
;
46
47
void
tcpdump_init
(
void
) {
48
char
*fname;
49
50
fname =
"tcpdump"
;
51
file
=
fopen
(fname,
"wb"
);
52
if
(
file
==
NULL
) {
53
kpanic
(
"tcpdump_init: fopen\n"
);
54
}
55
}
56
57
void
tcpdump
(
struct
pbuf
*p) {
58
/*
59
struct ip_hdr *iphdr;
60
struct tcp_hdr *tcphdr;
61
struct udp_hdr *udphdr;
62
char flags[5];
63
int i;
64
int len;
65
int offset;
66
*/
67
if
(
file
==
NULL
) {
68
return
;
69
}
70
71
/*
72
iphdr = p->payload;
73
switch(IPH_PROTO(iphdr)) {
74
case IP_PROTO_TCP:
75
tcphdr = (struct tcp_hdr *)((char *)iphdr + IP_HLEN);
76
77
pbuf_header(p, -IP_HLEN);
78
if(inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
79
(struct ip_addr *)&(iphdr->dest),
80
IP_PROTO_TCP, p->tot_len) != 0) {
81
DEBUGF(TCPDUMP_DEBUG, ("tcpdump: IP checksum failed!\n"));
82
fprintf(file, "!chksum ");
83
}
84
85
i = 0;
86
if(TCPH_FLAGS(tcphdr) & TCP_SYN) {
87
flags[i++] = 'S';
88
}
89
if(TCPH_FLAGS(tcphdr) & TCP_PSH) {
90
flags[i++] = 'P';
91
}
92
if(TCPH_FLAGS(tcphdr) & TCP_FIN) {
93
flags[i++] = 'F';
94
}
95
if(TCPH_FLAGS(tcphdr) & TCP_RST) {
96
flags[i++] = 'R';
97
}
98
if(i == 0) {
99
flags[i++] = '.';
100
}
101
flags[i++] = 0;
102
103
104
105
fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ",
106
(int)(ntohl(iphdr->src.addr) >> 24) & 0xff,
107
(int)(ntohl(iphdr->src.addr) >> 16) & 0xff,
108
(int)(ntohl(iphdr->src.addr) >> 8) & 0xff,
109
(int)(ntohl(iphdr->src.addr) >> 0) & 0xff,
110
ntohs(tcphdr->src),
111
(int)(ntohl(iphdr->dest.addr) >> 24) & 0xff,
112
(int)(ntohl(iphdr->dest.addr) >> 16) & 0xff,
113
(int)(ntohl(iphdr->dest.addr) >> 8) & 0xff,
114
(int)(ntohl(iphdr->dest.addr) >> 0) & 0xff,
115
ntohs(tcphdr->dest));
116
offset = TCPH_OFFSET(tcphdr) >> 4;
117
118
len = ntohs(IPH_LEN(iphdr)) - offset * 4 - IP_HLEN;
119
if(len != 0 || flags[0] != '.') {
120
fprintf(file, "%s %lu:%lu(%u) ",
121
flags,
122
ntohl(tcphdr->seqno),
123
ntohl(tcphdr->seqno) + len,
124
len);
125
}
126
if(TCPH_FLAGS(tcphdr) & TCP_ACK) {
127
fprintf(file, "ack %lu ",
128
ntohl(tcphdr->ackno));
129
}
130
fprintf(file, "wnd %u\n",
131
ntohs(tcphdr->wnd));
132
133
fflush(file);
134
135
pbuf_header(p, IP_HLEN);
136
break;
137
138
case IP_PROTO_UDP:
139
udphdr = (struct udp_hdr *)((char *)iphdr + IP_HLEN);
140
141
pbuf_header(p, -IP_HLEN);
142
if(inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
143
(struct ip_addr *)&(iphdr->dest),
144
IP_PROTO_UDP, p->tot_len) != 0) {
145
kprintf("tcpdump: IP checksum failed!\n");
146
fprintf(file, "!chksum ");
147
}
148
149
fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ",
150
(int)(ntohl(iphdr->src.addr) >> 24) & 0xff,
151
(int)(ntohl(iphdr->src.addr) >> 16) & 0xff,
152
(int)(ntohl(iphdr->src.addr) >> 8) & 0xff,
153
(int)(ntohl(iphdr->src.addr) >> 0) & 0xff,
154
ntohs(udphdr->src),
155
(int)(ntohl(iphdr->dest.addr) >> 24) & 0xff,
156
(int)(ntohl(iphdr->dest.addr) >> 16) & 0xff,
157
(int)(ntohl(iphdr->dest.addr) >> 8) & 0xff,
158
(int)(ntohl(iphdr->dest.addr) >> 0) & 0xff,
159
ntohs(udphdr->dest));
160
fprintf(file, "U ");
161
len = ntohs(IPH_LEN(iphdr)) - sizeof(struct udp_hdr) - IP_HLEN;
162
fprintf(file, " %d\n", len);
163
164
fflush(file);
165
166
pbuf_header(p, IP_HLEN);
167
break;
168
169
}
170
*/
171
}
172
173
/***
174
END
175
***/
176
file.h
fopen
fileDescriptor_t * fopen(const char *file, const char *flags)
Definition:
file.c:388
fileDescriptor
Definition:
file.h:62
file
Definition:
descrip.h:67
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition:
kpanic.c:41
kpanic.h
tcpdump_init
void tcpdump_init(void)
Definition:
tcpdump.c:47
tcp.h
udp.h
pbuf
Definition:
pbuf.h:142
tcpdump
void tcpdump(struct pbuf *p)
Definition:
tcpdump.c:57
NULL
#define NULL
Definition:
fat_string.h:17
C:
Dev
git
UbixOS
sys
net
netif
tcpdump.c
Generated by
1.8.16