Try to convert an IP address to packed binary form Supports IPv4 addresses on all platforms and IPv6 on platforms with IPv6 support.
(ipname)
| 327 | |
| 328 | |
| 329 | def _inet_paton(ipname): |
| 330 | """Try to convert an IP address to packed binary form |
| 331 | |
| 332 | Supports IPv4 addresses on all platforms and IPv6 on platforms with IPv6 |
| 333 | support. |
| 334 | """ |
| 335 | # inet_aton() also accepts strings like '1', '127.1', some also trailing |
| 336 | # data like '127.0.0.1 whatever'. |
| 337 | try: |
| 338 | addr = _socket.inet_aton(ipname) |
| 339 | except OSError: |
| 340 | # not an IPv4 address |
| 341 | pass |
| 342 | else: |
| 343 | if _socket.inet_ntoa(addr) == ipname: |
| 344 | # only accept injective ipnames |
| 345 | return addr |
| 346 | else: |
| 347 | # refuse for short IPv4 notation and additional trailing data |
| 348 | raise ValueError( |
| 349 | "{!r} is not a quad-dotted IPv4 address.".format(ipname) |
| 350 | ) |
| 351 | |
| 352 | try: |
| 353 | return _socket.inet_pton(_socket.AF_INET6, ipname) |
| 354 | except OSError: |
| 355 | raise ValueError("{!r} is neither an IPv4 nor an IP6 " |
| 356 | "address.".format(ipname)) |
| 357 | except AttributeError: |
| 358 | # AF_INET6 not available |
| 359 | pass |
| 360 | |
| 361 | raise ValueError("{!r} is not an IPv4 address.".format(ipname)) |
| 362 | |
| 363 | |
| 364 | def _ipaddress_match(cert_ipaddress, host_ip): |
no test coverage detected
searching dependent graphs…