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

Go to the source code of this file.

Functions

int atoi (char *)
 ASCII文字列をint型に変換する。 More...
 
shellcmd xsh_udpecho (int nargs, char *args[])
 

Function Documentation

◆ atoi()

int atoi ( char *  p)

ASCII文字列をint型に変換する。

処理の流れは、
Step1. 「不要な文字(空白、TAB)の削除」もしくは「正数/負数の判定」を行う
Step2. 0〜9の範囲の値であれば、一桁ずつ記録する。それ以外の文字は呼び飛ばす
ASCIIコードとして、0=0x30、1=0x31、2=0x32、…、9=0x39であるため、
「0x30〜0x39の範囲のASCIIコード値(0〜9の値) - '0'(=0x30)」と計算すれば、
0〜9の整数値が得られる。
Step3. Step.1で取得した符号情報(±)を反映した整数を返す。

Parameters
[in]pASCII文字列
Returns
ASCII文字列をint型に変換した整数値
Note
ASCIIコード情報は、ターミナル上で"$ man ascii"で確認できる。

Definition at line 19 of file atoi.c.

Referenced by xsh_udpecho().

20 {
21  int n = 0, f = 0;
22 
23  for (;; p++)
24  {
25  switch (*p)
26  {
27  case ' ':
28  case '\t':
29  continue;
30  case '-':
31  f++;
32  case '+':
33  p++;
34  }
35  break;
36  }
37 
38  while (*p >= '0' && *p <= '9')
39  {
40  n = n * 10 + *p++ - '0';
41  }
42 
43  return (f ? -n : n);
44 }
Here is the caller graph for this function:

◆ xsh_udpecho()

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

Definition at line 14 of file xsh_udpecho.c.

References atoi(), delay(), dot2ip(), fprintf(), printf(), stderr, strncmp(), strnlen(), SYSERR, TIMEOUT, udp_recv(), udp_register(), udp_release(), and udp_send().

15 {
16  int i; /* index into buffer */
17  int retval; /* return value */
18  char msg[] = "Xinu testing UDP echo"; /* message to send */
19  char inbuf[1500]; /* buffer for incoming reply */
20  int32 slot; /* UDP slot to use */
21  int32 msglen; /* length of outgoing message */
22  uint32 remoteip; /* remote IP address to use */
23  uint16 remport= 7; /* remote port number to use */
24  uint16 locport = 52743; /* local port to use */
25  int32 retries = 3; /* number of retries */
26  int32 delay = 500; /* reception delay in ms */
27 
28  /* For argument '--help', emit help about the 'udpecho' command */
29 
30  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
31  printf("Use: %s REMOTEIP\n\n", args[0]);
32  printf("Description:\n");
33  printf("\tBounce a message off a remote UDP echo server\n");
34  printf("Options:\n");
35  printf("\tREMOTEIP:\tIP address in dotted decimal\n");
36  printf("\t--help\t display this help and exit\n");
37  return 0;
38  }
39 
40  /* Check for valid IP address argument */
41 
42  if (nargs != 2) {
43  fprintf(stderr, "%s: invalid number of argument(s)\n", args[0]);
44  fprintf(stderr, "Try '%s --help' for more information\n",
45  args[0]);
46  return 1;
47  }
48 
49  if (dot2ip(args[1], &remoteip) == SYSERR) {
50  fprintf(stderr, "%s: invalid IP address argument\r\n",
51  args[0]);
52  return 1;
53  }
54  if (nargs == 3) {
55  retval = atoi(args[2]);
56  if ( (retval <= 0) || (retval > 64535) ) {
57  fprintf(stderr, "%s: invalid port argument\r\n",
58  args[0]);
59  return 1;
60  }
61  remport = (uint16) retval;
62  }
63 
64  fprintf(stderr, "using remote port %d\n", remport);
65 
66  /* register local UDP port */
67 
68  slot = udp_register(remoteip, remport, locport);
69  if (slot == SYSERR) {
70  fprintf(stderr, "%s: could not reserve UDP port %d\n",
71  args[0], locport);
72  return 1;
73  }
74 
75  /* Retry sending outgoing datagram and getting response */
76 
77  msglen = strnlen(msg, 1200);
78  for (i=0; i<retries; i++) {
79  retval = udp_send(slot, msg, msglen);
80  if (retval == SYSERR) {
81  fprintf(stderr, "%s: error sending UDP \n",
82  args[0]);
83  return 1;
84  }
85 
86  retval = udp_recv(slot, inbuf, sizeof(inbuf), delay);
87  if (retval == TIMEOUT) {
88  fprintf(stderr, "%s: timeout...\n", args[0]);
89  continue;
90  } else if (retval == SYSERR) {
91  fprintf(stderr, "%s: error from udp_recv \n",
92  args[0]);
93  udp_release(slot);
94  return 1;
95  }
96  break;
97  }
98 
99  udp_release(slot);
100  if (retval == TIMEOUT) {
101  fprintf(stderr, "%s: retry limit exceeded\n",
102  args[0]);
103  return 1;
104  }
105 
106  /* Response received - check contents */
107 
108  if (retval != msglen) {
109  fprintf(stderr, "%s: sent %d bytes and received %d\n",
110  args[0], msglen, retval);
111  return 1;
112  }
113  for (i = 0; i < msglen; i++) {
114  if (msg[i] != inbuf[i]) {
115  fprintf(stderr, "%s: reply differs at byte %d\n",
116  args[0], i);
117  return 1;
118  }
119  }
120 
121  printf("UDP echo test was successful\n");
122  return 0;
123 }
int32 udp_recv(uid32, char *, int32, uint32)
Definition: udp.c:146
status udp_send(uid32, char *, int32)
Definition: udp.c:316
int atoi(char *)
ASCII文字列をint型に変換する。
Definition: atoi.c:19
int32 strncmp(const char *, const char *, int32)
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define stderr
Definition: stdio.h:17
int32 printf(const char *,...)
Definition: printf.c:13
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
int32 delay(int n)
マイクロ秒単位で処理を遅らせる。
Definition: initialize.c:262
uid32 udp_register(uint32, uint16, uint16)
Definition: udp.c:85
unsigned short uint16
符号なし16ビット整数(unsigned short)
Definition: kernel.h:17
status udp_release(uid32)
Definition: udp.c:502
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
int32 strnlen(const char *, uint32)
NULL終端された文字列の長さを返す。NULL終端は長さに含まない。
Definition: strnlen.c:14
Here is the call graph for this function: