update DI disc code
[libogc.git] / libogc / network_common.c
blob7442a35ec30f54844b9ed00c86154bfb1eb8295d
1 #include "lwip/def.h"
2 #include "lwip/inet.h"
4 /* Here for now until needed in other places in lwIP */
5 #ifndef isascii
6 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
7 #define isascii(c) in_range(c, 0x20, 0x7f)
8 #define isdigit(c) in_range(c, '0', '9')
9 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
10 #define islower(c) in_range(c, 'a', 'z')
11 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
12 #endif
16 * Ascii internet address interpretation routine.
17 * The value returned is in network order.
20 /* */
21 /* inet_addr */
22 u32_t inet_addr(const char *cp)
24 struct in_addr val;
26 if (inet_aton(cp, &val)) {
27 return (val.s_addr);
29 return (INADDR_NONE);
33 * Check whether "cp" is a valid ascii representation
34 * of an Internet address and convert to a binary address.
35 * Returns 1 if the address is valid, 0 if not.
36 * This replaces inet_addr, the return value from which
37 * cannot distinguish between failure and a local broadcast address.
39 /* */
40 /* inet_aton */
41 s8_t
42 inet_aton(const char *cp, struct in_addr *addr)
44 u32_t val;
45 s32_t base, n;
46 char c;
47 u32_t parts[4];
48 u32_t* pp = parts;
50 c = *cp;
51 for (;;) {
53 * Collect number up to ``.''.
54 * Values are specified as for C:
55 * 0x=hex, 0=octal, isdigit=decimal.
57 if (!isdigit(c))
58 return (0);
59 val = 0; base = 10;
60 if (c == '0') {
61 c = *++cp;
62 if (c == 'x' || c == 'X')
63 base = 16, c = *++cp;
64 else
65 base = 8;
67 for (;;) {
68 if (isdigit(c)) {
69 val = (val * base) + (s16_t)(c - '0');
70 c = *++cp;
71 } else if (base == 16 && isxdigit(c)) {
72 val = (val << 4) |
73 (s16_t)(c + 10 - (islower(c) ? 'a' : 'A'));
74 c = *++cp;
75 } else
76 break;
78 if (c == '.') {
80 * Internet format:
81 * a.b.c.d
82 * a.b.c (with c treated as 16 bits)
83 * a.b (with b treated as 24 bits)
85 if (pp >= parts + 3)
86 return (0);
87 *pp++ = val;
88 c = *++cp;
89 } else
90 break;
93 * Check for trailing characters.
95 if (c != '\0' && (!isascii(c) || !isspace(c)))
96 return (0);
98 * Concoct the address according to
99 * the number of parts specified.
101 n = pp - parts + 1;
102 switch (n) {
104 case 0:
105 return (0); /* initial nondigit */
107 case 1: /* a -- 32 bits */
108 break;
110 case 2: /* a.b -- 8.24 bits */
111 if (val > 0xffffff)
112 return (0);
113 val |= parts[0] << 24;
114 break;
116 case 3: /* a.b.c -- 8.8.16 bits */
117 if (val > 0xffff)
118 return (0);
119 val |= (parts[0] << 24) | (parts[1] << 16);
120 break;
122 case 4: /* a.b.c.d -- 8.8.8.8 bits */
123 if (val > 0xff)
124 return (0);
125 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
126 break;
128 if (addr)
129 addr->s_addr = htonl(val);
130 return (1);
133 /* Convert numeric IP address into decimal dotted ASCII representation.
134 * returns ptr to static buffer; not reentrant!
136 char *inet_ntoa(struct in_addr addr)
138 static char str[16];
139 u32_t s_addr = addr.s_addr;
140 char inv[3];
141 char *rp;
142 u8_t *ap;
143 u8_t rem;
144 u8_t n;
145 u8_t i;
147 rp = str;
148 ap = (u8_t *)&s_addr;
149 for(n = 0; n < 4; n++) {
150 i = 0;
151 do {
152 rem = *ap % (u8_t)10;
153 *ap /= (u8_t)10;
154 inv[i++] = '0' + rem;
155 } while(*ap);
156 while(i--)
157 *rp++ = inv[i];
158 *rp++ = '.';
159 ap++;
161 *--rp = 0;
162 return str;
166 #ifndef BYTE_ORDER
167 #error BYTE_ORDER is not defined
168 #endif
169 #if BYTE_ORDER == LITTLE_ENDIAN
171 u16_t
172 htons(u16_t n)
174 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
177 u16_t
178 ntohs(u16_t n)
180 return htons(n);
183 u32_t
184 htonl(u32_t n)
186 return ((n & 0xff) << 24) |
187 ((n & 0xff00) << 8) |
188 ((n & 0xff0000) >> 8) |
189 ((n & 0xff000000) >> 24);
192 u32_t
193 ntohl(u32_t n)
195 return htonl(n);
198 #endif /* BYTE_ORDER == LITTLE_ENDIAN */