Added dropin replacements for get*info and helper functions.
[tinc] / lib / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000,2001 Guus Sliepen <guus@sliepen.warande.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., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: dropin.c,v 1.1.2.7 2001/11/16 17:36:56 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32
33 #include <xalloc.h>
34
35 #include <system.h>
36 #include <errno.h>
37
38 #ifndef HAVE_DAEMON
39 /*
40   Replacement for the daemon() function.
41   
42   The daemon() function is for programs wishing to detach themselves
43   from the controlling terminal and run in the background as system
44   daemons.
45
46   Unless the argument nochdir is non-zero, daemon() changes the
47   current working directory to the root (``/'').
48
49   Unless the argument noclose is non-zero, daemon() will redirect
50   standard input, standard output and standard error to /dev/null.
51 */
52 int daemon(int nochdir, int noclose)
53 {
54   pid_t pid;
55   int fd;
56   
57   pid = fork();
58   
59   /* Check if forking failed */
60   if(pid < 0)
61     {
62       perror("fork");
63       exit(-1);
64     }
65
66   /* If we are the parent, terminate */
67   if(pid)
68     exit(0);
69
70   /* Detach by becoming the new process group leader */
71   if(setsid() < 0)
72     {
73       perror("setsid");
74       return -1;
75     }
76   
77   /* Change working directory to the root (to avoid keeping mount
78      points busy) */
79   if(!nochdir)
80     {
81       chdir("/");
82     }
83     
84   /* Redirect stdin/out/err to /dev/null */
85   if(!noclose)
86     {
87       fd = open("/dev/null", O_RDWR);
88
89       if(fd < 0)
90         {
91           perror("opening /dev/null");
92           return -1;
93         }
94       else
95         {
96           dup2(fd, 0);
97           dup2(fd, 1);
98           dup2(fd, 2);
99         }
100     }
101
102   return 0;
103 }
104 #endif
105
106
107
108
109 #ifndef HAVE_GET_CURRENT_DIR_NAME
110 /*
111   Replacement for the GNU get_current_dir_name function:
112
113   get_current_dir_name will malloc(3) an array big enough to hold the
114   current directory name.  If the environment variable PWD is set, and
115   its value is correct, then that value will be returned.
116 */
117 char *get_current_dir_name(void)
118 {
119   size_t size;
120   char *buf;
121   char *r;
122
123   /* Start with 100 bytes.  If this turns out to be insufficient to
124      contain the working directory, double the size.  */
125   size = 100;
126   buf = xmalloc(size);
127
128   errno = 0; /* Success */
129   r = getcwd(buf, size);
130   /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
131      is insufficient to contain the entire working directory.  */
132   while(r == NULL && errno == ERANGE)
133     {
134       free(buf);
135       size <<= 1; /* double the size */
136       buf = xmalloc(size);
137       r = getcwd(buf, size);
138     }
139
140   return buf;
141 }
142 #endif
143
144 #ifndef HAVE_ASPRINTF
145 int asprintf(char **buf, const char *fmt, ...)
146 {
147   int status;
148   va_list ap;
149   int len;
150   
151   len = 4096;
152   *buf = xmalloc(len);
153
154   va_start(ap, fmt);
155   status = vsnprintf (*buf, len, fmt, ap);
156   va_end (ap);
157
158   if(status >= 0)
159     *buf = xrealloc(*buf, status);
160
161   if(status > len-1)
162     {
163       len = status;
164       va_start(ap, fmt);
165       status = vsnprintf (*buf, len, fmt, ap);
166       va_end (ap);
167     }
168
169   return status;
170 }
171 #endif
172
173
174 /*
175  * fake library for ssh
176  *
177  * This file is included in getaddrinfo.c and getnameinfo.c.
178  * See getaddrinfo.c and getnameinfo.c.
179  */
180
181 /* $Id: dropin.c,v 1.1.2.7 2001/11/16 17:36:56 zarq Exp $ */
182
183 /* for old netdb.h */
184 #ifndef EAI_NODATA
185 #define EAI_NODATA      1
186 #define EAI_MEMORY      2
187 #endif
188
189 /*
190  * fake library for ssh
191  *
192  * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
193  * These funtions are defined in rfc2133.
194  *
195  * But these functions are not implemented correctly. The minimum subset
196  * is implemented for ssh use only. For exapmle, this routine assumes
197  * that ai_family is AF_INET. Don't use it for another purpose.
198  */
199
200 #ifndef HAVE_GAI_STRERROR
201 char *gai_strerror(int ecode)
202 {
203         switch (ecode) {
204                 case EAI_NODATA:
205                         return "no address associated with hostname.";
206                 case EAI_MEMORY:
207                         return "memory allocation failure.";
208                 default:
209                         return "unknown error.";
210         }
211 }    
212 #endif /* !HAVE_GAI_STRERROR */
213
214 #ifndef HAVE_FREEADDRINFO
215 void freeaddrinfo(struct addrinfo *ai)
216 {
217         struct addrinfo *next;
218
219         do {
220                 next = ai->ai_next;
221                 free(ai);
222         } while (NULL != (ai = next));
223 }
224 #endif /* !HAVE_FREEADDRINFO */
225
226 #ifndef HAVE_GETADDRINFO
227 static struct addrinfo *malloc_ai(int port, u_long addr)
228 {
229         struct addrinfo *ai;
230
231         ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
232         if (ai == NULL)
233                 return(NULL);
234         
235         memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
236         
237         ai->ai_addr = (struct sockaddr *)(ai + 1);
238         /* XXX -- ssh doesn't use sa_len */
239         ai->ai_addrlen = sizeof(struct sockaddr_in);
240         ai->ai_addr->sa_family = ai->ai_family = AF_INET;
241
242         ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
243         ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
244         
245         return(ai);
246 }
247
248 int getaddrinfo(const char *hostname, const char *servname, 
249                 const struct addrinfo *hints, struct addrinfo **res)
250 {
251         struct addrinfo *cur, *prev = NULL;
252         struct hostent *hp;
253         struct in_addr in;
254         int i, port;
255
256         if (servname)
257                 port = htons(atoi(servname));
258         else
259                 port = 0;
260
261         if (hints && hints->ai_flags & AI_PASSIVE) {
262                 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
263                         return 0;
264                 else
265                         return EAI_MEMORY;
266         }
267                 
268         if (!hostname) {
269                 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
270                         return 0;
271                 else
272                         return EAI_MEMORY;
273         }
274         
275         if (inet_aton(hostname, &in)) {
276                 if (NULL != (*res = malloc_ai(port, in.s_addr)))
277                         return 0;
278                 else
279                         return EAI_MEMORY;
280         }
281         
282         hp = gethostbyname(hostname);
283         if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
284                 for (i = 0; hp->h_addr_list[i]; i++) {
285                         cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
286                         if (cur == NULL) {
287                                 if (*res)
288                                         freeaddrinfo(*res);
289                                 return EAI_MEMORY;
290                         }
291                         
292                         if (prev)
293                                 prev->ai_next = cur;
294                         else
295                                 *res = cur;
296
297                         prev = cur;
298                 }
299                 return 0;
300         }
301         
302         return EAI_NODATA;
303 }
304 #endif /* !HAVE_GETADDRINFO */
305
306
307 /*
308  * fake library for ssh
309  *
310  * This file includes getnameinfo().
311  * These funtions are defined in rfc2133.
312  *
313  * But these functions are not implemented correctly. The minimum subset
314  * is implemented for ssh use only. For exapmle, this routine assumes
315  * that ai_family is AF_INET. Don't use it for another purpose.
316  */
317
318 #ifndef HAVE_GETNAMEINFO
319 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, 
320                 size_t hostlen, char *serv, size_t servlen, int flags)
321 {
322         struct sockaddr_in *sin = (struct sockaddr_in *)sa;
323         struct hostent *hp;
324         char tmpserv[16];
325
326         if (serv) {
327                 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
328                 if (strlen(tmpserv) >= servlen)
329                         return EAI_MEMORY;
330                 else
331                         strcpy(serv, tmpserv);
332         }
333
334         if (host) {
335                 if (flags & NI_NUMERICHOST) {
336                         if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
337                                 return EAI_MEMORY;
338
339                         strcpy(host, inet_ntoa(sin->sin_addr));
340                         return 0;
341                 } else {
342                         hp = gethostbyaddr((char *)&sin->sin_addr, 
343                                 sizeof(struct in_addr), AF_INET);
344                         if (hp == NULL)
345                                 return EAI_NODATA;
346                         
347                         if (strlen(hp->h_name) >= hostlen)
348                                 return EAI_MEMORY;
349
350                         strcpy(host, hp->h_name);
351                         return 0;
352                 }
353         }
354         return 0;
355 }
356 #endif /* !HAVE_GETNAMEINFO */