PageRenderTime 354ms CodeModel.GetById 210ms app.highlight 7ms RepoModel.GetById 135ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/http-host.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 95 lines | 70 code | 6 blank | 19 comment | 22 complexity | 544aad444c1f9c7aefebf2aef7e9b259 MD5 | raw file
 1/*
 2    http-host.c - thread-safe host lookup
 3    Copyright (C) 2008 siliconforks.com
 4
 5    This program is free software; you can redistribute it and/or modify
 6    it under the terms of the GNU General Public License as published by
 7    the Free Software Foundation; either version 2 of the License, or
 8    (at your option) any later version.
 9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#include <config.h>
21
22#include "http-server.h"
23
24#include "util.h"
25
26int xgethostbyname(const char * host, struct in_addr * a) {
27#if defined(__CYGWIN__) || defined(__MINGW32__)
28  /* gethostbyname is thread-safe */
29  struct hostent * p = gethostbyname(host);
30  if (p == NULL || p->h_addrtype != AF_INET) {
31    return -1;
32  }
33  *a = *((struct in_addr *) p->h_addr);
34  return 0;
35#elif HAVE_GETADDRINFO
36  struct addrinfo hints;
37  hints.ai_flags = 0;
38  hints.ai_family = PF_INET;
39  hints.ai_socktype = 0;
40  hints.ai_protocol = 0;
41  hints.ai_addrlen = 0;
42  hints.ai_addr = NULL;
43  hints.ai_canonname = NULL;
44  hints.ai_next = NULL;
45  struct addrinfo * p;
46  int result = getaddrinfo(host, NULL, &hints, &p);
47  if (result != 0 || p == NULL) {
48    return -1;
49  }
50  if (p->ai_family != PF_INET) {
51    freeaddrinfo(p);
52    return -1;
53  }
54  struct sockaddr_in * address_and_port = (struct sockaddr_in *) p->ai_addr;
55  *a = address_and_port->sin_addr;
56  freeaddrinfo(p);
57  return 0;
58#elif HAVE_GETHOSTBYNAME_R
59  struct hostent h;
60  struct hostent * p;
61  char * buffer;
62  size_t buffer_size;
63  int error;
64  int result;
65
66  buffer_size = 1024;
67  buffer = xmalloc(buffer_size);
68  while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) {
69    buffer_size = mulst(buffer_size, 2);
70    buffer = xrealloc(buffer, buffer_size);
71  }
72  if (result != 0 || p == NULL || p->h_addrtype != AF_INET) {
73    free(buffer);
74    return -1;
75  }
76  *a = *((struct in_addr *) p->h_addr);
77  free(buffer);
78  return 0;
79#else
80#error "No thread-safe host lookup available"
81#endif
82}
83
84#ifndef HAVE_INET_ATON
85int inet_aton(const char * name, struct in_addr * a) {
86  unsigned long result = inet_addr(name);
87  if (result == INADDR_NONE) {
88    return 0;
89  }
90  else {
91    a->s_addr = result;
92    return 1;
93  }
94}
95#endif