File: udp.c

package info (click to toggle)
pktstat 1.8.5-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 916 kB
  • sloc: ansic: 9,195; sh: 1,032; makefile: 34
file content (149 lines) | stat: -rw-r--r-- 2,944 bytes parent folder | download
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
/* David Leonard, 2002. Public domain. */
/* $Id: udp.c 1205 2012-03-04 14:10:49Z d $ */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#if STDC_HEADERS
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#endif
#if HAVE_NETDB_H
# include <netdb.h>
#endif
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if defined(__linux__) || defined(__GLIBC__)
# define __FAVOR_BSD
#endif
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if HAVE_NETINET_IN_SYSTM_H
# include <netinet/in_systm.h>
#endif
#if HAVE_NETINET_IP_H
# include <netinet/ip.h>
#endif
#if HAVE_NETINET_IP6_H
# include <netinet/ip6.h>
#endif
#if HAVE_NETINET_UDP_H
# include <netinet/udp.h>
#endif
#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#include "compat.h"
#include "tag.h"
#include "flow.h"
#include "hash.h"
#include "main.h"
#include "display.h"

static int
udp_cmp(const void *a, const void *b)
{
	const u_int16_t*ia = (const u_int16_t*)a;
	const u_int16_t*ib = (const u_int16_t*)b;

	return *ia - *ib;
}

static unsigned int
udp_hash(const void *a)
{
	const u_int16_t*ia = (const u_int16_t*)a;

	return *ia;
}

static struct hash udp_hashtab = {
	udp_cmp,		/* cmp */
	udp_hash,		/* hashfn */
	(free_t)free,		/* freekey */
	(free_t)free		/* freedata */
};

void
udp_reset()
{
	hash_clear(&udp_hashtab);
}

/* Look up an IP address */
const char *
udp_lookup(u_int16_t port)
{
	const char *result;
	static int oldnflag = -1;

	if (oldnflag != nflag) {
		udp_reset();
		oldnflag = nflag;
	}

	result = (const char *)hash_lookup(&udp_hashtab, &port);
	if (result == NULL) {
		struct servent *se;
		u_int16_t *a2;
		char buf[TAGLEN];

		if (nflag)
			se = NULL;
		else {
			display_message("resolving udp port %u", port);
#if HAVE_GETSERVBYPORT
			se = getservbyport(htons(port), "udp");
			display_message("");
#else
			se = NULL;
#endif
		}
		if (se == NULL) {
			snprintf(buf, sizeof buf, "%u", port);
			result = buf;
		} else
			result = se->s_name;
		result = strdup(result);
		a2 = (u_int16_t *)malloc(sizeof (u_int16_t));
		*a2 = port;
		hash_store(&udp_hashtab, a2, result);
	}
	return result;
}

const char *
udp_tag(const char *p, const char *end, const struct ip *ip,
				const struct ip6_hdr *ip6)
{
	static char src[TAGLEN], dst[TAGLEN];
	static char tag[TAGLEN];
	struct udphdr *udp = (struct udphdr *)p;
	u_int16_t sport = ntohs(udp->uh_sport);
	u_int16_t dport = ntohs(udp->uh_dport);

	if (ip) {
		snprintf(src, sizeof src, "%s:%s", 
			ip_lookup(&ip->ip_src), udp_lookup(sport));
		snprintf(dst, sizeof dst, "%s:%s", 
			ip_lookup(&ip->ip_dst), udp_lookup(dport));
		snprintf(tag, sizeof tag, "udp %s", tag_combine(src, dst));
	}
#if HAVE_NETINET_IP6_H
	if (ip6) {
		snprintf(src, sizeof src, "%s,%s", 
			ip6_lookup(&ip6->ip6_src), udp_lookup(sport));
		snprintf(dst, sizeof dst, "%s,%s", 
			ip6_lookup(&ip6->ip6_dst), udp_lookup(dport));
		snprintf(tag, sizeof tag, "udp6 %s", tag_combine(src, dst));
	}
#endif

	return tag;
}