~ubuntu-branches/ubuntu/maverick/gpsdrive/maverick

« back to all changes in this revision

Viewing changes to src/netlib.c

  • Committer: Bazaar Package Importer
  • Author(s): Frank Kirschner
  • Date: 2004-05-25 11:44:03 UTC
  • Revision ID: james.westby@ubuntu.com-20040525114403-j3rsu57cavfax6z8
Tags: upstream-2.09
ImportĀ upstreamĀ versionĀ 2.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
$Log: netlib.c,v $
 
3
Revision 1.4  2003/01/23 14:05:35  ganter
 
4
added greek translation
 
5
added geocache scripts
 
6
added geocache icon
 
7
improved search for libmysqlclient.so
 
8
 
 
9
Revision 1.3  2002/07/14 18:22:19  ganter
 
10
v1.25pre1
 
11
 
 
12
Revision 1.2  2002/04/06 00:36:01  ganter
 
13
changing filelist
 
14
 
 
15
*/
 
16
 
 
17
#include "config.h"
 
18
#include <stdlib.h>
 
19
#include <string.h>
 
20
 
 
21
#if defined (HAVE_STRINGS_H)
 
22
#include <strings.h>
 
23
#endif
 
24
 
 
25
#include <sys/types.h>
 
26
#include <sys/socket.h>
 
27
#include <sys/time.h>
 
28
#include <netinet/in.h>
 
29
#include <stdarg.h>
 
30
#include <netdb.h>
 
31
#include <stdio.h>
 
32
#include <arpa/inet.h>
 
33
 
 
34
#if defined (HAVE_SYS_PARAM_H)
 
35
#include <sys/param.h>
 
36
#endif
 
37
 
 
38
#include "gpsd.h"
 
39
 
 
40
 
 
41
#if !defined (INADDR_NONE)
 
42
#define INADDR_NONE   ((in_addr_t)-1)
 
43
#endif
 
44
 
 
45
static char mbuf[128];
 
46
 
 
47
int passivesock(char *service, char *protocol, int qlen)
 
48
{
 
49
    struct servent *pse;
 
50
    struct protoent *ppe;
 
51
    struct sockaddr_in sin;
 
52
    int s, type, i;
 
53
 
 
54
    bzero((char *) &sin, sizeof(sin));
 
55
    sin.sin_family = AF_INET;
 
56
    sin.sin_addr.s_addr = INADDR_ANY;
 
57
 
 
58
    if ( (pse = getservbyname(service, protocol)) )
 
59
        sin.sin_port = htons(ntohs((u_short) pse->s_port));
 
60
    else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
 
61
        sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
 
62
        errexit(mbuf);
 
63
    }
 
64
    if ((ppe = getprotobyname(protocol)) == 0) {
 
65
        sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
 
66
        errexit(mbuf);
 
67
    }
 
68
    if (strcmp(protocol, "udp") == 0)
 
69
        type = SOCK_DGRAM;
 
70
    else
 
71
        type = SOCK_STREAM;
 
72
 
 
73
    s = socket(PF_INET, type, ppe->p_proto);
 
74
    if (s < 0)
 
75
        errexit("Can't create socket:");
 
76
 
 
77
    i = 1;
 
78
    if (setsockopt(s,  SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) {
 
79
       sprintf(mbuf, "Can't set SO_REUSEADDR, port %s", service);
 
80
       errexit(mbuf);
 
81
    }
 
82
 
 
83
    if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
 
84
        sprintf(mbuf, "Can't bind to port %s", service);
 
85
        errexit(mbuf);
 
86
    }
 
87
    if (type == SOCK_STREAM && listen(s, qlen) < 0) {
 
88
        sprintf(mbuf, "Can't listen on %s port:", service);
 
89
        errexit(mbuf);
 
90
    }
 
91
    return s;
 
92
}
 
93
 
 
94
int passiveTCP(char *service, int qlen)
 
95
{
 
96
    return passivesock(service, "tcp", qlen);
 
97
}
 
98
 
 
99
 
 
100
int connectsock(char *host, char *service, char *protocol)
 
101
{
 
102
    struct hostent *phe;
 
103
    struct servent *pse;
 
104
    struct protoent *ppe;
 
105
    struct sockaddr_in sin;
 
106
    int s, type;
 
107
 
 
108
    bzero((char *) &sin, sizeof(sin));
 
109
    sin.sin_family = AF_INET;
 
110
 
 
111
    if ( (pse = getservbyname(service, protocol)) )
 
112
        sin.sin_port = htons(ntohs((u_short) pse->s_port));
 
113
    else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
 
114
        sprintf(mbuf, "Can't get \"%s\" service entry.\n", service);
 
115
        errexit(mbuf);
 
116
    }
 
117
    if ( (phe = gethostbyname(host)) )
 
118
        bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
 
119
    else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
 
120
        sprintf(mbuf, "Can't get host entry: \"%s\".\n", host);
 
121
        errexit(mbuf);
 
122
    }
 
123
    if ((ppe = getprotobyname(protocol)) == 0) {
 
124
        sprintf(mbuf, "Can't get \"%s\" protocol entry.\n", protocol);
 
125
        errexit(mbuf);
 
126
    }
 
127
    if (strcmp(protocol, "udp") == 0)
 
128
        type = SOCK_DGRAM;
 
129
    else
 
130
        type = SOCK_STREAM;
 
131
 
 
132
    s = socket(PF_INET, type, ppe->p_proto);
 
133
    if (s < 0)
 
134
        errexit("Can't create socket:");
 
135
 
 
136
    if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
 
137
        sprintf(mbuf, "Can't connect to %s.%s", host, service);
 
138
        errexit(mbuf);
 
139
    }
 
140
    return s;
 
141
}
 
142
 
 
143
int connectTCP(char *host, char *service)
 
144
{
 
145
    return connectsock(host, service, "tcp");
 
146
}