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

Last change on this file since 1086 was 933, checked in by stuerze, 16 years ago

initial ntrip_str value changed

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