File: icmpinfo.c

package info (click to toggle)
icmpinfo 1.11-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 216 kB
  • ctags: 217
  • sloc: ansic: 1,024; makefile: 50
file content (148 lines) | stat: -rw-r--r-- 4,315 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
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
/*
 * icmpinfo
 *    It is a tool to look at the icmp you receive
 *    Its source comes from a modified BSD ping source by Laurent Demailly
 *
 *              (c) 1995 by Laurent Demailly - <dl@hplyot.obspm.fr>
 *              it comes AS IS - no warranty, etc...
 *                    see LICENSE
 *
 * see the README for usage infos...etc...
 *
 */
/*
 * Copyright (c) 1987 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1987 Regents of the University of California.\n\
 All rights reserved.\n augmented 94/8-95 by dl\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)ping.c	4.10 (Berkeley) 10/10/88 - $Author: icmpinfo-1.11 - Laurent Demailly <dl@hplyot.obspm.fr> - 8/1995 $";
#endif /* not lint */

#define DCLARE /* def : */

#include  "defs.h"

/*
 *			P I N G . C
 *
 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
 * measure round-trip-delays and packet loss across network paths.
 *
 * Author -
 *	Mike Muuss
 *	U. S. Army Ballistic Research Laboratory
 *	December, 1983
 * Modified at Uc Berkeley
 *
 * Original ping status -
 *	Public Domain.  Distribution Unlimited.
 *      see LICENSE for icmpinfo distribution and modifications conditions.
 *
 * Bugs -
 *	More statistics could always be gathered.
 *	This program has to run SUID to ROOT to access the ICMP socket.
 */

char	usage[] = "Usage:  icmpinfo [-v[v[v]]] [-s] [-n] [-p] [-l] [-k]\n   -v : more and more info\n   -s : show local interface address\n   -n : no name query (dot ip only)\n   -p : no port -> service name query\n   -l : fork + syslog output\n   -k : kill background process\nv1.11 - 8/1995 - dl";
char	*pname;

int main(argc, argv)
int	argc;
char	**argv;
{
	int			sockoptions, on;
	struct protoent		*proto;

	on = 1;
	pname = argv[0];
	argc--;
	argv++;

	sockoptions=nonamequery=noportquery=syslogdoutput=showsrcip=0;
	while (argc > 0 && *argv[0] == '-') {
		while (*++argv[0]) switch (*argv[0]) {
			case 'd':
				sockoptions |= SO_DEBUG;
				break;
			case 'r':
				sockoptions |= SO_DONTROUTE;
				break;
			case 'v':
				verbose++;
				break;
			case 'n':
				nonamequery++;
				break;
			case 'p':
				noportquery++;
				break;
			case 'l':
				syslogdoutput++;
				break;
			case 's':
				showsrcip++;
				break;
			case 'k':
				pid_kill();
				exit(0);
				/*NOTREACHED*/
				break;
			case 'h':
		        default :
				err_quit(usage);
		}
		argc--, argv++;
	}
	if (argc!=0) err_quit(usage);
	if ( (proto = getprotobyname("icmp")) == NULL)
		err_quit("unknown protocol: icmp");
	if ( (sockfd = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
		err_sys("can't create raw socket (root and/or bit s needed)");
	if (sockoptions & SO_DEBUG)
		if (setsockopt(sockfd, SOL_SOCKET, SO_DEBUG, &on,
								sizeof(on)) < 0)
			err_sys("setsockopt SO_DEBUG error");
	if (sockoptions & SO_DONTROUTE)
		if (setsockopt(sockfd, SOL_SOCKET, SO_DONTROUTE, &on,
								sizeof(on)) < 0)
			err_sys("setsockopt SO_DONTROUTE error");

	if (syslogdoutput) {
	  if (getuid()!=0)
	    err_quit("You need root id to use the syslog/daemon -l option");
	  if (fork()) {exit(0);}
	  /* Can't check openlog & syslog retcodes 'cause lot of
             unixes have void openlog(); and void syslog(); !! */
	  openlog("icmpinfo",0,LOG_DAEMON);
	  syslog(LOG_NOTICE,"started, PID=%d.",getpid());
	  setsid();
	  pid_file();
	  close(0);
	  close(1);
	  close(2);
	} else {
	    printf("icmpinfo: Icmp monitoring in progress...\n");
	}
	recv_ping();	/* and start the receive */
	/* NOTREACHED */
	return(0);
}