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

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

added sisnet support

File size: 19.4 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.11 2005/04/27 10:31:12 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 * Changes - Version 0.11
63 * Jun 02 2005 Dirk Stoecker <soft@dstoecker.de>
64 * - added SISNeT support
65 * - added UDP support
66 * - cleanup of host and port handling
67 * - added inactivity alarm of 60 seconds
68 */
69
70#include <ctype.h>
71#include <errno.h>
72#include <fcntl.h>
73#include <getopt.h>
74#include <netdb.h>
75#include <signal.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79#include <unistd.h>
80#include <arpa/inet.h>
81#include <netinet/in.h>
82#include <sys/socket.h>
83#include <sys/termios.h>
84#include <sys/types.h>
85
86#ifndef MSG_DONTWAIT
87#define MSG_DONTWAIT 0 /* prevent compiler errors */
88#endif
89#ifndef O_EXLOCK
90#define O_EXLOCK 0 /* prevent compiler errors */
91#endif
92
93enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5 };
94
95#define VERSION "NTRIP NtripServerLinux/0.11"
96#define BUFSZ 1024
97
98/* default socket source */
99#define SERV_HOST_ADDR "127.0.0.1"
100#define SERV_TCP_PORT 1025
101
102/* default destination */
103#define NTRIP_CASTER "www.euref-ip.net"
104#define NTRIP_PORT 80
105
106/* default sisnet source */
107#define SISNET_SERVER "131.176.49.142"
108#define SISNET_PORT 7777
109
110#define ALARMTIME 60
111
112static int ttybaud = 19200;
113static const char *ttyport = "/dev/gps";
114static const char *filepath = "/dev/stdin";
115static enum MODE mode = INFILE;
116static int sisnetv3 = 0;
117static int gpsfd = -1;
118
119/* Forward references */
120static int openserial(const char * tty, int blocksz, int baud);
121static void send_receive_loop(int sock, int fd, int sisnet);
122static void usage(int);
123
124static void sighandler_alarm(/*int arg*/)
125{
126 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
127 exit(1);
128}
129
130/*
131* main
132*
133* Main entry point for the program. Processes command-line arguments and
134* prepares for action.
135*
136* Parameters:
137* argc : integer : Number of command-line arguments.
138* argv : array of char : Command-line arguments as an array of zero-terminated
139* pointers to strings.
140*
141* Return Value:
142* The function does not return a value (although its return type is int).
143*
144* Remarks:
145*
146*/
147
148int main(int argc, char **argv)
149{
150 int c;
151 int size = 2048; /* for setting send buffer size */
152
153 const char *inhost = 0;
154 const char *outhost = 0;
155 unsigned int outport = 0;
156 unsigned int inport = 0;
157 const char *mountpoint = NULL;
158 const char *password = "";
159 const char *sisnetpassword = "";
160 const char *sisnetuser = "";
161 const char *initfile = NULL;
162 int sock_id;
163 char szSendBuffer[BUFSZ];
164 int nBufferBytes;
165 struct hostent *he;
166 struct sockaddr_in addr;
167
168 signal(SIGALRM,sighandler_alarm);
169 alarm(ALARMTIME);
170 /* get and check program arguments */
171 if(argc <= 1)
172 {
173 usage(2);
174 exit(1);
175 }
176 while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:f:l:u:V:")) != EOF)
177 {
178 switch (c)
179 {
180 case 'M':
181 if(!strcmp(optarg, "serial")) mode = 1;
182 else if(!strcmp(optarg, "tcpsocket")) mode = 2;
183 else if(!strcmp(optarg, "file")) mode = 3;
184 else if(!strcmp(optarg, "sisnet")) mode = 4;
185 else if(!strcmp(optarg, "udpsocket")) mode = 5;
186 else mode = atoi(optarg);
187 if((mode == 0) || (mode > 5))
188 {
189 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
190 usage(-1);
191 }
192 break;
193 case 'i': /* gps serial ttyport */
194 ttyport = optarg;
195 break;
196 case 'V':
197 if(!strcmp("3.0", optarg)) sisnetv3 = 1;
198 else if(strcmp("2.1", optarg))
199 {
200 fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
201 usage(-2);
202 }
203 case 'b': /* serial ttyin speed */
204 ttybaud = atoi(optarg);
205 if(ttybaud <= 1)
206 {
207 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg);
208 usage(1);
209 }
210 break;
211 case 'a': /* http server IP address A.B.C.D */
212 outhost = optarg;
213 break;
214 case 'p': /* http server port */
215 outport = atoi(optarg);
216 if(outport <= 1 || outport > 65535)
217 {
218 fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",
219 optarg);
220 usage(1);
221 }
222 break;
223 case 'm': /* http server mountpoint */
224 mountpoint = optarg;
225 break;
226 case 's': /* datastream from file */
227 filepath = optarg;
228 break;
229 case 'f':
230 initfile = optarg;
231 break;
232 case 'u':
233 sisnetuser = optarg;
234 break;
235 case 'l':
236 sisnetpassword = optarg;
237 break;
238 case 'c': /* password */
239 password = optarg;
240 break;
241 case 'H': /* host */
242 inhost = optarg;
243 break;
244 case 'P': /* port */
245 inport = atoi(optarg);
246 if(inport <= 1 || inport > 65535)
247 {
248 fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
249 optarg);
250 usage(1);
251 }
252 break;
253 case 'h': /* help */
254 case '?':
255 usage(0);
256 break;
257 default:
258 usage(2);
259 break;
260 }
261 }
262
263 argc -= optind;
264 argv += optind;
265
266 if(argc > 0)
267 {
268 fprintf(stderr, "ERROR: Extra args on command line: ");
269 for(; argc > 0; argc--)
270 {
271 fprintf(stderr, " %s", *argv++);
272 }
273 fprintf(stderr, "\n");
274 usage(1); /* never returns */
275 }
276
277 if(mountpoint == NULL)
278 {
279 fprintf(stderr, "ERROR: Missing mountpoint argument\n");
280 exit(1);
281 }
282 if(!password[0])
283 {
284 fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n");
285 }
286
287 if(!outhost) outhost = NTRIP_CASTER;
288 if(!outport) outport = NTRIP_PORT;
289
290 switch(mode)
291 {
292 case INFILE:
293 {
294 gpsfd = open(filepath, O_RDONLY);
295 if(!gpsfd)
296 {
297 perror("ERROR: opening input file");
298 exit(1);
299 }
300 /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */
301 fcntl(gpsfd, F_SETFL, 0);
302 printf("file input: file = %s\n", filepath);
303 }
304 break;
305 case SERIAL: /* open serial port */
306 {
307 gpsfd = openserial(ttyport, 1, ttybaud);
308 if(gpsfd < 0)
309 {
310 exit(1);
311 }
312 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
313 }
314 break;
315 case TCPSOCKET: case UDPSOCKET: case SISNET:
316 {
317 if(mode == SISNET)
318 {
319 if(!inhost) inhost = SISNET_SERVER;
320 if(!inport) inport = SISNET_PORT;
321 }
322 else
323 {
324 if(!inport) inport = SERV_TCP_PORT;
325 if(!inhost) inhost = "127.0.0.1";
326 }
327
328 if(!(he = gethostbyname(inhost)))
329 {
330 fprintf(stderr, "ERROR: host %s unknown\n", inhost);
331 usage(-2);
332 }
333
334 if((gpsfd = socket(AF_INET, mode == UDPSOCKET ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
335 {
336 fprintf(stderr, "ERROR: can't create socket\n");
337 exit(1);
338 }
339
340 memset((char *) &addr, 0x00, sizeof(addr));
341 memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
342 addr.sin_family = AF_INET;
343 addr.sin_port = htons(inport);
344
345 printf("%s input: host = %s, port = %d%s%s\n", mode == SISNET ? "sisnet"
346 : "socket",
347 inet_ntoa(addr.sin_addr), inport, initfile ? ", initfile = " : "",
348 initfile ? initfile : "");
349
350 if(connect(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
351 {
352 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
353 inet_ntoa(addr.sin_addr), inport);
354 exit(1);
355 }
356 if(initfile && mode != SISNET)
357 {
358 char buffer[1024];
359 FILE *fh;
360 int i;
361
362 if((fh = fopen(initfile, "r")))
363 {
364 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
365 {
366 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
367 {
368 perror("ERROR: sending init file");
369 exit(1);
370 }
371 }
372 if(i < 0)
373 {
374 perror("ERROR: reading init file");
375 exit(1);
376 }
377 fclose(fh);
378 }
379 else
380 {
381 fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
382 exit(1);
383 }
384 }
385 }
386 if(mode == SISNET)
387 {
388 int i, j;
389 char buffer[1024];
390
391 i = snprintf(buffer, sizeof(buffer), sisnetv3 ? "AUTH,%s,%s\r\n" : "AUTH,%s,%s",
392 sisnetuser,sisnetpassword);
393 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
394 {
395 perror("ERROR: sending authentication");
396 exit(1);
397 }
398 i = sisnetv3 ? 7 : 5;
399 if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
400 {
401 fprintf(stderr, "ERROR: SISNeT connect failed:");
402 for(i = 0; i < j; ++i)
403 {
404 if(buffer[i] != '\r' && buffer[i] != '\n')
405 {
406 fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
407 }
408 }
409 fprintf(stderr, "\n");
410 exit(1);
411 }
412 }
413 break;
414 default:
415 usage(-1);
416 break;
417 }
418
419 /* ----- main part ----- */
420 while(1)
421 {
422 if(!(he = gethostbyname(outhost)))
423 {
424 fprintf(stderr, "ERROR: host %s unknown\n", outhost);
425 usage(-2);
426 }
427
428 /* create socket */
429 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
430 {
431 fprintf(stderr, "ERROR: could not create socket\n");
432 exit(2);
433 }
434
435 memset((char *) &addr, 0x00, sizeof(addr));
436 memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
437 addr.sin_family = AF_INET;
438 addr.sin_port = htons(outport);
439
440 /* connect to caster */
441 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
442 inet_ntoa(addr.sin_addr), outport, mountpoint);
443 if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
444 {
445 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
446 inet_ntoa(addr.sin_addr), outport);
447 close(sock_id);
448 exit(3);
449 }
450
451 /* set socket buffer size */
452 setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
453 sizeof(const char *));
454 /* send message to caster */
455 szSendBuffer[0] = '\0';
456 sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
457 strcat(szSendBuffer, "Source-Agent: ");
458 strcat(szSendBuffer, VERSION);
459 strcat(szSendBuffer, "\r\n");
460 strcat(szSendBuffer, "\r\n");
461 strcat(szSendBuffer, "\0");
462 nBufferBytes = strlen(szSendBuffer);
463 if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
464 {
465 fprintf(stderr, "ERROR: could not send to caster\n");
466 close(sock_id);
467 sleep(5);
468 exit(0);
469 }
470 /* check caster's response */
471 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
472 szSendBuffer[nBufferBytes] = '\0';
473 if(strcmp(szSendBuffer, "OK\r\n"))
474 {
475 char *a;
476 fprintf(stderr, "ERROR: caster's reply is not OK : ");
477 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
478 {
479 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
480 }
481 fprintf(stderr, "\n");
482 close(sock_id);
483 sleep(5);
484 exit(0);
485 }
486 printf("connection succesfull\n");
487 send_receive_loop(sock_id, gpsfd, mode == SISNET);
488 }
489 exit(0);
490}
491
492static void send_receive_loop(int sock, int fd, int sisnet)
493{
494 char buffer[BUFSZ] = { 0 };
495 char sisnetbackbuffer[200];
496 int nBufferBytes = 0, i;
497 /* data transmission */
498 printf("transfering data ...\n");
499 while(1)
500 {
501 alarm(ALARMTIME);
502
503 if(!nBufferBytes)
504 {
505 if(sisnet)
506 {
507 int i;
508 /* a somewhat higher rate than 1 second to get really each block */
509 /* means we need to skip double blocks sometimes */
510 struct timeval tv = {0,700000};
511 select(0, 0, 0, 0, &tv);
512 memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
513 i = (sisnetv3 ? 5 : 3);
514 if((send(gpsfd, "MSG\r\n", i, 0)) != i)
515 {
516 perror("ERROR: sending data request");
517 exit(1);
518 }
519 }
520 /* receiving data */
521 nBufferBytes = read(fd, buffer, BUFSZ);
522 if(!nBufferBytes)
523 {
524 printf("WARNING: no data received from input\n");
525 continue;
526 }
527 else if(nBufferBytes < 0)
528 {
529 perror("ERROR: reading input failed");
530 exit(1);
531 }
532 /* we can compare the whole buffer, as the additional bytes remain unchanged */
533 if(!memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
534 {
535 nBufferBytes = 0;
536 }
537 }
538 if(nBufferBytes)
539 {
540 /* send data */
541 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
542 != nBufferBytes)
543 {
544 if(i < 0 && errno != EAGAIN)
545 {
546 perror("WARNING: could not send data - retry connection");
547 close(sock);
548 sleep(5);
549 return;
550 }
551 else if(i)
552 {
553 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
554 nBufferBytes -= i;
555 }
556 }
557 else
558 {
559 nBufferBytes = 0;
560 }
561 }
562 }
563}
564
565/*
566 * openserial
567 *
568 * Open the serial port with the given device name and configure it for
569 * reading NMEA data from a GPS receiver.
570 *
571 * Parameters:
572 * tty : pointer to : A zero-terminated string containing the device
573 * unsigned char name of the appropriate serial port.
574 * blocksz : integer : Block size for port I/O
575 * baud : integer : Baud rate for port I/O
576 *
577 * Return Value:
578 * The function returns a file descriptor for the opened port if successful.
579 * The function returns -1 in the event of an error.
580 *
581 * Remarks:
582 *
583 */
584
585static int openserial(const char * tty, int blocksz, int baud)
586{
587 int fd;
588 struct termios termios;
589
590 fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
591 if(fd < 0)
592 {
593 perror("ERROR: opening serial connection");
594 return (-1);
595 }
596 if(tcgetattr(fd, &termios) < 0)
597 {
598 perror("ERROR: get serial attributes");
599 return (-1);
600 }
601 termios.c_iflag = 0;
602 termios.c_oflag = 0; /* (ONLRET) */
603 termios.c_cflag = CS8 | CLOCAL | CREAD;
604 termios.c_lflag = 0;
605 {
606 int cnt;
607 for(cnt = 0; cnt < NCCS; cnt++)
608 termios.c_cc[cnt] = -1;
609 }
610 termios.c_cc[VMIN] = blocksz;
611 termios.c_cc[VTIME] = 2;
612
613#if (B4800 != 4800)
614 /*
615 * Not every system has speed settings equal to absolute speed value.
616 */
617
618 switch (baud)
619 {
620 case 300:
621 baud = B300;
622 break;
623 case 1200:
624 baud = B1200;
625 break;
626 case 2400:
627 baud = B2400;
628 break;
629 case 4800:
630 baud = B4800;
631 break;
632 case 9600:
633 baud = B9600;
634 break;
635 case 19200:
636 baud = B19200;
637 break;
638 case 38400:
639 baud = B38400;
640 break;
641#ifdef B57600
642 case 57600:
643 baud = B57600;
644 break;
645#endif
646#ifdef B115200
647 case 115200:
648 baud = B115200;
649 break;
650#endif
651#ifdef B230400
652 case 230400:
653 baud = B230400;
654 break;
655#endif
656 default:
657 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
658 baud = B19200;
659 break;
660 }
661#endif
662
663 if(cfsetispeed(&termios, baud) != 0)
664 {
665 perror("ERROR: setting serial speed with cfsetispeed");
666 return (-1);
667 }
668 if(cfsetospeed(&termios, baud) != 0)
669 {
670 perror("ERROR: setting serial speed with cfsetospeed");
671 return (-1);
672 }
673 if(tcsetattr(fd, TCSANOW, &termios) < 0)
674 {
675 perror("ERROR: setting serial attributes");
676 return (-1);
677 }
678 if(fcntl(fd, F_SETFL, 0) == -1)
679 {
680 perror("WARNING: setting blocking mode failed");
681 }
682 return (fd);
683}
684
685/*
686 * usage
687 *
688 * Send a usage message to standard error and quit the program.
689 *
690 * Parameters:
691 * None.
692 *
693 * Return Value:
694 * The function does not return a value.
695 *
696 * Remarks:
697 *
698 */
699
700static void usage(int rc)
701{
702 fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
703 fprintf(stderr, " Options are:\n");
704 fprintf(stderr, " -a caster name or address (default: %s)\n",
705 NTRIP_CASTER);
706 fprintf(stderr, " -p caster port (default: %d)\n", NTRIP_PORT);
707 fprintf(stderr, " -m caster mountpoint\n");
708 fprintf(stderr, " -c password for caster login\n");
709 fprintf(stderr, " -h|? print this help screen\n");
710 fprintf(stderr, " -M <mode> sets the mode\n");
711 fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file, 4=sisnet, 5=udpsocket)\n");
712 fprintf(stderr, " Mode = file:\n");
713 fprintf(stderr, " -s file, simulate data stream by reading log file\n");
714 fprintf(stderr, " default/current setting is %s\n", filepath);
715 fprintf(stderr, " Mode = serial:\n");
716 fprintf(stderr, " -b baud_rate, sets serial input baud rate\n");
717 fprintf(stderr, " default/current value is %d\n", ttybaud);
718 fprintf(stderr, " -i input_device, sets name of serial input device\n");
719 fprintf(stderr, " default/current value is %s\n", ttyport);
720 fprintf(stderr, " (normally a symbolic link to /dev/tty\?\?)\n");
721 fprintf(stderr, " Mode = tcpsocket or udpsocket:\n");
722 fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT);
723 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", SERV_HOST_ADDR);
724 fprintf(stderr, " -f initfile send to server\n");
725 fprintf(stderr, " Mode = sisnet:\n");
726 fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT);
727 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", SISNET_SERVER);
728 fprintf(stderr, " -u username\n");
729 fprintf(stderr, " -l password\n");
730 fprintf(stderr, " -V version [2.1 or 3.0] (default: 2.1)\n");
731 fprintf(stderr, "\n");
732 exit(rc);
733}
Note: See TracBrowser for help on using the repository browser.