summaryrefslogtreecommitdiff
path: root/src/util/gps_fwd.c
blob: 3270c48cb93af54b108b56e9e243f22b25718765 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* GPS/AIS client extractor, forwad to gpsd via UDP
 *
 * (C) 2013 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */ 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <stdint.h>
#include <netdb.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include <netinet/in.h>

#include <libgsmd/libgsmd.h>
#include <libgsmd/event.h>
#include <gsmd/usock.h>
#include <gsmd/ts0705.h>
#include <common/linux_list.h>

#ifndef __GSMD__
#define __GSMD__
#include <gsmd/talloc.h>
#undef __GSMD__
#endif

static int out_fd = -1;
static FILE *outf;

static int celllog_main(struct lgsm_handle *lgsmh)
{
	int rc;
	char buf[8192+1];
	fd_set readset;
	int gsm_fd = lgsm_fd(lgsmh);

	fcntl(gsm_fd, F_SETFD, O_NONBLOCK);
	FD_ZERO(&readset);

	while (1) {
		fd_set readset;
		FD_SET(gsm_fd, &readset);

		rc = select(gsm_fd+1, &readset, NULL, NULL, NULL);
		if (rc < 0)
			break;

		if (FD_ISSET(gsm_fd, &readset)) {
			/* we've received something on the gsmd socket, pass it
			 * on to the library */
			rc = read(gsm_fd, buf, sizeof(buf));
			if (rc <= 0) {
				syslog(LOG_ERR, "ERROR reading from gsm_fd\n");
				break;
			}
			rc = lgsm_handle_packet(lgsmh, buf, rc);
			if (rc < 0)
				syslog(LOG_ERR, "ERROR processing packet: %d(%s)\n", rc, strerror(-rc));
		}
		fflush(outf);
	}
	return 0;
}

static int gps_handler(struct lgsm_handle *lh, int evt, struct gsmd_evt_auxdata *aux)
{
	int rc;

	rc = fprintf(outf, "%s\r\n", aux->data);
	if (rc < 0) {
		syslog(LOG_ERR, "error %d during fprintf() on socket, aborting\n", rc);
		exit(3);
	}

	return 0;
}


/* more or less a copy form libosmocore, we could simply make this a
 * dependency... */
static int out_sock_init(uint16_t family, uint16_t type, uint8_t proto,
			 const char *host, uint16_t port)
{
	int fd = -1, rc;
	struct addrinfo hints, *result, *rp;
	char portbuf[16];

	sprintf(portbuf, "%u", port);
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;

	rc = getaddrinfo(host, portbuf, &hints, &result);
	if (rc != 0) {
		syslog(LOG_ERR, "Error during getaddrinfo(%s:%d)\n",
			host, port);
		return -EINVAL;
	}

	for (rp = result; rp != NULL; rp = rp->ai_next) {

		fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (fd < 0)
			continue;

		rc = connect(fd, rp->ai_addr, rp->ai_addrlen);
		if (rc != -1 || (rc == -1 && errno == EINPROGRESS))
			break;

		close(fd);
	}
	freeaddrinfo(result);

	if (rp == NULL) {
		syslog(LOG_ERR, "unable to connect socket\n");
		return -ENODEV;
	}

	return fd;
}

int main(int argc, char **argv)
{
	static struct lgsm_handle *lgsmh;
	char *host;
	int port;
	int rc;

	printf("gps_fwd - (C) 2013 by Harald Welte <laforge@gnumonks.org>.\n"
		"This program is Free Software and has ABSOLUTELY NO WARRANTY\n\n");

	if (argc < 3) {
		fprintf(stderr, "You have to specify the remote hostname + port\n");
		exit(2);
	}

	host = argv[1];
	port = atoi(argv[2]);

	openlog("gps_fwd", 0, LOG_LOCAL0);

	lgsmh = lgsm_init(LGSMD_DEVICE_GSMD);
	if (!lgsmh) {
		fprintf(stderr, "Can't connect to gsmd\n");
		exit(1);
	}

	lgsm_evt_handler_register(lgsmh, GSMD_EVT_GPS, &gps_handler);
	lgsm_evt_handler_register(lgsmh, GSMD_EVT_AIS, &gps_handler);

	out_fd = out_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port);
	if (out_fd < 0)
		exit(1);

	outf = fdopen(out_fd, "w");

	celllog_main(lgsmh);

	exit(0);
}
personal git repositories of Harald Welte. Your mileage may vary