apple / ccs-calendarserver

The Calendar and Contacts Server.
https://www.calendarserver.org
Apache License 2.0
486 stars 136 forks source link

socket exception handling #467

Open macosforgebot opened 11 years ago

macosforgebot commented 11 years ago

mat.davey@… originally submitted this as ticket:799


Whilst installing darwin calendar server on a ubutu 11.10 cloud image (aws), i came across the following.

In twext/web2/channel/http.py, within the function _cachedGetHostByAddr (Line 113), tries to get the host by addr using the python socket library, however the error raise is as follows

socket.error: Address family not supported by protocol

and not the socket.herror: [Errno 1] Unknown host which is handled.

as a quick fix i placed the following code within the try except so the exception is not raised.

hostname = hostaddr

Apologies if this has been reported before I had a quick search but could not find anything.

Let me know if you need any further details.

Mat

macosforgebot commented 11 years ago

billyw.seminars@… originally submitted this as comment:1:⁠ticket:799

macosforgebot commented 11 years ago

narge-calendarserver@… originally submitted this as comment:2:⁠ticket:799


The problem seems to be that socket.gethostbyaddr doesn't accept most IPv4-Mapped IPv6 addresses.

>>> socket.gethostbyaddr("::ffff:8.8.8.8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: Address family not supported by protocol

Though for some reason it seems to work for addresses in /etc/hosts.

>>> socket.gethostbyaddr("::ffff:127.0.0.1")
('localhost', [], ['::ffff:127.0.0.1'])

A solution that works for me is to strip "::ffff:" from the start of the hostaddr:

    if hostname is None:
        if hostaddr.startswith("::ffff:"):
            hostaddr = hostaddr[7:]
        try:
            hostname = socket.gethostbyaddr(hostaddr)[0]
macosforgebot commented 11 years ago

@wsanchez originally submitted this as comment:3:⁠ticket:799

macosforgebot commented 11 years ago

@wsanchez originally submitted this as comment:4:⁠ticket:799

macosforgebot commented 10 years ago

@wsanchez originally submitted this as comment:5:⁠ticket:799

macosforgebot commented 10 years ago

@wsanchez originally submitted this as comment:6:⁠ticket:799

macosforgebot commented 9 years ago

@wsanchez originally submitted this as comment:7:⁠ticket:799


Expiring old bugs with unknown state and impact.

macosforgebot commented 9 years ago

narge-macosforge@… originally submitted this as comment:8:⁠ticket:799


This is still broken in CalendarServer-6.0. The workaround I posted above still applies.

tnacsak commented 6 years ago

To handle the issue I did a similar change as macosforgebot wrote. It was submitted like this:

hostname = _cachedHostNames.get(hostaddr)
if hostname is None:
    try:
        if hostaddr[0:7] == "::ffff:":
            hostaddr = hostaddr[7:]
        hostname = socket.gethostbyaddr(hostaddr)[0]
    except (socket.herror, socket.gaierror):
        hostname = hostaddr

As I see folks already verified, I believe it will been applied soon.