Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv6 support #783

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add IPv6 support
  • Loading branch information
phil-scale committed Jul 16, 2023
commit 84f605f021176c6a6d0f02768ad3d0d9227cac86
13 changes: 13 additions & 0 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
) + "\n"


# This method returns if a hostname is an IPv6 address
def is_ipv6_hostname(hostname):
# type: (str) -> bool
if not isinstance(hostname, str):
return False
try:
socket.inet_pton(socket.AF_INET6, hostname)
return True
except socket.error: # not a valid address
return False


# pylint: disable=useless-object-inheritance,too-many-instance-attributes
# pylint: disable=too-many-arguments,too-many-locals
class DogStatsd(object):
Expand Down Expand Up @@ -522,6 +534,7 @@ def _get_uds_socket(cls, socket_path):

@classmethod
def _get_udp_socket(cls, host, port):
family = socket.AF_INET6 if is_ipv6_hostname(host) else socket.AF_INET
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
cls._ensure_min_send_buffer_size(sock)
Expand Down