summaryrefslogtreecommitdiff
path: root/netlib.c
blob: 10d4df5b545681bb208317bbe72b595ffc2c88a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* $Id$ */
#include <sys/types.h>

#include "gpsd_config.h"

#ifndef S_SPLINT_S
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/ip.h>
#endif
#endif /* S_SPLINT_S */
#ifndef S_SPLINT_S
 #ifdef HAVE_NETDB_H
  #include <netdb.h>
 #endif /* HAVE_NETDB_H */
 #ifdef HAVE_ARPA_INET_H
  #include <arpa/inet.h>
 #endif /* HAVE_ARPA_INET_H */
#endif /* S_SPLINT_S */
#include <errno.h>
#include <stdlib.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */
#include <string.h>

#include "gpsd.h"

#if !defined (INADDR_NONE)
#define INADDR_NONE   ((in_addr_t)-1)
#endif

socket_t netlib_connectsock(const char *host, const char *service, const char *protocol)
{
    struct hostent *phe;
    struct servent *pse;
    struct protoent *ppe;
    struct sockaddr_in sin;
    int type, proto, one = 1;
    socket_t s;

    memset((char *) &sin, 0, sizeof(sin));
    /*@ -type -mustfreefresh @*/
    sin.sin_family = AF_INET;
    if ((pse = getservbyname(service, protocol)))
	sin.sin_port = htons(ntohs((unsigned short) pse->s_port));
    else if ((sin.sin_port = htons((unsigned short) atoi(service))) == 0)
	return NL_NOSERVICE;

    if ((phe = gethostbyname(host)))
	memcpy((char *) &sin.sin_addr, phe->h_addr, phe->h_length);
#ifndef S_SPLINT_S
    else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
	return NL_NOHOST;
#endif /* S_SPLINT_S */

    ppe = getprotobyname(protocol);
    if (strcmp(protocol, "udp") == 0) {
	type = SOCK_DGRAM;
	proto = (ppe) ? ppe->p_proto : IPPROTO_UDP;
    } else {
	type = SOCK_STREAM;
	proto = (ppe) ? ppe->p_proto : IPPROTO_TCP;
    }

    if ((s = socket(PF_INET, type, proto)) == -1)
	return NL_NOSOCK;
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one))==-1) {
	(void)close(s);
	return NL_NOSOCKOPT;
    }
    if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
	(void)close(s);
	return NL_NOCONNECT;
    }

#ifdef IPTOS_LOWDELAY
    {
    int opt = IPTOS_LOWDELAY;
    /*@ -unrecog @*/
    (void)setsockopt(s, IPPROTO_IP, IP_TOS, &opt, sizeof opt);
    /*@ +unrecog @*/
    }
#endif
#ifdef TCP_NODELAY
    if (type == SOCK_STREAM)
	setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof one);
#endif
    return s;
    /*@ +type +mustfreefresh @*/
}

char /*@observer@*/ *netlib_errstr(const int err)
{
    switch (err) {
    case NL_NOSERVICE:  return "can't get service entry";
    case NL_NOHOST:     return "can't get host entry";
    case NL_NOPROTO:    return "can't get protocol entry";
    case NL_NOSOCK:     return "can't create socket";
    case NL_NOSOCKOPT:  return "error SETSOCKOPT SO_REUSEADDR";
    case NL_NOCONNECT:  return "can't connect to host/port pair";
    default:		return "unknown error";
    }
}

char *sock2ip(int fd)
{
    struct sockaddr fsin;
    socklen_t alen = (socklen_t)sizeof(fsin);
    char *ip;
    int r;

    r = getpeername(fd, (struct sockaddr *) &fsin, &alen);
    /*@ -branchstate @*/
    if (r == 0) {
	ip = inet_ntoa(((struct sockaddr_in *)(&fsin))->sin_addr);
    } else {
	gpsd_report(LOG_INF, "getpeername() = %d, error = %s (%d)\n", r, strerror(errno), errno);
	ip = "<unknown>";
    }
    /*@ +branchstate @*/
    return ip;
}