1 /**************************************************************************************************
2 $Id: ip.c,v 1.5 2005/03/22 17:44:56 bboy Exp $
3
4 Copyright (C) 2002-2005 Don Moore <bboy@bboy.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at Your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 **************************************************************************************************/
20
21 #include "mydnsutil.h"
22
23
24
25 /**************************************************************************************************
26 _SOCKCLOSE
27 Close/shutdown a socket.
28 **************************************************************************************************/
29 void
_sockclose(int fd,const char * file,int line)30 _sockclose(int fd, const char *file, int line) {
31 if (fd >= 0) {
32 #if DEBUG_ENABLED
33 DebugX("enabled", 1, _("sockclose(%d) as requested by %s:%d"), fd, file, line);
34 #endif
35 #if HAVE_SHUTDOWN
36 shutdown(fd, 2);
37 #endif
38 close(fd);
39 }
40 }
41 /*--- _sockclose() ------------------------------------------------------------------------------*/
42
43
44 /**************************************************************************************************
45 IPADDR
46 Returns a textual representation of an IP address.
47 'family' should be AF_INET or (if supported) AF_INET6.
48 'addr' should be a pointer to a 'struct in_addr' or a 'struct in6_addr'.
49 **************************************************************************************************/
50 const char *
ipaddr(int family,void * addr)51 ipaddr(int family, void *addr) {
52 static char *addrbuf = NULL;
53 int addrbufsize = INET_ADDRSTRLEN;
54 #if HAVE_IPV6
55 addrbufsize = MAX(addrbufsize, INET6_ADDRSTRLEN);
56 #endif
57
58 addrbuf = REALLOCATE(addrbuf, addrbufsize, char[]);
59 memset(addrbuf, 0, addrbufsize);
60
61 if (family == AF_INET) {
62 return(inet_ntop(AF_INET, addr, addrbuf, addrbufsize));
63 #if HAVE_IPV6
64 } else if (family == AF_INET6) {
65 return(inet_ntop(AF_INET6, addr, addrbuf, addrbufsize));
66 #endif
67 } else {
68 Err(_("Unknown address family"));
69 }
70 /* NOTREACHED */
71 return NULL;
72 }
73 /*--- ipaddr() ----------------------------------------------------------------------------------*/
74
75
76 #if HAVE_IPV6
77 /**************************************************************************************************
78 IS_IPV6
79 Returns 1 if string 's' has two or more ':' characters.
80 **************************************************************************************************/
81 int
is_ipv6(char * addr)82 is_ipv6(char *addr) {
83 register char *c; /* Current position in 's' */
84 register int colons = 0; /* Number of colons (':') found */
85
86 for (c = addr; *c && (colons < 2); c++)
87 if (*c == ':')
88 colons++;
89 return (colons == 2);
90 }
91 /*--- is_ipv6() ---------------------------------------------------------------------------------*/
92 #endif
93
94 /* vi:set ts=3: */
95