source: ntrip/trunk/ntripserver/NtripLinuxServer.c@ 18

Last change on this file since 18 was 18, checked in by stoecker, 19 years ago

larger rework

File size: 15.3 KB
Line 
1/*
2 * NtripServerLinux.c
3 *
4 * Copyright (c) 2003...2005
5 * German Federal Agency for Cartography and Geodesy (BKG)
6 *
7 * Developed for Networked Transport of RTCM via Internet Protocol (NTRIP)
8 * for streaming GNSS data over the Internet.
9 *
10 * Designed by Informatik Centrum Dortmund http://www.icd.de
11 *
12 * NTRIP is currently an experimental technology.
13 * The BKG disclaims any liability nor responsibility to any person or
14 * entity with respect to any loss or damage caused, or alleged to be
15 * caused, directly or indirectly by the use and application of the NTRIP
16 * technology.
17 *
18 * For latest information and updates, access:
19 * http://igs.ifag.de/index_ntrip.htm
20 *
21 * Georg Weber
22 * BKG, Frankfurt, Germany, June 2003-06-13
23 * E-mail: euref-ip@bkg.bund.de
24 *
25 * Based on the GNU General Public License published nmead
26 *
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version 2
30 * of the License, or (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
40 * USA.
41 */
42
43/* $Id: NtripLinuxServer.c,v 1.8 2005/03/21 13:02:47 stoecker Exp $
44 * Changes - Version 0.7
45 * Sep 22 2003 Steffen Tschirpke <St.Tschirpke@actina.de>
46 * - socket support
47 * - command line option handling
48 * - error handling
49 * - help screen
50 *
51 * Changes - Version 0.9
52 * Feb 15 2005 Dirk Stoecker <soft@dstoecker.de>
53 * - some minor updates, fixed serial baudrate settings
54 *
55 * Changes - Version 0.10
56 * Apr 05 2005 Dirk Stoecker <soft@dstoecker.de>
57 * - some cleanup and miscellaneous fixes
58 * - replaced non-working simulate with file input (stdin)
59 * - TCP sending now somewhat more stable
60 * - cleanup of error handling
61 */
62
63#include <ctype.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <getopt.h>
67#include <netdb.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <unistd.h>
72#include <arpa/inet.h>
73#include <netinet/in.h>
74#include <sys/socket.h>
75#include <sys/termios.h>
76#include <sys/types.h>
77
78#ifndef MSG_DONTWAIT
79#define MSG_DONTWAIT 0 /* prevent compiler errors */
80#endif
81#ifndef O_EXLOCK
82#define O_EXLOCK 0 /* prevent compiler errors */
83#endif
84
85enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3 };
86
87#define VERSION "NTRIP NtripServerLinux/0.10"
88#define BUFSZ 1024
89
90/* default socket source */
91#define SERV_HOST_ADDR "127.0.0.1"
92#define SERV_TCP_PORT 1025
93
94/* default destination */
95#define NTRIP_CASTER "www.euref-ip.net"
96#define NTRIP_PORT 80
97
98int ttybaud = 19200;
99char *ttyport = "/dev/gps";
100char *filepath = "/dev/stdin";
101enum MODE mode = INFILE;
102
103/* Forward references */
104int openserial(u_char * tty, int blocksz, int ttybaud);
105void send_receive_loop(int socket, int fd);
106void usage(int);
107
108/*
109* main
110*
111* Main entry point for the program. Processes command-line arguments and
112* prepares for action.
113*
114* Parameters:
115* argc : integer : Number of command-line arguments.
116* argv : array of char : Command-line arguments as an array of zero-terminated
117* pointers to strings.
118*
119* Return Value:
120* The function does not return a value (although its return type is int).
121*
122* Remarks:
123*
124*/
125
126int main(int argc, char **argv)
127{
128 u_char *ttyin = ttyport;
129 int c, gpsfd = -1;
130 int size = 2048; /* for setting send buffer size */
131
132 unsigned int out_port = 0;
133 unsigned int in_port = 0;
134 char *mountpoint = NULL;
135 char *password = "";
136 int sock_id;
137 char szSendBuffer[BUFSZ];
138 int nBufferBytes;
139
140 struct hostent *inhost;
141 struct hostent *outhost;
142
143 struct sockaddr_in in_addr;
144 struct sockaddr_in out_addr;
145
146 if(!(outhost = gethostbyname(NTRIP_CASTER)))
147 {
148 fprintf(stderr, "WARNING: host %s unknown\n", NTRIP_CASTER);
149 }
150 else
151 {
152 memset((char *) &out_addr, 0x00, sizeof(out_addr));
153 memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
154 }
155
156 /* get and check program arguments */
157 if(argc <= 1)
158 {
159 usage(2);
160 exit(1);
161 }
162 while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:")) != EOF)
163 {
164 switch (c)
165 {
166 case 'M':
167 mode = atoi(optarg);
168 if((mode == 0) || (mode > 3))
169 {
170 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
171 usage(-1);
172 }
173 break;
174 case 'i': /* gps serial ttyin */
175 ttyin = optarg;
176 break;
177 case 'b': /* serial ttyin speed */
178 ttybaud = atoi(optarg);
179 if(ttybaud <= 1)
180 {
181 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg);
182 usage(1);
183 }
184 break;
185 case 'a': /* http server IP address A.B.C.D */
186 outhost = gethostbyname(optarg);
187 if(outhost == NULL)
188 {
189 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
190 usage(-2);
191 }
192 memset((char *) &out_addr, 0x00, sizeof(out_addr));
193 memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
194 break;
195 case 'p': /* http server port */
196 out_port = atoi(optarg);
197 if(out_port <= 1)
198 {
199 fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",
200 optarg);
201 usage(1);
202 }
203 break;
204 case 'm': /* http server mountpoint */
205 mountpoint = optarg;
206 break;
207 case 'f': /* datastream from file */
208 filepath = optarg;
209 break;
210 case 'c': /* password */
211 password = optarg;
212 break;
213 case 'H': /* host */
214 inhost = gethostbyname(optarg);
215 if(inhost == NULL)
216 {
217 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
218 usage(-2);
219 }
220 memset((char *) &in_addr, 0x00, sizeof(in_addr));
221 memcpy(&in_addr.sin_addr, inhost->h_addr, inhost->h_length);
222 break;
223 case 'P': /* port */
224 in_port = atoi(optarg);
225 if(in_port <= 1)
226 {
227 fprintf(stderr, "ERROR: can't convert %s to a valid receiver port\n",
228 optarg);
229 usage(1);
230 }
231 break;
232 case 'h': /* help */
233 case '?':
234 usage(0);
235 break;
236 default:
237 usage(2);
238 break;
239 }
240
241 if(in_port <= 0)
242 {
243 in_port = SERV_TCP_PORT;
244 }
245 if(out_port <= 0)
246 {
247 out_port = NTRIP_PORT;
248 }
249 }
250
251 argc -= optind;
252 argv += optind;
253
254 if(argc > 0)
255 {
256 fprintf(stderr, "ERROR: Extra args on command line: ");
257 for(; argc > 0; argc--)
258 {
259 fprintf(stderr, " %s", *argv++);
260 }
261 fprintf(stderr, "\n");
262 usage(1); /* never returns */
263 }
264
265 if(mountpoint == NULL)
266 {
267 fprintf(stderr, "ERROR: Missing mountpoint argument\n");
268 exit(1);
269 }
270 if(!password[0])
271 {
272 fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n");
273 }
274
275 switch (mode)
276 {
277 case INFILE:
278 {
279 gpsfd = open(filepath, O_RDONLY);
280 if(!gpsfd)
281 {
282 perror("ERROR: opening input file");
283 exit(1);
284 }
285 /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */
286 fcntl(gpsfd, F_SETFL, 0);
287 printf("file input: file = %s\n", filepath);
288 }
289 break;
290 case SERIAL: /* open serial port */
291 {
292 gpsfd = openserial(ttyin, 1, ttybaud);
293 if(gpsfd < 0)
294 {
295 exit(1);
296 }
297 printf("serial input: device = %s, speed = %d\n", ttyin, ttybaud);
298 }
299 break;
300 case TCPSOCKET:
301 {
302 in_addr.sin_family = AF_INET;
303 in_addr.sin_port = htons(in_port);
304
305 if((gpsfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
306 {
307 fprintf(stderr, "ERROR: can't create socket\n");
308 exit(1);
309 }
310
311 printf("socket input: host = %s, port = %d\n",
312 inet_ntoa(in_addr.sin_addr), in_port);
313
314 if(connect(gpsfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) < 0)
315 {
316 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
317 inet_ntoa(in_addr.sin_addr), in_port);
318 exit(1);
319 }
320 }
321 break;
322 default:
323 usage(-1);
324 break;
325 }
326
327 out_addr.sin_family = AF_INET;
328 out_addr.sin_port = htons((u_short) (out_port));
329
330 /* ----- main part ----- */
331 while(1)
332 {
333 /* create socket */
334 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
335 {
336 fprintf(stderr, "ERROR : could not create socket\n");
337 exit(2);
338 }
339 /* connect to caster */
340 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
341 inet_ntoa(out_addr.sin_addr), out_port, mountpoint);
342 if(connect(sock_id, (struct sockaddr *) &out_addr, sizeof(out_addr)) < 0)
343 {
344 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
345 inet_ntoa(out_addr.sin_addr), out_port);
346 close(sock_id);
347 exit(3);
348 }
349
350 /* set socket buffer size */
351 setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
352 sizeof(const char *));
353 /* send message to caster */
354 szSendBuffer[0] = '\0';
355 sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
356 strcat(szSendBuffer, "Source-Agent: ");
357 strcat(szSendBuffer, VERSION);
358 strcat(szSendBuffer, "\r\n");
359 strcat(szSendBuffer, "\r\n");
360 strcat(szSendBuffer, "\0");
361 nBufferBytes = strlen(szSendBuffer);
362 if((send(sock_id, szSendBuffer, nBufferBytes, 0)) != nBufferBytes)
363 {
364 fprintf(stderr, "ERROR: could not send to caster\n");
365 close(sock_id);
366 sleep(5);
367 exit(0);
368 }
369 /* check caster's response */
370 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
371 szSendBuffer[nBufferBytes] = '\0';
372 if(strcmp(szSendBuffer, "OK\r\n"))
373 {
374 char *a;
375 fprintf(stderr, "ERROR: caster's reply is not OK : ");
376 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
377 {
378 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
379 }
380 fprintf(stderr, "\n");
381 close(sock_id);
382 sleep(5);
383 exit(0);
384 }
385 printf("connection succesfull\n");
386 send_receive_loop(sock_id, gpsfd);
387 }
388 exit(0);
389}
390
391void send_receive_loop(int socket, int fd)
392{
393 char buffer[BUFSZ] = { 0 };
394 int nBufferBytes = 0, i;
395 /* data transmission */
396 printf("transfering data ...\n");
397 while(1)
398 {
399 if(!nBufferBytes)
400 {
401 /* receiving data */
402 nBufferBytes = read(fd, buffer, BUFSZ);
403 if(!nBufferBytes)
404 {
405 printf("WARNING: no data received from input\n");
406 continue;
407 }
408 else if(nBufferBytes < 0)
409 {
410 perror("ERROR: reading input failed");
411 exit(1);
412 }
413 }
414 /* send data */
415 if((i = send(socket, buffer, nBufferBytes, MSG_DONTWAIT)) != nBufferBytes)
416 {
417 if(i < 0 && errno != EAGAIN)
418 {
419 perror("WARNING: could not send data - retry connection");
420 close(socket);
421 sleep(5);
422 return;
423 }
424 else if(i)
425 {
426 memmove(buffer, buffer+i, nBufferBytes-i);
427 nBufferBytes -= i;
428 }
429 }
430 else
431 {
432 nBufferBytes = 0;
433 }
434 }
435}
436
437/*
438 * openserial
439 *
440 * Open the serial port with the given device name and configure it for
441 * reading NMEA data from a GPS receiver.
442 *
443 * Parameters:
444 * tty : pointer to : A zero-terminated string containing the device
445 * unsigned char name of the appropriate serial port.
446 * blocksz : integer : Block size for port I/O
447 * ttybaud : integer : Baud rate for port I/O
448 *
449 * Return Value:
450 * The function returns a file descriptor for the opened port if successful.
451 * The function returns -1 in the event of an error.
452 *
453 * Remarks:
454 *
455 */
456
457int openserial(u_char * tty, int blocksz, int ttybaud)
458{
459 int fd;
460 struct termios termios;
461
462 fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
463 if(fd < 0)
464 {
465 perror("ERROR: opening serial connection");
466 return (-1);
467 }
468 if(tcgetattr(fd, &termios) < 0)
469 {
470 perror("ERROR: get serial attributes");
471 return (-1);
472 }
473 termios.c_iflag = 0;
474 termios.c_oflag = 0; /* (ONLRET) */
475 termios.c_cflag = CS8 | CLOCAL | CREAD;
476 termios.c_lflag = 0;
477 {
478 int cnt;
479 for(cnt = 0; cnt < NCCS; cnt++)
480 termios.c_cc[cnt] = -1;
481 }
482 termios.c_cc[VMIN] = blocksz;
483 termios.c_cc[VTIME] = 2;
484
485#if (B4800 != 4800)
486 /*
487 * Not every system has speed settings equal to absolute speed value.
488 */
489
490 switch (ttybaud)
491 {
492 case 300:
493 ttybaud = B300;
494 break;
495 case 1200:
496 ttybaud = B1200;
497 break;
498 case 2400:
499 ttybaud = B2400;
500 break;
501 case 4800:
502 ttybaud = B4800;
503 break;
504 case 9600:
505 ttybaud = B9600;
506 break;
507 case 19200:
508 ttybaud = B19200;
509 break;
510 case 38400:
511 ttybaud = B38400;
512 break;
513#ifdef B57600
514 case 57600:
515 ttybaud = B57600;
516 break;
517#endif
518#ifdef B115200
519 case 115200:
520 ttybaud = B115200;
521 break;
522#endif
523#ifdef B230400
524 case 230400:
525 ttybaud = B230400;
526 break;
527#endif
528 default:
529 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
530 ttybaud = B19200;
531 break;
532 }
533#endif
534
535 if(cfsetispeed(&termios, ttybaud) != 0)
536 {
537 perror("ERROR: setting serial speed with cfsetispeed");
538 return (-1);
539 }
540 if(cfsetospeed(&termios, ttybaud) != 0)
541 {
542 perror("ERROR: setting serial speed with cfsetospeed");
543 return (-1);
544 }
545 if(tcsetattr(fd, TCSANOW, &termios) < 0)
546 {
547 perror("ERROR: setting serial attributes");
548 return (-1);
549 }
550 if(fcntl(fd, F_SETFL, 0) == -1)
551 {
552 perror("WARNING: setting blocking mode failed");
553 }
554 return (fd);
555}
556
557/*
558 * usage
559 *
560 * Send a usage message to standard error and quit the program.
561 *
562 * Parameters:
563 * None.
564 *
565 * Return Value:
566 * The function does not return a value.
567 *
568 * Remarks:
569 *
570 */
571
572void usage(int rc)
573{
574 fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
575 fprintf(stderr, " Options are:\n");
576 fprintf(stderr, " -a caster name or address (default: %s)\n",
577 NTRIP_CASTER);
578 fprintf(stderr, " -p caster port (default: %d)\n", NTRIP_PORT);
579 fprintf(stderr, " -m caster mountpoint\n");
580 fprintf(stderr, " -c password for caster login\n");
581 fprintf(stderr, " -h|? print this help screen\n");
582 fprintf(stderr, " -M <mode> sets the mode\n");
583 fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file)\n");
584 fprintf(stderr, " Mode = file:\n");
585 fprintf(stderr, " -s file, simulate data stream by reading log file\n");
586 fprintf(stderr, " default/current setting is %s\n", filepath);
587 fprintf(stderr, " Mode = serial:\n");
588 fprintf(stderr, " -b baud_rate, sets serial input baud rate\n");
589 fprintf(stderr, " default/current value is %d\n", ttybaud);
590 fprintf(stderr, " -i input_device, sets name of serial input device\n");
591 fprintf(stderr, " default/current value is %s\n", ttyport);
592 fprintf(stderr, " (normally a symbolic link to /dev/tty\?\?)\n");
593 fprintf(stderr, " Mode = tcpsocket:\n");
594 fprintf(stderr, " -P receiver port (default: 1025)\n");
595 fprintf(stderr, " -H hostname of TCP server (default: 127.0.0.1)\n");
596 fprintf(stderr, " \n");
597 exit(rc);
598}
Note: See TracBrowser for help on using the repository browser.