File: listen.c

package info (click to toggle)
hping3 3.a2.ds2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,184 kB
  • ctags: 1,376
  • sloc: ansic: 11,582; sh: 133; makefile: 130
file content (80 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download | duplicates (6)
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
/* 
 * $smu-mark$ 
 * $name: listen.c$ 
 * $author: Salvatore Sanfilippo <antirez@invece.org>$ 
 * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$ 
 * $license: This software is under GPL version 2 of license$ 
 * $date: Fri Nov  5 11:55:48 MET 1999$ 
 * $rev: 8$ 
 */ 

/* $Id: listen.c,v 1.2 2003/09/01 00:22:06 antirez Exp $ */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "hping2.h" /* hping2.h includes hcmp.h */
#include "globals.h"

void listenmain(void)
{
	int size, ip_size;
	int stdoutFD = fileno(stdout);
	char packet[IP_MAX_SIZE+linkhdr_size];
	char *p, *ip_packet;
	struct myiphdr ip;
	__u16 id;
	static __u16 exp_id; /* expected id */

	exp_id = 1;

	while(1) {
		size = read_packet(packet, IP_MAX_SIZE+linkhdr_size);
		switch(size) {
		case 0:
			continue;
		case -1:
			exit(1);
		}
	
		/* Skip truncated packets */
		if (size < linkhdr_size+IPHDR_SIZE)
			continue;
		ip_packet = packet + linkhdr_size;

		/* copy the ip header so it will be aligned */
		memcpy(&ip, ip_packet, sizeof(ip));
		id = ntohs(ip.id);
		ip_size = ntohs(ip.tot_len);
		if (size-linkhdr_size > ip_size)
			size = ip_size;
		else
			size -= linkhdr_size;

		if ((p = memstr(ip_packet, sign, size))) {
			if (opt_verbose)
				fprintf(stderr, "packet %d received\n", id);

			if (opt_safe) {
				if (id == exp_id)
					exp_id++;
				else {
					if (opt_verbose)
						fprintf(stderr, "packet not in sequence (id %d) received\n", id);
					send_hcmp(HCMP_RESTART, exp_id);
					if (opt_verbose)
						fprintf(stderr, "HCMP restart from %d sent\n", exp_id);
					continue; /* discard this packet */
				}
			}

			p+=strlen(sign);
			write(stdoutFD, p, size-(p-ip_packet));
		}
	}
}