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

Last change on this file since 469 was 469, checked in by stoecker, 17 years ago

cleanup and tests

File size: 52.5 KB
Line 
1/*
2 * $Id: NtripLinuxServer.c,v 1.29 2007/06/06 12:16:08 stoecker Exp $
3 *
4 * Copyright (c) 2003...2007
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 * The BKG disclaims any liability nor responsibility to any person or
13 * entity with respect to any loss or damage caused, or alleged to be
14 * caused, directly or indirectly by the use and application of the NTRIP
15 * technology.
16 *
17 * For latest information and updates, access:
18 * http://igs.bkg.bund.de/index_ntrip_down.htm
19 *
20 * BKG, Frankfurt, Germany, February 2007
21 * E-mail: euref-ip@bkg.bund.de
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
37
38/* CVS revision and version */
39static char revisionstr[] = "$Revision: 1.29 $";
40static char datestr[] = "$Date: 2007/06/06 12:16:08 $";
41
42#include <ctype.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <getopt.h>
46#include <netdb.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include <arpa/inet.h>
53#include <netinet/in.h>
54#include <sys/socket.h>
55#include <sys/termios.h>
56#include <sys/types.h>
57#include <sys/time.h>
58
59#ifndef COMPILEDATE
60#define COMPILEDATE " built " __DATE__
61#endif
62
63#ifndef MSG_DONTWAIT
64#define MSG_DONTWAIT 0 /* prevent compiler errors */
65#endif
66#ifndef O_EXLOCK
67#define O_EXLOCK 0 /* prevent compiler errors */
68#endif
69
70enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
71CASTER = 6, LAST };
72
73enum OUTMODE { HTTP = 1, RTSP = 2, NTRIPV1 = 3, END };
74
75#define AGENTSTRING "NTRIP NtripServerLinux"
76#define BUFSZ 1024
77#define SZ 64
78
79/* default socket source */
80#define SERV_HOST_ADDR "localhost"
81#define SERV_TCP_PORT 2101
82
83/* default destination */
84#define NTRIP_CASTER "www.euref-ip.net"
85#define NTRIP_PORT 2101
86
87/* default sisnet source */
88#define SISNET_SERVER "131.176.49.142"
89#define SISNET_PORT 7777
90
91#define ALARMTIME 60
92
93#define RTP_VERSION 2
94#define TIME_RESOLUTION 125
95
96static int ttybaud = 19200;
97static const char *ttyport = "/dev/gps";
98static const char *filepath = "/dev/stdin";
99static enum MODE inputmode = INFILE;
100static int sisnet = 31;
101static int gpsfd = -1;
102static int sigint_received = 0;
103
104/* Forward references */
105static int openserial(const char * tty, int blocksz, int baud);
106static void send_receive_loop(int sock, int fd, int outmode,
107 struct sockaddr * pcasterRTP, socklen_t length);
108static void usage(int, char *);
109static int encode(char *buf, int size, const char *user, const char *pwd);
110static int send_to_caster(char *input, int socket, int input_size);
111static void close_session(int sock_udp, int sock_tcp,
112 const char *caster_addr, const char *mountpoint, int cseq,
113 int session, char *rtsp_ext, int in_fd);
114
115/* Signal Handling */
116static void handle_sigint(int sig);
117static void setup_signal_handler(int sig, void (*handler)(int));
118static int signal_was_caught(void);
119
120#ifdef __GNUC__
121static __attribute__ ((noreturn)) void sighandler_alarm(
122int sig __attribute__((__unused__)))
123#else /* __GNUC__ */
124static void sighandler_alarm(int sig)
125#endif /* __GNUC__ */
126{
127 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
128 exit(1);
129}
130
131/*
132* main
133*
134* Main entry point for the program. Processes command-line arguments and
135* prepares for action.
136*
137* Parameters:
138* argc : integer : Number of command-line arguments.
139* argv : array of char : Command-line arguments as an array of
140* zero-terminated pointers to strings.
141*
142* Return Value:
143* The function does not return a value (although its return type is int).
144*
145* Remarks:
146*
147*/
148
149int main(int argc, char **argv)
150{
151 int c;
152 int size = 2048; /* for setting send buffer size */
153 struct sockaddr_in caster;
154 const char * proxyhost = "";
155 unsigned int proxyport = 0;
156 /*** INPUT ***/
157 const char * casterinhost = 0;
158 unsigned int casterinport = 0;
159 const char * inhost = 0;
160 unsigned int inport = 0;
161
162 char get_extension[SZ] = "";
163
164 struct hostent * he;
165 const char * mountpoint = NULL;
166
167 const char * sisnetpassword = "";
168 const char * sisnetuser = "";
169
170 const char * stream_name = 0;
171 const char * stream_user = 0;
172 const char * stream_password = 0;
173
174 const char * recvrid= 0;
175 const char * recvrpwd = 0;
176
177 const char * initfile = NULL;
178
179 int bindmode = 0;
180
181 /*** OUTPUT ***/
182 const char * casterouthost = NTRIP_CASTER;
183 unsigned int casteroutport = NTRIP_PORT;
184 const char * outhost = 0;
185 unsigned int outport = 0;
186 char post_extension[SZ] = "";
187 char rtsp_extension[SZ] = "";
188
189 const char * ntrip_str = "";
190
191 const char * user = "";
192 const char * password = "";
193
194 int socket_tcp = 0;
195 int outputmode = NTRIPV1;
196
197 struct sockaddr_in casterRTP;
198 struct sockaddr_in local;
199 int socket_udp = 0;
200 int client_port = 0;
201 int server_port = 0;
202 int session = 0;
203 int cseq = 0;
204 socklen_t len = 0;
205 int i = 0;
206
207 char szSendBuffer[BUFSZ];
208 char authorization[SZ];
209 int nBufferBytes = 0;
210 char * dlim = " \r\n=";
211 char * token;
212 char * tok_buf[BUFSZ];
213
214 setbuf(stdout, 0);
215 setbuf(stdin, 0);
216 setbuf(stderr, 0);
217
218 {
219 char *a;
220 int i = 0;
221 for(a = revisionstr+11; *a && *a != ' '; ++a)
222 revisionstr[i++] = *a;
223 revisionstr[i] = 0;
224 datestr[0] = datestr[7];
225 datestr[1] = datestr[8];
226 datestr[2] = datestr[9];
227 datestr[3] = datestr[10];
228 datestr[5] = datestr[12];
229 datestr[6] = datestr[13];
230 datestr[8] = datestr[15];
231 datestr[9] = datestr[16];
232 datestr[4] = datestr[7] = '-';
233 datestr[10] = 0;
234 }
235
236 /* setup signal handler for timeout */
237 signal(SIGALRM,sighandler_alarm);
238 alarm(ALARMTIME);
239
240 /* get and check program arguments */
241 if(argc <= 1)
242 {
243 usage(2, argv[0]);
244 exit(1);
245 }
246 while((c = getopt(argc, argv,
247 "M:i:h:b:p:s:a:m:c:H:P:f:x:y:l:u:V:D:U:W:B:O:E:F:N:n:")) != EOF)
248 {
249 switch (c)
250 {
251 case 'M': /*** InputMode ***/
252 if(!strcmp(optarg, "serial")) inputmode = SERIAL;
253 else if(!strcmp(optarg, "tcpsocket")) inputmode = TCPSOCKET;
254 else if(!strcmp(optarg, "file")) inputmode = INFILE;
255 else if(!strcmp(optarg, "sisnet")) inputmode = SISNET;
256 else if(!strcmp(optarg, "udpsocket")) inputmode = UDPSOCKET;
257 else if(!strcmp(optarg, "caster")) inputmode = CASTER;
258 else inputmode = atoi(optarg);
259 if((inputmode == 0) || (inputmode >= LAST))
260 {
261 fprintf(stderr, "ERROR: can't convert <%s> to a valid InputMode\n",
262 optarg);
263 usage(-1, argv[0]);
264 }
265 break;
266 case 'i': /* serial input device */
267 ttyport = optarg;
268 break;
269 case 'B': /* bind to incoming UDP stream */
270 bindmode = 1;
271 break;
272 case 'V': /* Sisnet data server version number */
273 if(!strcmp("3.0", optarg)) sisnet = 30;
274 else if(!strcmp("3.1", optarg)) sisnet = 31;
275 else if(!strcmp("2.1", optarg)) sisnet = 21;
276 else
277 {
278 fprintf(stderr, "ERROR: unknown SISNeT version <%s>\n", optarg);
279 usage(-2, argv[0]);
280 }
281 break;
282 case 'b': /* serial input baud rate */
283 ttybaud = atoi(optarg);
284 if(ttybaud <= 1)
285 {
286 fprintf(stderr, "ERROR: can't convert <%s> to valid serial baud rate\n",
287 optarg);
288 usage(1, argv[0]);
289 }
290 break;
291 case 'a': /* Destination caster address */
292 casterouthost = optarg;
293 break;
294 case 'p': /* Destination caster port */
295 casteroutport = atoi(optarg);
296 if(casteroutport <= 1 || casteroutport > 65535)
297 {
298 fprintf(stderr,
299 "ERROR: can't convert <%s> to a valid HTTP server port\n", optarg);
300 usage(1, argv[0]);
301 }
302 break;
303 case 'm': /* Destination caster mountpoint for stream upload */
304 mountpoint = optarg;
305 break;
306 case 's': /* File name for input data simulation from file */
307 filepath = optarg;
308 break;
309 case 'f': /* name of an initialization file */
310 initfile = optarg;
311 break;
312 case 'x': /* user ID to access incoming stream */
313 recvrid = optarg;
314 break;
315 case 'y': /* password to access incoming stream */
316 recvrpwd = optarg;
317 break;
318 case 'u': /* Sisnet data server user ID */
319 sisnetuser = optarg;
320 break;
321 case 'l': /* Sisnet data server password */
322 sisnetpassword = optarg;
323 break;
324 case 'c': /* DestinationCaster password for stream upload to mountpoint */
325 password = optarg;
326 break;
327 case 'H': /* Input host address*/
328 casterinhost = optarg;
329 break;
330 case 'P': /* Input port */
331 casterinport = atoi(optarg);
332 if(casterinport <= 1 || casterinport > 65535)
333 {
334 fprintf(stderr, "ERROR: can't convert <%s> to a valid port number\n",
335 optarg);
336 usage(1, argv[0]);
337 }
338 break;
339 case 'D': /* Source caster mountpoint for stream input */
340 stream_name = optarg;
341 break;
342 case 'U': /* Source caster user ID for input stream access */
343 stream_user = optarg;
344 break;
345 case 'W': /* Source caster password for input stream access */
346 stream_password = optarg;
347 break;
348 case 'E': /* Proxy Server */
349 proxyhost = optarg;
350 break;
351 case 'F': /* Proxy port */
352 proxyport = atoi(optarg);
353 break;
354 case 'O': /* OutputMode */
355 outputmode = 0;
356 if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
357 outputmode = NTRIPV1;
358 else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
359 outputmode = HTTP;
360 else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
361 outputmode = RTSP;
362 else outputmode = atoi(optarg);
363 if((outputmode == 0) || (outputmode >= END))
364 {
365 fprintf(stderr, "ERROR: can't convert <%s> to a valid OutputMode\n",
366 optarg);
367 usage(-1, argv[0]);
368 }
369 break;
370 case 'n': /* Destination caster user ID for stream upload to mountpoint */
371 user = optarg;
372 break;
373 case 'N': /* Ntrip-STR, optional for Ntrip Version 2.0 */
374 ntrip_str = optarg;
375 break;
376 case 'h': /* print help screen */
377 case '?':
378 usage(0, argv[0]);
379 break;
380 default:
381 usage(2, argv[0]);
382 break;
383 }
384 }
385
386 argc -= optind;
387 argv += optind;
388
389 /*** argument analysis ***/
390 if(argc > 0)
391 {
392 fprintf(stderr, "ERROR: Extra args on command line: ");
393 for(; argc > 0; argc--)
394 {
395 fprintf(stderr, " %s", *argv++);
396 }
397 fprintf(stderr, "\n");
398 usage(1, argv[0]); /* never returns */
399 }
400
401 if(*ntrip_str && (outputmode == NTRIPV1))
402 {
403 fprintf(stderr, "WARNING: OutputMode is Ntrip version 1.0"
404 " - Ntrip-STR will not be considered\n");
405 }
406
407 if(!mountpoint)
408 {
409 fprintf(stderr, "ERROR: Missing mountpoint argument for stream upload\n");
410 exit(1);
411 }
412
413 if(!password[0])
414 {
415 fprintf(stderr, "WARNING: Missing password argument for stream upload - "
416 "are you really sure?\n");
417 }
418 else
419 {
420 nBufferBytes += encode(authorization, sizeof(authorization), user,
421 password);
422 if(nBufferBytes > (int)sizeof(authorization))
423 {
424 fprintf(stderr, "ERROR: user ID and/or password too long: %d (%d)\n"
425 " user ID: %s \npassword: <%s>\n",
426 nBufferBytes, (int)sizeof(authorization), user, password);
427 exit(1);
428 }
429 }
430
431 if(stream_name && stream_user && !stream_password)
432 {
433 fprintf(stderr, "WARNING: Missing password argument for stream download"
434 " - are you really sure?\n");
435 }
436
437 /*** proxy server handling ***/
438 if(*proxyhost)
439 {
440 outhost = inhost = proxyhost;
441 outport = inport = proxyport;
442 i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d",
443 casterouthost, casteroutport);
444 if((i > SZ) || (i < 0))
445 {
446 fprintf(stderr, "ERROR: Destination caster name/port to long - "
447 "length = %d (max: %d)\n", i, SZ);
448 exit(0);
449 }
450 else
451 {
452 strncpy(post_extension, szSendBuffer, (size_t)i);
453 strcpy(szSendBuffer, "");
454 i = snprintf(szSendBuffer, sizeof(szSendBuffer),":%d", casteroutport);
455 strncpy(rtsp_extension, szSendBuffer, SZ);
456 strcpy(szSendBuffer,""); i = 0;
457 }
458 i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d", casterinhost, casterinport);
459 if((i > SZ) || (i < 0))
460 {
461 fprintf(stderr, "ERROR: Destination caster name/port to long - length = %d (max: %d)\n", i, SZ);
462 exit(0);
463 }
464 else
465 {
466 strncpy(get_extension, szSendBuffer, (size_t)i);
467 strcpy(szSendBuffer, "");
468 i = 0;
469 }
470 }
471 else
472 {
473 outhost = casterouthost; outport = casteroutport;
474 inhost = casterinhost; inport = casterinport;
475 }
476
477 /*** InputMode handling ***/
478 switch(inputmode)
479 {
480 case INFILE:
481 {
482 if((gpsfd = open(filepath, O_RDONLY)) < 0)
483 {
484 perror("ERROR: opening input file");
485 exit(1);
486 }
487 /* set blocking inputmode in case it was not set
488 (seems to be sometimes for fifo's) */
489 fcntl(gpsfd, F_SETFL, 0);
490 printf("file input: file = %s\n", filepath);
491 }
492 break;
493 case SERIAL: /* open serial port */
494 {
495 gpsfd = openserial(ttyport, 1, ttybaud);
496 if(gpsfd < 0)
497 {
498 exit(1);
499 }
500 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
501 }
502 break;
503 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
504 {
505 if(inputmode == SISNET)
506 {
507 if(!inhost) inhost = SISNET_SERVER;
508 if(!inport) inport = SISNET_PORT;
509 }
510 else if(inputmode == CASTER)
511 {
512 if(!inport) inport = NTRIP_PORT;
513 if(!inhost) inhost = NTRIP_CASTER;
514 }
515 else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
516 {
517 if(!inport) inport = SERV_TCP_PORT;
518 if(!inhost) inhost = SERV_HOST_ADDR;
519 }
520
521 if(!(he = gethostbyname(inhost)))
522 {
523 fprintf(stderr, "ERROR: Input host <%s> unknown\n", inhost);
524 usage(-2, argv[0]);
525 }
526
527 if((gpsfd = socket(AF_INET, inputmode == UDPSOCKET
528 ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
529 {
530 fprintf(stderr,
531 "ERROR: can't create socket for incoming data stream\n");
532 exit(1);
533 }
534
535 memset((char *) &caster, 0x00, sizeof(caster));
536 if(!bindmode)
537 memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
538 caster.sin_family = AF_INET;
539 caster.sin_port = htons(inport);
540
541 fprintf(stderr, "%s input: host = %s, port = %d, %s%s%s%s%s\n",
542 inputmode == CASTER ? "caster" : inputmode == SISNET ? "sisnet" :
543 inputmode == TCPSOCKET ? "tcp socket" : "udp socket",
544 bindmode ? "127.0.0.1" : inet_ntoa(caster.sin_addr),
545 inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
546 initfile ? ", initfile = " : "", initfile ? initfile : "",
547 bindmode ? "binding mode" : "");
548
549 if(bindmode)
550 {
551 if(bind(gpsfd, (struct sockaddr *) &caster, sizeof(caster)) < 0)
552 {
553 fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
554 exit(1);
555 }
556 } /* connect to input-caster or proxy server*/
557 else if(connect(gpsfd, (struct sockaddr *)&caster, sizeof(caster)) < 0)
558 {
559 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
560 inet_ntoa(caster.sin_addr), inport);
561 exit(1);
562 }
563
564 if(stream_name) /* input from Ntrip Version 1.0 caster*/
565 {
566 int init = 0;
567
568 /* set socket buffer size */
569 setsockopt(gpsfd, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
570 sizeof(const char *));
571 if(stream_user && stream_password)
572 {
573 /* leave some space for login */
574 nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
575 "GET %s/%s HTTP/1.0\r\n"
576 "User-Agent: %s/%s\r\n"
577 "Authorization: Basic ", get_extension, stream_name,
578 AGENTSTRING, revisionstr);
579 /* second check for old glibc */
580 if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
581 {
582 fprintf(stderr, "ERROR: Source caster request too long\n");
583 exit(1);
584 }
585 nBufferBytes += encode(szSendBuffer+nBufferBytes,
586 sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
587 if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
588 {
589 fprintf(stderr,
590 "ERROR: Source caster user ID and/or password too long\n");
591 exit(1);
592 }
593 szSendBuffer[nBufferBytes++] = '\r';
594 szSendBuffer[nBufferBytes++] = '\n';
595 szSendBuffer[nBufferBytes++] = '\r';
596 szSendBuffer[nBufferBytes++] = '\n';
597 }
598 else
599 {
600 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
601 "GET %s/%s HTTP/1.0\r\n"
602 "User-Agent: %s/%s\r\n"
603 "\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
604 }
605 if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
606 != nBufferBytes)
607 {
608 fprintf(stderr, "ERROR: could not send Source caster request\n");
609 exit(1);
610 }
611 nBufferBytes = 0;
612 /* check Source caster's response */
613 while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
614 && (nBufferBytes += recv(gpsfd, szSendBuffer,
615 sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
616 {
617 if(strstr(szSendBuffer, "\r\n"))
618 {
619 if(!strncmp(szSendBuffer, "ICY 200 OK\r\n", 10))
620 init = 1;
621 else
622 {
623 int k;
624 fprintf(stderr,
625 "ERROR: could not get requested data from Source caster: ");
626 for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
627 && szSendBuffer[k] != '\r'; ++k)
628 {
629 fprintf(stderr, "%c", isprint(szSendBuffer[k])
630 ? szSendBuffer[k] : '.');
631 }
632 fprintf(stderr, "\n");
633 exit(1);
634 }
635 }
636 }
637 if(!init)
638 {
639 fprintf(stderr, "ERROR: could not init Source caster download\n");
640 exit(1);
641 }
642 } /* end input from Ntrip Version 1.0 caster */
643
644 if(initfile && inputmode != SISNET)
645 {
646 char buffer[1024];
647 FILE *fh;
648 int i;
649
650 if((fh = fopen(initfile, "r")))
651 {
652 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
653 {
654 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
655 {
656 perror("ERROR: sending init file");
657 exit(1);
658 }
659 }
660 if(i < 0)
661 {
662 perror("ERROR: reading init file");
663 exit(1);
664 }
665 fclose(fh);
666 }
667 else
668 {
669 fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
670 exit(1);
671 }
672 }
673 }
674 if(inputmode == SISNET)
675 {
676 int i, j;
677 char buffer[1024];
678
679 i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
680 : "AUTH,%s,%s", sisnetuser, sisnetpassword);
681 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
682 {
683 perror("ERROR: sending authentication for SISNeT data server");
684 exit(1);
685 }
686 i = sisnet >= 30 ? 7 : 5;
687 if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
688 {
689 fprintf(stderr, "ERROR: SISNeT connect failed:");
690 for(i = 0; i < j; ++i)
691 {
692 if(buffer[i] != '\r' && buffer[i] != '\n')
693 {
694 fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
695 }
696 }
697 fprintf(stderr, "\n");
698 exit(1);
699 }
700 if(sisnet >= 31)
701 {
702 if((send(gpsfd, "START\r\n", 7, 0)) != i)
703 {
704 perror("ERROR: sending Sisnet start command");
705 exit(1);
706 }
707 }
708 }
709 /*** receiver authentication ***/
710 if (recvrid && recvrpwd && ((inputmode == TCPSOCKET)
711 || (inputmode == UDPSOCKET)))
712 {
713 if (strlen(recvrid) > (BUFSZ-3))
714 {
715 fprintf(stderr, "ERROR: Receiver ID too long\n");
716 exit(0);
717 }
718 else
719 {
720 fprintf(stderr, "Sending user ID for receiver...\n");
721 nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
722 strcpy(szSendBuffer, recvrid);
723 strcat(szSendBuffer,"\r\n");
724 send(gpsfd,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);
725 }
726
727 if (strlen(recvrpwd) > (BUFSZ-3))
728 {
729 fprintf(stderr, "ERROR: Receiver password too long\n");
730 exit(0);
731 }
732 else
733 {
734 fprintf(stderr, "Sending user password for receiver...\n");
735 nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
736 strcpy(szSendBuffer, recvrpwd);
737 strcat(szSendBuffer,"\r\n");
738 send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);
739 }
740 }
741 break;
742 default:
743 usage(-1, argv[0]);
744 break;
745 }
746
747 /* ----- main part ----- */
748 /* setup signal handler for CTRL+C */
749 setup_signal_handler(SIGINT, handle_sigint);
750
751 while(outputmode != END)
752 {
753 if(signal_was_caught())
754 {
755 fprintf(stderr, "NtripLinuxServer terminates\n");
756 break;
757 }
758
759 if(!(he = gethostbyname(outhost)))
760 {
761 fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
762 outhost);
763 usage(-2, argv[0]);
764 }
765
766 /* create socket */
767 if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) < 0)
768 {
769 perror("ERROR: tcp socket");
770 exit(2);
771 }
772
773 memset((char *) &caster, 0x00, sizeof(caster));
774 memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
775 caster.sin_family = AF_INET;
776 caster.sin_port = htons(outport);
777
778 /* connect to Destination caster or Proxy server*/
779 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s"
780 ", mode = %s\n", inet_ntoa(caster.sin_addr), outport, mountpoint,
781 outputmode == NTRIPV1 ? "ntrip1" : outputmode == HTTP ? "http" : "rtsp");
782
783 if(connect(socket_tcp, (struct sockaddr *) &caster, sizeof(caster)) < 0)
784 {
785 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
786 inet_ntoa(caster.sin_addr), outport);
787 if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
788 exit(3);
789 }
790
791 /*** OutputMode handling ***/
792 switch(outputmode){
793 case NTRIPV1: /*** OutputMode Ntrip Version 1.0 ***/
794 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
795 "SOURCE %s %s/%s\r\n"
796 "Source-Agent: %s\r\n\r\n",
797 password, post_extension, mountpoint, AGENTSTRING);
798 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
799 {
800 fprintf(stderr, "ERROR: Destination caster request to long\n");
801 outputmode = END;
802 break;
803 }
804 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
805 {
806 outputmode = END;
807 break;
808 }
809 /* check Destination caster's response */
810 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
811 szSendBuffer[nBufferBytes] = '\0';
812 if(!strstr(szSendBuffer, "OK"))
813 {
814 char *a;
815 fprintf(stderr,
816 "ERROR: Destination caster's or Proxy's reply is not OK: ");
817 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
818 {
819 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
820 }
821 fprintf(stderr, "\n");
822 outputmode = END;
823 break;
824 }
825#ifndef NDEBUG
826 else
827 {
828 fprintf(stderr,
829 "Destination caster response:\n%s\nconnection successfull\n",
830 szSendBuffer);
831 }
832#endif
833 send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
834 break;
835 case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
836 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
837 "POST %s/%s HTTP/1.1\r\n"
838 "Host: %s\r\n"
839 "Ntrip-Version: Ntrip/2.0\r\n"
840 "User-Agent: %s/%s\r\n"
841 "Authorization: Basic %s\r\n"
842 "Ntrip-STR: %s\r\n"
843 "Transfer-Encoding: chunked\r\n\r\n",
844 post_extension, mountpoint, casterouthost, AGENTSTRING,
845 revisionstr, authorization, ntrip_str);
846 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
847 {
848 fprintf(stderr, "ERROR: Destination caster request to long\n");
849 outputmode = END;
850 break;
851 }
852 if (!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
853 {
854 outputmode = END;
855 break;
856 }
857 /* check Destination caster's response */
858 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
859 szSendBuffer[nBufferBytes] = '\0';
860 if(!strstr(szSendBuffer, "HTTP/1.1 200 OK"))
861 {
862 char *a;
863 fprintf(stderr,
864 "ERROR: Destination caster's%s reply is not OK: ",
865 *proxyhost ? " or Proxy's" : "");
866 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
867 {
868 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
869 }
870 fprintf(stderr, "\n");
871 /* fallback if necessary */
872 if(!strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
873 {
874 fprintf(stderr,
875 " Ntrip Version 2.0 not implemented at Destination caster"
876 " <%s>%s%s%s\n%s\n"
877 "caster fallback: Fallback to Ntrip Version 1.0\n",
878 casterouthost,
879 *proxyhost ? " or Proxy <" : "", proxyhost, *proxyhost ? ">" : "",
880 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "");
881 outputmode = NTRIPV1;
882 break;
883 }
884 outputmode = END;
885 break;
886 }
887 else
888 {
889 fprintf(stderr, "Destination caster response:\n%s\n"
890 "connection successfull\n",szSendBuffer);
891 }
892 send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
893 break;
894 case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
895 if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) < 0)
896 {
897 perror("ERROR: udp socket");
898 exit(4);
899 }
900 /* fill structure with local address information for UDP */
901 memset(&local, 0, sizeof(local));
902 local.sin_family = AF_INET;
903 local.sin_port = htons(0);
904 local.sin_addr.s_addr = htonl(INADDR_ANY);
905 len = (socklen_t)sizeof(local);
906 /* bind() in order to get a random RTP client_port */
907 if((bind(socket_udp,(struct sockaddr *)&local, len)) < 0)
908 {
909 perror("ERROR: udp bind");
910 if(close(socket_udp)==-1) perror("ERROR: close udp socket");
911 if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
912 exit(4);
913 }
914 if((getsockname(socket_udp, (struct sockaddr*)&local, &len)) != -1)
915 {
916 client_port = (unsigned int)ntohs(local.sin_port);
917 }else{
918 perror("ERROR: getsockname(localhost)");
919 if(close(socket_udp)==-1) perror("ERROR: close udp socket");
920 if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
921 exit(4);
922 }
923 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
924 "SETUP rtsp://%s%s/%s RTSP/1.0\r\n"
925 "CSeq: 1\r\n"
926 "Ntrip-Version: Ntrip/2.0\r\n"
927 "Ntrip-Component: Ntripserver\r\n"
928 "User-Agent: %s/%s\r\n"
929 "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
930 "Authorization: Basic %s\r\n"
931 "Ntrip-STR: %s\r\n\r\n",
932 casterouthost, rtsp_extension, mountpoint, AGENTSTRING, revisionstr,
933 client_port, authorization, ntrip_str);
934 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
935 {
936 fprintf(stderr, "ERROR: Destination caster request to long\n");
937 outputmode = END;
938 break;
939 }
940 if (!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
941 {
942 outputmode = END;
943 break;
944 }
945 while((nBufferBytes = recv(socket_tcp, szSendBuffer,
946 sizeof(szSendBuffer), 0)) > 0)
947 {
948 /* check Destination caster's response */
949 szSendBuffer[nBufferBytes] = '\0';
950 if(!strstr(szSendBuffer, "RTSP/1.0 200 OK"))
951 {
952 char *a;
953 fprintf(stderr,
954 "ERROR: Destination caster's%s reply is not OK: ",
955 *proxyhost ? " or Proxy's" : "");
956 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
957 {
958 fprintf(stderr, "%c", isprint(*a) ? *a : '.');
959 }
960 fprintf(stderr, "\n");
961 /* fallback if necessary */
962 if(strncmp(szSendBuffer, "RTSP",4) != 0)
963 {
964 if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
965 {
966 fprintf(stderr,
967 " - RTSP not implemented at Destination caster <%s>%s%s%s\n\n"
968 "caster fallback: Fallback to Ntrip Version 2.0 in TCP/IP"
969 " mode\n\n", casterouthost,
970 *proxyhost ? " or Proxy <" :"", proxyhost, *proxyhost ? ">":"");
971 outputmode = HTTP;
972 break;
973 }
974 else
975 {
976 fprintf(stderr,
977 " Ntrip-Version 2.0 not implemented at Destination caster"
978 "<%s>%s%s%s\n%s"
979 " or RTSP/1.0 not implemented at Destination caster%s\n\n"
980 "caster fallback: Fallback to Ntrip Version 1.0\n",
981 casterouthost, *proxyhost ? " or Proxy <" :"", proxyhost,
982 *proxyhost ? ">":"",
983 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "",
984 *proxyhost ? " or Proxy" :"");
985 outputmode = NTRIPV1;
986 break;
987 }
988 }
989 else
990 {
991 outputmode = END;
992 break;
993 }
994 }
995#ifndef NDEBUG
996 else
997 {
998 fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
999 }
1000#endif
1001 if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n"))
1002 && (strstr(szSendBuffer,"CSeq: 1\r\n")))
1003 {
1004 for(token = strtok(szSendBuffer, dlim); token != NULL;
1005 token = strtok(NULL, dlim))
1006 {
1007 tok_buf[i] = token; i++;
1008 }
1009 session = atoi(tok_buf[6]);
1010 server_port = atoi(tok_buf[10]);
1011 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1012 "POST rtsp://%s%s/%s RTSP/1.0\r\n"
1013 "CSeq: 2\r\n"
1014 "Session: %d\r\n"
1015 "\r\n",
1016 casterouthost, rtsp_extension, mountpoint, session);
1017 if((nBufferBytes >= (int)sizeof(szSendBuffer))
1018 || (nBufferBytes < 0))
1019 {
1020 fprintf(stderr, "ERROR: Destination caster request to long\n");
1021 outputmode = END;
1022 break;
1023 }
1024 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1025 {
1026 outputmode = END;
1027 break;
1028 }
1029 }else if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n")) && (strstr(szSendBuffer,"CSeq: 2\r\n"))) {
1030 /* fill structure with caster address information for UDP */
1031 memset(&casterRTP, 0, sizeof(casterRTP));
1032 casterRTP.sin_family = AF_INET;
1033 casterRTP.sin_port = htons(((uint16_t)server_port));
1034 if((he = gethostbyname(outhost))== NULL) {
1035 fprintf(stderr, "ERROR: Destination caster unknown\n");
1036 if(close(socket_udp)==-1) perror("ERROR: close udp socket");
1037 if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
1038 exit(4);
1039 }else{
1040 memcpy((char *)&casterRTP.sin_addr.s_addr,
1041 he->h_addr_list[0], (size_t)he->h_length);
1042 }
1043 cseq = 2;
1044 len = (socklen_t)sizeof(casterRTP);
1045 send_receive_loop(socket_udp, gpsfd, outputmode, (struct sockaddr *)&casterRTP, (socklen_t)len);
1046 break;
1047 }else{break;}
1048 }
1049 break;
1050 }
1051 }
1052 close_session(socket_udp, socket_tcp, casterouthost, mountpoint, cseq, session, rtsp_extension, gpsfd);
1053 return 0;
1054}
1055
1056static void send_receive_loop(int sock, int fd, int outmode, struct sockaddr* pcasterRTP,
1057socklen_t length)
1058{
1059 int nodata = 0;
1060 char buffer[BUFSZ] = { 0 };
1061 char sisnetbackbuffer[200];
1062 char szSendBuffer[BUFSZ] = "";
1063 int nBufferBytes = 0;
1064
1065 /* RTSP / RTP Mode */
1066 int isfirstpacket = 1;
1067 struct timeval now;
1068 struct timeval last = {0,0};
1069 long int sendtimediff;
1070 int rtpseq = 0;
1071 int rtpssrc = 0;
1072 int rtptime = 0;
1073
1074 /* data transmission */
1075 fprintf(stderr,"transfering data ...\n");
1076
1077 while(1)
1078 {
1079 /* signal handling*/
1080 if(signal_was_caught())
1081 {
1082 fprintf(stderr, "NtripLinuxServer exits send-receive mode\n");
1083 break;
1084 }
1085 if(!nodata) alarm(ALARMTIME);
1086 else nodata = 0;
1087
1088 if(!nBufferBytes)
1089 {
1090 if(inputmode == SISNET && sisnet <= 30)
1091 {
1092 int i;
1093 /* a somewhat higher rate than 1 second to get really each block */
1094 /* means we need to skip double blocks sometimes */
1095 struct timeval tv = {0,700000};
1096 select(0, 0, 0, 0, &tv);
1097 memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
1098 i = (sisnet >= 30 ? 5 : 3);
1099 if((send(gpsfd, "MSG\r\n", i, 0)) != i)
1100 {
1101 perror("ERROR: sending SISNeT data request");
1102 exit(1);
1103 }
1104 }
1105 /*** receiving data ****/
1106 nBufferBytes = read(fd, buffer, sizeof(buffer));
1107 if(!nBufferBytes)
1108 {
1109 printf("WARNING: no data received from input\n");
1110 sleep(3);
1111 nodata = 1;
1112 continue;
1113 }
1114 else if(nBufferBytes < 0)
1115 {
1116 perror("ERROR: reading input failed");
1117 exit(1);
1118 }
1119 /* we can compare the whole buffer, as the additional bytes
1120 remain unchanged */
1121 if(inputmode == SISNET && sisnet <= 30 &&
1122 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
1123 {
1124 nBufferBytes = 0;
1125 }
1126 }
1127 /** send data ***/
1128 if((nBufferBytes) && (outmode == NTRIPV1)) /*** Ntrip-Version 1.0 ***/
1129 {
1130 int i;
1131 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1132 != nBufferBytes)
1133 {
1134 if(i < 0)
1135 {
1136 if(errno != EAGAIN)
1137 {
1138 perror("WARNING: could not send data to Destination caster"
1139 " - retry connection");
1140 close(sock);
1141 sleep(5);
1142 return;
1143 }
1144 }
1145 else if(i)
1146 {
1147 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
1148 nBufferBytes -= i;
1149 }
1150 }else
1151 {
1152 nBufferBytes = 0;
1153 }
1154 }
1155 /*** Ntrip-Version 2.0 HTTP/1.1 ***/
1156 else if((nBufferBytes) && (outmode == HTTP))
1157 {
1158 int i, nChunkBytes, j = 1;
1159 nChunkBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),"%x\r\n",
1160 nBufferBytes);
1161 send(sock, szSendBuffer, nChunkBytes, MSG_DONTWAIT);
1162 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1163 != nBufferBytes)
1164 {
1165 if(i < 0)
1166 {
1167 if(errno != EAGAIN)
1168 {
1169 perror("WARNING: could not send data to Destination caster"
1170 " - retry connection");
1171 close(sock);
1172 sleep(5);
1173 return;
1174 }
1175 }
1176 else if(i)
1177 {
1178 while(j>0)
1179 {
1180 j = send(sock, buffer, (size_t)BUFSZ, MSG_DONTWAIT);
1181 }
1182 }
1183 }
1184 else
1185 {
1186 send(sock, "\r\n", strlen("\r\n"), MSG_DONTWAIT);
1187 nBufferBytes = 0;
1188 }
1189 }
1190 /*** Ntrip-Version 2.0 RTSP(TCP) / RTP(UDP) ***/
1191 else if((nBufferBytes) && (outmode == RTSP))
1192 {
1193 char rtpbuffer[BUFSZ+12];
1194 int i, j;
1195 /* Signal Handling */
1196 if(signal_was_caught())
1197 {
1198 fprintf(stderr, "NtripLinuxServer exits send-receive mode\n");
1199 break;
1200 }
1201 gettimeofday(&now, NULL);
1202 /* RTP data packet generation*/
1203 if(isfirstpacket){
1204 rtpseq = rand();
1205 rtptime = rand();
1206 rtpssrc = rand();
1207 last = now;
1208 isfirstpacket = 0;
1209 }
1210 else
1211 {
1212 ++rtpseq;
1213 sendtimediff = (((now.tv_sec - last.tv_sec)*1000000)
1214 + (now.tv_usec - last.tv_usec));
1215 rtptime += sendtimediff/TIME_RESOLUTION;
1216 }
1217 rtpbuffer[0] = (RTP_VERSION<<6);
1218 /* padding, extension, csrc are empty */
1219 rtpbuffer[1] = 96;
1220 /* marker is empty */
1221 rtpbuffer[2] = rtpseq>>8;
1222 rtpbuffer[3] = rtpseq;
1223 rtpbuffer[4] = rtptime>>24;
1224 rtpbuffer[5] = rtptime>>16;
1225 rtpbuffer[6] = rtptime>>8;
1226 rtpbuffer[7] = rtptime;
1227 rtpbuffer[8] = rtpssrc>>24;
1228 rtpbuffer[9] = rtpssrc>>16;
1229 rtpbuffer[10] = rtpssrc>>8;
1230 rtpbuffer[11] = rtpssrc;
1231 for(j=0; j<nBufferBytes; j++) {rtpbuffer[12+j] = buffer[j];}
1232 last.tv_sec = now.tv_sec;
1233 last.tv_usec = now.tv_usec;
1234
1235 if ((i = sendto(sock, rtpbuffer, 12 + nBufferBytes, 0, pcasterRTP,
1236 length)) != (nBufferBytes + 12))
1237 {
1238 if(i < 0)
1239 {
1240 if(errno != EAGAIN)
1241 {
1242 perror("WARNING: could not send data to Destination caster - retry connection");
1243 close(sock);
1244 sleep(5);
1245 return;
1246 }
1247 }
1248 else if(i)
1249 {
1250 memmove(buffer, buffer+(i-12), (size_t)(nBufferBytes-(i-12)));
1251 nBufferBytes -= i-12;
1252 }
1253 }
1254 else
1255 {
1256 nBufferBytes = 0;
1257 }
1258 }
1259 }
1260 return;
1261}
1262
1263
1264/********************************************************************
1265 * openserial
1266 *
1267 * Open the serial port with the given device name and configure it for
1268 * reading NMEA data from a GPS receiver.
1269 *
1270 * Parameters:
1271 * tty : pointer to : A zero-terminated string containing the device
1272 * unsigned char name of the appropriate serial port.
1273 * blocksz : integer : Block size for port I/O
1274 * baud : integer : Baud rate for port I/O
1275 *
1276 * Return Value:
1277 * The function returns a file descriptor for the opened port if successful.
1278 * The function returns -1 in the event of an error.
1279 *
1280 * Remarks:
1281 *
1282 ********************************************************************/
1283
1284static int openserial(const char * tty, int blocksz, int baud)
1285{
1286 int fd;
1287 struct termios termios;
1288
1289 fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
1290 if(fd < 0)
1291 {
1292 perror("ERROR: opening serial connection");
1293 return (-1);
1294 }
1295 if(tcgetattr(fd, &termios) < 0)
1296 {
1297 perror("ERROR: get serial attributes");
1298 return (-1);
1299 }
1300 termios.c_iflag = 0;
1301 termios.c_oflag = 0; /* (ONLRET) */
1302 termios.c_cflag = CS8 | CLOCAL | CREAD;
1303 termios.c_lflag = 0;
1304 {
1305 int cnt;
1306 for(cnt = 0; cnt < NCCS; cnt++)
1307 termios.c_cc[cnt] = -1;
1308 }
1309 termios.c_cc[VMIN] = blocksz;
1310 termios.c_cc[VTIME] = 2;
1311
1312#if (B4800 != 4800)
1313/* Not every system has speed settings equal to absolute speed value. */
1314
1315 switch (baud)
1316 {
1317 case 300:
1318 baud = B300;
1319 break;
1320 case 1200:
1321 baud = B1200;
1322 break;
1323 case 2400:
1324 baud = B2400;
1325 break;
1326 case 4800:
1327 baud = B4800;
1328 break;
1329 case 9600:
1330 baud = B9600;
1331 break;
1332 case 19200:
1333 baud = B19200;
1334 break;
1335 case 38400:
1336 baud = B38400;
1337 break;
1338#ifdef B57600
1339 case 57600:
1340 baud = B57600;
1341 break;
1342#endif
1343#ifdef B115200
1344 case 115200:
1345 baud = B115200;
1346 break;
1347#endif
1348#ifdef B230400
1349 case 230400:
1350 baud = B230400;
1351 break;
1352#endif
1353 default:
1354 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
1355 baud = B19200;
1356 break;
1357 }
1358#endif
1359
1360 if(cfsetispeed(&termios, baud) != 0)
1361 {
1362 perror("ERROR: setting serial speed with cfsetispeed");
1363 return (-1);
1364 }
1365 if(cfsetospeed(&termios, baud) != 0)
1366 {
1367 perror("ERROR: setting serial speed with cfsetospeed");
1368 return (-1);
1369 }
1370 if(tcsetattr(fd, TCSANOW, &termios) < 0)
1371 {
1372 perror("ERROR: setting serial attributes");
1373 return (-1);
1374 }
1375 if(fcntl(fd, F_SETFL, 0) == -1)
1376 {
1377 perror("WARNING: setting blocking inputmode failed");
1378 }
1379 return (fd);
1380} /* openserial */
1381
1382/********************************************************************
1383* usage
1384*
1385* Send a usage message to standard error and quit the program.
1386*
1387* Parameters:
1388* None.
1389*
1390* Return Value:
1391* The function does not return a value.
1392*
1393* Remarks:
1394*
1395*********************************************************************/
1396static
1397#ifdef __GNUC__
1398__attribute__ ((noreturn))
1399#endif /* __GNUC__ */
1400void usage(int rc, char *name)
1401{
1402 fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]\n",
1403 revisionstr, datestr, name);
1404 fprintf(stderr, "PURPOSE\n");
1405 fprintf(stderr, " The purpose of this program is to pick up a GNSS data stream (Input, Source)\n");
1406 fprintf(stderr, " from either\n\n");
1407 fprintf(stderr, " 1. a Serial port, or\n");
1408 fprintf(stderr, " 2. an IP server, or\n");
1409 fprintf(stderr, " 3. a File, or\n");
1410 fprintf(stderr, " 4. a SISNeT Data Server, or\n");
1411 fprintf(stderr, " 5. a UDP server, or\n");
1412 fprintf(stderr, " 6. an NTRIP Version 1.0 Caster\n\n");
1413 fprintf(stderr, " and forward that incoming stream (Output, Destination) to either\n\n");
1414 fprintf(stderr, " - an NTRIP Version 1.0 Caster, or\n");
1415 fprintf(stderr, " - an NTRIP Version 2.0 Caster via TCP/IP or RTSP/RTP.\n\n\n");
1416 fprintf(stderr, "OPTIONS\n");
1417 fprintf(stderr, " -h|? print this help screen\n\n");
1418 fprintf(stderr, " -E <ProxyHost> Proxy server host name or address, required i.e. when\n");
1419 fprintf(stderr, " running the program in a proxy server protected LAN,\n");
1420 fprintf(stderr, " optional\n");
1421 fprintf(stderr, " -F <ProxyPort> Proxy server IP port, required i.e. when running\n");
1422 fprintf(stderr, " the program in a proxy server protected LAN, optional\n\n");
1423 fprintf(stderr, " -M <InputMode> Sets the input mode (1 = Serial Port, 2 = IP server,\n");
1424 fprintf(stderr, " 3 = File, 4 = SISNeT Data Server, 5 = UDP server, 6 = NTRIP Caster),\n");
1425 fprintf(stderr, " mandatory\n\n");
1426 fprintf(stderr, " <InputMode> = 1 (Serial Port):\n");
1427 fprintf(stderr, " -i <Device> Serial input device, default: /dev/gps, mandatory if\n");
1428 fprintf(stderr, " <InputMode>=1\n");
1429 fprintf(stderr, " -b <BaudRate> Serial input baud rate, default: 19200 bps, mandatory\n");
1430 fprintf(stderr, " if <InputMode>=1\n\n");
1431 fprintf(stderr, " <InputMode> = 2|5 (IP port | UDP port):\n");
1432 fprintf(stderr, " -H <ServerHost> Input host name or address, default: 127.0.0.1,\n");
1433 fprintf(stderr, " mandatory if <InputMode> = 2|5\n");
1434 fprintf(stderr, " -P <ServerPort> Input port, default: 1025, mandatory if <InputMode>= 2|5\n");
1435 fprintf(stderr, " -f <ServerFile> Name of initialization file to be send to server,\n");
1436 fprintf(stderr, " optional\n");
1437 fprintf(stderr, " -x <ServerUser> User ID to access incoming stream, optional\n");
1438 fprintf(stderr, " -y <ServerPass> Password, to access incoming stream, optional\n");
1439 fprintf(stderr, " -B Bind to incoming UDP stream, optional for <InputMode> = 5\n\n");
1440 fprintf(stderr, " <InputMode> = 3 (File):\n");
1441 fprintf(stderr, " -s <File> File name to simulate stream by reading data from (log)\n");
1442 fprintf(stderr, " file, default is /dev/stdin, mandatory for <InputMode> = 3\n\n");
1443 fprintf(stderr, " <InputMode> = 4 (SISNeT Data Server):\n");
1444 fprintf(stderr, " -H <SisnetHost> SISNeT Data Server name or address,\n");
1445 fprintf(stderr, " default: 131.176.49.142, mandatory if <InputMode> = 4\n");
1446 fprintf(stderr, " -P <SisnetPort> SISNeT Data Server port, default: 7777, mandatory if\n");
1447 fprintf(stderr, " <InputMode> = 4\n");
1448 fprintf(stderr, " -u <SisnetUser> SISNeT Data Server user ID, mandatory if <InputMode> = 4\n");
1449 fprintf(stderr, " -l <SisnetPass> SISNeT Data Server password, mandatory if <InputMode> = 4\n");
1450 fprintf(stderr, " -V <SisnetVers> SISNeT Data Server Version number, options are 2.1, 3.0\n");
1451 fprintf(stderr, " or 3.1, default: 3.1, mandatory if <InputMode> = 4\n\n");
1452 fprintf(stderr, " <InputMode> = 6 (NTRIP Version 1.0 Caster):\n");
1453 fprintf(stderr, " -H <SourceHost> Source caster name or address, default: 127.0.0.1,\n");
1454 fprintf(stderr, " mandatory if <InputMode> = 6\n");
1455 fprintf(stderr, " -P <SourcePort> Source caster port, default: 2101, mandatory if\n");
1456 fprintf(stderr, " <InputMode> = 6\n");
1457 fprintf(stderr, " -D <SourceMount> Source caster mountpoint for stream input, mandatory if\n");
1458 fprintf(stderr, " <InputMode> = 6\n");
1459 fprintf(stderr, " -U <SourceUser> Source caster user Id for input stream access, mandatory\n");
1460 fprintf(stderr, " for protected streams if <InputMode> = 6\n");
1461 fprintf(stderr, " -W <SourcePass> Source caster password for input stream access, mandatory\n");
1462 fprintf(stderr, " for protected streams if <InputMode> = 6\n\n");
1463 fprintf(stderr, " -O <OutputMode> Sets output mode for communatation with destination caster\n");
1464 fprintf(stderr, " 0 = h = http: Ntrip Version 2.0 Caster in TCP/IP mode\n");
1465 fprintf(stderr, " 1 = r = rtsp: NTRIP Version 2.0 Caster in RTSP/RTP mode\n");
1466 fprintf(stderr, " 2 = n = ntrip1: NTRIP Version 1.0 Caster\n\n");
1467 fprintf(stderr, " Defaults to NTRIP1.0, but wil change to 2.0 in future versions\n");
1468 fprintf(stderr, " Note that the program automatically falls back from mode r to mode t and\n");
1469 fprintf(stderr, " further to mode f if necessary.\n\n");
1470 fprintf(stderr, " -a <DestHost> Destination caster name or address, default: 127.0.0.1,\n");
1471 fprintf(stderr, " mandatory\n");
1472 fprintf(stderr, " -p <DestPort> Destination caster port, default: 2101, mandatory\n");
1473 fprintf(stderr, " -m <DestMount> Destination caster mountpoint for stream upload,\n");
1474 fprintf(stderr, " mandatory\n");
1475 fprintf(stderr, " -n <DestUser> Destination caster user ID for stream upload to\n");
1476 fprintf(stderr, " mountpoint, only for NTRIP Version 2.0 destination\n");
1477 fprintf(stderr, " casters, mandatory\n");
1478 fprintf(stderr, " -c <DestPass> Destination caster password for stream upload to\n");
1479 fprintf(stderr, " mountpoint, mandatory\n");
1480 fprintf(stderr, " -N <STR-record> Sourcetable STR-record\n");
1481 fprintf(stderr, " optional for Ntrip Version 2.0 in RTSP/RTP and TCP/IP mode\n\n");
1482 exit(rc);
1483} /* usage */
1484
1485
1486/********************************************************************/
1487/* signal handling */
1488/********************************************************************/
1489#ifdef __GNUC__
1490static void handle_sigint(int sig __attribute__((__unused__)))
1491#else /* __GNUC__ */
1492static void handle_sigint(int sig)
1493#endif /* __GNUC__ */
1494{sigint_received = 1;}
1495
1496static void setup_signal_handler(int sig, void (*handler)(int))
1497{
1498#if _POSIX_VERSION > 198800L
1499 struct sigaction action;
1500
1501 action.sa_handler = handler;
1502 sigemptyset(&(action.sa_mask));
1503 sigaddset(&(action.sa_mask), sig);
1504 action.sa_flags = 0;
1505 sigaction(sig, &action, 0);
1506#else
1507 signal(sig, handler);
1508#endif
1509 return;
1510} /* setupsignal_handler */
1511
1512static int signal_was_caught(void)
1513{
1514 fflush(stdout);
1515 if(sigint_received)
1516 fprintf(stderr, "SIGINT received: ");
1517
1518 return (sigint_received);
1519} /* signal_was_caught */
1520
1521/********************************************************************
1522 * base64-encoding *
1523*******************************************************************/
1524static const char encodingTable [64] =
1525{
1526 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
1527 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
1528 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
1529 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
1530};
1531
1532/* does not buffer overrun, but breaks directly after an error */
1533/* returns the number of required bytes */
1534static int encode(char *buf, int size, const char *user, const char *pwd)
1535{
1536 unsigned char inbuf[3];
1537 char *out = buf;
1538 int i, sep = 0, fill = 0, bytes = 0;
1539
1540 while(*user || *pwd)
1541 {
1542 i = 0;
1543 while(i < 3 && *user) inbuf[i++] = *(user++);
1544 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
1545 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
1546 while(i < 3) {inbuf[i++] = 0; ++fill; }
1547 if(out-buf < size-1)
1548 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
1549 if(out-buf < size-1)
1550 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
1551 | ((inbuf [1] & 0xF0) >> 4)];
1552 if(out-buf < size-1)
1553 {
1554 if(fill == 2)
1555 *(out++) = '=';
1556 else
1557 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
1558 | ((inbuf [2] & 0xC0) >> 6)];
1559 }
1560 if(out-buf < size-1)
1561 {
1562 if(fill >= 1)
1563 *(out++) = '=';
1564 else
1565 *(out++) = encodingTable[inbuf [2] & 0x3F];
1566 }
1567 bytes += 4;
1568 }
1569 if(out-buf < size)
1570 *out = 0;
1571 return bytes;
1572}/* base64 Encoding */
1573
1574
1575/********************************************************************
1576 * send message to caster *
1577*********************************************************************/
1578static int send_to_caster(char *input, int socket, int input_size)
1579{
1580 int send_error = 1;
1581
1582 if((send(socket, input, (size_t)input_size, 0)) != input_size)
1583 {
1584 fprintf(stderr, "ERROR: could not send full header to Destination caster\n");
1585 send_error = 0;
1586 }
1587#ifndef NDEBUG
1588 else
1589 {
1590 fprintf(stderr, "\nDestination caster request:\n");
1591 fprintf(stderr, "%s", input);
1592 }
1593#endif
1594 return send_error;
1595}/* send_to_caster */
1596
1597
1598/********************************************************************
1599 * close session *
1600*********************************************************************/
1601static void close_session(int sock_udp, int sock_tcp,
1602const char *caster_addr, const char *mountpoint, int cseq,
1603int session, char *rtsp_ext, int in_fd)
1604{
1605 int size_send_buf;
1606 char send_buf[BUFSZ];
1607
1608 if(in_fd)
1609 {
1610 if(close(in_fd)==-1)
1611 {
1612 perror("ERROR: close input device ");
1613 exit(0);
1614 }
1615#ifndef NDEBUG
1616 else
1617 {
1618 fprintf(stderr, "\nclose input device: successful\n");
1619 }
1620#endif
1621 }
1622
1623 if(sock_udp != 0)
1624 {
1625 if(cseq == 2)
1626 {
1627 size_send_buf = snprintf(send_buf, sizeof(send_buf),
1628 "TEARDOWN rtsp://%s%s/%s RTSP/1.0\r\n"
1629 "CSeq: 2\r\n"
1630 "Session: %d\r\n"
1631 "\r\n",
1632 caster_addr, rtsp_ext, mountpoint, session);
1633 if((size_send_buf >= (int)sizeof(send_buf)) || (size_send_buf < 0))
1634 {
1635 fprintf(stderr, "ERROR: Destination caster request to long\n");
1636 exit(0);
1637 }
1638 send_to_caster(send_buf, sock_tcp, size_send_buf); strcpy(send_buf,"");
1639 size_send_buf = recv(sock_tcp, send_buf, sizeof(send_buf), 0);
1640 send_buf[size_send_buf] = '\0';
1641#ifndef NDEBUG
1642 fprintf(stderr, "Destination caster response:\n%s", send_buf);
1643#endif
1644 }
1645 if(close(sock_udp)==-1)
1646 {
1647 perror("ERROR: close udp socket");
1648 exit(0);
1649 }
1650#ifndef NDEBUG
1651 else
1652 {
1653 fprintf(stderr, "close udp socket: successful\n");
1654 }
1655#endif
1656 }
1657
1658 if(close(sock_tcp)==-1)
1659 {
1660 perror("ERROR: close tcp socket");
1661 exit(0);
1662 }
1663#ifndef NDEBUG
1664 else
1665 {
1666 fprintf(stderr, "close tcp socket: successful\n\n");
1667 }
1668#endif
1669} /* close_session */
Note: See TracBrowser for help on using the repository browser.