Add new option -m which allows the user to set IP TTL.

This commit is contained in:
Ruslan Ermilov 2001-03-09 13:20:23 +00:00
parent ff7405adba
commit 211bfbd228
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=74029
2 changed files with 34 additions and 12 deletions

View File

@ -46,6 +46,7 @@ packets to network hosts
.Op Fl c Ar count .Op Fl c Ar count
.Op Fl i Ar wait .Op Fl i Ar wait
.Op Fl l Ar preload .Op Fl l Ar preload
.Op Fl m Ar ttl
.Op Fl p Ar pattern .Op Fl p Ar pattern
.Op Fl P Ar policy .Op Fl P Ar policy
.Op Fl s Ar packetsize .Op Fl s Ar packetsize
@ -136,6 +137,11 @@ is specified,
sends that many packets as fast as possible before falling into its normal sends that many packets as fast as possible before falling into its normal
mode of behavior. mode of behavior.
Only the super-user may use this option. Only the super-user may use this option.
.It Fl m Ar ttl
Set the IP Time To Live for outgoing packets.
If not specified, the kernel uses the value of the
.Va net.inet.ip.ttl
MIB variable.
.It Fl L .It Fl L
Suppress loopback of multicast packets. Suppress loopback of multicast packets.
This flag only applies if the ping destination is a multicast address. This flag only applies if the ping destination is a multicast address.

View File

@ -134,6 +134,7 @@ int options;
#define F_POLICY 0x4000 #define F_POLICY 0x4000
#endif /*IPSEC_POLICY_IPSEC*/ #endif /*IPSEC_POLICY_IPSEC*/
#endif /*IPSEC*/ #endif /*IPSEC*/
#define F_TTL 0x8000
/* /*
* MAX_DUP_CHK is the number of bits in received table, i.e. the maximum * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
@ -198,9 +199,9 @@ main(argc, argv)
struct sockaddr_in *to, sin; struct sockaddr_in *to, sin;
struct termios ts; struct termios ts;
register int i; register int i;
int ch, hold, packlen, preload, sockerrno, almost_done = 0; int ch, hold, packlen, preload, sockerrno, almost_done = 0, ttl;
struct in_addr ifaddr; struct in_addr ifaddr;
unsigned char ttl, loop; unsigned char mttl, loop;
u_char *datap, *packet; u_char *datap, *packet;
char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN]; char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN];
char snamebuf[MAXHOSTNAMELEN]; char snamebuf[MAXHOSTNAMELEN];
@ -234,13 +235,14 @@ main(argc, argv)
alarmtimeout = preload = 0; alarmtimeout = preload = 0;
datap = &outpack[8 + PHDR_LEN]; datap = &outpack[8 + PHDR_LEN];
#ifndef IPSEC while ((ch = getopt(argc, argv,
while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:t:v")) != -1) "I:LQRS:T:c:adfi:l:m:np:qrs:t:v"
#else #ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC #ifdef IPSEC_POLICY_IPSEC
while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:t:vP:")) != -1) "P:"
#endif /*IPSEC_POLICY_IPSEC*/ #endif /*IPSEC_POLICY_IPSEC*/
#endif #endif /*IPSEC*/
)) != -1)
{ {
switch(ch) { switch(ch) {
case 'a': case 'a':
@ -306,6 +308,14 @@ main(argc, argv)
options |= F_NOLOOP; options |= F_NOLOOP;
loop = 0; loop = 0;
break; break;
case 'm': /* TTL */
ultmp = strtoul(optarg, &ep, 0);
if (*ep || ep == optarg || ultmp > 255)
errx(EX_USAGE, "invalid TTL: `%s'",
optarg);
ttl = ultmp;
options |= F_TTL;
break;
case 'n': case 'n':
options |= F_NUMERIC; options |= F_NUMERIC;
break; break;
@ -357,7 +367,7 @@ main(argc, argv)
if (*ep || ep == optarg || ultmp > 255) if (*ep || ep == optarg || ultmp > 255)
errx(EX_USAGE, "invalid multicast TTL: `%s'", errx(EX_USAGE, "invalid multicast TTL: `%s'",
optarg); optarg);
ttl = ultmp; mttl = ultmp;
options |= F_MTTL; options |= F_MTTL;
break; break;
case 'v': case 'v':
@ -508,6 +518,12 @@ main(argc, argv)
#endif /* IP_OPTIONS */ #endif /* IP_OPTIONS */
} }
if (options & F_TTL) {
if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl)) < 0) {
err(EX_OSERR, "setsockopt IP_TTL");
}
}
if (options & F_NOLOOP) { if (options & F_NOLOOP) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
sizeof(loop)) < 0) { sizeof(loop)) < 0) {
@ -515,8 +531,8 @@ main(argc, argv)
} }
} }
if (options & F_MTTL) { if (options & F_MTTL) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
sizeof(ttl)) < 0) { sizeof(mttl)) < 0) {
err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
} }
} }
@ -1402,8 +1418,8 @@ static void
usage() usage()
{ {
fprintf(stderr, "%s\n%s\n%s\n", fprintf(stderr, "%s\n%s\n%s\n",
"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", "usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-m ttl]",
" " " [-p pattern] "
#ifdef IPSEC #ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC #ifdef IPSEC_POLICY_IPSEC
"[-P policy] " "[-P policy] "