OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2004--2005, Google Inc. | 3 * Copyright 2004--2005, Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 case AF_INET: { | 56 case AF_INET: { |
57 return inet_ntop_v4(src, dst, size); | 57 return inet_ntop_v4(src, dst, size); |
58 } | 58 } |
59 case AF_INET6: { | 59 case AF_INET6: { |
60 return inet_ntop_v6(src, dst, size); | 60 return inet_ntop_v6(src, dst, size); |
61 } | 61 } |
62 } | 62 } |
63 return NULL; | 63 return NULL; |
64 } | 64 } |
65 | 65 |
66 // As above, but for inet_pton. Wraps inet_addr for v4, and implements inet_pton | 66 // As above, but for inet_pton. Implements inet_pton for v4 and v6. |
67 // for v6. Slightly more permissive than the RFC specified inet_pton, as it uses | |
68 // windows' inet_addr which permits octal and hexadecimal values in v4 | |
69 // addresses, while inet_pton only allows decimal. | |
70 // Note that our inet_ntop will output normal 'dotted' v4 addresses only. | 67 // Note that our inet_ntop will output normal 'dotted' v4 addresses only. |
71 int win32_inet_pton(int af, const char* src, void* dst) { | 68 int win32_inet_pton(int af, const char* src, void* dst) { |
72 if (!src || !dst) { | 69 if (!src || !dst) { |
73 return 0; | 70 return 0; |
74 } | 71 } |
75 if (af == AF_INET) { | 72 if (af == AF_INET) { |
76 return inet_pton_v4(src, dst); | 73 return inet_pton_v4(src, dst); |
77 } else if (af == AF_INET6) { | 74 } else if (af == AF_INET6) { |
78 return inet_pton_v6(src, dst); | 75 return inet_pton_v6(src, dst); |
79 } | 76 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 *cursor++ = ':'; | 162 *cursor++ = ':'; |
166 *cursor++ = ':'; | 163 *cursor++ = ':'; |
167 i += (max - 1); | 164 i += (max - 1); |
168 } | 165 } |
169 } | 166 } |
170 } | 167 } |
171 return dst; | 168 return dst; |
172 } | 169 } |
173 | 170 |
174 // Helper function for inet_pton for IPv4 addresses. | 171 // Helper function for inet_pton for IPv4 addresses. |
175 // Uses win32's inet_addr. | 172 // |src| points to a character string containing an IPv4 network address in |
| 173 // dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number |
| 174 // of up to three digits in the range 0 to 255. |
| 175 // The address is converted and copied to dst, |
| 176 // which must be sizeof(struct in_addr) (4) bytes (32 bits) long. |
176 int inet_pton_v4(const char* src, void* dst) { | 177 int inet_pton_v4(const char* src, void* dst) { |
177 uint32 ip = inet_addr(src); | 178 const int kIpv4AddressSize = 4; |
178 if (ip == 0xFFFFFFFF && strcmp(src, "255.255.255.255") != 0) { | 179 int num_dot = 0; |
| 180 const char* src_pos = src; |
| 181 unsigned char result[kIpv4AddressSize] = {0}; |
| 182 |
| 183 while (*src_pos != 0) { |
| 184 // strtol won't treat whitespace characters in the begining as an error, |
| 185 // so check to ensure this is started with digit before passing to strtol. |
| 186 if (!isdigit(*src_pos)) { |
| 187 return 0; |
| 188 } |
| 189 char* end_pos; |
| 190 long int value = strtol(src_pos, &end_pos, 10); |
| 191 if (value < 0 || value > 255 || src_pos == end_pos) { |
| 192 return 0; |
| 193 } |
| 194 result[num_dot] = value; |
| 195 if (*end_pos == 0) { |
| 196 break; |
| 197 } |
| 198 src_pos = end_pos; |
| 199 if (*src_pos == '.') { |
| 200 ++num_dot; |
| 201 if (num_dot > kIpv4AddressSize - 1) { |
| 202 return 0; |
| 203 } |
| 204 } |
| 205 ++src_pos; |
| 206 } |
| 207 if (num_dot != kIpv4AddressSize - 1) { |
179 return 0; | 208 return 0; |
180 } | 209 } |
181 struct in_addr* dst_as_in_addr = reinterpret_cast<struct in_addr*>(dst); | 210 memcpy(dst, result, sizeof(result)); |
182 dst_as_in_addr->s_addr = ip; | |
183 return 1; | 211 return 1; |
184 } | 212 } |
185 | 213 |
186 // Helper function for inet_pton for IPv6 addresses. | 214 // Helper function for inet_pton for IPv6 addresses. |
187 int inet_pton_v6(const char* src, void* dst) { | 215 int inet_pton_v6(const char* src, void* dst) { |
188 // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. | 216 // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. |
189 // Check for literal x in the input string. | 217 // Check for literal x in the input string. |
190 const char* readcursor = src; | 218 const char* readcursor = src; |
191 char c = *readcursor++; | 219 char c = *readcursor++; |
192 while (c) { | 220 while (c) { |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); | 459 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); |
432 *level = *GetSidSubAuthority(til->Label.Sid, count - 1); | 460 *level = *GetSidSubAuthority(til->Label.Sid, count - 1); |
433 ret = true; | 461 ret = true; |
434 } | 462 } |
435 } | 463 } |
436 CloseHandle(token); | 464 CloseHandle(token); |
437 } | 465 } |
438 return ret; | 466 return ret; |
439 } | 467 } |
440 } // namespace talk_base | 468 } // namespace talk_base |
OLD | NEW |