XINU
dot2ip.c
Go to the documentation of this file.
1 /* dot2ip.c - dot2ip */
2 
3 #include <xinu.h>
4 
5 /*------------------------------------------------------------------------
6  * dot2ip - Convert a string of dotted decimal to an unsigned integer
7  *------------------------------------------------------------------------
8  */
10  char *dotted, /* IP address in dotted decimal */
11  uint32 *result /* Location to which binary IP */
12  /* address will be stored */
13  /* (host byte order) */
14  )
15 {
16  int32 seg; /* Counts segments */
17  int32 nch; /* Counts chars within segment */
18  char ch; /* Next character */
19  uint32 ipaddr; /* IP address in binary */
20  int32 val; /* Binary value of one segment */
21 
22  /* Input must have the form X.X.X.X, where X is 1 to 3 digits */
23 
24  ipaddr = 0;
25  for (seg=0 ; seg<4 ; seg++) { /* For each segment */
26  val = 0;
27  for (nch=0 ; nch<4; nch++) { /* Up to four chars per segment*/
28  ch = *dotted++;
29  if ( (ch==NULLCH) || (ch == '.') ) {
30  if (nch == 0) {
31  return SYSERR;
32  } else {
33  break;
34  }
35  }
36 
37  /* Non-digit is an error */
38 
39  if ( (ch<'0') || (ch>'9') ) {
40  return SYSERR;
41  }
42 
43  val = 10*val + (ch-'0');
44  }
45 
46  if (val > 255) { /* Out of valid range */
47  return SYSERR;
48  }
49  ipaddr = (ipaddr << 8) | val;
50 
51  if (ch == NULLCH) {
52  break;
53  }
54  }
55  if ( (seg != 3) || (ch != NULLCH) ) {
56  return SYSERR;
57  }
58  *result = ipaddr;
59  return OK;
60 }
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define OK
処理が成功した場合
Definition: kernel.h:77
uint32 dot2ip(char *dotted, uint32 *result)
Definition: dot2ip.c:9
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70