XINU
xsh_udpserver.c
Go to the documentation of this file.
1 /* xsh_udpeserver.c - xsh_udpeserver */
2 
3 #include <xinu.h>
4 #include <stdio.h>
5 #include <string.h>
6 
7 /*------------------------------------------------------------------------
8  * xsh_udpeserver - shell command that acts as a UDP echo server (is
9  * usually run in background)
10  *------------------------------------------------------------------------
11  */
12 shellcmd xsh_udpeserver(int nargs, char *args[])
13 {
14  int32 retval; /* return value from sys calls */
15  uint32 localip; /* local IP address */
16  uint32 remip; /* remote sender's IP address */
17  uint16 remport; /* remote sender's UDP port */
18  char buff[1500]; /* buffer for incoming reply */
19  int32 msglen; /* length of outgoing message */
20  int32 slot; /* slot in UDP table */
21  uint16 echoserverport= 7; /* port number for UDP echo */
22 
23  /* For argument '--help', emit a help message */
24 
25  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
26  printf("Use: %s\n\n", args[0]);
27  printf("Description:\n");
28  printf("\tBecome a UDP echo server\n");
29  printf("Options:\n");
30  printf("\t--help\t display this help and exit\n");
31  return 0;
32  }
33 
34  /* Check for valid IP address argument */
35 
36  if (nargs != 1) {
37  fprintf(stderr, "%s: no arguments expected\n", args[0]);
38  fprintf(stderr, "Try '%s --help' for more information\n",
39  args[0]);
40  return 1;
41  }
42 
43  localip = getlocalip();
44  if (localip == SYSERR) {
46  "%s: could not obtain a local IP address\n",
47  args[0]);
48  return 1;
49  }
50 
51  /* register local UDP port */
52 
53  slot = udp_register(0, 0, echoserverport);
54  if (slot == SYSERR) {
55  fprintf(stderr, "%s: could not reserve UDP port %d\n",
56  args[0], echoserverport);
57  return 1;
58  }
59 
60  /* Do forever: read an incoming datagram and send it back */
61 
62  while (TRUE) {
63  retval = udp_recvaddr(slot, &remip, &remport, buff,
64  sizeof(buff), 600000);
65 
66  if (retval == TIMEOUT) {
67  continue;
68  } else if (retval == SYSERR) {
69  fprintf(stderr, "%s: error receiving UDP\n",
70  args[0]);
71  return 1;
72  }
73  msglen = retval;
74  retval = udp_sendto(slot, remip, remport, buff, msglen);
75  if (retval == SYSERR) {
76  fprintf(stderr, "%s: udp_sendto failed\n",
77  args[0]);
78  return 1;
79  }
80  }
81  return 0;
82 }
int32 strncmp(const char *, const char *, int32)
shellcmd xsh_udpeserver(int nargs, char *args[])
Definition: xsh_udpserver.c:12
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
int32 udp_recvaddr(uid32, uint32 *, uint16 *, char *, int32, uint32)
Definition: udp.c:227
#define stderr
Definition: stdio.h:17
int32 printf(const char *,...)
Definition: printf.c:13
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
#define TRUE
Boolean True(1)
Definition: kernel.h:65
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
uid32 udp_register(uint32, uint16, uint16)
Definition: udp.c:85
unsigned short uint16
符号なし16ビット整数(unsigned short)
Definition: kernel.h:17
uint32 getlocalip(void)
Definition: dhcp.c:142
int32 shellcmd
シェルコール関数 返り値の型
Definition: kernel.h:51
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
status udp_sendto(uid32, uint32, uint16, char *, int32)
Definition: udp.c:417
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15