Fixed ZDE build - missing header file
[ZeXOS.git] / libc / socket / inet_pton.c
blob2a4d761660c139f43df79aa10078deb7a57539ac
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 #define NET_IPV4_TO_ADDR(a, b, c, d) \
28 (((unsigned)(d) << 24) | (((unsigned)(c) & 0xff) << 16) | (((unsigned)(b) & 0xff) << 8) | ((unsigned)(a) & 0xff))
30 void NET_IPV6_TO_ADDR (struct in6_addr *ip, unsigned short a, unsigned short b, unsigned short c, unsigned short d,
31 unsigned short e, unsigned short f, unsigned short g, unsigned short h) {
33 unsigned short buf[8];
35 buf[0] = htons (a);
36 buf[1] = htons (b);
37 buf[2] = htons (c);
38 buf[3] = htons (d);
39 buf[4] = htons (e);
40 buf[5] = htons (f);
41 buf[6] = htons (g);
42 buf[7] = htons (h);
44 memcpy (ip, (void *) buf, sizeof (struct in6_addr));
47 int inet_pton (int af, const char *src, void *dst)
49 if (!src || !dst)
50 return 0;
52 if (af == AF_INET) {
53 unsigned *ip = (unsigned *) src;
55 unsigned char a = 0;
56 unsigned char b = 0;
57 unsigned char c = 0;
58 unsigned char d = 0;
60 unsigned g = 0;
61 unsigned i = 0;
62 unsigned y = strlen (src);
64 if (!y)
65 return 0;
67 char *str = (char *) malloc (sizeof (char) * (y + 1));
69 if (!str)
70 return 0;
72 memcpy (str, ip, y);
73 str[y] = '\0';
75 unsigned h[4];
77 while (i < y) {
78 if (str[i] == '.') {
79 str[i] = '\0';
80 h[g] = i+1;
81 g ++;
84 i ++;
87 if (g != 3) {
88 free (str);
89 return -1;
92 a = atoi (str);
93 b = atoi (str+h[0]);
94 c = atoi (str+h[1]);
95 d = atoi (str+h[2]);
97 free (str);
99 g = NET_IPV4_TO_ADDR (a, b, c, d);
101 memcpy (dst, &g, sizeof (struct in_addr));
103 return 0;
104 } else if (af == AF_INET6) {
105 unsigned short a;
106 unsigned short b;
107 unsigned short c;
108 unsigned short d;
109 unsigned short e;
110 unsigned short f;
111 unsigned short g;
112 unsigned short h;
114 unsigned j = 0;
115 unsigned i = 0;
116 unsigned y = strlen (src);
118 char *ip = strndup (src, y);
120 if (!y || !ip)
121 return -1;
123 unsigned k[8];
125 while (i < y) {
126 if (ip[i] == ':') {
127 ip[i] = '\0';
128 k[j] = i+1;
129 j ++;
132 i ++;
135 if (j != 7)
136 return 0;
138 char *endptr;
140 a = strtol (ip, &endptr, 16);
141 b = strtol (ip+k[0], &endptr, 16);
142 c = strtol (ip+k[1], &endptr, 16);
143 d = strtol (ip+k[2], &endptr, 16);
144 e = strtol (ip+k[3], &endptr, 16);
145 f = strtol (ip+k[4], &endptr, 16);
146 g = strtol (ip+k[5], &endptr, 16);
147 h = strtol (ip+k[6], &endptr, 16);
149 NET_IPV6_TO_ADDR ((struct in6_addr *) dst, a, b, c, d, e, f, g, h);
151 free (ip);
153 return 0;
156 return -1;