File: getinfos.c

package info (click to toggle)
cvsd 1.0.21
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,152 kB
  • ctags: 171
  • sloc: sh: 4,959; ansic: 1,869; perl: 198; makefile: 147
file content (167 lines) | stat: -rw-r--r-- 5,067 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
   getinfos.c - implementation of getaddrinfo(), getnameinfo() and related
                functions for systems that lack it
                note that these functions are implemented only for use
                in cvsd and are probably incomplete for other purposes

   Copyright (C) 2002, 2003 Arthur de Jong

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "config.h"

#include "getinfos.h"
#include "xmalloc.h"

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif /* HAVE_NETDB_H */


/* this is a very poor implementation of the getaddrinfo() function
   it only returns IPv4 stream addresses, regardless of the input
   only numericly specified services (port numbers) are supported
   hints are completely ignored */
int getaddrinfo(const char *node, const char *service,
                const struct addrinfo *hints,
                struct addrinfo **res)
{
  char *tmp;
  struct sockaddr_in *addr;
  struct hostent *hent;
  *res=NULL;
  /* allocate address */
  addr=(struct sockaddr_in *)xmalloc(sizeof(struct sockaddr_in));
  addr->sin_family=AF_INET;
  /* check node */
  if ((node==NULL)||
      (strcmp(node,"*")==0)||
      (strcmp(node,"0.0.0.0")==0))
    addr->sin_addr.s_addr=INADDR_ANY;
  else if ((hent=gethostbyname(node))!=NULL)
    memcpy(&addr->sin_addr.s_addr,hent->h_addr,4);
  else
  {
    free(addr);
    return EAI_HERR;
  }
  /* check service */
  /* TODO: maybe use getservbyname() */
  addr->sin_port=htons((int)strtol(service,&tmp,0));
  if (*tmp!='\0')
  {
    free(addr);
    return EAI_WRONSERVC;
  }
  /* build result */
  *res=(struct addrinfo *)xmalloc(sizeof(struct addrinfo));
  (*res)->ai_flags=0;
  (*res)->ai_family=AF_INET;
  (*res)->ai_socktype=SOCK_STREAM;
  (*res)->ai_protocol=0;
  (*res)->ai_addrlen=(socklen_t)sizeof(struct sockaddr_in);
  (*res)->ai_addr=(struct sockaddr *)addr;
  (*res)->ai_canonname=NULL;
  (*res)->ai_next=NULL;
  /* and we're done */
  return EAI_OK;
}


/* this is a poor implementation of getnameinfo()
   it lacks name lookups and only returns numeric values */
int getnameinfo(const struct sockaddr *sa,socklen_t salen,
                char *host,size_t hostlen,
                char *serv,size_t servlen,int flags)
{
  const struct sockaddr_in *caddr;
  caddr=(const struct sockaddr_in *)sa;
  if (salen!=(socklen_t)sizeof(struct sockaddr_in))
    return EAI_WRONGSOCK;
  /* save ip */
  if ((host!=NULL)&&(hostlen>0))
  {
    /* TODO: use gethostbyaddr() or getipnodebyaddr() */
    snprintf(host,hostlen,"%s",inet_ntoa(caddr->sin_addr));
    host[hostlen-1]='\0';
  }
  /* save port */
  if ((serv!=NULL)&&(servlen>0))
  {
    /* TODO: use getservbyport() */
    snprintf(serv,servlen,"%d",(int)ntohs(caddr->sin_port));
    serv[servlen-1]='\0';
  }
  return EAI_OK;
}


/* free address information allocated by getaddrinfo */
void freeaddrinfo(struct addrinfo *res)
{
  /* do recursively */
  if (res==NULL) return;
  freeaddrinfo(res->ai_next);
  /* free memory */
  free(res->ai_addr);
  free(res);
}


/* translate an error codes returned by getaddrinfo() and
   getnameinfo() to printable error strings */
const char *gai_strerror(int errcode)
{
  switch (errcode)
  {
    case EAI_OK:             return "nothing wrong";
    case EAI_SYSTEM:         return "see errno";
    case EAI_WRONGNODE:      return "currently only '*' is supported";
    case EAI_WRONSERVC:      return "only support for numeric services";
    case EAI_WRONGSOCK:      return "wrong socket type returned";
#ifdef HAVE_HSTRERROR
    case EAI_HERR:           return hstrerror(h_errno);
#else /* HAVE_HSTRERROR */
    case EAI_HERR:
      switch(h_errno)
      {
#ifdef HOST_NOT_FOUND
        case HOST_NOT_FOUND: return "host not found";
#endif /* HOST_NOT_FOUND */
#ifdef NO_ADDRESS
        case NO_ADDRESS:     return "no address associated with that name";
#else /* NO_ADDRESS */
#ifdef NO_DATA
        case NO_DATA:        return "no address associated with that name";
#endif /* NO_DATA */
#endif /* not NO_ADDRESS */
#ifdef NO_RECOVERY
        case NO_RECOVERY:    return "unrecoverable error";
#endif /* NO_RECOVERY */
#ifdef TRY_AGAIN
        case TRY_AGAIN:      return "temporary error";
#endif /* TRY_AGAIN */
        default:             return "unknown";
      }
#endif /* not HAVE_HSTRERROR */
    default:                 return "unknown";
  }
}