UbixOS  2.0
net.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <net/inet.h>
31 #include <net/sockets.h>
32 #include <string.h>
33 
34 #include "lib/kprintf.h"
35 
36 #ifndef _IN_ADDR_T_DECLARED
37 typedef uInt32 in_addr_t;
38 #define _IN_ADDR_T_DECLARED
39 #endif
40 
41 /*
42  uInt32 htonl(uInt32 n) {
43  uInt32 retVal = 0x0;
44  retVal += ((n & 0xff) << 24);
45  retVal += ((n & 0xff00) << 8);
46  retVal += ((n & 0xff0000) >> 8);
47  retVal += ((n & 0xff000000) >> 24);
48  return(retVal);
49  }
50  */
51 
52 /*
53  uInt32 htons(uInt32 n) {
54  uInt32 retVal = 0x0;
55  retVal = (((n & 0xff) << 8) | ((n & 0xff00) >> 8));
56  return(retVal);
57  }
58  */
59 
60 
61 #ifdef _INET_ATON
62 int inet_aton(cp, addr)
63 const char *cp;
64 struct in_addr *addr;
65 {
66  uInt32 parts[4];
67  in_addr_t val;
68  char *c;
69  char *endptr;
70  int gotend, n;
71 
72  c = (char *)cp;
73  n = 0;
74  /*
75  * Run through the string, grabbing numbers until
76  * the end of the string, or some error
77  */
78  gotend = 0;
79  while (!gotend) {
80  //errno = 0;
81  val = strtol(c, &endptr, 0);
82  kprintf("VAL: [%x]",val);
83 
84  //if (errno == ERANGE) /* Fail completely if it overflowed. */
85  // return (0);
86 
87  /*
88  * If the whole string is invalid, endptr will equal
89  * c.. this way we can make sure someone hasn't
90  * gone '.12' or something which would get past
91  * the next check.
92  */
93  if (endptr == c)
94  return (0);
95  parts[n] = val;
96  c = endptr;
97 
98  /* Check the next character past the previous number's end */
99  switch (*c) {
100  case '.' :
101  /* Make sure we only do 3 dots .. */
102  if (n == 3) /* Whoops. Quit. */
103  return (0);
104  n++;
105  c++;
106  break;
107 
108  case '\0':
109  gotend = 1;
110  break;
111 
112  default:
113  /*
114  if (isspace((unsigned char)*c)) {
115  gotend = 1;
116  break;
117  } else
118  */
119  return (0); /* Invalid character, so fail */
120  }
121 
122  }
123 
124  /*
125  * Concoct the address according to
126  * the number of parts specified.
127  */
128 
129  switch (n) {
130  case 0: /* a -- 32 bits */
131  /*
132  * Nothing is necessary here. Overflow checking was
133  * already done in strtoul().
134  */
135  break;
136  case 1: /* a.b -- 8.24 bits */
137  if (val > 0xffffff || parts[0] > 0xff)
138  return (0);
139  val |= parts[0] << 24;
140  break;
141 
142  case 2: /* a.b.c -- 8.8.16 bits */
143  if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
144  return (0);
145  val |= (parts[0] << 24) | (parts[1] << 16);
146  break;
147 
148  case 3: /* a.b.c.d -- 8.8.8.8 bits */
149  if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
150  parts[2] > 0xff)
151  return (0);
152  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
153  break;
154  }
155 
156  if (addr != NULL)
157  addr->s_addr = htonl(val);
158  return (1);
159 }
160 #endif
161 
162 /***
163  END
164  ***/
165 
inet.h
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
strtol
long strtol(const char *__restrict nptr, char **__restrict endptr, int base)
Definition: strtol.c:37
string.h
types.h
in_addr_t
u32_t in_addr_t
Definition: inet.h:55
htonl
#define htonl(x)
Definition: def.h:110
in_addr
Definition: inet.h:58
in_addr::s_addr
in_addr_t s_addr
Definition: inet.h:59
kprintf.h
in_addr_t
uInt32 in_addr_t
Definition: net.c:37
sockets.h
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
NULL
#define NULL
Definition: fat_string.h:17