source: ntrip/trunk/ntripserver/ntripserver.c@ 583

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

unified a bit

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