XINU
xsh_netinfo.c
Go to the documentation of this file.
1 /* xsh_netinfo.c - xsh_netinfo */
2 
3 #include <xinu.h>
4 #include <string.h>
5 #include <stdio.h>
6 
7 /*------------------------------------------------------------------------
8  * xsh_netinfo - obtain and print the IP address, subnet mask, default
9  * router address and other network information
10  *------------------------------------------------------------------------
11  */
12 shellcmd xsh_netinfo(int nargs, char *args[]) {
13 
14  uint32 ipaddr; /* An IP address in binary */
15  uint32 ipbcast; /* IP broadcast addr. in binary */
16  uint32 ipprefix; /* IP network prefix in binary */
17  uint32 router; /* Router address in binary */
18  uint32 tserver; /* NTP server address in binary */
19  uint32 dserver; /* DNS server address in binary */
20  char str[40]; /* Temporary used for formatting*/
21  uint32 ipmask; /* Subnet mask in binary */
22 
23  /* Output info for '--help' argument */
24 
25  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
26  printf("Usage: %s\n\n", args[0]);
27  printf("Description:\n");
28  printf("\tDisplays IP address information\n");
29  printf("Options:\n");
30  printf("\t-f\tforce a new DHCP request\n");
31  printf("\t--help\tdisplay this help and exit\n");
32  return OK;
33  }
34 
35  /* Check argument count */
36 
37  if (nargs > 2) {
38  fprintf(stderr, "%s: too many arguments\n", args[0]);
39  fprintf(stderr, "Try '%s --help' for more information\n",
40  args[0]);
41  return SYSERR;
42  }
43 
44  if (nargs == 2) {
45  if (strncmp(args[1], "-f", 3) != 0) {
46  fprintf(stderr, "%s: invalid argument\n", args[0]);
48  "Try '%s --help' for more information\n",
49  args[0]);
50  return 1;
51  }
53  getlocalip();
54  }
55 
56 
57  /* IP unicast address in dotted decimal and hex */
58 
59  ipaddr = NetData.ipucast;
60  sprintf(str, "%d.%d.%d.%d",
61  (ipaddr>>24)&0xff, (ipaddr>>16)&0xff,
62  (ipaddr>>8)&0xff, ipaddr&0xff);
63  printf(" %-16s %-16s 0x%08x\n",
64  "IP address:", str, ipaddr);
65 
66  /* IP network broadcast address in dotted decimal & hex */
67 
68  ipbcast = NetData.ipbcast;
69  sprintf(str, "%d.%d.%d.%d",
70  (ipbcast>>24)&0xff, (ipbcast>>16)&0xff,
71  (ipbcast>>8)&0xff, ipbcast&0xff);
72  printf(" %-16s %-16s 0x%08x\n",
73  "IP broadcast:", str, ipbcast);
74 
75  /* IP network prefix in dotted decimal & hex */
76 
77  ipprefix = NetData.ipprefix;
78  sprintf(str, "%d.%d.%d.%d",
79  (ipprefix>>24)&0xff, (ipprefix>>16)&0xff,
80  (ipprefix>>8)&0xff, ipprefix&0xff);
81  printf(" %-16s %-16s 0x%08x\n",
82  "IP prefix:", str, ipprefix);
83 
84  /* IP network mask in dotted decimal & hex */
85 
86  ipmask = NetData.ipmask;
87  ipaddr = NetData.ipucast;
88  sprintf(str, "%d.%d.%d.%d",
89  (ipmask>>24)&0xff, (ipmask>>16)&0xff,
90  (ipmask>>8)&0xff, ipmask&0xff);
91  printf(" %-16s %-16s 0x%08x\n",
92  "Address mask:", str, ipmask);
93 
94  /* Default router in dotted decimal & hex */
95 
96  router = NetData.iprouter;
97  sprintf(str, "%d.%d.%d.%d",
98  (router>>24)&0xff, (router>>16)&0xff,
99  (router>>8)&0xff, router&0xff);
100  printf(" %-16s %-16s 0x%08x\n",
101  "IP router:", str, router);
102 
103  /* NTP time server in dotted decimal & hex */
104 
105  tserver = NetData.ntpserver;
106  if (tserver != 0) {
107  sprintf(str, "%d.%d.%d.%d",
108  (tserver>>24)&0xff, (tserver>>16)&0xff,
109  (tserver>>8)&0xff, tserver&0xff);
110  printf(" %-16s %-16s 0x%08x\n",
111  "NTP time server:", str, tserver);
112  }
113 
114  /* DNS server in dotted decimal & hex */
115 
116  dserver = NetData.dnsserver;
117  if (dserver != 0) {
118  sprintf(str, "%d.%d.%d.%d",
119  (dserver>>24)&0xff, (dserver>>16)&0xff,
120  (dserver>>8)&0xff, dserver&0xff);
121  printf(" %-16s %-16s 0x%08x\n",
122  "DNS server:", str, dserver);
123  }
124 
125  printf(" %-16s %02x:%02x:%02x:%02x:%02x:%02x\n",
126  "MAC unicast:",
127  0xff & NetData.ethucast[0],
128  0xff & NetData.ethucast[1],
129  0xff & NetData.ethucast[2],
130  0xff & NetData.ethucast[3],
131  0xff & NetData.ethucast[4],
132  0xff & NetData.ethucast[5]);
133 
134  printf(" %-16s %02x:%02x:%02x:%02x:%02x:%02x\n",
135  "MAC broadcast:",
136  0xff & NetData.ethbcast[0],
137  0xff & NetData.ethbcast[1],
138  0xff & NetData.ethbcast[2],
139  0xff & NetData.ethbcast[3],
140  0xff & NetData.ethbcast[4],
141  0xff & NetData.ethbcast[5]);
142 
143  return OK;
144 }
uint32 ipucast
Definition: net.h:55
int32 strncmp(const char *, const char *, int32)
uint32 ipmask
Definition: net.h:57
struct network NetData
Definition: net.c:6
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define stderr
Definition: stdio.h:17
#define OK
処理が成功した場合
Definition: kernel.h:77
int32 sprintf(char *, char *,...)
Definition: sprintf.c:12
int32 printf(const char *,...)
Definition: printf.c:13
byte ethucast[ETH_ADDR_LEN]
Definition: net.h:64
#define FALSE
Boolean False(0)
Definition: kernel.h:63
uint32 iprouter
Definition: net.h:59
shellcmd xsh_netinfo(int nargs, char *args[])
Definition: xsh_netinfo.c:12
uint32 ipprefix
Definition: net.h:58
uint32 ipbcast
Definition: net.h:56
byte ethbcast[ETH_ADDR_LEN]
Definition: net.h:65
uint32 getlocalip(void)
Definition: dhcp.c:142
int32 shellcmd
シェルコール関数 返り値の型
Definition: kernel.h:51
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
uint32 ntpserver
Definition: net.h:62
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
uint32 dnsserver
Definition: net.h:61
bool8 ipvalid
Definition: net.h:63