~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to lib/ts/ink_inet.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
#include "libts.h"
 
25
 
 
26
#if defined(darwin)
 
27
extern "C"
 
28
{
 
29
  struct hostent *gethostbyname_r(const char *name, struct hostent *result, char *buffer, int buflen, int *h_errnop);
 
30
  struct hostent *gethostbyaddr_r(const char *name, size_t size, int type,
 
31
                                  struct hostent *result, char *buffer, int buflen, int *h_errnop);
 
32
}
 
33
#endif
 
34
 
 
35
 
 
36
struct hostent *
 
37
ink_gethostbyname_r(char *hostname, ink_gethostbyname_r_data * data)
 
38
{
 
39
#ifdef RENTRENT_GETHOSTBYNAME
 
40
  struct hostent *r = gethostbyname(hostname);
 
41
  if (r)
 
42
    data->ent = *r;
 
43
  data->herrno = errno;
 
44
 
 
45
#else //RENTRENT_GETHOSTBYNAME
 
46
#if GETHOSTBYNAME_R_GLIBC2
 
47
 
 
48
  struct hostent *addrp = NULL;
 
49
  int res = gethostbyname_r(hostname, &data->ent, data->buf,
 
50
                            INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp,
 
51
                            &data->herrno);
 
52
  struct hostent *r = NULL;
 
53
  if (!res && addrp)
 
54
    r = addrp;
 
55
 
 
56
#else
 
57
  struct hostent *r = gethostbyname_r(hostname, &data->ent, data->buf,
 
58
                                      INK_GETHOSTBYNAME_R_DATA_SIZE,
 
59
                                      &data->herrno);
 
60
#endif
 
61
#endif
 
62
  return r;
 
63
}
 
64
 
 
65
struct hostent *
 
66
ink_gethostbyaddr_r(char *ip, int len, int type, ink_gethostbyaddr_r_data * data)
 
67
{
 
68
#if GETHOSTBYNAME_R_GLIBC2
 
69
  struct hostent *r = NULL;
 
70
  struct hostent *addrp = NULL;
 
71
  int res = gethostbyaddr_r((char *) ip, len, type, &data->ent, data->buf,
 
72
                            INK_GETHOSTBYNAME_R_DATA_SIZE, &addrp,
 
73
                            &data->herrno);
 
74
  if (!res && addrp)
 
75
    r = addrp;
 
76
#else
 
77
#ifdef RENTRENT_GETHOSTBYADDR
 
78
  struct hostent *r = gethostbyaddr((const void *) ip, len, type);
 
79
 
 
80
#else
 
81
  struct hostent *r = gethostbyaddr_r((char *) ip, len, type, &data->ent,
 
82
                                      data->buf,
 
83
                                      INK_GETHOSTBYNAME_R_DATA_SIZE,
 
84
                                      &data->herrno);
 
85
#endif
 
86
#endif //LINUX
 
87
  return r;
 
88
}
 
89
 
 
90
unsigned int
 
91
host_to_ip(char *hostname)
 
92
{
 
93
  struct hostent *he;
 
94
 
 
95
  he = gethostbyname(hostname);
 
96
  if (he == NULL)
 
97
    return INADDR_ANY;
 
98
 
 
99
  return *(unsigned int *) he->h_addr;
 
100
}
 
101
 
 
102
uint32_t
 
103
ink_inet_addr(const char *s)
 
104
{
 
105
  uint32_t u[4];
 
106
  uint8_t *pc = (uint8_t *) s;
 
107
  int n = 0;
 
108
  uint32_t base = 10;
 
109
 
 
110
  while (n < 4) {
 
111
 
 
112
    u[n] = 0;
 
113
    base = 10;
 
114
 
 
115
    // handle hex, octal
 
116
 
 
117
    if (*pc == '0') {
 
118
      if (*++pc == 'x' || *pc == 'X')
 
119
        base = 16, pc++;
 
120
      else
 
121
        base = 8;
 
122
    }
 
123
    // handle hex, octal, decimal
 
124
 
 
125
    while (*pc) {
 
126
      if (ParseRules::is_digit(*pc)) {
 
127
        u[n] = u[n] * base + (*pc++ - '0');
 
128
        continue;
 
129
      }
 
130
      if (base == 16 && ParseRules::is_hex(*pc)) {
 
131
        u[n] = u[n] * 16 + ParseRules::ink_tolower(*pc++) - 'a' + 10;
 
132
        continue;
 
133
      }
 
134
      break;
 
135
    }
 
136
 
 
137
    n++;
 
138
    if (*pc == '.')
 
139
      pc++;
 
140
    else
 
141
      break;
 
142
  }
 
143
 
 
144
  if (*pc && !ParseRules::is_wslfcr(*pc))
 
145
    return htonl((uint32_t) - 1);
 
146
 
 
147
  switch (n) {
 
148
  case 1:
 
149
    return htonl(u[0]);
 
150
  case 2:
 
151
    if (u[0] > 0xff || u[1] > 0xffffff)
 
152
      return htonl((uint32_t) - 1);
 
153
    return htonl((u[0] << 24) | u[1]);
 
154
  case 3:
 
155
    if (u[0] > 0xff || u[1] > 0xff || u[2] > 0xffff)
 
156
      return htonl((uint32_t) - 1);
 
157
    return htonl((u[0] << 24) | (u[1] << 16) | u[2]);
 
158
  case 4:
 
159
    if (u[0] > 0xff || u[1] > 0xff || u[2] > 0xff || u[3] > 0xff)
 
160
      return htonl((uint32_t) - 1);
 
161
    return htonl((u[0] << 24) | (u[1] << 16) | (u[2] << 8) | u[3]);
 
162
  }
 
163
  return htonl((uint32_t) - 1);
 
164
}
 
165
 
 
166
const char *ink_inet_ntop(const struct sockaddr *addr, char *dst, size_t size)
 
167
{
 
168
  void *address = NULL;
 
169
 
 
170
  switch (addr->sa_family) {
 
171
  case AF_INET:
 
172
    address = &((struct sockaddr_in *)addr)->sin_addr;
 
173
    break;
 
174
  case AF_INET6:
 
175
    address = &((struct sockaddr_in6 *)addr)->sin6_addr;
 
176
    break;
 
177
  }
 
178
 
 
179
  return inet_ntop(addr->sa_family, address, dst, size);
 
180
}
 
181
 
 
182
uint16_t ink_inet_port(const struct sockaddr *addr)
 
183
{
 
184
  uint16_t port = 0;
 
185
 
 
186
  switch (addr->sa_family) {
 
187
  case AF_INET:
 
188
    port = ntohs(((struct sockaddr_in *)addr)->sin_port);
 
189
    break;
 
190
  case AF_INET6:
 
191
    port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
 
192
    break;
 
193
  }
 
194
 
 
195
  return port;
 
196
}