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
|
#include "config.h"
#include <stdlib.h>
#include <string.h>
#if defined (HAVE_STRINGS_H)
#include <strings.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <varargs.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#if defined (HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#include "gpsd.h"
#if !defined (INADDR_NONE)
#define INADDR_NONE ((in_addr_t)-1)
#endif
static char mbuf[128];
int passivesock(char *service, char *protocol, int qlen)
{
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type;
int one = 1;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
errexit(mbuf);
}
if ((ppe = getprotobyname(protocol)) == 0) {
sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
errexit(mbuf);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) == -1) {
sprintf(mbuf, "%s", "Error: SETSOCKOPT SO_REUSEADDR");
errexit(mbuf);
}
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
sprintf(mbuf, "Can't bind to port %s", service);
errexit(mbuf);
}
if (type == SOCK_STREAM && listen(s, qlen) < 0) {
sprintf(mbuf, "Can't listen on %s port:", service);
errexit(mbuf);
}
return s;
}
int passiveTCP(char *service, int qlen)
{
return passivesock(service, "tcp", qlen);
}
int connectsock(char *host, char *service, char *protocol)
{
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type;
bzero((char *) &sin, sizeof(sin));
sin.sin_family = AF_INET;
if ( (pse = getservbyname(service, protocol)) )
sin.sin_port = htons(ntohs((u_short) pse->s_port));
else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
errexit(mbuf);
}
if ( (phe = gethostbyname(host)) )
bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
sprintf(mbuf, "Can't get host entry: \"%s\".\n", host);
errexit(mbuf);
}
if ((ppe = getprotobyname(protocol)) == 0) {
sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
errexit(mbuf);
}
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
errexit("Can't create socket:");
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
sprintf(mbuf, "Can't connect to %s.%s", host, service);
errexit(mbuf);
}
return s;
}
int connectTCP(char *host, char *service)
{
return connectsock(host, service, "tcp");
}
|