~ubuntu-branches/ubuntu/edgy/binkd/edgy

« back to all changes in this revision

Viewing changes to iptools.c

  • Committer: Bazaar Package Importer
  • Author(s): Marco d'Itri
  • Date: 2005-04-09 19:05:59 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050409190559-j7ygbycavxr93dfa
Tags: 0.9.8-1
* New upstream release.
* I AM LOOKING FOR A CO-MAINTAINER!

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 */
13
13
 
14
14
/*
15
 
 * $Id: iptools.c,v 2.0 2001/01/10 12:12:38 gul Exp $
 
15
 * $Id: iptools.c,v 2.10 2003/05/26 20:37:59 gul Exp $
16
16
 *
17
17
 * $Log: iptools.c,v $
 
18
 * Revision 2.10  2003/05/26 20:37:59  gul
 
19
 * typo in previous patch
 
20
 *
 
21
 * Revision 2.9  2003/05/26 20:34:38  gul
 
22
 * Bugfix on resolving raw IP when HAVE_FORK
 
23
 *
 
24
 * Revision 2.8  2003/05/04 08:45:30  gul
 
25
 * Lock semaphores more safely for resolve and IP-addr print
 
26
 *
 
27
 * Revision 2.7  2003/03/26 12:59:16  gul
 
28
 * Fix previous patch
 
29
 *
 
30
 * Revision 2.6  2003/03/26 10:44:40  gul
 
31
 * Code cleanup
 
32
 *
 
33
 * Revision 2.5  2003/03/25 20:37:46  gul
 
34
 * free_hostent() function
 
35
 *
 
36
 * Revision 2.4  2003/03/01 18:46:05  gul
 
37
 * Use HAVE_SYS_IOCTL_H macro
 
38
 *
 
39
 * Revision 2.3  2003/02/28 08:53:38  gul
 
40
 * Fixed proxy usage
 
41
 *
 
42
 * Revision 2.2  2003/02/22 12:12:33  gul
 
43
 * Cleanup sources
 
44
 *
 
45
 * Revision 2.1  2003/02/22 11:45:41  gul
 
46
 * Do not resolve hosts if proxy or socks5 using
 
47
 *
18
48
 * Revision 2.0  2001/01/10 12:12:38  gul
19
49
 * Binkd is under CVS again
20
50
 *
34
64
#include <fcntl.h>
35
65
#include <ctype.h>
36
66
 
37
 
#if !defined(WIN32) && !defined(DOS)
 
67
#if defined(HAVE_SYS_IOCTL_H)
38
68
#include <sys/ioctl.h>
39
69
#endif
40
70
 
 
71
#include "iphdr.h"
 
72
#include "iptools.h"
41
73
#include "tools.h"
42
74
#include "readcfg.h"
43
 
#include "iphdr.h"
44
 
#include "iptools.h"
 
75
#include "sem.h"
45
76
 
46
77
/*
47
78
 * Finds ASCIIZ address
52
83
  struct sockaddr_in s;
53
84
 
54
85
  memcpy(&s, addr, sizeof(s));
55
 
  hp = ((backresolv == 0) ? 0 :
56
 
        gethostbyaddr ((char *) &s.sin_addr,
57
 
                       sizeof s.sin_addr,
58
 
                       AF_INET));
59
 
 
60
 
  strnzcpy (host, hp ? hp->h_name : inet_ntoa (s.sin_addr), len);
61
 
 
 
86
  if (backresolv)
 
87
  {
 
88
    lockresolvsem();
 
89
    hp = gethostbyaddr ((char *) &s.sin_addr, sizeof s.sin_addr, AF_INET);
 
90
    if (hp)
 
91
    {
 
92
      strnzcpy (host, hp->h_name, len);
 
93
      releaseresolvsem();
 
94
      return host;
 
95
    }
 
96
    releaseresolvsem();
 
97
  }
 
98
  lockhostsem();
 
99
  strncpy (host, inet_ntoa (s.sin_addr), len);
 
100
  releasehostsem();
62
101
  return host;
63
102
}
64
103
 
65
104
#ifdef HAVE_THREADS
66
 
void copy_hostent(struct hostent *dest, struct hostent *src)
 
105
struct hostent *copy_hostent(struct hostent *dest, struct hostent *src)
67
106
{
68
107
  int naddr;
69
108
  char **cp;
84
123
      dest->h_addr_list[naddr] = NULL;
85
124
    }
86
125
  }
 
126
  return dest;
 
127
}
 
128
 
 
129
void free_hostent(struct hostent *hp)
 
130
{
 
131
  if (hp)
 
132
  {
 
133
    if (hp->h_addr_list && hp->h_addr_list[0])
 
134
      free(hp->h_addr_list[0]);
 
135
    if (hp->h_addr_list)
 
136
      free(hp->h_addr_list);
 
137
    hp->h_addr_list = NULL;
 
138
  }
87
139
}
88
140
#endif
89
141
 
136
188
  Log (1, "%s: incorrect port", s);
137
189
  return 0;
138
190
}
 
191
 
 
192
/*
 
193
 * Find the host IP address list by a domain name or IP address string.
 
194
 * Returns NULL on error.
 
195
 */
 
196
struct hostent *find_host(char *host, struct hostent *he, struct in_addr *defaddr)
 
197
{
 
198
  struct hostent *hp;
 
199
#ifdef HAVE_THREADS
 
200
  struct hostent ht;
 
201
  char *alist[2];
 
202
#else
 
203
  static struct hostent ht;
 
204
  static char *alist[2];
 
205
#endif
 
206
 
 
207
  if (!isdigit(host[0]) ||
 
208
      (defaddr->s_addr = inet_addr (host)) == INADDR_NONE)
 
209
  {
 
210
    /* If not a raw ip address, try nameserver */
 
211
    Log (5, "resolving `%s'...", host);
 
212
    lockresolvsem();
 
213
    if ((hp = gethostbyname(host)) == NULL)
 
214
    {
 
215
      Log(1, "%s: unknown host", host);
 
216
      releaseresolvsem();
 
217
      return NULL;
 
218
    }
 
219
    hp = copy_hostent(he, hp);
 
220
    releaseresolvsem();
 
221
    return hp;
 
222
  }
 
223
  /* Raw ip address, fake */
 
224
  hp = &ht;
 
225
  hp->h_name = host;
 
226
  hp->h_aliases = 0;
 
227
  hp->h_addrtype = AF_INET;
 
228
  hp->h_length = sizeof (struct in_addr);
 
229
  hp->h_addr_list = alist;
 
230
  hp->h_addr_list[0] = (char *) defaddr;
 
231
  hp->h_addr_list[1] = (char *) 0;
 
232
  hp = copy_hostent(he, hp);
 
233
  return hp;
 
234
}