Skip to content

Commit

Permalink
Merge pull request #70 from UniversityRadioYork/mw-fix-windoze-iocore
Browse files Browse the repository at this point in the history
Fix Connection::Name() on Microsoft Windows.
  • Loading branch information
adfw committed Dec 7, 2014
2 parents 065687f + b9ec613 commit 4dc8c81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
language: cpp

before_install:
- if [ "$CXX" == "g++" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
- if [ "$CXX" == "clang++" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm; fi
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- if [ "$CXX" == "clang++" ]; then wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key|sudo apt-key add -; fi
- if [ "$CXX" == "clang++" ]; then sudo sh -c "echo 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.5 main' >> /etc/apt/sources.list"; fi
- sudo apt-get update -qq

install:
- if [ "$CXX" = "g++" ]; then sudo apt-get install -qq g++-4.8; fi
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
- if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.4; fi
- if [ "$CXX" == "clang++" ]; then export CXX="clang++-3.4" CC="clang-3.4"; fi
- if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.5; fi
- if [ "$CXX" == "clang++" ]; then export CXX="clang++-3.5" CC="clang-3.5"; fi

- sudo apt-get install -y libsox-dev portaudio19-dev
- sudo pip install cpp-coveralls
Expand All @@ -27,7 +28,7 @@ script:

# coveralls doesn't like gcc (segfaults)
after_success:
- if [ "$CXX" == "clang++-3.4" ]; then coveralls -b . --exclude src/tests --exclude src/contrib; fi
- if [ "$CXX" == "clang++-3.5" ]; then coveralls -b . --exclude src/tests --exclude src/contrib --gcov="`which llvm-cov-3.5`"; fi

notifications:
irc:
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,18 @@ $(TEST_BIN): $(COBJECTS) $(TEST_OBJECTS)
# Special targets
#

# Make sure we clean up coverage artefacts in a make clean, too.
COV_ARTEFACTS = $(OBJECTS:.o=.gcno)
COV_ARTEFACTS += $(OBJECTS:.o=.gcda)
COV_ARTEFACTS += $(COBJECTS:.o=.gcda)
COV_ARTEFACTS += $(COBJECTS:.o=.gcno)

# Cleans up the results of a previous build.
clean:
@echo CLEAN
@rm -f $(OBJECTS) $(COBJECTS) $(MAN_HTML) $(MAN_GZ) $(BIN)
@rm -f $(TEST_OBJECTS) $(TEST_BIN)
@rm -f $(COV_ARTEFACTS)

# Makes the build subdirectories.
mkdir:
Expand All @@ -209,4 +216,4 @@ format: $(TO_FORMAT)

coverage: CXXFLAGS += -fprofile-arcs -ftest-coverage
coverage: LDFLAGS += -fprofile-arcs -ftest-coverage
coverage: mkdir test
coverage: clean mkdir test
19 changes: 11 additions & 8 deletions src/io/io_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <string>

extern "C" {
// If UNICODE is defined on Windows, it'll select the wide-char gai_strerror.
// We don't want this.
#undef UNICODE
#include <uv.h>
}

Expand Down Expand Up @@ -251,11 +254,13 @@ std::string Connection::Name()
// Using this instead of struct sockaddr is advised by the libuv docs,
// for IPv6 compatibility.
struct sockaddr_storage s;
int namelen;
auto sp = (struct sockaddr *) &s;

if (uv_tcp_getpeername(this->tcp, (struct sockaddr *)&s, &namelen) < 0) {
return "(error)";
}
// Turns out if you don't do this, Windows (and only Windows?) is upset.
socklen_t namelen = sizeof(s);

int pe = uv_tcp_getpeername(this->tcp, sp, (int *)&namelen);
if (pe) return std::string("(error getting peer info: ") + uv_strerror(pe) + ")";

// Now, split the sockaddr into host and service.
char host[NI_MAXHOST];
Expand All @@ -265,10 +270,8 @@ std::string Connection::Name()
// Otherwise, we could get a (likely erroneous) string description of
// what
// the network stack *thinks* the port is used for.
if (getnameinfo((struct sockaddr *)&s, namelen, host, sizeof(host),
serv, sizeof(serv), NI_NUMERICSERV)) {
return "(error)";
}
int ne = getnameinfo(sp, namelen, host, sizeof(host), serv, sizeof(serv), NI_NUMERICSERV);
if (ne) return std::string("(error getting name: ") + gai_strerror(ne) + ")";

return std::string(host) + ":" + std::string(serv);
}
Expand Down

0 comments on commit 4dc8c81

Please sign in to comment.