]> granicus.if.org Git - postgresql/blob - src/backend/libpq/ip.c
20f5df311a832cda37d9f305eec3583094733d36
[postgresql] / src / backend / libpq / ip.c
1 /*-------------------------------------------------------------------------
2  *
3  * ip.c
4  *        Handles IPv6
5  *
6  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.2 2003/01/09 14:35:03 petere Exp $
12  *
13  * This file and the IPV6 implementation were initially provided by
14  * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
15  * http://www.lbsd.net.
16  *
17  *-------------------------------------------------------------------------
18  */
19
20 #ifndef FRONTEND
21 #include "postgres.h"
22 #else
23 #include "postgres_fe.h"
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/socket.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #ifdef HAVE_NETINET_TCP_H
34 #include <netinet/tcp.h>
35 #endif
36 #include <arpa/inet.h>
37 #include <sys/file.h>
38
39 #include "libpq/libpq.h"
40 #include "miscadmin.h"
41
42 #ifdef FRONTEND
43 #define elog fprintf
44 #define LOG  stderr
45 #endif
46
47 #if defined(HAVE_UNIX_SOCKETS) && defined(HAVE_IPV6)
48 static int getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
49                 struct addrinfo **result);
50 #endif   /* HAVE_UNIX_SOCKETS */
51
52 /*
53  *      getaddrinfo2 - get address info for Unix, IPv4 and IPv6 sockets
54  */
55 int
56 getaddrinfo2(const char *hostname, const char *servname,
57 #ifdef HAVE_IPV6
58                          const struct addrinfo *hintp, struct addrinfo **result)
59 #else
60                          int family, SockAddr *result)
61 #endif
62 {
63 #ifdef HAVE_UNIX_SOCKETS
64 #ifdef HAVE_IPV6
65         if (hintp != NULL && hintp->ai_family == AF_UNIX)
66                 return getaddrinfo_unix(servname, hintp, result);
67 #else
68         if (family == AF_UNIX)
69                 return 0;
70 #endif
71         else
72         {
73 #endif   /* HAVE_UNIX_SOCKETS */
74 #ifdef HAVE_IPV6
75                 /* NULL has special meaning to getaddrinfo */
76                 return getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
77                                                    servname, hintp, result);
78 #else
79                 if (hostname[0] == '\0')
80                         result->in.sin_addr.s_addr = htonl(INADDR_ANY);
81                 else
82                 {
83                         struct hostent *hp;
84
85                         hp = gethostbyname(hostname);
86                         if ((hp == NULL) || (hp->h_addrtype != AF_INET))
87                         {
88                                 elog(LOG, "getaddrinfo2: gethostbyname(%s) failed\n", hostname);
89                                 return STATUS_ERROR;
90                         }
91                         memmove((char *) &(result->in.sin_addr), (char *) hp->h_addr,
92                                         hp->h_length);
93                 }
94
95                 result->in.sin_port = htons((unsigned short)atoi(servname));
96                 return 0;
97 #endif  /* HAVE_IPV6 */
98
99 #ifdef HAVE_UNIX_SOCKETS
100         }
101 #endif   /* HAVE_UNIX_SOCKETS */
102 }
103
104
105 /*
106  *      freeaddrinfo2 - free IPv6 addrinfo structures
107  */
108 #ifdef HAVE_IPV6
109 void
110 freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
111 {
112 #ifdef HAVE_UNIX_SOCKETS
113         if (hint_ai_family == AF_UNIX)
114         {
115                 struct addrinfo *p;
116
117                 while (ai != NULL)
118                 {
119                         p = ai;
120                         ai = ai->ai_next;
121                         free(p->ai_addr);
122                         free(p);
123                 }
124         }
125         else
126 #endif   /* HAVE_UNIX_SOCKETS */
127                 freeaddrinfo(ai);
128 }
129 #endif
130
131
132 #if defined(HAVE_UNIX_SOCKETS) && defined(HAVE_IPV6)
133 /* -------
134  *      getaddrinfo_unix - get unix socket info using IPv6
135  *
136  *      Bug:  only one addrinfo is set even though hintsp is NULL or
137  *                ai_socktype is 0
138  *                AI_CANNONNAME does not support.
139  * -------
140  */
141 static int
142 getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
143                 struct addrinfo **result)
144 {
145         struct addrinfo hints;
146         struct addrinfo *aip;
147         struct sockaddr_un *unp;
148
149         MemSet(&hints, 0, sizeof(hints));
150
151         if (hintsp == NULL)
152         {
153                 hints.ai_family = AF_UNIX;
154                 hints.ai_socktype = SOCK_STREAM;
155         }
156         else
157                 memcpy(&hints, hintsp, sizeof(hints));
158
159         if (hints.ai_socktype == 0)
160                 hints.ai_socktype = SOCK_STREAM;
161
162         if (!(hints.ai_family == AF_UNIX))
163         {
164                 elog(LOG, "hints.ai_family is invalied getaddrinfo_unix()\n");
165                 return EAI_ADDRFAMILY;
166         }
167
168         aip = calloc(1, sizeof(struct addrinfo));
169         if (aip == NULL)
170                 return EAI_MEMORY;
171
172         aip->ai_family = AF_UNIX;
173         aip->ai_socktype = hints.ai_socktype;
174         aip->ai_protocol = hints.ai_protocol;
175         aip->ai_next = NULL;
176         aip->ai_canonname = NULL;
177         *result = aip;
178
179         unp = calloc(1, sizeof(struct sockaddr_un));
180         if (aip == NULL)
181                 return EAI_MEMORY;
182
183         unp->sun_family = AF_UNIX;
184         aip->ai_addr = (struct sockaddr *) unp;
185         aip->ai_addrlen = sizeof(struct sockaddr_un);
186
187         if (strlen(path) >= sizeof(unp->sun_path))
188                 return EAI_SERVICE;
189         strcpy(unp->sun_path, path);
190
191 #if SALEN
192         unp->sun_len = sizeof(struct sockaddr_un);
193 #endif   /* SALEN */
194
195         if (hints.ai_flags & AI_PASSIVE)
196                 unlink(unp->sun_path);
197
198         return 0;
199 }
200 #endif   /* HAVE_UNIX_SOCKETS && HAVE_IPV6 */
201
202 /* ----------
203  * SockAddr_ntop - set IP address string from SockAddr
204  *
205  * parameters...  sa    : SockAddr union
206  *                                dst   : buffer for address string
207  *                                cnt   : sizeof dst
208  *                                v4conv: non-zero: if address is IPv4 mapped IPv6 address then
209  *                                                convert to IPv4 address.
210  * returns... pointer to dst
211  * if sa.sa_family is not AF_INET or AF_INET6 dst is set as empy string.
212  * ----------
213  */
214 char *
215 SockAddr_ntop(const SockAddr *sa, char *dst, size_t cnt, int v4conv)
216 {
217         switch (sa->sa.sa_family)
218         {
219                 case AF_INET:
220 #ifdef HAVE_IPV6
221                         inet_ntop(AF_INET, &sa->in.sin_addr, dst, cnt);
222 #else
223                         StrNCpy(dst, inet_ntoa(sa->in.sin_addr), cnt);
224 #endif
225                         break;
226 #ifdef HAVE_IPV6
227                 case AF_INET6:
228                         inet_ntop(AF_INET6, &sa->in6.sin6_addr, dst, cnt);
229                         if (v4conv && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr))
230                                 strcpy(dst, dst + 7);
231                         break;
232 #endif
233                 default:
234                         dst[0] = '\0';
235                         break;
236         }
237         return dst;
238 }
239
240
241 /*
242  *      SockAddr_pton - IPv6 pton
243  */
244 int
245 SockAddr_pton(SockAddr *sa, const char *src)
246 {
247         int                     family = AF_INET;
248 #ifdef HAVE_IPV6
249         const char              *ch;
250
251         for (ch = src; *ch != '\0'; ch++)
252         {
253                 if (*ch == ':')
254                 {
255                         family = AF_INET6;
256                         break;
257                 }
258         }
259 #endif
260
261         sa->sa.sa_family = family;
262
263         switch (family)
264         {
265                 case AF_INET:
266 #ifdef HAVE_IPV6
267                         return inet_pton(AF_INET, src, &sa->in.sin_addr);
268 #else
269                         return inet_aton(src, &sa->in.sin_addr);
270 #endif
271
272 #ifdef HAVE_IPV6
273                 case AF_INET6:
274                         return inet_pton(AF_INET6, src, &sa->in6.sin6_addr);
275                         break;
276 #endif
277                 default:
278                         return -1;
279         }
280 }
281
282
283
284
285 /*
286  *      isAF_INETx - check to see if sa is AF_INET or AF_INET6
287  */
288 int
289 isAF_INETx(const int family)
290 {
291         if (family == AF_INET
292 #ifdef HAVE_IPV6
293                 || family == AF_INET6
294 #endif
295                 )
296                 return 1;
297         else
298                 return 0;
299 }
300
301
302 int
303 rangeSockAddr(const SockAddr *addr, const SockAddr *netaddr, const SockAddr *netmask)
304 {
305         if (addr->sa.sa_family == AF_INET)
306                 return rangeSockAddrAF_INET(addr, netaddr, netmask);
307 #ifdef HAVE_IPV6
308         else if (addr->sa.sa_family == AF_INET6)
309                 return rangeSockAddrAF_INET6(addr, netaddr, netmask);
310 #endif
311         else
312                 return 0;
313 }
314
315 int
316 rangeSockAddrAF_INET(const SockAddr *addr, const SockAddr *netaddr,
317                                          const SockAddr *netmask)
318 {
319         if (addr->sa.sa_family != AF_INET ||
320                 netaddr->sa.sa_family != AF_INET ||
321                 netmask->sa.sa_family != AF_INET)
322                 return 0;
323         if (((addr->in.sin_addr.s_addr ^ netaddr->in.sin_addr.s_addr) &
324                  netmask->in.sin_addr.s_addr) == 0)
325                 return 1;
326         else
327                 return 0;
328 }
329
330 #ifdef HAVE_IPV6
331 int
332 rangeSockAddrAF_INET6(const SockAddr *addr, const SockAddr *netaddr,
333                                           const SockAddr *netmask)
334 {
335         int                     i;
336
337         if (IN6_IS_ADDR_V4MAPPED(&addr->in6.sin6_addr))
338         {
339                 SockAddr        addr4;
340
341                 convSockAddr6to4(addr, &addr4);
342                 if (rangeSockAddrAF_INET(&addr4, netaddr, netmask))
343                         return 1;
344         }
345
346         if (netaddr->sa.sa_family != AF_INET6 ||
347                 netmask->sa.sa_family != AF_INET6)
348                 return 0;
349
350         for (i = 0; i < 16; i++)
351         {
352                 if (((addr->in6.sin6_addr.s6_addr[i] ^ netaddr->in6.sin6_addr.s6_addr[i]) &
353                          netmask->in6.sin6_addr.s6_addr[i]) != 0)
354                         return 0;
355         }
356
357         return 1;
358 }
359
360 void
361 convSockAddr6to4(const SockAddr *src, SockAddr *dst)
362 {
363         char            addr_str[INET6_ADDRSTRLEN];
364
365         dst->in.sin_family = AF_INET;
366         dst->in.sin_port = src->in6.sin6_port;
367
368         dst->in.sin_addr.s_addr =
369                 (src->in6.sin6_addr.s6_addr[15])
370                 + (src->in6.sin6_addr.s6_addr[14] << 8)
371                 + (src->in6.sin6_addr.s6_addr[13] << 16)
372                 + (src->in6.sin6_addr.s6_addr[12] << 24);
373         SockAddr_ntop(src, addr_str, INET6_ADDRSTRLEN, 0);
374 }
375 #endif