1 /*
2    getinfos.c - implementation of getaddrinfo(), getnameinfo() and related
3                 functions for systems that lack it
4                 note that these functions are implemented only for use
5                 in cvsd and are probably incomplete for other purposes
6 
7    Copyright (C) 2002, 2003 Arthur de Jong
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 
24 
25 #include "config.h"
26 
27 #include "getinfos.h"
28 #include "xmalloc.h"
29 
30 #include <stdio.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #ifdef HAVE_NETDB_H
35 #include <netdb.h>
36 #endif /* HAVE_NETDB_H */
37 
38 
39 /* this is a very poor implementation of the getaddrinfo() function
40    it only returns IPv4 stream addresses, regardless of the input
41    only numericly specified services (port numbers) are supported
42    hints are completely ignored */
getaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res)43 int getaddrinfo(const char *node, const char *service,
44                 const struct addrinfo *hints,
45                 struct addrinfo **res)
46 {
47   char *tmp;
48   struct sockaddr_in *addr;
49   struct hostent *hent;
50   *res=NULL;
51   /* allocate address */
52   addr=(struct sockaddr_in *)xmalloc(sizeof(struct sockaddr_in));
53   addr->sin_family=AF_INET;
54   /* check node */
55   if ((node==NULL)||
56       (strcmp(node,"*")==0)||
57       (strcmp(node,"0.0.0.0")==0))
58     addr->sin_addr.s_addr=INADDR_ANY;
59   else if ((hent=gethostbyname(node))!=NULL)
60     memcpy(&addr->sin_addr.s_addr,hent->h_addr,4);
61   else
62   {
63     free(addr);
64     return EAI_HERR;
65   }
66   /* check service */
67   /* TODO: maybe use getservbyname() */
68   addr->sin_port=htons((int)strtol(service,&tmp,0));
69   if (*tmp!='\0')
70   {
71     free(addr);
72     return EAI_WRONSERVC;
73   }
74   /* build result */
75   *res=(struct addrinfo *)xmalloc(sizeof(struct addrinfo));
76   (*res)->ai_flags=0;
77   (*res)->ai_family=AF_INET;
78   (*res)->ai_socktype=SOCK_STREAM;
79   (*res)->ai_protocol=0;
80   (*res)->ai_addrlen=(socklen_t)sizeof(struct sockaddr_in);
81   (*res)->ai_addr=(struct sockaddr *)addr;
82   (*res)->ai_canonname=NULL;
83   (*res)->ai_next=NULL;
84   /* and we're done */
85   return EAI_OK;
86 }
87 
88 
89 /* this is a poor implementation of getnameinfo()
90    it lacks name lookups and only returns numeric values */
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,size_t hostlen,char * serv,size_t servlen,int flags)91 int getnameinfo(const struct sockaddr *sa,socklen_t salen,
92                 char *host,size_t hostlen,
93                 char *serv,size_t servlen,int flags)
94 {
95   const struct sockaddr_in *caddr;
96   caddr=(const struct sockaddr_in *)sa;
97   if (salen!=(socklen_t)sizeof(struct sockaddr_in))
98     return EAI_WRONGSOCK;
99   /* save ip */
100   if ((host!=NULL)&&(hostlen>0))
101   {
102     /* TODO: use gethostbyaddr() or getipnodebyaddr() */
103     snprintf(host,hostlen,"%s",inet_ntoa(caddr->sin_addr));
104     host[hostlen-1]='\0';
105   }
106   /* save port */
107   if ((serv!=NULL)&&(servlen>0))
108   {
109     /* TODO: use getservbyport() */
110     snprintf(serv,servlen,"%d",(int)ntohs(caddr->sin_port));
111     serv[servlen-1]='\0';
112   }
113   return EAI_OK;
114 }
115 
116 
117 /* free address information allocated by getaddrinfo */
freeaddrinfo(struct addrinfo * res)118 void freeaddrinfo(struct addrinfo *res)
119 {
120   /* do recursively */
121   if (res==NULL) return;
122   freeaddrinfo(res->ai_next);
123   /* free memory */
124   free(res->ai_addr);
125   free(res);
126 }
127 
128 
129 /* translate an error codes returned by getaddrinfo() and
130    getnameinfo() to printable error strings */
gai_strerror(int errcode)131 const char *gai_strerror(int errcode)
132 {
133   switch (errcode)
134   {
135     case EAI_OK:             return "nothing wrong";
136     case EAI_SYSTEM:         return "see errno";
137     case EAI_WRONGNODE:      return "currently only '*' is supported";
138     case EAI_WRONSERVC:      return "only support for numeric services";
139     case EAI_WRONGSOCK:      return "wrong socket type returned";
140 #ifdef HAVE_HSTRERROR
141     case EAI_HERR:           return hstrerror(h_errno);
142 #else /* HAVE_HSTRERROR */
143     case EAI_HERR:
144       switch(h_errno)
145       {
146 #ifdef HOST_NOT_FOUND
147         case HOST_NOT_FOUND: return "host not found";
148 #endif /* HOST_NOT_FOUND */
149 #ifdef NO_ADDRESS
150         case NO_ADDRESS:     return "no address associated with that name";
151 #else /* NO_ADDRESS */
152 #ifdef NO_DATA
153         case NO_DATA:        return "no address associated with that name";
154 #endif /* NO_DATA */
155 #endif /* not NO_ADDRESS */
156 #ifdef NO_RECOVERY
157         case NO_RECOVERY:    return "unrecoverable error";
158 #endif /* NO_RECOVERY */
159 #ifdef TRY_AGAIN
160         case TRY_AGAIN:      return "temporary error";
161 #endif /* TRY_AGAIN */
162         default:             return "unknown";
163       }
164 #endif /* not HAVE_HSTRERROR */
165     default:                 return "unknown";
166   }
167 }
168