summaryrefslogtreecommitdiff
path: root/netlib.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2010-04-28 15:12:48 -0400
committerEric S. Raymond <esr@thyrsus.com>2010-04-28 15:12:48 -0400
commit664bdf0180091da0c720712b4c6d00f0bc066b02 (patch)
treef8498c2722b7ad57a32223605d5f5ec231d5ef15 /netlib.c
parent9035f482d21c56793a25f1357799076c462ee764 (diff)
downloadgpsd-664bdf0180091da0c720712b4c6d00f0bc066b02.tar.gz
A long step towards reading UDP packets.
Diffstat (limited to 'netlib.c')
-rw-r--r--netlib.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/netlib.c b/netlib.c
index f9a854be..a385ba95 100644
--- a/netlib.c
+++ b/netlib.c
@@ -51,6 +51,7 @@ socket_t netlib_connectsock(int af, const char *host, const char *service,
struct addrinfo *result, *rp;
int ret, type, proto, one = 1;
socket_t s = -1;
+ bool bind_me;
/*@-type@*/
ppe = getprotobyname(protocol);
@@ -63,9 +64,14 @@ socket_t netlib_connectsock(int af, const char *host, const char *service,
}
/*@+type@*/
+ /* we probably ought to pass this in as an explicit flag argument */
+ bind_me = (type == SOCK_DGRAM);
+
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = af;
hints.ai_socktype = type;
+ if (bind_me)
+ hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = proto;
#ifndef S_SPLINT_S
if ((ret = getaddrinfo(host, service, &hints, &result))) {
@@ -92,9 +98,18 @@ socket_t netlib_connectsock(int af, const char *host, const char *service,
(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
sizeof(one)) == -1)
ret = NL_NOSOCKOPT;
- else if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0) {
- ret = 0;
- break;
+ else {
+ if (bind_me) {
+ if (bind(s, rp->ai_addr, rp->ai_addrlen) == 0) {
+ ret = 0;
+ break;
+ }
+ } else {
+ if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0) {
+ ret = 0;
+ break;
+ }
+ }
}
if (s > 0) {