-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpmon.c
147 lines (129 loc) · 4.52 KB
/
udpmon.c
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
/*
udpmon - udp payload dump
Copyright (C) 2012 David Kierzokwski ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "/usr/include/pcap/pcap.h" // NEED TO FIX THIS
#include<sys/socket.h>
#include<arpa/inet.h>
#include<net/ethernet.h>
#include<netinet/udp.h>
#include<netinet/ip.h>
#include<getopt.h>
#include<time.h>
struct UDP_hdr {
u_short uh_sport; //Source Port
u_short uh_dport; //Destnation Port
u_short uh_ulen; //Datagram Length
u_short uh_sum; //Datagram Checksum
};
int debug = 0;
char *devname = NULL;
uint16_t SrcID = ((uint16_t)67 << 8) | ((uint16_t)65 << 0);
void usage( int8_t e );
void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char * packet)
{
struct ip * ip;
struct UDP_hdr * udp;
unsigned int IP_header_length;
unsigned int capture_len = pkthdr->len;
int i=0, *counter = (int *)arg;
time_t Time;
char* c_time_string;
packet += sizeof (struct ether_header);
capture_len -= sizeof(struct ether_header);
ip = (struct ip*) packet;
IP_header_length = ip->ip_hl *4;
packet += IP_header_length;
capture_len -= IP_header_length;
udp = (struct UDP_hdr*) packet;
packet += sizeof (struct UDP_hdr);
capture_len -= sizeof (struct UDP_hdr);
Time = time(NULL);
c_time_string = time(&Time);
printf("L:%3i ",capture_len);
printf("T:%10ju ",(uintmax_t)Time);
printf("S:%15s ",inet_ntoa(ip->ip_src));
printf(":%5d ",ntohs(udp->uh_sport));
printf("D:%15s", inet_ntoa(ip->ip_dst));
printf(":%5d ",ntohs(udp->uh_dport));
while (i < capture_len) {
printf("%02X", packet[i]);
i++;
};
printf("\n");
fflush(stdout);
}
int main(int argc, char *argv[] )
{
setbuf(stdout, NULL);
char packet_filter[] ="udp";
struct bpf_program fcode;
u_int netmask;
pcap_t *descr = NULL;
int32_t c;
while ((c = getopt(argc, argv, "Vhi:")) != EOF) {
switch (c) {
case 'V':
version();
break;
case 'i':
devname = optarg;
break;
case 'h':
usage(-1);
break;
}
}
if (devname == NULL) {
usage(-1);
}
if (debug == 1) {
printf("USING CAPTURE DEVICE: %s\n", devname);
}
pcap_if_t *alldevsp , *device;
pcap_t *handle;
char errbuf[100] , devs[100][100];
int count = 1 , n;
handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
exit(1);
}
pcap_compile(handle, &fcode, packet_filter, 1, netmask);
if ( pcap_loop(handle, -1, processPacket, (u_char *)&count) == -1) {
fprintf(stderr, "ERROR: %s\n", pcap_geterr(descr) );
exit(1);
}
return 0;
}
void usage(int8_t e)
{
printf( "Usage: udpmon [OPTION]... \n"
"Listen for udp packets and dump payload on screen\n"
"\n"
" -i, --interface Interface to listen on\n"
" -h, --help This Help\n"
" -V, --version Version Information\n"
" -d, --debug Show verbose information\n"
"\n"
"Report bugs to [email protected]\n");
exit(e);
}
int version ( void )
{
printf ("udpmon 1.00\n");
exit(1);
}