File: udp.c

package info (click to toggle)
ipgrab 0.5-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 268 kB
  • ctags: 169
  • sloc: sh: 1,507; ansic: 1,234; makefile: 56
file content (92 lines) | stat: -rw-r--r-- 2,033 bytes parent folder | download | duplicates (2)
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
/**************************************************************************** 
** File: udp.c
**
** Author: Mike Borella
**
** Comments: Dump UDP header information
**
*****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "config.h"
#include "udp.h"

extern u_char *packet_end;
extern struct arg_t *my_args;

/*----------------------------------------------------------------------------
**
** dump_udp()
**
** Parse UDP header and dump fields
**
**----------------------------------------------------------------------------
*/

void dump_udp(u_char *bp, int length)
{
  UDPHdr *up;
  u_char *ep = bp + length;
  u_short sport, dport, len;
  char *udpport_string(u_short);
  void dump_payload(u_char *, int);
  
  /*
   * Make sure we don't run off the end of the packet
   */

  if (ep > packet_end) ep = packet_end;

  /*
   * Check for truncated packet
   */

  if (length < sizeof(UDPHdr))
    {
      printf("Truncated header, length = %d bytes\n", length);
      return;
    }

  up = (UDPHdr *) bp;
  sport = ntohs(up->uh_src);
  dport = ntohs(up->uh_dst);
  len = ntohs(up->uh_len);

  /*
   * Dump UDP header info
   */
  
  printf("----------------------------------------------------------\n");
  printf("                        UDP Header\n");
  printf("----------------------------------------------------------\n");

  if (!my_args->t)
    {  
      printf("Source port:            %d", sport);
      if (sport < 1024) 
	printf(" (%s)\n", udpport_string(sport));
      else 
	printf("\n");
      
      printf("Destination port:       %d", dport);
      if (dport < 1024) 
	printf(" (%s)\n", udpport_string(dport));
      else 
	printf("\n");
      
      printf("Length:                 %d\n", len*4);
      printf("Checksum:               %d\n", ntohs(up->uh_chk));
    }


  /*
   * print payload if there is one
   */

  if (my_args->p && len > 0) 
    dump_payload((u_char *) bp + sizeof(UDPHdr), len);

  return;
}