XINU
Functions
xsh_ping.c File Reference
#include <xinu.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for xsh_ping.c:

Go to the source code of this file.

Functions

shellcmd xsh_ping (int nargs, char *args[])
 

Function Documentation

◆ xsh_ping()

shellcmd xsh_ping ( int  nargs,
char *  args[] 
)

Definition at line 11 of file xsh_ping.c.

References dnslookup(), dot2ip(), FALSE, fprintf(), ICMP_ECHOREQST, icmp_recv(), icmp_register(), icmp_release(), icmp_send(), printf(), stderr, strlen(), strncmp(), SYSERR, TIMEOUT, and TRUE.

12 {
13  uint32 ipaddr; /* IP address in binary */
14  int32 retval; /* return value */
15  int32 slot; /* Slot in ICMP to use */
16  static int32 seq = 0; /* sequence number */
17  char buf[56]; /* buffer of chars */
18  int32 i; /* index into buffer */
19  int32 nextval; /* next value to use */
20  bool8 dname;
21 
22  /* For argument '--help', emit help about the 'ping' command */
23 
24  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
25  printf("Use: %s address\n\n", args[0]);
26  printf("Description:\n");
27  printf("\tUse ICMP Echo to ping a remote host\n");
28  printf("Options:\n");
29  printf("\t--help\t display this help and exit\n");
30  printf("\taddress\t an IP address in dotted decimal\n");
31  return 0;
32  }
33 
34  /* Check for valid number of arguments */
35 
36  if (nargs != 2) {
37  fprintf(stderr, "%s: invalid arguments\n", args[0]);
38  fprintf(stderr, "Try '%s --help' for more information\n",
39  args[0]);
40  return 1;
41  }
42 
43  dname = FALSE;
44  for(i = 0; i < strlen(args[1]); i++) {
45  if( ( (args[1][i] >= 65) && (args[1][i] <= 90) ) ||
46  ( (args[1][i] >= 97) && (args[1][i] <= 122)) ) {
47  dname = TRUE;
48  break;
49  }
50  }
51 
52  if(dname == TRUE) {
53  ipaddr = dnslookup(args[1]);
54  if((int32)ipaddr == SYSERR) {
55  fprintf(stderr, "DNS cannot resolve %s\n", args[1]);
56  return 1;
57  }
58  printf("Pinging %d.%d.%d.%d\n", (ipaddr>>24)&0xff,
59  (ipaddr>>16)&0xff,
60  (ipaddr>>8)&0xff,
61  (ipaddr)&0xff);
62  }
63  else {
64  /* convert argument to binary */
65 
66  retval = dot2ip(args[1], &ipaddr);
67  if ((int32)retval == SYSERR) {
68  fprintf(stderr, "%s: invalid IP address\n", args[0]);
69  return 1;
70  }
71  }
72 
73  /* Register to receive an ICMP Echo Reply */
74 
75  slot = icmp_register(ipaddr);
76  if (slot == SYSERR) {
77  fprintf(stderr,"%s: ICMP registration failed\n", args[0]);
78  return 1;
79  }
80 
81  /* Fill the buffer with values - start with low-order byte of */
82  /* the sequence number and increment */
83 
84  nextval = seq;
85  for (i = 0; i<sizeof(buf); i++) {
86  buf[i] = 0xff & nextval++;
87  }
88 
89  /* Send an ICMP Echo Request */
90  retval = icmp_send(ipaddr, ICMP_ECHOREQST, slot,
91  seq++, buf, sizeof(buf));
92  if (retval == SYSERR) {
93  fprintf(stderr, "%s: no response from host %s\n", args[0], args[1]);
94  icmp_release(slot);
95  return 1;
96  }
97 
98  /* Read a reply */
99 
100  retval = icmp_recv(slot, buf, sizeof(buf), 3000);
101  icmp_release(slot);
102  if (retval == TIMEOUT) {
103  fprintf(stderr, "%s: no response from host %s\n", args[0],
104  args[1]);
105  return 1;
106  }
107 
108  if (retval != sizeof(buf)) {
109  fprintf(stderr,"expected %d bytes but got back %d\n",
110  sizeof(buf), retval);
111  }
112  fprintf(stderr, "host %s is alive\n", args[1]);
113  return 0;
114 }
status icmp_release(int32)
Definition: icmp.c:312
int32 strncmp(const char *, const char *, int32)
#define ICMP_ECHOREQST
Definition: icmp.h:17
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define stderr
Definition: stdio.h:17
int strlen(char *str)
NULL終端された文字列の長さを返す。NULL終端は長さに含まない。
Definition: strlen.c:11
byte bool8
Boolean値
Definition: kernel.h:36
int32 printf(const char *,...)
Definition: printf.c:13
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
status icmp_send(uint32, uint16, uint16, uint16, char *, int32)
Definition: icmp.c:225
int32 icmp_recv(int32, char *, int32, uint32)
Definition: icmp.c:150
int32 icmp_register(uint32)
Definition: icmp.c:103
#define FALSE
Boolean False(0)
Definition: kernel.h:63
#define TRUE
Boolean True(1)
Definition: kernel.h:65
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
uint32 dnslookup(char *)
Definition: dns.c:15
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
uint32 dot2ip(char *, uint32 *)
Definition: dot2ip.c:9
Here is the call graph for this function: