summaryrefslogtreecommitdiff
path: root/tools/parser/tcpip.c
blob: 91de10458426263196c4ad47d67fba0d3f9cc3bc (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2003-2011  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "parser.h"

void arp_dump(int level, struct frame *frm)
{
	int i;
	char buf[20];
	struct sockaddr_in sai;
	struct ether_arp *arp = (struct ether_arp *) frm->ptr;

	printf("Src ");
	for (i = 0; i < 5; i++)
		printf("%02x:", arp->arp_sha[i]);
	printf("%02x", arp->arp_sha[5]);
	sai.sin_family = AF_INET;
	memcpy(&sai.sin_addr, &arp->arp_spa, sizeof(sai.sin_addr));
	getnameinfo((struct sockaddr *) &sai, sizeof(sai), buf, sizeof(buf),
		    NULL, 0, NI_NUMERICHOST);
	printf("(%s) ", buf);
	printf("Tgt ");
	for (i = 0; i < 5; i++)
		printf("%02x:", arp->arp_tha[i]);
	printf("%02x", arp->arp_tha[5]);
	memcpy(&sai.sin_addr, &arp->arp_tpa, sizeof(sai.sin_addr));
	getnameinfo((struct sockaddr *) &sai, sizeof(sai), buf, sizeof(buf),
		    NULL, 0, NI_NUMERICHOST);
	printf("(%s)\n", buf);
	frm->ptr += sizeof(struct ether_arp);
	frm->len -= sizeof(struct ether_arp);
	raw_dump(level, frm);		// not needed.
}

void ip_dump(int level, struct frame *frm)
{
	char src[50], dst[50];
	struct ip *ip = (struct ip *) (frm->ptr);
	uint8_t proto;
	int len;

	if (ip->ip_v == 4) {
		struct sockaddr_in sai;
		proto = ip->ip_p;
		len = ip->ip_hl << 2;
		memset(&sai, 0, sizeof(sai));
		sai.sin_family = AF_INET;
		memcpy(&sai.sin_addr, &ip->ip_src, sizeof(struct in_addr));
		getnameinfo((struct sockaddr *) &sai, sizeof(sai),
			    src, sizeof(src), NULL, 0, NI_NUMERICHOST);
		memcpy(&sai.sin_addr, &ip->ip_dst, sizeof(struct in_addr));
		getnameinfo((struct sockaddr *) &sai, sizeof(sai),
			    dst, sizeof(dst), NULL, 0, NI_NUMERICHOST);
	} else if (ip->ip_v == 6) {
		struct sockaddr_in6 sai6;
		struct ip6_hdr *ip6 = (struct ip6_hdr *) ip;
		proto = ip6->ip6_nxt;
		len = sizeof(struct ip6_hdr);
		memset(&sai6, 0, sizeof(sai6));
		sai6.sin6_family = AF_INET6;
		memcpy(&sai6.sin6_addr, &ip6->ip6_src, sizeof(struct in6_addr));
		getnameinfo((struct sockaddr *) &sai6, sizeof(sai6),
			    src, sizeof(src), NULL, 0, NI_NUMERICHOST);
		memcpy(&sai6.sin6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
		getnameinfo((struct sockaddr *) &sai6, sizeof(sai6),
			    dst, sizeof(dst), NULL, 0, NI_NUMERICHOST);
	} else {
		raw_dump(level, frm);
		return;
	}

	printf("src %s ", src);
	printf("dst %s\n", dst);

	frm->ptr += len;
	frm->len -= len;
	p_indent(++level, frm);

	switch (proto) {
	case IPPROTO_TCP:
		printf("TCP:\n");
		break;

	case IPPROTO_UDP:
		printf("UDP:\n");
		break;

	case IPPROTO_ICMP:
		printf("ICMP:\n");
		break;

	case IPPROTO_ICMPV6:
		printf("ICMPv6:\n");
		break;

	default:
		printf("Unknown Protocol: 0x%02x\n", ip->ip_p);
		break;
	}

	raw_dump(level, frm);
}