[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lwip-users] inet_ntoa implementation
From: |
Marc Boucher |
Subject: |
Re: [lwip-users] inet_ntoa implementation |
Date: |
Fri, 11 Apr 2003 17:59:06 -0400 |
User-agent: |
Mutt/1.5.1i |
On Fri, Apr 11, 2003 at 10:50:58AM +0200, Sergio PérezAlcañiz wrote:
> Hello, I'm using the porting of ares in my own lwip porting (RTLinux porting)
> and I find very interesting to have a inet_ntoa implementation. Next lines
> are a
> simple implementation for that.
>
> Include in the file inet.h this line:
> char *inet_ntoa(struct in_addr in);
>
>
> and at the end of inet.c:
>
>
> static unsigned int i2a(char* dest,unsigned int x) {
> register unsigned int tmp=x;
> register unsigned int len=0;
> if (x>=100) { *dest++=tmp/100+'0'; tmp=tmp%100; ++len; }
> if (x>=10) { *dest++=tmp/10+'0'; tmp=tmp%10; ++len; }
> *dest++=tmp+'0';
> return len+1;
> }
>
> char *inet_ntoa(struct in_addr in) {
> static char buf[20];
> unsigned int len;
> unsigned char *ip=(unsigned char*)∈
> len=i2a(buf,ip[0]); buf[len]='.'; ++len;
> len+=i2a(buf+ len,ip[1]); buf[len]='.'; ++len;
> len+=i2a(buf+ len,ip[2]); buf[len]='.'; ++len;
> len+=i2a(buf+ len,ip[3]); buf[len]=0;
> return buf;
> }
>
>
> I think that this is useful for everybody who uses the socket api.
>
>
> Regards.
> Sergio.
>
In the same category, here's inet_aton() and inet_addr();
they are used by the ares DNS resolver under lwIP
and a few other things here..
Marc
diff -ur lwip-cvs-03021900/src/include/ipv6/lwip/inet.h
lwip-cvs-03021900-mb/src/include/ipv6/lwip/inet.h
--- lwip-cvs-03021900/src/include/ipv6/lwip/inet.h 2003-01-08
05:09:42.000000000 -0500
+++ lwip-cvs-03021900-mb/src/include/ipv6/lwip/inet.h 2003-02-27
17:31:42.000000000 -0500
@@ -44,6 +44,8 @@
struct ip_addr *src, struct ip_addr *dest,
u8_t proto, u32_t proto_len);
+u32_t inet_addr(const char *cp);
+int inet_aton(const char *cp, struct in_addr *addr);
#ifndef _MACHINE_ENDIAN_H_
#ifndef _NETINET_IN_H
diff -ur lwip-cvs-03021900/src/core/inet.c lwip-cvs-03021900-mb/src/core/inet.c
--- lwip-cvs-03021900/src/core/inet.c 2003-02-07 04:16:02.000000000 -0500
+++ lwip-cvs-03021900-mb/src/core/inet.c 2003-02-27 17:31:06.000000000
-0500
@@ -170,3 +170,122 @@
/*-----------------------------------------------------------------------------------*/
+
+/*
+ * Ascii internet address interpretation routine.
+ * The value returned is in network order.
+ */
+
+/* */
+/* inet_addr */
+u32_t inet_addr(const char *cp)
+{
+ struct in_addr val;
+
+ if (inet_aton(cp, &val)) {
+ return (val.s_addr);
+ }
+ return (INADDR_NONE);
+}
+
+/*
+ * Check whether "cp" is a valid ascii representation
+ * of an Internet address and convert to a binary address.
+ * Returns 1 if the address is valid, 0 if not.
+ * This replaces inet_addr, the return value from which
+ * cannot distinguish between failure and a local broadcast address.
+ */
+/* */
+/* inet_aton */
+int inet_aton(const char *cp, struct in_addr *addr)
+{
+ u32_t val;
+ int base, n;
+ char c;
+// u_int parts[4];
+// u_int *pp = parts;
+ u32_t parts[4];
+ u32_t* pp = parts;
+
+ c = *cp;
+ for (;;) {
+ /*
+ * Collect number up to ``.''.
+ * Values are specified as for C:
+ * 0x=hex, 0=octal, isdigit=decimal.
+ */
+ if (!isdigit(c))
+ return (0);
+ val = 0; base = 10;
+ if (c == '0') {
+ c = *++cp;
+ if (c == 'x' || c == 'X')
+ base = 16, c = *++cp;
+ else
+ base = 8;
+ }
+ for (;;) {
+ if (isascii(c) && isdigit(c)) {
+ val = (val * base) + (c - '0');
+ c = *++cp;
+ } else if (base == 16 && isascii(c) && isxdigit(c)) {
+ val = (val << 4) |
+ (c + 10 - (islower(c) ? 'a' : 'A'));
+ c = *++cp;
+ } else
+ break;
+ }
+ if (c == '.') {
+ /*
+ * Internet format:
+ * a.b.c.d
+ * a.b.c (with c treated as 16 bits)
+ * a.b (with b treated as 24 bits)
+ */
+ if (pp >= parts + 3)
+ return (0);
+ *pp++ = val;
+ c = *++cp;
+ } else
+ break;
+ }
+ /*
+ * Check for trailing characters.
+ */
+ if (c != '\0' && (!isascii(c) || !isspace(c)))
+ return (0);
+ /*
+ * Concoct the address according to
+ * the number of parts specified.
+ */
+ n = pp - parts + 1;
+ switch (n) {
+
+ case 0:
+ return (0); /* initial nondigit */
+
+ case 1: /* a -- 32 bits */
+ break;
+
+ case 2: /* a.b -- 8.24 bits */
+ if (val > 0xffffff)
+ return (0);
+ val |= parts[0] << 24;
+ break;
+
+ case 3: /* a.b.c -- 8.8.16 bits */
+ if (val > 0xffff)
+ return (0);
+ val |= (parts[0] << 24) | (parts[1] << 16);
+ break;
+
+ case 4: /* a.b.c.d -- 8.8.8.8 bits */
+ if (val > 0xff)
+ return (0);
+ val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+ break;
+ }
+ if (addr)
+ addr->s_addr = htonl(val);
+ return (1);
+}
diff -ur lwip-cvs-03021900/src/include/ipv4/lwip/inet.h
lwip-cvs-03021900-mb/src/include/ipv4/lwip/inet.h
--- lwip-cvs-03021900/src/include/ipv4/lwip/inet.h 2003-01-08
05:09:42.000000000 -0500
+++ lwip-cvs-03021900-mb/src/include/ipv4/lwip/inet.h 2003-02-27
17:31:31.000000000 -0500
@@ -44,6 +44,9 @@
struct ip_addr *src, struct ip_addr *dest,
u8_t proto, u16_t proto_len);
+u32_t inet_addr(const char *cp);
+int inet_aton(const char *cp, struct in_addr *addr);
+
#ifdef HTONS
#undef HTONS
#endif /* HTONS */
diff -ur lwip-cvs-03021900/src/include/ipv4/lwip/ip_addr.h
lwip-cvs-03021900-mb/src/include/ipv4/lwip/ip_addr.h
--- lwip-cvs-03021900/src/include/ipv4/lwip/ip_addr.h 2003-01-08
05:09:42.000000000 -0500
+++ lwip-cvs-03021900-mb/src/include/ipv4/lwip/ip_addr.h 2003-02-12
11:44:30.000000000 -0500
@@ -38,6 +38,42 @@
#define IP_ADDR_BROADCAST (&ip_addr_broadcast)
+#define INADDR_NONE ((u32_t) 0xffffffff) /* 255.255.255.255 */
+#define INADDR_LOOPBACK ((u32_t) 0x7f000001) /* 127.0.0.1 */
+
+/* Definitions of the bits in an Internet address integer.
+
+ On subnets, host and network parts are found according to
+ the subnet mask, not these masks. */
+
+#define IN_CLASSA(a) ((((u32_t)(a)) & 0x80000000) == 0)
+#define IN_CLASSA_NET 0xff000000
+#define IN_CLASSA_NSHIFT 24
+#define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
+#define IN_CLASSA_MAX 128
+
+#define IN_CLASSB(a) ((((u32_t)(a)) & 0xc0000000) ==
0x80000000)
+#define IN_CLASSB_NET 0xffff0000
+#define IN_CLASSB_NSHIFT 16
+#define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
+#define IN_CLASSB_MAX 65536
+
+#define IN_CLASSC(a) ((((u32_t)(a)) & 0xe0000000) ==
0xc0000000)
+#define IN_CLASSC_NET 0xffffff00
+#define IN_CLASSC_NSHIFT 8
+#define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET)
+
+#define IN_CLASSD(a) (((u32_t)(a) & 0xf0000000) == 0xe0000000)
+#define IN_CLASSD_NET 0xf0000000 /* These ones aren't really */
+#define IN_CLASSD_NSHIFT 28 /* net and host fields, but */
+#define IN_CLASSD_HOST 0x0fffffff /* routing needn't know. */
+#define IN_MULTICAST(a) IN_CLASSD(a)
+
+#define IN_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000) == 0xf0000000)
+#define IN_BADCLASS(a) (((u32_t)(a) & 0xf0000000) == 0xf0000000)
+
+#define IN_LOOPBACKNET 127 /* official! */
+
#ifdef PACK_STRUCT_USE_INCLUDES
# include "arch/bpstruct.h"
#endif
@@ -50,6 +86,11 @@
# include "arch/epstruct.h"
#endif
+/* For compatibility with BSD code */
+struct in_addr {
+ u32_t s_addr;
+};
+
extern struct ip_addr ip_addr_broadcast;
#define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = htonl(((u32_t)(a & 0xff) <<
24) | ((u32_t)(b & 0xff) << 16) | \
diff -ur lwip-cvs-03021900/src/include/lwip/sockets.h
lwip-cvs-03021900-mb/src/include/lwip/sockets.h
--- lwip-cvs-03021900/src/include/lwip/sockets.h 2003-02-19
03:28:32.000000000 -0500
+++ lwip-cvs-03021900-mb/src/include/lwip/sockets.h 2003-02-26
10:25:47.000000000 -0500
@@ -34,11 +34,6 @@
#ifndef __LWIP_SOCKETS_H__
#define __LWIP_SOCKETS_H__
-struct in_addr {
- u32_t s_addr;
-};
-
-
struct sockaddr_in {
u8_t sin_len;
u8_t sin_family;