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

Last change on this file since 9453 was 9453, checked in by stuerze, 3 years ago

data upload to localhost added

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