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
|
/*
* Copyright (c) 2000 Paul Herman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: print_packet.c,v 1.29 2002/08/12 17:47:24 pherman Exp $
*/
#include "tcpstat.h"
#include "packetdump.h"
char gstr[8192];
void print_arp_header(struct arphdr *arp);
void print_ip_header(struct ip *ip);
void print_tcp_header(struct tcphdr *tcp, unsigned int len, int wtp);
void print_udp_header(struct udphdr *udp, unsigned int len, int wtp);
void print_icmp_header(struct icmp *icmp, unsigned int len, int wtp);
#ifdef INET6
void print_ip6_header(struct ip6_hdr *ip);
void print_icmp6_header(struct icmp6_hdr *icmp6, unsigned int len, int wtp);
#endif
char *my_charconv(char c) {
char *s;
gstr[0] = '\0';
s = gstr;
if (isgraph((int)c)) *s++ = c;
else if (c == ' ') *s++ = c;
/* else if (c == '\n') { *s++ = '\\'; *s++ = 'n'; } */
else if (c == '\n' || c == '\r') { *s++ = c; }
/* else if (c == '\r') { *s++ = '\\'; *s++ = 'r'; } */
else *s++ = '.';
*s = '\0';
return gstr;
}
void print_packet(packet_data *p, int what_to_print) {
struct ip *ip;
struct tcphdr *tcp;
struct udphdr *udp;
struct icmp *icmp;
#ifdef INET6
struct ip6_hdr *ip6;
struct icmp6_hdr *icmp6;
#endif
if (what_to_print & PP_SHOW_BASICINFO) {
printf("PACKET SIZE: %u", p->packet_len);
if (p->packet_len != p->buffer_len)
printf(", (BUFFER SIZE: %u)", p->buffer_len);
printf("\n");
}
if (what_to_print & PP_SHOW_LINKLAYER) {
if (p->ether.ether_type == ETHERTYPE_ARP) {
print_arp_header(&(p->data.arp));
printf("---------------------------\n");
return;
}
}
if ((p->link_type & GENERIC_LINK_OTHER) != GENERIC_LINK_IP
#ifdef INET6
&& (p->link_type & GENERIC_LINK_OTHER) != GENERIC_LINK_IP6
#endif
)
return;
/* OK, it's an IP Packet */
ip = &(p->data.ip.hdr);
tcp= &(p->data.ip.body.tcphdr);
udp= &(p->data.ip.body.udphdr);
icmp= &(p->data.ip.body.icmp);
#ifdef INET6
ip6 = &(p->data.ip6.hdr);
tcp= &(p->data.ip6.body.tcphdr);
udp= &(p->data.ip6.body.udphdr);
icmp6= &(p->data.ip6.body.icmp6hdr);
#endif
if (what_to_print & PP_SHOW_IPHEADER) {
if (is_ip_packet(p)) print_ip_header(ip);
#ifdef INET6
if (is_ip6_packet(p)) print_ip6_header(ip6);
#endif
}
p->buffer_len -= sizeof(struct ip);
switch (get_ip_proto(p)) {
case IPPROTO_TCP:
print_tcp_header(tcp, p->buffer_len, what_to_print);
break;
case IPPROTO_UDP:
print_udp_header(udp, p->buffer_len, what_to_print);
break;
case IPPROTO_ICMP:
print_icmp_header(icmp, p->buffer_len, what_to_print);
break;
#ifdef INET6
case IPPROTO_ICMPV6:
print_icmp6_header(icmp6, p->buffer_len, what_to_print);
break;
#endif
default:
printf("UNKNOWN IP PROTOCOL! (0x%.4X)\n", (unsigned int)get_ip_proto(p));
break;
}
}
void print_arp_header(struct arphdr *arp) {
printf("ARP PACKET:\n");
printf("\thrd=%u pro=%u hln=%u plen=%u op=%u",
(unsigned int)htons(arp->ar_hrd),
(unsigned int)htons(arp->ar_pro),
(unsigned int)arp->ar_hln,
(unsigned int)arp->ar_pln,
(unsigned int)htons(arp->ar_op)
);
printf("\n");
}
void print_ip_header(struct ip *ip) {
char indnt[64] = " ";
struct protoent *ip_prot;
printf(" IP HEADER:\n");
ip_prot = getprotobynumber(ip->ip_p);
if (ip_prot == NULL) {
printf("Couldn't get IP Protocol\n");
return;
}
printf("%sver=%d hlen=%u TOS=%u len=%u ID=0x%.2X", indnt,
#ifdef _IP_VHL
ip->ip_vhl >> 4, (ip->ip_vhl & 0x0f) << 2,
#else
ip->ip_v, ip->ip_hl<<2,
#endif
(unsigned int)ip->ip_tos,
(unsigned int)htons(ip->ip_len),
(unsigned int)htons(ip->ip_id)
);
printf("\n%sFRAG=0x%.2x TTL=%u Proto=%s cksum=0x%.2X\n", indnt,
htons(ip->ip_off),
(unsigned int)ip->ip_ttl,
ip_prot->p_name,
htons(ip->ip_sum)
);
printf("%s%s", indnt, inet_ntoa(ip->ip_src) );
printf(" -> %s\n", inet_ntoa(ip->ip_dst) );
}
#ifdef INET6
void print_ip6_header(struct ip6_hdr *ip) {
char indnt[64] = " ";
struct protoent *ip_prot;
printf(" IPv6 HEADER:\n");
ip_prot = getprotobynumber(ip->ip6_nxt);
if (ip_prot == NULL) {
printf("Couldn't get IP Protocol\n");
return;
}
printf("%sver=%u class=0x%.2X label=0x%.5X plen=%u nxt=%u hlim=%u\n", indnt,
(unsigned int)(ip->ip6_vfc & 0xf0) >> 4,
(unsigned int)(htonl(ip->ip6_flow) & 0x0ff00000) >> 20,
(unsigned int)(htonl(ip->ip6_flow) & 0x000fffff),
htons(ip->ip6_plen),
(unsigned int)ip->ip6_nxt,
(unsigned int)ip->ip6_hlim
);
printf("%s%s", indnt,
inet_ntop(AF_INET6, &(ip->ip6_src), gstr, sizeof(gstr)) );
printf(" -> %s\n",
inet_ntop(AF_INET6, &(ip->ip6_src), gstr, sizeof(gstr)) );
}
#endif /* INET6 */
void print_tcp_header(struct tcphdr *tcp, unsigned int len, int wtp) {
char indnt[64] = " ";
if (wtp & PP_SHOW_TCPHEADER) {
printf("%sTCP HEADER:\n", indnt);
printf("%ssport=%u dport=%u seq=0x%lX ack=0x%lX doff=0x%.2X\n",
indnt,
(unsigned int)htons(tcp->th_sport),
(unsigned int)htons(tcp->th_dport),
(unsigned long int)htonl(tcp->th_seq),
(unsigned long int)htonl(tcp->th_ack),
tcp->th_off
);
printf("%sflags=(0x%X)", indnt, (unsigned int)tcp->th_flags);
if (tcp->th_flags & TH_FIN) printf(",FIN");
if (tcp->th_flags & TH_SYN) printf(",SYN");
if (tcp->th_flags & TH_RST) printf(",RST");
if (tcp->th_flags & TH_PUSH) printf(",PUSH");
if (tcp->th_flags & TH_ACK) printf(",ACK");
if (tcp->th_flags & TH_URG) printf(",URG");
printf(" win=%u cksm=0x%.4X urp=0x%.4X",
htons(tcp->th_win),
htons(tcp->th_sum),
htons(tcp->th_urp)
);
printf("\n");
}
if (wtp & PP_SHOW_PACKETCONTENT) {
u_char *p;
unsigned int i;
p = (u_char *)tcp;
p += (tcp->th_off)*sizeof(int);
len -= sizeof(struct tcphdr) + sizeof(int);
/* Options (sizeof(int)) is there for padding */
if (len>0) {
for (i=0; i<len; i++)
printf("%s", my_charconv(p[i]));
printf("\n");
}
}
}
void print_udp_header(struct udphdr *udp, unsigned int len, int wtp) {
if (wtp & PP_SHOW_UDPHEADER) {
printf("\tUDP HEADER:\n");
printf("\t\tsport=%u dport=%u len=%u cksum=0x%.2X\n",
(unsigned int)htons(udp->uh_sport),
(unsigned int)htons(udp->uh_dport),
(unsigned int)htons(udp->uh_ulen),
(unsigned int)htons(udp->uh_sum)
);
}
if (wtp & PP_SHOW_PACKETCONTENT) {
u_char *p;
unsigned int i;
p = (u_char *)udp;
p += sizeof(struct udphdr);
len -= sizeof(struct udphdr);
for (i=0; i<len; i++)
printf("%s", my_charconv(p[i]));
printf("\n");
}
}
void print_icmp_header(struct icmp *icmp, unsigned int len, int wtp) {
if (wtp & PP_SHOW_ICMPHEADER) {
printf("\tICMP HEADER:\n");
printf("\t\ttype=0x%X code=0x%X cksum=0x%.4X\n",
(unsigned int)icmp->icmp_type,
(unsigned int)icmp->icmp_code,
(unsigned int)htons(icmp->icmp_cksum)
);
}
if (wtp & PP_SHOW_PACKETCONTENT) {
u_char *p;
unsigned int i;
p = (u_char *)icmp;
p += sizeof(struct icmp);
len -= sizeof(struct icmp);
for (i=0; i<len; i++)
printf("%s", my_charconv(p[i]));
printf("\n");
}
}
#ifdef INET6
void print_icmp6_header(struct icmp6_hdr *icmp6, unsigned int len, int wtp) {
if (wtp & PP_SHOW_ICMPHEADER) {
printf("\tICMPv6 HEADER:\n");
printf("\t\ttype=0x%X code=0x%X cksum=0x%.4X\n",
(unsigned int)icmp6->icmp6_type,
(unsigned int)icmp6->icmp6_code,
(unsigned int)htons(icmp6->icmp6_cksum)
);
}
if (wtp & PP_SHOW_PACKETCONTENT) {
u_char *p;
unsigned int i;
p = (u_char *)icmp6;
p += sizeof(struct icmp6_hdr);
len -= sizeof(struct icmp6_hdr);
for (i=0; i<len; i++)
printf("%s", my_charconv(p[i]));
printf("\n");
}
}
#endif
|