* Christmas edition *; fixed irc /os command; small cleanup in fd.c; improvements...
[ZeXOS.git] / libc / socket / inet_ntop.c
blob7ad00b02ffb29dbeeaf69ffbcf41a6c36c5ca796
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 <string.h>
22 #include <sys/socket.h>
25 const char *inet_ntop (int af, const void *src, char *dst, size_t cnt)
27 if (!src || !dst || !cnt)
28 return (char *) 0;
30 if (af == AF_INET && cnt == 4) {
31 unsigned *ip = (unsigned *) src;
33 unsigned char a = (unsigned char) *ip;
34 unsigned char b = (unsigned char) (*ip >> 8);
35 unsigned char c = (unsigned char) (*ip >> 16);
36 unsigned char d = (unsigned char) (*ip >> 24);
38 sprintf (dst, "%d.%d.%d.%d", a, b, c, d);
40 return dst;
43 if (af == AF_INET6 && cnt == 16) {
44 unsigned short ip[8];
45 memcpy (ip, src, cnt);
47 sprintf (dst, "%x:%x:%x:%x:%x:%x:%x:%x", htons (ip[0]), htons (ip[1]), htons (ip[2]), htons (ip[3]),
48 htons (ip[4]), htons (ip[5]), htons (ip[6]), htons (ip[7]));
50 return dst;
53 return (char *) 0;