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; | |
Sergey Ulanov
2012/03/01 01:18:11
nit: add {} around this line
Ronghua Wu (Left Chromium)
2012/03/01 01:21:57
Done.
| |
188 char* end_pos; | |
189 long int value = strtol(src_pos, &end_pos, 10); | |
190 if (value < 0 || value > 255 || src_pos == end_pos) { | |
191 return 0; | |
192 } | |
193 result[num_dot] = value; | |
194 if (*end_pos == 0) { | |
195 break; | |
196 } | |
197 src_pos = end_pos; | |
198 if (*src_pos == '.') { | |
199 ++num_dot; | |
200 if (num_dot > kIpv4AddressSize - 1) { | |
201 return 0; | |
202 } | |
203 } | |
204 ++src_pos; | |
205 } | |
206 if (num_dot != 3) { | |
Sergey Ulanov
2012/03/01 01:18:11
nit: kIpv4AddressSize - 1
Ronghua Wu (Left Chromium)
2012/03/01 01:21:57
Done.
| |
179 return 0; | 207 return 0; |
180 } | 208 } |
181 struct in_addr* dst_as_in_addr = reinterpret_cast<struct in_addr*>(dst); | 209 memcpy(dst, result, sizeof(result)); |
182 dst_as_in_addr->s_addr = ip; | |
183 return 1; | 210 return 1; |
184 } | 211 } |
185 | 212 |
186 // Helper function for inet_pton for IPv6 addresses. | 213 // Helper function for inet_pton for IPv6 addresses. |
187 int inet_pton_v6(const char* src, void* dst) { | 214 int inet_pton_v6(const char* src, void* dst) { |
188 // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. | 215 // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. |
189 // Check for literal x in the input string. | 216 // Check for literal x in the input string. |
190 const char* readcursor = src; | 217 const char* readcursor = src; |
191 char c = *readcursor++; | 218 char c = *readcursor++; |
192 while (c) { | 219 while (c) { |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); | 458 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); |
432 *level = *GetSidSubAuthority(til->Label.Sid, count - 1); | 459 *level = *GetSidSubAuthority(til->Label.Sid, count - 1); |
433 ret = true; | 460 ret = true; |
434 } | 461 } |
435 } | 462 } |
436 CloseHandle(token); | 463 CloseHandle(token); |
437 } | 464 } |
438 return ret; | 465 return ret; |
439 } | 466 } |
440 } // namespace talk_base | 467 } // namespace talk_base |
OLD | NEW |