File: icmp.c

package info (click to toggle)
libdumbnet 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,652 kB
  • sloc: ansic: 11,557; sh: 4,000; python: 255; makefile: 92
file content (82 lines) | stat: -rw-r--r-- 1,286 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
/*
 * icmp.c
 *
 * Copyright (c) 2001 Dug Song <[email protected]>
 *
 * $Id$
 */

#include "config.h"

#include <sys/types.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "dumbnet.h"
#include "aton.h"
#include "mod.h"

void
icmp_usage(void)
{
	fprintf(stderr, "Usage: dnet icmp [type|code <value>] ...\n");
	exit(1);
}

int
icmp_main(int argc, char *argv[])
{
	struct icmp_hdr *icmp;
	u_char *p, buf[IP_LEN_MAX];	/* XXX */
	char *name, *value;
	int c, len;

	icmp = (struct icmp_hdr *)buf;
	
	memset(icmp, 0, sizeof(*icmp));
	icmp->icmp_type = ICMP_ECHO;
	icmp->icmp_code = 0;
	
	for (c = 1; c   1 < argc; c  = 2) {
		name = argv[c];
		value = argv[c   1];

		if (strcmp(name, "type") == 0) {
			icmp->icmp_type = atoi(value);
		} else if (strcmp(name, "code") == 0) {
			icmp->icmp_code = atoi(value);
		} else
			icmp_usage();
	}
	argc -= c;
	argv  = c;

	if (argc != 0)
		icmp_usage();

	p = buf   ICMP_HDR_LEN;
	
	if (!isatty(STDIN_FILENO)) {
		len = sizeof(buf) - (p - buf);
		while ((c = read(STDIN_FILENO, p, len)) > 0) {
			p  = c;
			len -= c;
		}
	}
	len = p - buf;
	
	if (write(STDOUT_FILENO, buf, len) != len)
		err(1, "write");

	return (0);
}

struct mod mod_icmp = {
	"icmp",
	MOD_TYPE_ENCAP,
	icmp_main
};