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

Last change on this file since 1570 was 1570, checked in by stoecker, 15 years ago

added plain UDP mode

File size: 73.1 KB
Line 
1/*
2 * $Id: ntripserver.c,v 1.45 2008/09/11 09:18:23 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.45 $";
40static char datestr[] = "$Date: 2008/09/11 09:18:23 $";
41
42#include <ctype.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <getopt.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <sys/time.h>
50#include <sys/types.h>
51#include <time.h>
52#include <signal.h>
53#include <unistd.h>
54
55#ifdef WINDOWSVERSION
56 #include <winsock2.h>
57 #include <io.h>
58 #include <sys/stat.h>
59 #include <windows.h>
60 typedef SOCKET sockettype;
61 typedef u_long in_addr_t;
62 typedef size_t socklen_t;
63 typedef u_short uint16_t;
64#else
65 typedef int sockettype;
66 #include <arpa/inet.h>
67 #include <sys/socket.h>
68 #include <netinet/in.h>
69 #include <netdb.h>
70 #include <sys/termios.h>
71 #define closesocket(sock) close(sock)
72 #define INVALID_HANDLE_VALUE -1
73 #define INVALID_SOCKET -1
74#endif
75
76#ifndef COMPILEDATE
77#define COMPILEDATE " built " __DATE__
78#endif
79
80#define ALARMTIME (2*60)
81
82#ifndef MSG_DONTWAIT
83#define MSG_DONTWAIT 0 /* prevent compiler errors */
84#endif
85#ifndef O_EXLOCK
86#define O_EXLOCK 0 /* prevent compiler errors */
87#endif
88
89enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
90CASTER = 6, LAST };
91
92enum OUTMODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, UDP = 4, END };
93
94#define AGENTSTRING "NTRIP NtripServerPOSIX"
95#define BUFSZ 1024
96#define SZ 64
97
98/* default socket source */
99#define SERV_HOST_ADDR "localhost"
100#define SERV_TCP_PORT 2101
101
102/* default destination */
103#define NTRIP_CASTER "www.euref-ip.net"
104#define NTRIP_PORT 2101
105
106#define SISNET_SERVER "131.176.49.142"
107#define SISNET_PORT 7777
108
109#define RTP_VERSION 2
110#define TIME_RESOLUTION 125
111
112static int ttybaud = 19200;
113static const char *ttyport = "/dev/gps";
114static const char *filepath = "/dev/stdin";
115static enum MODE inputmode = INFILE;
116static int sisnet = 31;
117static int gps_file = -1;
118static sockettype gps_socket = INVALID_SOCKET;
119static sockettype socket_tcp = INVALID_SOCKET;
120static sockettype socket_udp = INVALID_SOCKET;
121#ifndef WINDOWSVERSION
122static int gps_serial = INVALID_HANDLE_VALUE;
123static int sigpipe_received = 0;
124#else
125HANDLE gps_serial = INVALID_HANDLE_VALUE;
126#endif
127static int sigalarm_received = 0;
128static int sigint_received = 0;
129static int reconnect_sec = 1;
130static const char * casterouthost = NTRIP_CASTER;
131static char rtsp_extension[SZ] = "";
132static const char * mountpoint = NULL;
133static int udp_cseq = 1;
134static int udp_tim, udp_seq, udp_init;
135
136/* Forward references */
137static void send_receive_loop(sockettype sock, int outmode,
138 struct sockaddr * pcasterRTP, socklen_t length, int rtpssrc);
139static void usage(int, char *);
140static int encode(char *buf, int size, const char *user, const char *pwd);
141static int send_to_caster(char *input, sockettype socket, int input_size);
142static void close_session(const char *caster_addr, const char *mountpoint,
143 int session, char *rtsp_ext, int fallback);
144static int reconnect(int rec_sec, int rec_sec_max);
145static void handle_sigint(int sig);
146static void setup_signal_handler(int sig, void (*handler)(int));
147#ifndef WINDOWSVERSION
148static int openserial(const char * tty, int blocksz, int baud);
149static void handle_sigpipe(int sig);
150static void handle_alarm(int sig);
151#else
152static HANDLE openserial(const char * tty, int baud);
153#endif
154
155
156/*
157* main
158*
159* Main entry point for the program. Processes command-line arguments and
160* prepares for action.
161*
162* Parameters:
163* argc : integer : Number of command-line arguments.
164* argv : array of char : Command-line arguments as an array of
165* zero-terminated pointers to strings.
166*
167* Return Value:
168* The function does not return a value (although its return type is int).
169*
170* Remarks:
171*
172*/
173
174int main(int argc, char **argv)
175{
176 int c;
177 int size = 2048; /* for setting send buffer size */
178 struct sockaddr_in caster;
179 const char * proxyhost = "";
180 unsigned int proxyport = 0;
181 /*** INPUT ***/
182 const char * casterinhost = 0;
183 unsigned int casterinport = 0;
184 const char * inhost = 0;
185 unsigned int inport = 0;
186
187 char get_extension[SZ] = "";
188
189 struct hostent * he;
190
191 const char * sisnetpassword = "";
192 const char * sisnetuser = "";
193
194 const char * stream_name = 0;
195 const char * stream_user = 0;
196 const char * stream_password = 0;
197
198 const char * recvrid= 0;
199 const char * recvrpwd = 0;
200
201 const char * initfile = NULL;
202
203 int bindmode = 0;
204
205 /*** OUTPUT ***/
206 unsigned int casteroutport = NTRIP_PORT;
207 const char * outhost = 0;
208 unsigned int outport = 0;
209 char post_extension[SZ] = "";
210
211 const char * ntrip_str = "";
212
213 const char * user = "";
214 const char * password = "";
215
216 int outputmode = NTRIP1;
217
218 struct sockaddr_in casterRTP;
219 struct sockaddr_in local;
220 int client_port = 0;
221 int server_port = 0;
222 int session = 0;
223 socklen_t len = 0;
224 int i = 0;
225
226 char szSendBuffer[BUFSZ];
227 char authorization[SZ];
228 int nBufferBytes = 0;
229 char * dlim = " \r\n=";
230 char * token;
231 char * tok_buf[BUFSZ];
232
233 int reconnect_sec_max = 0;
234
235 setbuf(stdout, 0);
236 setbuf(stdin, 0);
237 setbuf(stderr, 0);
238
239 {
240 char *a;
241 int i = 0;
242 for(a = revisionstr+11; *a && *a != ' '; ++a)
243 revisionstr[i++] = *a;
244 revisionstr[i] = 0;
245 datestr[0] = datestr[7];
246 datestr[1] = datestr[8];
247 datestr[2] = datestr[9];
248 datestr[3] = datestr[10];
249 datestr[5] = datestr[12];
250 datestr[6] = datestr[13];
251 datestr[8] = datestr[15];
252 datestr[9] = datestr[16];
253 datestr[4] = datestr[7] = '-';
254 datestr[10] = 0;
255 }
256
257 /* setup signal handler for CTRL+C */
258 setup_signal_handler(SIGINT, handle_sigint);
259#ifndef WINDOWSVERSION
260 /* setup signal handler for boken pipe */
261 setup_signal_handler(SIGPIPE, handle_sigpipe);
262 /* setup signal handler for timeout */
263 setup_signal_handler(SIGALRM, handle_alarm);
264 alarm(ALARMTIME);
265#else
266 /* winsock initialization */
267 WSADATA wsaData;
268 if (WSAStartup(MAKEWORD(1,1), &wsaData))
269 {
270 fprintf(stderr, "Could not init network access.\n");
271 return 20;
272 }
273#endif
274
275 /* get and check program arguments */
276 if(argc <= 1)
277 {
278 usage(2, argv[0]);
279 exit(1);
280 }
281 while((c = getopt(argc, argv,
282 "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)
283 {
284 switch (c)
285 {
286 case 'M': /*** InputMode ***/
287 if(!strcmp(optarg, "serial")) inputmode = SERIAL;
288 else if(!strcmp(optarg, "tcpsocket")) inputmode = TCPSOCKET;
289 else if(!strcmp(optarg, "file")) inputmode = INFILE;
290 else if(!strcmp(optarg, "sisnet")) inputmode = SISNET;
291 else if(!strcmp(optarg, "udpsocket")) inputmode = UDPSOCKET;
292 else if(!strcmp(optarg, "caster")) inputmode = CASTER;
293 else inputmode = atoi(optarg);
294 if((inputmode == 0) || (inputmode >= LAST))
295 {
296 fprintf(stderr, "ERROR: can't convert <%s> to a valid InputMode\n",
297 optarg);
298 usage(-1, argv[0]);
299 }
300 break;
301 case 'i': /* serial input device */
302 ttyport = optarg;
303 break;
304 case 'B': /* bind to incoming UDP stream */
305 bindmode = 1;
306 break;
307 case 'V': /* Sisnet data server version number */
308 if(!strcmp("3.0", optarg)) sisnet = 30;
309 else if(!strcmp("3.1", optarg)) sisnet = 31;
310 else if(!strcmp("2.1", optarg)) sisnet = 21;
311 else
312 {
313 fprintf(stderr, "ERROR: unknown SISNeT version <%s>\n", optarg);
314 usage(-2, argv[0]);
315 }
316 break;
317 case 'b': /* serial input baud rate */
318 ttybaud = atoi(optarg);
319 if(ttybaud <= 1)
320 {
321 fprintf(stderr, "ERROR: can't convert <%s> to valid serial baud rate\n",
322 optarg);
323 usage(1, argv[0]);
324 }
325 break;
326 case 'a': /* Destination caster address */
327 casterouthost = optarg;
328 break;
329 case 'p': /* Destination caster port */
330 casteroutport = atoi(optarg);
331 if(casteroutport <= 1 || casteroutport > 65535)
332 {
333 fprintf(stderr,
334 "ERROR: can't convert <%s> to a valid HTTP server port\n", optarg);
335 usage(1, argv[0]);
336 }
337 break;
338 case 'm': /* Destination caster mountpoint for stream upload */
339 mountpoint = optarg;
340 break;
341 case 's': /* File name for input data simulation from file */
342 filepath = optarg;
343 break;
344 case 'f': /* name of an initialization file */
345 initfile = optarg;
346 break;
347 case 'x': /* user ID to access incoming stream */
348 recvrid = optarg;
349 break;
350 case 'y': /* password to access incoming stream */
351 recvrpwd = optarg;
352 break;
353 case 'u': /* Sisnet data server user ID */
354 sisnetuser = optarg;
355 break;
356 case 'l': /* Sisnet data server password */
357 sisnetpassword = optarg;
358 break;
359 case 'c': /* DestinationCaster password for stream upload to mountpoint */
360 password = optarg;
361 break;
362 case 'H': /* Input host address*/
363 casterinhost = optarg;
364 break;
365 case 'P': /* Input port */
366 casterinport = atoi(optarg);
367 if(casterinport <= 1 || casterinport > 65535)
368 {
369 fprintf(stderr, "ERROR: can't convert <%s> to a valid port number\n",
370 optarg);
371 usage(1, argv[0]);
372 }
373 break;
374 case 'D': /* Source caster mountpoint for stream input */
375 stream_name = optarg;
376 break;
377 case 'U': /* Source caster user ID for input stream access */
378 stream_user = optarg;
379 break;
380 case 'W': /* Source caster password for input stream access */
381 stream_password = optarg;
382 break;
383 case 'E': /* Proxy Server */
384 proxyhost = optarg;
385 break;
386 case 'F': /* Proxy port */
387 proxyport = atoi(optarg);
388 break;
389 case 'R': /* maximum delay between reconnect attempts in seconds */
390 reconnect_sec_max = atoi(optarg);
391 break;
392 case 'O': /* OutputMode */
393 outputmode = 0;
394 if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
395 outputmode = NTRIP1;
396 else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
397 outputmode = HTTP;
398 else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
399 outputmode = RTSP;
400 else if(!strcmp(optarg,"u") || !strcmp(optarg,"udp"))
401 outputmode = UDP;
402 else outputmode = atoi(optarg);
403 if((outputmode == 0) || (outputmode >= END))
404 {
405 fprintf(stderr, "ERROR: can't convert <%s> to a valid OutputMode\n",
406 optarg);
407 usage(-1, argv[0]);
408 }
409 break;
410 case 'n': /* Destination caster user ID for stream upload to mountpoint */
411 user = optarg;
412 break;
413 case 'N': /* Ntrip-STR, optional for Ntrip Version 2.0 */
414 ntrip_str = optarg;
415 break;
416 case 'h': /* print help screen */
417 case '?':
418 usage(0, argv[0]);
419 break;
420 default:
421 usage(2, argv[0]);
422 break;
423 }
424 }
425
426 argc -= optind;
427 argv += optind;
428
429 /*** argument analysis ***/
430 if(argc > 0)
431 {
432 fprintf(stderr, "ERROR: Extra args on command line: ");
433 for(; argc > 0; argc--)
434 {
435 fprintf(stderr, " %s", *argv++);
436 }
437 fprintf(stderr, "\n");
438 usage(1, argv[0]); /* never returns */
439 }
440
441 if(outputmode != NTRIP1)
442 {
443 fprintf(stderr, "\nWARNING: *** NTRIP VERSION 2 PROTOCOL IS STILL"
444 " BETA AND MAY BE CHANGED ***\n\n");
445 }
446
447 if(ntrip_str && (outputmode == NTRIP1))
448 {
449 fprintf(stderr, "WARNING: OutputMode is Ntrip version 1.0"
450 " - Ntrip-STR will not be considered\n");
451 }
452
453 if((reconnect_sec_max > 0) && (reconnect_sec_max < 256))
454 {
455 fprintf(stderr,
456 "WARNING: maximum delay between reconnect attemts changed from %d to 256 seconds\n"
457 , reconnect_sec_max);
458 reconnect_sec_max = 256;
459 }
460
461 if(!mountpoint)
462 {
463 fprintf(stderr, "ERROR: Missing mountpoint argument for stream upload\n");
464 exit(1);
465 }
466
467 if(!password[0])
468 {
469 fprintf(stderr, "WARNING: Missing password argument for stream upload - "
470 "are you really sure?\n");
471 }
472 else
473 {
474 nBufferBytes += encode(authorization, sizeof(authorization), user,
475 password);
476 if(nBufferBytes > (int)sizeof(authorization))
477 {
478 fprintf(stderr, "ERROR: user ID and/or password too long: %d (%d)\n"
479 " user ID: %s \npassword: <%s>\n",
480 nBufferBytes, (int)sizeof(authorization), user, password);
481 exit(1);
482 }
483 }
484
485 if(stream_name && stream_user && !stream_password)
486 {
487 fprintf(stderr, "WARNING: Missing password argument for stream download"
488 " - are you really sure?\n");
489 }
490
491 /*** proxy server handling ***/
492 if(*proxyhost)
493 {
494 outhost = inhost = proxyhost;
495 outport = inport = proxyport;
496 i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d",
497 casterouthost, casteroutport);
498 if((i > SZ) || (i < 0))
499 {
500 fprintf(stderr, "ERROR: Destination caster name/port to long - "
501 "length = %d (max: %d)\n", i, SZ);
502 exit(0);
503 }
504 else
505 {
506 strncpy(post_extension, szSendBuffer, (size_t)i);
507 strcpy(szSendBuffer, "");
508 i = snprintf(szSendBuffer, sizeof(szSendBuffer),":%d", casteroutport);
509 strncpy(rtsp_extension, szSendBuffer, SZ);
510 strcpy(szSendBuffer,""); i = 0;
511 }
512 i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d", casterinhost, casterinport);
513 if((i > SZ) || (i < 0))
514 {
515 fprintf(stderr,"ERROR: Destination caster name/port to long - length = %d (max: %d)\n", i, SZ);
516 exit(0);
517 }
518 else
519 {
520 strncpy(get_extension, szSendBuffer, (size_t)i);
521 strcpy(szSendBuffer, "");
522 i = 0;
523 }
524 }
525 else
526 {
527 outhost = casterouthost; outport = casteroutport;
528 inhost = casterinhost; inport = casterinport;
529 }
530
531 while(inputmode != LAST)
532 {
533 int input_init = 1;
534 if(sigint_received) break;
535 /*** InputMode handling ***/
536 switch(inputmode)
537 {
538 case INFILE:
539 {
540 if((gps_file = open(filepath, O_RDONLY)) < 0)
541 {
542 perror("ERROR: opening input file");
543 exit(1);
544 }
545#ifndef WINDOWSVERSION
546 /* set blocking inputmode in case it was not set
547 (seems to be sometimes for fifo's) */
548 fcntl(gps_file, F_SETFL, 0);
549#endif
550 printf("file input: file = %s\n", filepath);
551 }
552 break;
553 case SERIAL: /* open serial port */
554 {
555#ifndef WINDOWSVERSION
556 gps_serial = openserial(ttyport, 1, ttybaud);
557#else
558 gps_serial = openserial(ttyport, ttybaud);
559#endif
560 if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
561 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
562
563 if(initfile)
564 {
565 char buffer[1024];
566 FILE *fh;
567 int i;
568
569 if((fh = fopen(initfile, "r")))
570 {
571 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
572 {
573#ifndef WINDOWSVERSION
574 if((write(gps_serial, buffer, i)) != i)
575 {
576 perror("WARNING: sending init file");
577 input_init = 0;
578 break;
579 }
580#else
581 DWORD nWrite = -1;
582 if(!WriteFile(gps_serial, buffer, sizeof(buffer), &nWrite, NULL))
583 {
584 fprintf(stderr,"ERROR: sending init file \n");
585 input_init = 0;
586 break;
587 }
588 i = (int)nWrite;
589#endif
590 }
591 if(i < 0)
592 {
593 perror("ERROR: reading init file");
594 reconnect_sec_max = 0;
595 input_init = 0;
596 break;
597 }
598 fclose(fh);
599 }
600 else
601 {
602 fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
603 reconnect_sec_max = 0;
604 input_init = 0;
605 break;
606 }
607 }
608 }
609 break;
610 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
611 {
612 if(inputmode == SISNET)
613 {
614 if(!inhost) inhost = SISNET_SERVER;
615 if(!inport) inport = SISNET_PORT;
616 }
617 else if(inputmode == CASTER)
618 {
619 if(!inport) inport = NTRIP_PORT;
620 if(!inhost) inhost = NTRIP_CASTER;
621 }
622 else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
623 {
624 if(!inport) inport = SERV_TCP_PORT;
625 if(!inhost) inhost = SERV_HOST_ADDR;
626 }
627
628 if(!(he = gethostbyname(inhost)))
629 {
630 fprintf(stderr, "ERROR: Input host <%s> unknown\n", inhost);
631 usage(-2, argv[0]);
632 }
633
634 if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
635 ? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
636 {
637 fprintf(stderr,
638 "ERROR: can't create socket for incoming data stream\n");
639 exit(1);
640 }
641
642 memset((char *) &caster, 0x00, sizeof(caster));
643 if(!bindmode)
644 memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
645 caster.sin_family = AF_INET;
646 caster.sin_port = htons(inport);
647
648 fprintf(stderr, "%s input: host = %s, port = %d, %s%s%s%s%s\n",
649 inputmode == CASTER ? "caster" : inputmode == SISNET ? "sisnet" :
650 inputmode == TCPSOCKET ? "tcp socket" : "udp socket",
651 bindmode ? "127.0.0.1" : inet_ntoa(caster.sin_addr),
652 inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
653 initfile ? ", initfile = " : "", initfile ? initfile : "",
654 bindmode ? "binding mode" : "");
655
656 if(bindmode)
657 {
658 if(bind(gps_socket, (struct sockaddr *) &caster, sizeof(caster)) < 0)
659 {
660 fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
661 reconnect_sec_max = 0;
662 input_init = 0;
663 break;
664 }
665 } /* connect to input-caster or proxy server*/
666 else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
667 {
668 fprintf(stderr, "WARNING: can't connect input to %s at port %d\n",
669 inet_ntoa(caster.sin_addr), inport);
670 input_init = 0;
671 break;
672 }
673
674 if(stream_name) /* input from Ntrip Version 1.0 caster*/
675 {
676 int init = 0;
677
678 /* set socket buffer size */
679 setsockopt(gps_socket, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
680 sizeof(const char *));
681 if(stream_user && stream_password)
682 {
683 /* leave some space for login */
684 nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
685 "GET %s/%s HTTP/1.0\r\n"
686 "User-Agent: %s/%s\r\n"
687 "Connection: close\r\n"
688 "Authorization: Basic ", get_extension, stream_name,
689 AGENTSTRING, revisionstr);
690 /* second check for old glibc */
691 if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
692 {
693 fprintf(stderr, "ERROR: Source caster request too long\n");
694 input_init = 0;
695 reconnect_sec_max =0;
696 break;
697 }
698 nBufferBytes += encode(szSendBuffer+nBufferBytes,
699 sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
700 if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
701 {
702 fprintf(stderr,
703 "ERROR: Source caster user ID and/or password too long\n");
704 input_init = 0;
705 reconnect_sec_max =0;
706 break;
707 }
708 szSendBuffer[nBufferBytes++] = '\r';
709 szSendBuffer[nBufferBytes++] = '\n';
710 szSendBuffer[nBufferBytes++] = '\r';
711 szSendBuffer[nBufferBytes++] = '\n';
712 }
713 else
714 {
715 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
716 "GET %s/%s HTTP/1.0\r\n"
717 "User-Agent: %s/%s\r\n"
718 "Connection: close\r\n"
719 "\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
720 }
721 if((send(gps_socket, szSendBuffer, (size_t)nBufferBytes, 0))
722 != nBufferBytes)
723 {
724 fprintf(stderr, "WARNING: could not send Source caster request\n");
725 input_init = 0;
726 break;
727 }
728 nBufferBytes = 0;
729 /* check Source caster's response */
730 while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
731 && (nBufferBytes += recv(gps_socket, szSendBuffer,
732 sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
733 {
734 if(strstr(szSendBuffer, "\r\n"))
735 {
736 if(!strstr(szSendBuffer, "ICY 200 OK"))
737 {
738 int k;
739 fprintf(stderr,
740 "ERROR: could not get requested data from Source caster: ");
741 for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
742 && szSendBuffer[k] != '\r'; ++k)
743 {
744 fprintf(stderr, "%c", isprint(szSendBuffer[k])
745 ? szSendBuffer[k] : '.');
746 }
747 fprintf(stderr, "\n");
748 if(!strstr(szSendBuffer, "SOURCETABLE 200 OK"))
749 {
750 reconnect_sec_max =0;
751 }
752 input_init = 0;
753 break;
754 }
755 else init = 1;
756 }
757 }
758 } /* end input from Ntrip Version 1.0 caster */
759
760 if(initfile && inputmode != SISNET)
761 {
762 char buffer[1024];
763 FILE *fh;
764 int i;
765
766 if((fh = fopen(initfile, "r")))
767 {
768 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
769 {
770 if((send(gps_socket, buffer, (size_t)i, 0)) != i)
771 {
772 perror("WARNING: sending init file");
773 input_init = 0;
774 break;
775 }
776 }
777 if(i < 0)
778 {
779 perror("ERROR: reading init file");
780 reconnect_sec_max = 0;
781 input_init = 0;
782 break;
783 }
784 fclose(fh);
785 }
786 else
787 {
788 fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
789 reconnect_sec_max = 0;
790 input_init = 0;
791 break;
792 }
793 }
794 }
795 if(inputmode == SISNET)
796 {
797 int i, j;
798 char buffer[1024];
799
800 i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
801 : "AUTH,%s,%s", sisnetuser, sisnetpassword);
802 if((send(gps_socket, buffer, (size_t)i, 0)) != i)
803 {
804 perror("WARNING: sending authentication for SISNeT data server");
805 input_init = 0;
806 break;
807 }
808 i = sisnet >= 30 ? 7 : 5;
809 if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
810 {
811 fprintf(stderr, "WARNING: SISNeT connect failed:");
812 for(i = 0; i < j; ++i)
813 {
814 if(buffer[i] != '\r' && buffer[i] != '\n')
815 {
816 fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
817 }
818 }
819 fprintf(stderr, "\n");
820 input_init = 0;
821 break;
822 }
823 if(sisnet >= 31)
824 {
825 if((send(gps_socket, "START\r\n", 7, 0)) != i)
826 {
827 perror("WARNING: sending Sisnet start command");
828 input_init = 0;
829 break;
830 }
831 }
832 }
833 /*** receiver authentication ***/
834 if (recvrid && recvrpwd && ((inputmode == TCPSOCKET)
835 || (inputmode == UDPSOCKET)))
836 {
837 if (strlen(recvrid) > (BUFSZ-3))
838 {
839 fprintf(stderr, "ERROR: Receiver ID too long\n");
840 reconnect_sec_max = 0;
841 input_init = 0;
842 break;
843 }
844 else
845 {
846 fprintf(stderr, "Sending user ID for receiver...\n");
847 nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
848 strcpy(szSendBuffer, recvrid);
849 strcat(szSendBuffer,"\r\n");
850 if(send(gps_socket,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
851 {
852 perror("WARNING: sending user ID for receiver");
853 input_init = 0;
854 break;
855 }
856 }
857
858 if (strlen(recvrpwd) > (BUFSZ-3))
859 {
860 fprintf(stderr, "ERROR: Receiver password too long\n");
861 reconnect_sec_max = 0;
862 input_init = 0;
863 break;
864 }
865 else
866 {
867 fprintf(stderr, "Sending user password for receiver...\n");
868 nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
869 strcpy(szSendBuffer, recvrpwd);
870 strcat(szSendBuffer,"\r\n");
871 if(send(gps_socket, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
872 {
873 perror("WARNING: sending user password for receiver");
874 input_init = 0;
875 break;
876 }
877 }
878 }
879 break;
880 default:
881 usage(-1, argv[0]);
882 break;
883 }
884
885 /* ----- main part ----- */
886 int output_init = 1, fallback = 0;
887
888 while((input_init) && (output_init))
889 {
890#ifndef WINDOWSVERSION
891 if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
892#else
893 if((sigalarm_received) || (sigint_received)) break;
894#endif
895 if(!(he = gethostbyname(outhost)))
896 {
897 fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
898 outhost);
899 close_session(casterouthost, mountpoint, session, rtsp_extension, 0);
900 usage(-2, argv[0]);
901 }
902
903 /* create socket */
904 if((socket_tcp = socket(AF_INET, (outputmode == UDP ? SOCK_DGRAM
905 : SOCK_STREAM), 0)) == INVALID_SOCKET)
906 {
907 perror("ERROR: tcp socket");
908 reconnect_sec_max = 0;
909 break;
910 }
911
912 memset((char *) &caster, 0x00, sizeof(caster));
913 memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
914 caster.sin_family = AF_INET;
915 caster.sin_port = htons(outport);
916
917 /* connect to Destination caster or Proxy server*/
918 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s"
919 ", mode = %s\n\n", inet_ntoa(caster.sin_addr), outport, mountpoint,
920 outputmode == NTRIP1 ? "ntrip1" : outputmode == HTTP ? "http" :
921 outputmode == UDP ? "udp" : "rtsp");
922
923 if(connect(socket_tcp, (struct sockaddr *) &caster, sizeof(caster)) < 0)
924 {
925 fprintf(stderr, "WARNING: can't connect output to %s at port %d\n",
926 inet_ntoa(caster.sin_addr), outport);
927 break;
928 }
929
930 /*** OutputMode handling ***/
931 switch(outputmode)
932 {
933 case UDP:
934 {
935 int session;
936 char rtpbuf[1526];
937 int i=12, j;
938
939 udp_init = time(0);
940 srand(udp_init);
941 session = rand();
942 udp_tim = rand();
943 udp_seq = rand();
944
945 rtpbuf[0] = (2<<6);
946 /* padding, extension, csrc are empty */
947 rtpbuf[1] = 97;
948 /* marker is empty */
949 rtpbuf[2] = (udp_seq>>8)&0xFF;
950 rtpbuf[3] = (udp_seq)&0xFF;
951 rtpbuf[4] = (udp_tim>>24)&0xFF;
952 rtpbuf[5] = (udp_tim>>16)&0xFF;
953 rtpbuf[6] = (udp_tim>>8)&0xFF;
954 rtpbuf[7] = (udp_tim)&0xFF;
955 /* sequence and timestamp are empty */
956 rtpbuf[8] = (session>>24)&0xFF;
957 rtpbuf[9] = (session>>16)&0xFF;
958 rtpbuf[10] = (session>>8)&0xFF;
959 rtpbuf[11] = (session)&0xFF;
960 ++udp_seq;
961
962 j = snprintf(rtpbuf+i, sizeof(rtpbuf)-i-40, /* leave some space for login */
963 "POST /%s HTTP/1.1\r\n"
964 "Host: %s\r\n"
965 "Ntrip-Version: Ntrip/2.0\r\n"
966 "User-Agent: %s/%s\r\n"
967 "Authorization: Basic %s%s%s\r\n"
968 "Connection: close\r\n"
969 "Transfer-Encoding: chunked\r\n\r\n",
970 mountpoint, casterouthost, AGENTSTRING,
971 revisionstr, authorization, ntrip_str ? "\r\nNtrip-STR: " : "",
972 ntrip_str);
973 i += j;
974 if(i > (int)sizeof(rtpbuf)-40 || j < 0) /* second check for old glibc */
975 {
976 fprintf(stderr, "Requested data too long\n");
977 reconnect_sec_max = 0;
978 output_init = 0;
979 break;
980 }
981 else
982 {
983 rtpbuf[i++] = '\r';
984 rtpbuf[i++] = '\n';
985 rtpbuf[i++] = '\r';
986 rtpbuf[i++] = '\n';
987
988 if(send(socket_tcp, rtpbuf, i, 0) != i)
989 {
990 perror("Could not send UDP packet");
991 reconnect_sec_max = 0;
992 output_init = 0;
993 break;
994 }
995 else
996 {
997 int stop = 0;
998 int numbytes;
999 if((numbytes=recv(socket_tcp, rtpbuf, sizeof(rtpbuf)-1, 0)) > 0)
1000 {
1001 /* we don't expect message longer than 1513, so we cut the last
1002 byte for security reasons to prevent buffer overrun */
1003 rtpbuf[numbytes] = 0;
1004 if(numbytes > 17+12 &&
1005 (!strncmp(rtpbuf+12, "HTTP/1.1 200 OK\r\n", 17) ||
1006 !strncmp(rtpbuf+12, "HTTP/1.0 200 OK\r\n", 17)))
1007 {
1008 const char *sessioncheck = "session: ";
1009 int l = strlen(sessioncheck)-1;
1010 int j=0;
1011 for(i = 12; j != l && i < numbytes-l; ++i)
1012 {
1013 for(j = 0; j < l && tolower(rtpbuf[i+j]) == sessioncheck[j]; ++j)
1014 ;
1015 }
1016 if(i != numbytes-l) /* found a session number */
1017 {
1018 i+=l;
1019 session = 0;
1020 while(i < numbytes && rtpbuf[i] >= '0' && rtpbuf[i] <= '9')
1021 session = session * 10 + rtpbuf[i++]-'0';
1022 if(rtpbuf[i] != '\r')
1023 {
1024 fprintf(stderr, "Could not extract session number\n");
1025 stop = 1;
1026 }
1027 }
1028 }
1029 else
1030 {
1031 int k;
1032 fprintf(stderr, "Could not access mountpoint: ");
1033 for(k = 12; k < numbytes && rtpbuf[k] != '\n' && rtpbuf[k] != '\r'; ++k)
1034 {
1035 fprintf(stderr, "%c", isprint(rtpbuf[k]) ? rtpbuf[k] : '.');
1036 }
1037 fprintf(stderr, "\n");
1038 stop = 1;
1039 }
1040 }
1041 if(!stop)
1042 {
1043 send_receive_loop(socket_tcp, outputmode, NULL, 0, session);
1044 input_init = output_init = 0;
1045 /* send connection close always to allow nice session closing */
1046 udp_tim += (time(0)-udp_init)*1000000/TIME_RESOLUTION;
1047 rtpbuf[0] = (2<<6);
1048 /* padding, extension, csrc are empty */
1049 rtpbuf[1] = 98;
1050 /* marker is empty */
1051 rtpbuf[2] = (udp_seq>>8)&0xFF;
1052 rtpbuf[3] = (udp_seq)&0xFF;
1053 rtpbuf[4] = (udp_tim>>24)&0xFF;
1054 rtpbuf[5] = (udp_tim>>16)&0xFF;
1055 rtpbuf[6] = (udp_tim>>8)&0xFF;
1056 rtpbuf[7] = (udp_tim)&0xFF;
1057 /* sequence and timestamp are empty */
1058 rtpbuf[8] = (session>>24)&0xFF;
1059 rtpbuf[9] = (session>>16)&0xFF;
1060 rtpbuf[10] = (session>>8)&0xFF;
1061 rtpbuf[11] = (session)&0xFF;
1062
1063 send(socket_tcp, rtpbuf, 12, 0); /* cleanup */
1064 }
1065 else
1066 {
1067 reconnect_sec_max = 600;
1068 output_init = 0;
1069 }
1070 }
1071 }
1072 }
1073 break;
1074 case NTRIP1: /*** OutputMode Ntrip Version 1.0 ***/
1075 fallback = 0;
1076 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1077 "SOURCE %s %s/%s\r\n"
1078 "Source-Agent: %s/%s\r\n\r\n",
1079 password, post_extension, mountpoint, AGENTSTRING, revisionstr);
1080 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
1081 {
1082 fprintf(stderr, "ERROR: Destination caster request to long\n");
1083 reconnect_sec_max = 0;
1084 output_init = 0;
1085 break;
1086 }
1087 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1088 {
1089 output_init = 0;
1090 break;
1091 }
1092 /* check Destination caster's response */
1093 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
1094 szSendBuffer[nBufferBytes] = '\0';
1095 if(!strstr(szSendBuffer, "OK"))
1096 {
1097 char *a;
1098 fprintf(stderr,
1099 "ERROR: Destination caster's or Proxy's reply is not OK: ");
1100 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
1101 {
1102 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
1103 }
1104 fprintf(stderr, "\n");
1105 if((strstr(szSendBuffer,"ERROR - Bad Password"))
1106 || (strstr(szSendBuffer,"400 Bad Request")))
1107 reconnect_sec_max = 0;
1108 output_init = 0;
1109 break;
1110 }
1111#ifndef NDEBUG
1112 else
1113 {
1114 fprintf(stderr, "Destination caster response:\n%s\n",
1115 szSendBuffer);
1116 }
1117#endif
1118 send_receive_loop(socket_tcp, outputmode, NULL, 0, 0);
1119 input_init = output_init = 0;
1120 break;
1121 case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
1122 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1123 "POST %s/%s HTTP/1.1\r\n"
1124 "Host: %s\r\n"
1125 "Ntrip-Version: Ntrip/2.0\r\n"
1126 "User-Agent: %s/%s\r\n"
1127 "Authorization: Basic %s%s%s\r\n"
1128 "Connection: close\r\n"
1129 "Transfer-Encoding: chunked\r\n\r\n",
1130 post_extension, mountpoint, casterouthost, AGENTSTRING,
1131 revisionstr, authorization, ntrip_str ? "\r\nNtrip-STR: " : "",
1132 ntrip_str);
1133 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
1134 {
1135 fprintf(stderr, "ERROR: Destination caster request to long\n");
1136 reconnect_sec_max = 0;
1137 output_init = 0;
1138 break;
1139 }
1140 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1141 {
1142 output_init = 0;
1143 break;
1144 }
1145 /* check Destination caster's response */
1146 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
1147 szSendBuffer[nBufferBytes] = '\0';
1148 if(!strstr(szSendBuffer, "HTTP/1.1 200 OK"))
1149 {
1150 char *a;
1151 fprintf(stderr,
1152 "ERROR: Destination caster's%s reply is not OK: ",
1153 *proxyhost ? " or Proxy's" : "");
1154 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
1155 {
1156 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
1157 }
1158 fprintf(stderr, "\n");
1159 /* fallback if necessary */
1160 if(!strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
1161 {
1162 fprintf(stderr,
1163 " Ntrip Version 2.0 not implemented at Destination caster"
1164 " <%s>%s%s%s\n%s\n"
1165 "ntripserver falls back to Ntrip Version 1.0\n\n",
1166 casterouthost,
1167 *proxyhost ? " or Proxy <" : "", proxyhost, *proxyhost ? ">" : "",
1168 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "");
1169 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
1170 outputmode = NTRIP1;
1171 break;
1172 }
1173 else if((strstr(szSendBuffer,"HTTP/1.1 401 Unauthorized"))
1174 || (strstr(szSendBuffer,"501 Not Implemented")))
1175 {
1176 reconnect_sec_max = 0;
1177 }
1178 output_init = 0;
1179 break;
1180 }
1181#ifndef NDEBUG
1182 else
1183 {
1184 fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
1185 }
1186#endif
1187 send_receive_loop(socket_tcp, outputmode, NULL, 0, 0);
1188 input_init = output_init = 0;
1189 break;
1190 case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
1191 if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)
1192 {
1193 perror("ERROR: udp socket");
1194 exit(4);
1195 }
1196 /* fill structure with local address information for UDP */
1197 memset(&local, 0, sizeof(local));
1198 local.sin_family = AF_INET;
1199 local.sin_port = htons(0);
1200 local.sin_addr.s_addr = htonl(INADDR_ANY);
1201 len = (socklen_t)sizeof(local);
1202 /* bind() in order to get a random RTP client_port */
1203 if((bind(socket_udp,(struct sockaddr *)&local, len)) < 0)
1204 {
1205 perror("ERROR: udp bind");
1206 reconnect_sec_max = 0;
1207 output_init = 0;
1208 break;
1209 }
1210 if((getsockname(socket_udp, (struct sockaddr*)&local, &len)) != -1)
1211 {
1212 client_port = (unsigned int)ntohs(local.sin_port);
1213 }
1214 else
1215 {
1216 perror("ERROR: getsockname(localhost)");
1217 reconnect_sec_max = 0;
1218 output_init = 0;
1219 break;
1220 }
1221 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1222 "SETUP rtsp://%s%s/%s RTSP/1.0\r\n"
1223 "CSeq: %d\r\n"
1224 "Ntrip-Version: Ntrip/2.0\r\n"
1225 "Ntrip-Component: Ntripserver\r\n"
1226 "User-Agent: %s/%s\r\n"
1227 "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
1228 "Authorization: Basic %s%s%s\r\n\r\n",
1229 casterouthost, rtsp_extension, mountpoint, udp_cseq++, AGENTSTRING,
1230 revisionstr, client_port, authorization, ntrip_str
1231 ? "\r\nNtrip-STR: " : "", ntrip_str);
1232 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
1233 {
1234 fprintf(stderr, "ERROR: Destination caster request to long\n");
1235 reconnect_sec_max = 0;
1236 output_init = 0;
1237 break;
1238 }
1239 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1240 {
1241 output_init = 0;
1242 break;
1243 }
1244 while((nBufferBytes = recv(socket_tcp, szSendBuffer,
1245 sizeof(szSendBuffer), 0)) > 0)
1246 {
1247 /* check Destination caster's response */
1248 szSendBuffer[nBufferBytes] = '\0';
1249 if(!strstr(szSendBuffer, "RTSP/1.0 200 OK"))
1250 {
1251 char *a;
1252 fprintf(stderr,
1253 "ERROR: Destination caster's%s reply is not OK: ",
1254 *proxyhost ? " or Proxy's" : "");
1255 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
1256 {
1257 fprintf(stderr, "%c", isprint(*a) ? *a : '.');
1258 }
1259 fprintf(stderr, "\n");
1260 /* fallback if necessary */
1261 if(strncmp(szSendBuffer, "RTSP",4) != 0)
1262 {
1263 if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
1264 {
1265 fprintf(stderr,
1266 " RTSP not implemented at Destination caster <%s>%s%s%s\n\n"
1267 "ntripserver falls back to Ntrip Version 2.0 in TCP/IP"
1268 " mode\n\n", casterouthost,
1269 *proxyhost ? " or Proxy <" :"", proxyhost, *proxyhost ? ">":"");
1270 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
1271 outputmode = HTTP;
1272 fallback = 1;
1273 break;
1274 }
1275 else
1276 {
1277 fprintf(stderr,
1278 " Ntrip-Version 2.0 not implemented at Destination caster"
1279 "<%s>%s%s%s\n%s"
1280 " or RTSP/1.0 not implemented at Destination caster%s\n\n"
1281 "ntripserver falls back to Ntrip Version 1.0\n\n",
1282 casterouthost, *proxyhost ? " or Proxy <" :"", proxyhost,
1283 *proxyhost ? ">":"",
1284 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "",
1285 *proxyhost ? " or Proxy" :"");
1286 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
1287 outputmode = NTRIP1;
1288 fallback = 1;
1289 break;
1290 }
1291 }
1292 else if((strstr(szSendBuffer, "RTSP/1.0 401 Unauthorized"))
1293 || (strstr(szSendBuffer, "RTSP/1.0 501 Not Implemented")))
1294 {
1295 reconnect_sec_max = 0;
1296 }
1297 output_init = 0;
1298 break;
1299 }
1300#ifndef NDEBUG
1301 else
1302 {
1303 fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
1304 }
1305#endif
1306 if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n"))
1307 && (strstr(szSendBuffer,"CSeq: 1\r\n")))
1308 {
1309 for(token = strtok(szSendBuffer, dlim); token != NULL;
1310 token = strtok(NULL, dlim))
1311 {
1312 tok_buf[i] = token; i++;
1313 }
1314 session = atoi(tok_buf[6]);
1315 server_port = atoi(tok_buf[10]);
1316 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1317 "RECORD rtsp://%s%s/%s RTSP/1.0\r\n"
1318 "CSeq: %d\r\n"
1319 "Session: %d\r\n"
1320 "\r\n",
1321 casterouthost, rtsp_extension, mountpoint, udp_cseq++,
1322 session);
1323 if((nBufferBytes >= (int)sizeof(szSendBuffer))
1324 || (nBufferBytes < 0))
1325 {
1326 fprintf(stderr, "ERROR: Destination caster request to long\n");
1327 reconnect_sec_max = 0;
1328 output_init = 0;
1329 break;
1330 }
1331 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1332 {
1333 output_init = 0;
1334 break;
1335 }
1336 }
1337 else if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n")) && (strstr(szSendBuffer,
1338 "CSeq: 2\r\n")))
1339 {
1340 /* fill structure with caster address information for UDP */
1341 memset(&casterRTP, 0, sizeof(casterRTP));
1342 casterRTP.sin_family = AF_INET;
1343 casterRTP.sin_port = htons(((uint16_t)server_port));
1344 if((he = gethostbyname(outhost))== NULL)
1345 {
1346 fprintf(stderr, "ERROR: Destination caster unknown\n");
1347 reconnect_sec_max = 0;
1348 output_init = 0;
1349 break;
1350 }
1351 else
1352 {
1353 memcpy((char *)&casterRTP.sin_addr.s_addr,
1354 he->h_addr_list[0], (size_t)he->h_length);
1355 }
1356 len = (socklen_t)sizeof(casterRTP);
1357 send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP,
1358 (socklen_t)len, session);
1359 break;
1360 }
1361 else{break;}
1362 }
1363 input_init = output_init = 0;
1364 break;
1365 }
1366 }
1367 close_session(casterouthost, mountpoint, session, rtsp_extension, 0);
1368 if( (reconnect_sec_max || fallback) && !sigint_received )
1369 reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
1370 else inputmode = LAST;
1371 }
1372 return 0;
1373}
1374
1375static void send_receive_loop(sockettype sock, int outmode, struct sockaddr* pcasterRTP,
1376socklen_t length, int rtpssrc)
1377{
1378 int nodata = 0;
1379 char buffer[BUFSZ] = { 0 };
1380 char sisnetbackbuffer[200];
1381 char szSendBuffer[BUFSZ] = "";
1382 int nBufferBytes = 0;
1383
1384 /* RTSP / RTP Mode */
1385 int isfirstpacket = 1;
1386 struct timeval now;
1387 struct timeval last = {0,0};
1388 long int sendtimediff;
1389 int rtpseq = 0;
1390 int rtptime = 0;
1391 time_t laststate = time(0);
1392
1393 if(outmode == UDP)
1394 {
1395 rtptime = time(0);
1396#ifdef WINDOWSVERSION
1397 u_long blockmode = 1;
1398 if(ioctlsocket(socket_tcp, FIONBIO, &blockmode))
1399#else /* WINDOWSVERSION */
1400 if(fcntl(socket_tcp, F_SETFL, O_NONBLOCK) < 0)
1401#endif /* WINDOWSVERSION */
1402 {
1403 fprintf(stderr, "Could not set nonblocking mode\n");
1404 return;
1405 }
1406 }
1407 else if(outmode == RTSP)
1408 {
1409#ifdef WINDOWSVERSION
1410 u_long blockmode = 1;
1411 if(ioctlsocket(socket_tcp, FIONBIO, &blockmode))
1412#else /* WINDOWSVERSION */
1413 if(fcntl(socket_tcp, F_SETFL, O_NONBLOCK) < 0)
1414#endif /* WINDOWSVERSION */
1415 {
1416 fprintf(stderr, "Could not set nonblocking mode\n");
1417 return;
1418 }
1419 }
1420
1421 /* data transmission */
1422 fprintf(stderr,"transfering data ...\n");
1423 int send_recv_success = 0;
1424#ifdef WINDOWSVERSION
1425 time_t nodata_begin = 0, nodata_current = 0;
1426#endif
1427 while(1)
1428 {
1429 if(send_recv_success < 3) send_recv_success++;
1430 if(!nodata)
1431 {
1432#ifndef WINDOWSVERSION
1433 alarm(ALARMTIME);
1434#else
1435 time(&nodata_begin);
1436#endif
1437 }
1438 else
1439 {
1440 nodata = 0;
1441#ifdef WINDOWSVERSION
1442 time(&nodata_current);
1443 if(difftime(nodata_current, nodata_begin) >= ALARMTIME)
1444 {
1445 sigalarm_received = 1;
1446 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
1447 }
1448#endif
1449 }
1450 /* signal handling*/
1451#ifdef WINDOWSVERSION
1452 if((sigalarm_received) || (sigint_received)) break;
1453#else
1454 if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
1455#endif
1456 if(!nBufferBytes)
1457 {
1458 if(inputmode == SISNET && sisnet <= 30)
1459 {
1460 int i;
1461 /* a somewhat higher rate than 1 second to get really each block */
1462 /* means we need to skip double blocks sometimes */
1463 struct timeval tv = {0,700000};
1464 select(0, 0, 0, 0, &tv);
1465 memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
1466 i = (sisnet >= 30 ? 5 : 3);
1467 if((send(gps_socket, "MSG\r\n", i, 0)) != i)
1468 {
1469 perror("WARNING: sending SISNeT data request failed");
1470 return;
1471 }
1472 }
1473 /*** receiving data ****/
1474 if(inputmode == INFILE)
1475 nBufferBytes = read(gps_file, buffer, sizeof(buffer));
1476 else if(inputmode == SERIAL)
1477 {
1478#ifndef WINDOWSVERSION
1479 nBufferBytes = read(gps_serial, buffer, sizeof(buffer));
1480#else
1481 DWORD nRead = 0;
1482 if(!ReadFile(gps_serial, buffer, sizeof(buffer), &nRead, NULL))
1483 {
1484 fprintf(stderr,"ERROR: reading serial input failed\n");
1485 return;
1486 }
1487 nBufferBytes = (int)nRead;
1488#endif
1489 }
1490 else
1491#ifdef WINDOWSVERSION
1492 nBufferBytes = recv(gps_socket, buffer, sizeof(buffer), 0);
1493#else
1494 nBufferBytes = read(gps_socket, buffer, sizeof(buffer));
1495#endif
1496 if(!nBufferBytes)
1497 {
1498 fprintf(stderr, "WARNING: no data received from input\n");
1499 nodata = 1;
1500#ifndef WINDOWSVERSION
1501 sleep(3);
1502#else
1503 Sleep(3*1000);
1504#endif
1505 continue;
1506 }
1507 else if((nBufferBytes < 0) && (!sigint_received))
1508 {
1509 perror("WARNING: reading input failed");
1510 return;
1511 }
1512 /* we can compare the whole buffer, as the additional bytes
1513 remain unchanged */
1514 if(inputmode == SISNET && sisnet <= 30 &&
1515 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
1516 {
1517 nBufferBytes = 0;
1518 }
1519 }
1520 if(nBufferBytes < 0)
1521 return;
1522 /** send data ***/
1523 if((nBufferBytes) && (outmode == NTRIP1)) /*** Ntrip-Version 1.0 ***/
1524 {
1525 int i;
1526 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1527 != nBufferBytes)
1528 {
1529 if(i < 0)
1530 {
1531 if(errno != EAGAIN)
1532 {
1533 perror("WARNING: could not send data to Destination caster");
1534 return;
1535 }
1536 }
1537 else if(i)
1538 {
1539 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
1540 nBufferBytes -= i;
1541 }
1542 }else
1543 {
1544 nBufferBytes = 0;
1545 }
1546 }
1547 else if((nBufferBytes) && (outmode == UDP))
1548 {
1549 char rtpbuf[1592];
1550 int i;
1551 int ct = time(0);
1552 udp_tim += (ct-udp_init)*1000000/TIME_RESOLUTION;
1553 udp_init = ct;
1554 rtpbuf[0] = (2<<6);
1555 rtpbuf[1] = 96;
1556 rtpbuf[2] = (udp_seq>>8)&0xFF;
1557 rtpbuf[3] = (udp_seq)&0xFF;
1558 rtpbuf[4] = (udp_tim>>24)&0xFF;
1559 rtpbuf[5] = (udp_tim>>16)&0xFF;
1560 rtpbuf[6] = (udp_tim>>8)&0xFF;
1561 rtpbuf[7] = (udp_tim)&0xFF;
1562 rtpbuf[8] = (rtpssrc>>24)&0xFF;
1563 rtpbuf[9] = (rtpssrc>>16)&0xFF;
1564 rtpbuf[10] = (rtpssrc>>8)&0xFF;
1565 rtpbuf[11] = (rtpssrc)&0xFF;
1566 ++udp_seq;
1567 memcpy(rtpbuf+12, buffer, nBufferBytes);
1568 if((i = send(socket_tcp, rtpbuf, (size_t)nBufferBytes+12, MSG_DONTWAIT))
1569 != nBufferBytes+12)
1570 {
1571 if(errno != EAGAIN)
1572 {
1573 perror("WARNING: could not send data to Destination caster");
1574 return;
1575 }
1576 }
1577 else
1578 nBufferBytes = 0;
1579 i = recv(socket_tcp, rtpbuf, sizeof(rtpbuf), 0);
1580 if(i >= 12 && (unsigned char)rtpbuf[0] == (2 << 6) && rtpbuf[1] >= 96
1581 && rtpbuf[1] <= 98 && rtpssrc ==
1582 (((unsigned char)rtpbuf[8]<<24)+((unsigned char)rtpbuf[9]<<16)
1583 +((unsigned char)rtpbuf[10]<<8)+(unsigned char)rtpbuf[11]))
1584 {
1585 rtptime = time(0);
1586 }
1587 else if(time(0) > rtptime+60)
1588 {
1589 fprintf(stderr, "Timeout\n");
1590 return;
1591 }
1592 }
1593 /*** Ntrip-Version 2.0 HTTP/1.1 ***/
1594 else if((nBufferBytes) && (outmode == HTTP))
1595 {
1596 int i, nChunkBytes, j = 1;
1597 nChunkBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),"%x\r\n",
1598 nBufferBytes);
1599 send(sock, szSendBuffer, nChunkBytes, MSG_DONTWAIT);
1600 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1601 != nBufferBytes)
1602 {
1603 if(i < 0)
1604 {
1605 if(errno != EAGAIN)
1606 {
1607 perror("WARNING: could not send data to Destination caster");
1608 return;
1609 }
1610 }
1611 else if(i)
1612 {
1613 while(j>0)
1614 {
1615 j = send(sock, buffer, (size_t)BUFSZ, MSG_DONTWAIT);
1616 }
1617 }
1618 }
1619 else
1620 {
1621 send(sock, "\r\n", strlen("\r\n"), MSG_DONTWAIT);
1622 nBufferBytes = 0;
1623 }
1624 }
1625 /*** Ntrip-Version 2.0 RTSP(TCP) / RTP(UDP) ***/
1626 else if((nBufferBytes) && (outmode == RTSP))
1627 {
1628 time_t ct;
1629 int r;
1630 char rtpbuffer[BUFSZ+12];
1631 int i, j;
1632 gettimeofday(&now, NULL);
1633 /* RTP data packet generation*/
1634 if(isfirstpacket){
1635 rtpseq = rand();
1636 rtptime = rand();
1637 last = now;
1638 isfirstpacket = 0;
1639 }
1640 else
1641 {
1642 ++rtpseq;
1643 sendtimediff = (((now.tv_sec - last.tv_sec)*1000000)
1644 + (now.tv_usec - last.tv_usec));
1645 rtptime += sendtimediff/TIME_RESOLUTION;
1646 }
1647 rtpbuffer[0] = (RTP_VERSION<<6);
1648 /* padding, extension, csrc are empty */
1649 rtpbuffer[1] = 96;
1650 /* marker is empty */
1651 rtpbuffer[2] = rtpseq>>8;
1652 rtpbuffer[3] = rtpseq;
1653 rtpbuffer[4] = rtptime>>24;
1654 rtpbuffer[5] = rtptime>>16;
1655 rtpbuffer[6] = rtptime>>8;
1656 rtpbuffer[7] = rtptime;
1657 rtpbuffer[8] = rtpssrc>>24;
1658 rtpbuffer[9] = rtpssrc>>16;
1659 rtpbuffer[10] = rtpssrc>>8;
1660 rtpbuffer[11] = rtpssrc;
1661 for(j=0; j<nBufferBytes; j++) {rtpbuffer[12+j] = buffer[j];}
1662 last.tv_sec = now.tv_sec;
1663 last.tv_usec = now.tv_usec;
1664 if ((i = sendto(sock, rtpbuffer, 12 + nBufferBytes, 0, pcasterRTP,
1665 length)) != (nBufferBytes + 12))
1666 {
1667 if(i < 0)
1668 {
1669 if(errno != EAGAIN)
1670 {
1671 perror("WARNING: could not send data to Destination caster");
1672 return;
1673 }
1674 }
1675 else if(i)
1676 {
1677 memmove(buffer, buffer+(i-12), (size_t)(nBufferBytes-(i-12)));
1678 nBufferBytes -= i-12;
1679 }
1680 }
1681 else
1682 {
1683 nBufferBytes = 0;
1684 }
1685 ct = time(0);
1686 if(ct-laststate > 15)
1687 {
1688 i = snprintf(buffer, sizeof(buffer),
1689 "GET_PARAMETER rtsp://%s%s/%s RTSP/1.0\r\n"
1690 "CSeq: %d\r\n"
1691 "Session: %d\r\n"
1692 "\r\n",
1693 casterouthost, rtsp_extension, mountpoint, udp_cseq++, rtpssrc);
1694 if(i > (int)sizeof(buffer) || i < 0)
1695 {
1696 fprintf(stderr, "Requested data too long\n");
1697 return;
1698 }
1699 else if(send(socket_tcp, buffer, (size_t)i, 0) != i)
1700 {
1701 perror("send");
1702 return;
1703 }
1704 laststate = ct;
1705 }
1706 /* ignore RTSP server replies */
1707 if((r=recv(socket_tcp, buffer, sizeof(buffer), 0)) < 0)
1708 {
1709#ifdef WINDOWSVERSION
1710 if(WSAGetLastError() != WSAEWOULDBLOCK)
1711#else /* WINDOWSVERSION */
1712 if(errno != EAGAIN)
1713#endif /* WINDOWSVERSION */
1714 {
1715 fprintf(stderr, "Control connection closed\n");
1716 return;
1717 }
1718 }
1719 else if(!r)
1720 {
1721 fprintf(stderr, "Control connection read error\n");
1722 return;
1723 }
1724 }
1725 if(send_recv_success == 3) reconnect_sec = 1;
1726 }
1727 return;
1728}
1729
1730
1731/********************************************************************
1732 * openserial
1733 *
1734 * Open the serial port with the given device name and configure it for
1735 * reading NMEA data from a GPS receiver.
1736 *
1737 * Parameters:
1738 * tty : pointer to : A zero-terminated string containing the device
1739 * unsigned char name of the appropriate serial port.
1740 * blocksz : integer : Block size for port I/O (ifndef WINDOWSVERSION)
1741 * baud : integer : Baud rate for port I/O
1742 *
1743 * Return Value:
1744 * The function returns a file descriptor for the opened port if successful.
1745 * The function returns -1 / INVALID_HANDLE_VALUE in the event of an error.
1746 *
1747 * Remarks:
1748 *
1749 ********************************************************************/
1750#ifndef WINDOWSVERSION
1751static int openserial(const char * tty, int blocksz, int baud)
1752{
1753 struct termios termios;
1754
1755/*** opening the serial port ***/
1756 gps_serial = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
1757 if(gps_serial < 0)
1758 {
1759 perror("ERROR: opening serial connection");
1760 return (-1);
1761 }
1762
1763/*** configuring the serial port ***/
1764 if(tcgetattr(gps_serial, &termios) < 0)
1765 {
1766 perror("ERROR: get serial attributes");
1767 return (-1);
1768 }
1769 termios.c_iflag = 0;
1770 termios.c_oflag = 0; /* (ONLRET) */
1771 termios.c_cflag = CS8 | CLOCAL | CREAD;
1772 termios.c_lflag = 0;
1773 {
1774 int cnt;
1775 for(cnt = 0; cnt < NCCS; cnt++)
1776 termios.c_cc[cnt] = -1;
1777 }
1778 termios.c_cc[VMIN] = blocksz;
1779 termios.c_cc[VTIME] = 2;
1780
1781#if (B4800 != 4800)
1782/* Not every system has speed settings equal to absolute speed value. */
1783 switch (baud)
1784 {
1785 case 300:
1786 baud = B300;
1787 break;
1788 case 1200:
1789 baud = B1200;
1790 break;
1791 case 2400:
1792 baud = B2400;
1793 break;
1794 case 4800:
1795 baud = B4800;
1796 break;
1797 case 9600:
1798 baud = B9600;
1799 break;
1800 case 19200:
1801 baud = B19200;
1802 break;
1803 case 38400:
1804 baud = B38400;
1805 break;
1806#ifdef B57600
1807 case 57600:
1808 baud = B57600;
1809 break;
1810#endif
1811#ifdef B115200
1812 case 115200:
1813 baud = B115200;
1814 break;
1815#endif
1816#ifdef B230400
1817 case 230400:
1818 baud = B230400;
1819 break;
1820#endif
1821 default:
1822 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
1823 baud = B19200;
1824 break;
1825 }
1826#endif
1827
1828 if(cfsetispeed(&termios, baud) != 0)
1829 {
1830 perror("ERROR: setting serial speed with cfsetispeed");
1831 return (-1);
1832 }
1833 if(cfsetospeed(&termios, baud) != 0)
1834 {
1835 perror("ERROR: setting serial speed with cfsetospeed");
1836 return (-1);
1837 }
1838 if(tcsetattr(gps_serial, TCSANOW, &termios) < 0)
1839 {
1840 perror("ERROR: setting serial attributes");
1841 return (-1);
1842 }
1843 if(fcntl(gps_serial, F_SETFL, 0) == -1)
1844 {
1845 perror("WARNING: setting blocking inputmode failed");
1846 }
1847 return (gps_serial);
1848}
1849#else
1850static HANDLE openserial(const char * tty, int baud)
1851{
1852 DCB dcb;
1853 COMMTIMEOUTS cmt;
1854 char compath[15] = "";
1855
1856 snprintf(compath, sizeof(compath), "\\\\.\\%s", tty);
1857/*** opening the serial port ***/
1858 gps_serial = CreateFile(compath, GENERIC_READ | GENERIC_WRITE, 0, 0,
1859 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1860 if(gps_serial == INVALID_HANDLE_VALUE)
1861 {
1862 fprintf(stderr, "ERROR: opening serial connection\n");
1863 return (INVALID_HANDLE_VALUE);
1864 }
1865/*** configuring the serial port ***/
1866 FillMemory(&dcb, sizeof(dcb), 0);
1867 dcb.DCBlength = sizeof(dcb);
1868 if(!GetCommState(gps_serial, &dcb))
1869 {
1870 fprintf(stderr, "ERROR: get serial attributes\n");
1871 return (INVALID_HANDLE_VALUE);
1872 }
1873 switch (baud)
1874 {
1875 case 110:
1876 baud = CBR_110;
1877 break;
1878 case 300:
1879 baud = CBR_300;
1880 break;
1881 case 600:
1882 baud = CBR_600;
1883 break;
1884 case 1200:
1885 baud = CBR_1200;
1886 break;
1887 case 2400:
1888 baud = CBR_2400;
1889 break;
1890 case 4800:
1891 baud = CBR_4800;
1892 break;
1893 case 9600:
1894 baud = CBR_9600;
1895 break;
1896 case 14400:
1897 baud = CBR_14400;
1898 break;
1899 case 19200:
1900 baud = CBR_19200;
1901 break;
1902 case 38400:
1903 baud = CBR_38400;
1904 break;
1905 case 56000:
1906 baud = CBR_56000;
1907 break;
1908 case 57600:
1909 baud = CBR_57600;
1910 break;
1911 case 115200:
1912 baud = CBR_115200;
1913 break;
1914 case 128000:
1915 baud = CBR_128000;
1916 break;
1917 case 256000:
1918 baud = CBR_256000;
1919 break;
1920 default:
1921 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
1922 baud = CBR_19200;
1923 break;
1924 }
1925 dcb.BaudRate = baud;
1926 dcb.ByteSize = 8;
1927 dcb.StopBits = ONESTOPBIT;
1928 dcb.Parity = NOPARITY;
1929 if(!GetCommState(gps_serial, &dcb))
1930 {
1931 fprintf(stderr, "ERROR: get serial attributes\n");
1932 return (INVALID_HANDLE_VALUE);
1933 }
1934 FillMemory(&cmt, sizeof(cmt), 0);
1935 cmt.ReadIntervalTimeout = 1000;
1936 cmt.ReadTotalTimeoutMultiplier = 1;
1937 cmt.ReadTotalTimeoutConstant = 0;
1938 if(!SetCommTimeouts(gps_serial, &cmt))
1939 {
1940 fprintf(stderr, "ERROR: set serial timeouts\n");
1941 return (INVALID_HANDLE_VALUE);
1942 }
1943 return (gps_serial);
1944} /* openserial */
1945#endif
1946
1947/********************************************************************
1948* usage
1949*
1950* Send a usage message to standard error and quit the program.
1951*
1952* Parameters:
1953* None.
1954*
1955* Return Value:
1956* The function does not return a value.
1957*
1958* Remarks:
1959*
1960*********************************************************************/
1961#ifdef __GNUC__
1962__attribute__ ((noreturn))
1963#endif /* __GNUC__ */
1964void usage(int rc, char *name)
1965{
1966 fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]\n",
1967 revisionstr, datestr, name);
1968 fprintf(stderr, "PURPOSE\n");
1969 fprintf(stderr, " The purpose of this program is to pick up a GNSS data stream (Input, Source)\n");
1970 fprintf(stderr, " from either\n\n");
1971 fprintf(stderr, " 1. a Serial port, or\n");
1972 fprintf(stderr, " 2. an IP server, or\n");
1973 fprintf(stderr, " 3. a File, or\n");
1974 fprintf(stderr, " 4. a SISNeT Data Server, or\n");
1975 fprintf(stderr, " 5. a UDP server, or\n");
1976 fprintf(stderr, " 6. an NTRIP Version 1.0 Caster\n\n");
1977 fprintf(stderr, " and forward that incoming stream (Output, Destination) to either\n\n");
1978 fprintf(stderr, " - an NTRIP Version 1.0 Caster, or\n");
1979 fprintf(stderr, " - an NTRIP Version 2.0 Caster via TCP/IP or RTSP/RTP.\n\n\n");
1980 fprintf(stderr, "OPTIONS\n");
1981 fprintf(stderr, " -h|? print this help screen\n\n");
1982 fprintf(stderr, " -E <ProxyHost> Proxy server host name or address, required i.e. when\n");
1983 fprintf(stderr, " running the program in a proxy server protected LAN,\n");
1984 fprintf(stderr, " optional\n");
1985 fprintf(stderr, " -F <ProxyPort> Proxy server IP port, required i.e. when running\n");
1986 fprintf(stderr, " the program in a proxy server protected LAN, optional\n");
1987 fprintf(stderr, " -R <maxDelay> Reconnect mechanism with maximum delay between reconnect\n");
1988 fprintf(stderr, " attemts in seconds, default: no reconnect activated,\n");
1989 fprintf(stderr, " optional\n\n");
1990 fprintf(stderr, " -M <InputMode> Sets the input mode (1 = Serial Port, 2 = IP server,\n");
1991 fprintf(stderr, " 3 = File, 4 = SISNeT Data Server, 5 = UDP server, 6 = NTRIP Caster),\n");
1992 fprintf(stderr, " mandatory\n\n");
1993 fprintf(stderr, " <InputMode> = 1 (Serial Port):\n");
1994 fprintf(stderr, " -i <Device> Serial input device, default: /dev/gps, mandatory if\n");
1995 fprintf(stderr, " <InputMode>=1\n");
1996 fprintf(stderr, " -b <BaudRate> Serial input baud rate, default: 19200 bps, mandatory\n");
1997 fprintf(stderr, " if <InputMode>=1\n");
1998 fprintf(stderr, " -f <InitFile> Name of initialization file to be send to input device,\n");
1999 fprintf(stderr, " optional\n\n");
2000 fprintf(stderr, " <InputMode> = 2|5 (IP port | UDP port):\n");
2001 fprintf(stderr, " -H <ServerHost> Input host name or address, default: 127.0.0.1,\n");
2002 fprintf(stderr, " mandatory if <InputMode> = 2|5\n");
2003 fprintf(stderr, " -P <ServerPort> Input port, default: 1025, mandatory if <InputMode>= 2|5\n");
2004 fprintf(stderr, " -f <ServerFile> Name of initialization file to be send to server,\n");
2005 fprintf(stderr, " optional\n");
2006 fprintf(stderr, " -x <ServerUser> User ID to access incoming stream, optional\n");
2007 fprintf(stderr, " -y <ServerPass> Password, to access incoming stream, optional\n");
2008 fprintf(stderr, " -B Bind to incoming UDP stream, optional for <InputMode> = 5\n\n");
2009 fprintf(stderr, " <InputMode> = 3 (File):\n");
2010 fprintf(stderr, " -s <File> File name to simulate stream by reading data from (log)\n");
2011 fprintf(stderr, " file, default is /dev/stdin, mandatory for <InputMode> = 3\n\n");
2012 fprintf(stderr, " <InputMode> = 4 (SISNeT Data Server):\n");
2013 fprintf(stderr, " -H <SisnetHost> SISNeT Data Server name or address,\n");
2014 fprintf(stderr, " default: 131.176.49.142, mandatory if <InputMode> = 4\n");
2015 fprintf(stderr, " -P <SisnetPort> SISNeT Data Server port, default: 7777, mandatory if\n");
2016 fprintf(stderr, " <InputMode> = 4\n");
2017 fprintf(stderr, " -u <SisnetUser> SISNeT Data Server user ID, mandatory if <InputMode> = 4\n");
2018 fprintf(stderr, " -l <SisnetPass> SISNeT Data Server password, mandatory if <InputMode> = 4\n");
2019 fprintf(stderr, " -V <SisnetVers> SISNeT Data Server Version number, options are 2.1, 3.0\n");
2020 fprintf(stderr, " or 3.1, default: 3.1, mandatory if <InputMode> = 4\n\n");
2021 fprintf(stderr, " <InputMode> = 6 (NTRIP Version 1.0 Caster):\n");
2022 fprintf(stderr, " -H <SourceHost> Source caster name or address, default: 127.0.0.1,\n");
2023 fprintf(stderr, " mandatory if <InputMode> = 6\n");
2024 fprintf(stderr, " -P <SourcePort> Source caster port, default: 2101, mandatory if\n");
2025 fprintf(stderr, " <InputMode> = 6\n");
2026 fprintf(stderr, " -D <SourceMount> Source caster mountpoint for stream input, mandatory if\n");
2027 fprintf(stderr, " <InputMode> = 6\n");
2028 fprintf(stderr, " -U <SourceUser> Source caster user Id for input stream access, mandatory\n");
2029 fprintf(stderr, " for protected streams if <InputMode> = 6\n");
2030 fprintf(stderr, " -W <SourcePass> Source caster password for input stream access, mandatory\n");
2031 fprintf(stderr, " for protected streams if <InputMode> = 6\n\n");
2032 fprintf(stderr, " -O <OutputMode> Sets output mode for communatation with destination caster\n");
2033 fprintf(stderr, " 1 = http: NTRIP Version 2.0 Caster in TCP/IP mode\n");
2034 fprintf(stderr, " 2 = rtsp: NTRIP Version 2.0 Caster in RTSP/RTP mode\n");
2035 fprintf(stderr, " 3 = ntrip1: NTRIP Version 1.0 Caster\n");
2036 fprintf(stderr, " 4 = udp: NTRIP Version 2.0 Caster in Plain UDP mode\n");
2037 fprintf(stderr, " optional\n\n");
2038 fprintf(stderr, " Defaults to NTRIP1.0, but will change to 2.0 in future versions\n");
2039 fprintf(stderr, " Note that the program automatically falls back from mode rtsp to mode http and\n");
2040 fprintf(stderr, " further to mode ntrip1 if necessary.\n\n");
2041 fprintf(stderr, " -a <DestHost> Destination caster name or address, default: 127.0.0.1,\n");
2042 fprintf(stderr, " mandatory\n");
2043 fprintf(stderr, " -p <DestPort> Destination caster port, default: 2101, mandatory\n");
2044 fprintf(stderr, " -m <DestMount> Destination caster mountpoint for stream upload,\n");
2045 fprintf(stderr, " mandatory\n");
2046 fprintf(stderr, " -n <DestUser> Destination caster user ID for stream upload to\n");
2047 fprintf(stderr, " mountpoint, only for NTRIP Version 2.0 destination\n");
2048 fprintf(stderr, " casters, mandatory\n");
2049 fprintf(stderr, " -c <DestPass> Destination caster password for stream upload to\n");
2050 fprintf(stderr, " mountpoint, mandatory\n");
2051 fprintf(stderr, " -N <STR-record> Sourcetable STR-record\n");
2052 fprintf(stderr, " optional for NTRIP Version 2.0 in RTSP/RTP and TCP/IP mode\n\n");
2053 exit(rc);
2054} /* usage */
2055
2056
2057/********************************************************************/
2058/* signal handling */
2059/********************************************************************/
2060#ifdef __GNUC__
2061static void handle_sigint(int sig __attribute__((__unused__)))
2062#else /* __GNUC__ */
2063static void handle_sigint(int sig)
2064#endif /* __GNUC__ */
2065{
2066 sigint_received = 1;
2067 fprintf(stderr, "WARNING: SIGINT received - ntripserver terminates\n");
2068}
2069
2070#ifndef WINDOWSVERSION
2071#ifdef __GNUC__
2072static void handle_alarm(int sig __attribute__((__unused__)))
2073#else /* __GNUC__ */
2074static void handle_alarm(int sig)
2075#endif /* __GNUC__ */
2076{
2077 sigalarm_received = 1;
2078 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
2079}
2080
2081#ifdef __GNUC__
2082static void handle_sigpipe(int sig __attribute__((__unused__)))
2083#else /* __GNUC__ */
2084static void handle_sigpipe(int sig)
2085#endif /* __GNUC__ */
2086{
2087 sigpipe_received = 1;
2088}
2089#endif /* WINDOWSVERSION */
2090
2091static void setup_signal_handler(int sig, void (*handler)(int))
2092{
2093#if _POSIX_VERSION > 198800L
2094 struct sigaction action;
2095
2096 action.sa_handler = handler;
2097 sigemptyset(&(action.sa_mask));
2098 sigaddset(&(action.sa_mask), sig);
2099 action.sa_flags = 0;
2100 sigaction(sig, &action, 0);
2101#else
2102 signal(sig, handler);
2103#endif
2104 return;
2105} /* setupsignal_handler */
2106
2107
2108/********************************************************************
2109 * base64-encoding *
2110*******************************************************************/
2111static const char encodingTable [64] =
2112{
2113 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
2114 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
2115 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
2116 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
2117};
2118
2119/* does not buffer overrun, but breaks directly after an error */
2120/* returns the number of required bytes */
2121static int encode(char *buf, int size, const char *user, const char *pwd)
2122{
2123 unsigned char inbuf[3];
2124 char *out = buf;
2125 int i, sep = 0, fill = 0, bytes = 0;
2126
2127 while(*user || *pwd)
2128 {
2129 i = 0;
2130 while(i < 3 && *user) inbuf[i++] = *(user++);
2131 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
2132 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
2133 while(i < 3) {inbuf[i++] = 0; ++fill; }
2134 if(out-buf < size-1)
2135 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
2136 if(out-buf < size-1)
2137 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
2138 | ((inbuf [1] & 0xF0) >> 4)];
2139 if(out-buf < size-1)
2140 {
2141 if(fill == 2)
2142 *(out++) = '=';
2143 else
2144 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
2145 | ((inbuf [2] & 0xC0) >> 6)];
2146 }
2147 if(out-buf < size-1)
2148 {
2149 if(fill >= 1)
2150 *(out++) = '=';
2151 else
2152 *(out++) = encodingTable[inbuf [2] & 0x3F];
2153 }
2154 bytes += 4;
2155 }
2156 if(out-buf < size)
2157 *out = 0;
2158 return bytes;
2159}/* base64 Encoding */
2160
2161
2162/********************************************************************
2163 * send message to caster *
2164*********************************************************************/
2165static int send_to_caster(char *input, sockettype socket, int input_size)
2166{
2167 int send_error = 1;
2168
2169 if((send(socket, input, (size_t)input_size, 0)) != input_size)
2170 {
2171 fprintf(stderr, "WARNING: could not send full header to Destination caster\n");
2172 send_error = 0;
2173 }
2174#ifndef NDEBUG
2175 else
2176 {
2177 fprintf(stderr, "\nDestination caster request:\n");
2178 fprintf(stderr, "%s", input);
2179 }
2180#endif
2181 return send_error;
2182}/* send_to_caster */
2183
2184
2185/********************************************************************
2186 * reconnect *
2187*********************************************************************/
2188int reconnect(int rec_sec, int rec_sec_max)
2189{
2190 fprintf(stderr,"reconnect in <%d> seconds\n\n", rec_sec);
2191 rec_sec *= 2;
2192 if (rec_sec > rec_sec_max) rec_sec = rec_sec_max;
2193#ifndef WINDOWSVERSION
2194 sleep(rec_sec);
2195 sigpipe_received = 0;
2196#else
2197 Sleep(rec_sec*1000);
2198#endif
2199 sigalarm_received = 0;
2200 return rec_sec;
2201} /* reconnect */
2202
2203
2204/********************************************************************
2205 * close session *
2206*********************************************************************/
2207static void close_session(const char *caster_addr, const char *mountpoint,
2208int session, char *rtsp_ext, int fallback)
2209{
2210 int size_send_buf;
2211 char send_buf[BUFSZ];
2212
2213 if(!fallback)
2214 {
2215 if((gps_socket != INVALID_SOCKET) &&
2216 ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) ||
2217 (inputmode == CASTER) || (inputmode == SISNET)))
2218 {
2219 if(closesocket(gps_socket) == -1)
2220 {
2221 perror("ERROR: close input device ");
2222 exit(0);
2223 }
2224 else
2225 {
2226 gps_socket = -1;
2227#ifndef NDEBUG
2228 fprintf(stderr, "close input device: successful\n");
2229#endif
2230 }
2231 }
2232 else if((gps_serial != INVALID_HANDLE_VALUE) && (inputmode == SERIAL))
2233 {
2234#ifndef WINDOWSVERSION
2235 if(close(gps_serial) == INVALID_HANDLE_VALUE)
2236 {
2237 perror("ERROR: close input device ");
2238 exit(0);
2239 }
2240#else
2241 if(!CloseHandle(gps_serial))
2242 {
2243 fprintf(stderr, "ERROR: close input device ");
2244 exit(0);
2245 }
2246#endif
2247 else
2248 {
2249 gps_serial = INVALID_HANDLE_VALUE;
2250#ifndef NDEBUG
2251 fprintf(stderr, "close input device: successful\n");
2252#endif
2253 }
2254 }
2255 else if((gps_file != -1) && (inputmode == INFILE))
2256 {
2257 if(close(gps_file) == -1)
2258 {
2259 perror("ERROR: close input device ");
2260 exit(0);
2261 }
2262 else
2263 {
2264 gps_file = -1;
2265#ifndef NDEBUG
2266 fprintf(stderr, "close input device: successful\n");
2267#endif
2268 }
2269 }
2270 }
2271
2272 if(socket_udp != INVALID_SOCKET)
2273 {
2274 if(udp_cseq > 2)
2275 {
2276 size_send_buf = snprintf(send_buf, sizeof(send_buf),
2277 "TEARDOWN rtsp://%s%s/%s RTSP/1.0\r\n"
2278 "CSeq: %d\r\n"
2279 "Session: %d\r\n"
2280 "\r\n",
2281 caster_addr, rtsp_ext, mountpoint, udp_cseq++, session);
2282 if((size_send_buf >= (int)sizeof(send_buf)) || (size_send_buf < 0))
2283 {
2284 fprintf(stderr, "ERROR: Destination caster request to long\n");
2285 exit(0);
2286 }
2287 send_to_caster(send_buf, socket_tcp, size_send_buf); strcpy(send_buf,"");
2288 size_send_buf = recv(socket_tcp, send_buf, sizeof(send_buf), 0);
2289 send_buf[size_send_buf] = '\0';
2290#ifndef NDEBUG
2291 fprintf(stderr, "Destination caster response:\n%s", send_buf);
2292#endif
2293 }
2294 if(closesocket(socket_udp)==-1)
2295 {
2296 perror("ERROR: close udp socket");
2297 exit(0);
2298 }
2299 else
2300 {
2301 socket_udp = -1;
2302#ifndef NDEBUG
2303 fprintf(stderr, "close udp socket: successful\n");
2304#endif
2305 }
2306 }
2307
2308 if(socket_tcp != INVALID_SOCKET)
2309 {
2310 if(closesocket(socket_tcp) == -1)
2311 {
2312 perror("ERROR: close tcp socket");
2313 exit(0);
2314 }
2315 else
2316 {
2317 socket_tcp = -1;
2318#ifndef NDEBUG
2319 fprintf(stderr, "close tcp socket: successful\n");
2320#endif
2321 }
2322 }
2323} /* close_session */
Note: See TracBrowser for help on using the repository browser.