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

Last change on this file since 915 was 915, checked in by stoecker, 16 years ago

fixes for NTRIPV2 pre standard

File size: 63.7 KB
Line 
1/*
2 * $Id: ntripserver.c,v 1.40 2008/04/04 13:07:31 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.40 $";
40static char datestr[] = "$Date: 2008/04/04 13:07:31 $";
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 = 0;
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;
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 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
884 "SOURCE %s %s/%s\r\n"
885 "Source-Agent: %s/%s\r\n\r\n",
886 password, post_extension, mountpoint, AGENTSTRING, revisionstr);
887 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
888 {
889 fprintf(stderr, "ERROR: Destination caster request to long\n");
890 reconnect_sec_max = 0;
891 output_init = 0;
892 break;
893 }
894 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
895 {
896 output_init = 0;
897 break;
898 }
899 /* check Destination caster's response */
900 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
901 szSendBuffer[nBufferBytes] = '\0';
902 if(!strstr(szSendBuffer, "OK"))
903 {
904 char *a;
905 fprintf(stderr,
906 "ERROR: Destination caster's or Proxy's reply is not OK: ");
907 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
908 {
909 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
910 }
911 fprintf(stderr, "\n");
912 if((strstr(szSendBuffer,"ERROR - Bad Password"))
913 || (strstr(szSendBuffer,"400 Bad Request")))
914 reconnect_sec_max = 0;
915 output_init = 0;
916 break;
917 }
918#ifndef NDEBUG
919 else
920 {
921 fprintf(stderr, "Destination caster response:\n%s\n",
922 szSendBuffer);
923 }
924#endif
925 send_receive_loop(socket_tcp, outputmode, NULL, 0, 0);
926 input_init = output_init = 0;
927 break;
928 case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
929 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
930 "POST %s/%s HTTP/1.1\r\n"
931 "Host: %s\r\n"
932 "Ntrip-Version: Ntrip/2.0\r\n"
933 "User-Agent: %s/%s\r\n"
934 "Authorization: Basic %s%s%s\r\n"
935 "Connection: close\r\n"
936 "Transfer-Encoding: chunked\r\n\r\n",
937 post_extension, mountpoint, casterouthost, AGENTSTRING,
938 revisionstr, authorization, ntrip_str ? "\r\nNtrip-STR: " : "",
939 ntrip_str);
940 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
941 {
942 fprintf(stderr, "ERROR: Destination caster request to long\n");
943 reconnect_sec_max = 0;
944 output_init = 0;
945 break;
946 }
947 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
948 {
949 output_init = 0;
950 break;
951 }
952 /* check Destination caster's response */
953 nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
954 szSendBuffer[nBufferBytes] = '\0';
955 if(!strstr(szSendBuffer, "HTTP/1.1 200 OK"))
956 {
957 char *a;
958 fprintf(stderr,
959 "ERROR: Destination caster's%s reply is not OK: ",
960 *proxyhost ? " or Proxy's" : "");
961 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
962 {
963 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
964 }
965 fprintf(stderr, "\n");
966 /* fallback if necessary */
967 if(!strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
968 {
969 fprintf(stderr,
970 " Ntrip Version 2.0 not implemented at Destination caster"
971 " <%s>%s%s%s\n%s\n"
972 "ntripserver falls back to Ntrip Version 1.0\n\n",
973 casterouthost,
974 *proxyhost ? " or Proxy <" : "", proxyhost, *proxyhost ? ">" : "",
975 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "");
976 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
977 outputmode = NTRIP1;
978 break;
979 }
980 else if((strstr(szSendBuffer,"HTTP/1.1 401 Unauthorized"))
981 || (strstr(szSendBuffer,"501 Not Implemented")))
982 {
983 reconnect_sec_max = 0;
984 }
985 output_init = 0;
986 break;
987 }
988#ifndef NDEBUG
989 else
990 {
991 fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
992 }
993#endif
994 send_receive_loop(socket_tcp, outputmode, NULL, 0, 0);
995 input_init = output_init = 0;
996 break;
997 case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
998 if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)
999 {
1000 perror("ERROR: udp socket");
1001 exit(4);
1002 }
1003 /* fill structure with local address information for UDP */
1004 memset(&local, 0, sizeof(local));
1005 local.sin_family = AF_INET;
1006 local.sin_port = htons(0);
1007 local.sin_addr.s_addr = htonl(INADDR_ANY);
1008 len = (socklen_t)sizeof(local);
1009 /* bind() in order to get a random RTP client_port */
1010 if((bind(socket_udp,(struct sockaddr *)&local, len)) < 0)
1011 {
1012 perror("ERROR: udp bind");
1013 reconnect_sec_max = 0;
1014 output_init = 0;
1015 break;
1016 }
1017 if((getsockname(socket_udp, (struct sockaddr*)&local, &len)) != -1)
1018 {
1019 client_port = (unsigned int)ntohs(local.sin_port);
1020 }
1021 else
1022 {
1023 perror("ERROR: getsockname(localhost)");
1024 reconnect_sec_max = 0;
1025 output_init = 0;
1026 break;
1027 }
1028 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1029 "SETUP rtsp://%s%s/%s RTSP/1.0\r\n"
1030 "CSeq: %d\r\n"
1031 "Ntrip-Version: Ntrip/2.0\r\n"
1032 "Ntrip-Component: Ntripserver\r\n"
1033 "User-Agent: %s/%s\r\n"
1034 "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
1035 "Authorization: Basic %s%s%s\r\n\r\n",
1036 casterouthost, rtsp_extension, mountpoint, udp_cseq++, AGENTSTRING,
1037 revisionstr, client_port, authorization, ntrip_str
1038 ? "\r\nNtrip-STR: " : "", ntrip_str);
1039 if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
1040 {
1041 fprintf(stderr, "ERROR: Destination caster request to long\n");
1042 reconnect_sec_max = 0;
1043 output_init = 0;
1044 break;
1045 }
1046 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1047 {
1048 output_init = 0;
1049 break;
1050 }
1051 while((nBufferBytes = recv(socket_tcp, szSendBuffer,
1052 sizeof(szSendBuffer), 0)) > 0)
1053 {
1054 /* check Destination caster's response */
1055 szSendBuffer[nBufferBytes] = '\0';
1056 if(!strstr(szSendBuffer, "RTSP/1.0 200 OK"))
1057 {
1058 char *a;
1059 fprintf(stderr,
1060 "ERROR: Destination caster's%s reply is not OK: ",
1061 *proxyhost ? " or Proxy's" : "");
1062 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
1063 {
1064 fprintf(stderr, "%c", isprint(*a) ? *a : '.');
1065 }
1066 fprintf(stderr, "\n");
1067 /* fallback if necessary */
1068 if(strncmp(szSendBuffer, "RTSP",4) != 0)
1069 {
1070 if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
1071 {
1072 fprintf(stderr,
1073 " RTSP not implemented at Destination caster <%s>%s%s%s\n\n"
1074 "ntripserver falls back to Ntrip Version 2.0 in TCP/IP"
1075 " mode\n\n", casterouthost,
1076 *proxyhost ? " or Proxy <" :"", proxyhost, *proxyhost ? ">":"");
1077 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
1078 outputmode = HTTP;
1079 break;
1080 }
1081 else
1082 {
1083 fprintf(stderr,
1084 " Ntrip-Version 2.0 not implemented at Destination caster"
1085 "<%s>%s%s%s\n%s"
1086 " or RTSP/1.0 not implemented at Destination caster%s\n\n"
1087 "ntripserver falls back to Ntrip Version 1.0\n\n",
1088 casterouthost, *proxyhost ? " or Proxy <" :"", proxyhost,
1089 *proxyhost ? ">":"",
1090 *proxyhost ? " or HTTP/1.1 not implemented at Proxy\n" : "",
1091 *proxyhost ? " or Proxy" :"");
1092 close_session(casterouthost, mountpoint, session, rtsp_extension, 1);
1093 outputmode = NTRIP1;
1094 break;
1095 }
1096 }
1097 else if((strstr(szSendBuffer, "RTSP/1.0 401 Unauthorized"))
1098 || (strstr(szSendBuffer, "RTSP/1.0 501 Not Implemented")))
1099 {
1100 reconnect_sec_max = 0;
1101 }
1102 output_init = 0;
1103 break;
1104 }
1105#ifndef NDEBUG
1106 else
1107 {
1108 fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
1109 }
1110#endif
1111 if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n"))
1112 && (strstr(szSendBuffer,"CSeq: 1\r\n")))
1113 {
1114 for(token = strtok(szSendBuffer, dlim); token != NULL;
1115 token = strtok(NULL, dlim))
1116 {
1117 tok_buf[i] = token; i++;
1118 }
1119 session = atoi(tok_buf[6]);
1120 server_port = atoi(tok_buf[10]);
1121 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
1122 "RECORD rtsp://%s%s/%s RTSP/1.0\r\n"
1123 "CSeq: %d\r\n"
1124 "Session: %d\r\n"
1125 "\r\n",
1126 casterouthost, rtsp_extension, mountpoint, udp_cseq++,
1127 session);
1128 if((nBufferBytes >= (int)sizeof(szSendBuffer))
1129 || (nBufferBytes < 0))
1130 {
1131 fprintf(stderr, "ERROR: Destination caster request to long\n");
1132 reconnect_sec_max = 0;
1133 output_init = 0;
1134 break;
1135 }
1136 if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
1137 {
1138 output_init = 0;
1139 break;
1140 }
1141 }
1142 else if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n")) && (strstr(szSendBuffer,
1143 "CSeq: 2\r\n")))
1144 {
1145 /* fill structure with caster address information for UDP */
1146 memset(&casterRTP, 0, sizeof(casterRTP));
1147 casterRTP.sin_family = AF_INET;
1148 casterRTP.sin_port = htons(((uint16_t)server_port));
1149 if((he = gethostbyname(outhost))== NULL)
1150 {
1151 fprintf(stderr, "ERROR: Destination caster unknown\n");
1152 reconnect_sec_max = 0;
1153 output_init = 0;
1154 break;
1155 }
1156 else
1157 {
1158 memcpy((char *)&casterRTP.sin_addr.s_addr,
1159 he->h_addr_list[0], (size_t)he->h_length);
1160 }
1161 len = (socklen_t)sizeof(casterRTP);
1162 send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP,
1163 (socklen_t)len, session);
1164 break;
1165 }
1166 else{break;}
1167 }
1168 input_init = output_init = 0;
1169 break;
1170 }
1171 }
1172 close_session(casterouthost, mountpoint, session, rtsp_extension, 0);
1173 if((reconnect_sec_max) && (!sigint_received))
1174 reconnect_sec = reconnect(reconnect_sec, reconnect_sec_max);
1175 else inputmode = LAST;
1176 }
1177 return 0;
1178}
1179
1180static void send_receive_loop(sockettype sock, int outmode, struct sockaddr* pcasterRTP,
1181socklen_t length, int rtpssrc)
1182{
1183 int nodata = 0;
1184 char buffer[BUFSZ] = { 0 };
1185 char sisnetbackbuffer[200];
1186 char szSendBuffer[BUFSZ] = "";
1187 int nBufferBytes = 0;
1188
1189 /* RTSP / RTP Mode */
1190 int isfirstpacket = 1;
1191 struct timeval now;
1192 struct timeval last = {0,0};
1193 long int sendtimediff;
1194 int rtpseq = 0;
1195 int rtptime = 0;
1196 time_t laststate = time(0);
1197
1198 if(outmode == RTSP)
1199 {
1200#ifdef WINDOWSVERSION
1201 u_long blockmode = 1;
1202 if(ioctlsocket(socket_tcp, FIONBIO, &blockmode))
1203#else /* WINDOWSVERSION */
1204 if(fcntl(socket_tcp, F_SETFL, O_NONBLOCK) < 0)
1205#endif /* WINDOWSVERSION */
1206 {
1207 fprintf(stderr, "Could not set nonblocking mode\n");
1208 return;
1209 }
1210 }
1211
1212 /* data transmission */
1213 fprintf(stderr,"transfering data ...\n");
1214 int send_recv_success = 0;
1215#ifdef WINDOWSVERSION
1216 time_t nodata_begin = 0, nodata_current = 0;
1217#endif
1218 while(1)
1219 {
1220 if(send_recv_success < 3) send_recv_success++;
1221 if(!nodata)
1222 {
1223#ifndef WINDOWSVERSION
1224 alarm(ALARMTIME);
1225#else
1226 time(&nodata_begin);
1227#endif
1228 }
1229 else
1230 {
1231 nodata = 0;
1232#ifdef WINDOWSVERSION
1233 time(&nodata_current);
1234 if(difftime(nodata_current, nodata_begin) >= ALARMTIME)
1235 {
1236 sigalarm_received = 1;
1237 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
1238 }
1239#endif
1240 }
1241 /* signal handling*/
1242#ifdef WINDOWSVERSION
1243 if((sigalarm_received) || (sigint_received)) break;
1244#else
1245 if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
1246#endif
1247 if(!nBufferBytes)
1248 {
1249 if(inputmode == SISNET && sisnet <= 30)
1250 {
1251 int i;
1252 /* a somewhat higher rate than 1 second to get really each block */
1253 /* means we need to skip double blocks sometimes */
1254 struct timeval tv = {0,700000};
1255 select(0, 0, 0, 0, &tv);
1256 memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
1257 i = (sisnet >= 30 ? 5 : 3);
1258 if((send(gps_socket, "MSG\r\n", i, 0)) != i)
1259 {
1260 perror("WARNING: sending SISNeT data request failed");
1261 return;
1262 }
1263 }
1264 /*** receiving data ****/
1265 if(inputmode == INFILE)
1266 nBufferBytes = read(gps_file, buffer, sizeof(buffer));
1267 else if(inputmode == SERIAL)
1268 {
1269#ifndef WINDOWSVERSION
1270 nBufferBytes = read(gps_serial, buffer, sizeof(buffer));
1271#else
1272 DWORD nRead = 0;
1273 if(!ReadFile(gps_serial, buffer, sizeof(buffer), &nRead, NULL))
1274 {
1275 fprintf(stderr,"ERROR: reading serial input failed\n");
1276 return;
1277 }
1278 nBufferBytes = (int)nRead;
1279#endif
1280 }
1281 else
1282#ifdef WINDOWSVERSION
1283 nBufferBytes = recv(gps_socket, buffer, sizeof(buffer), 0);
1284#else
1285 nBufferBytes = read(gps_socket, buffer, sizeof(buffer));
1286#endif
1287 if(!nBufferBytes)
1288 {
1289 fprintf(stderr, "WARNING: no data received from input\n");
1290 nodata = 1;
1291#ifndef WINDOWSVERSION
1292 sleep(3);
1293#else
1294 Sleep(3*1000);
1295#endif
1296 continue;
1297 }
1298 else if((nBufferBytes < 0) && (!sigint_received))
1299 {
1300 perror("WARNING: reading input failed");
1301 return;
1302 }
1303 /* we can compare the whole buffer, as the additional bytes
1304 remain unchanged */
1305 if(inputmode == SISNET && sisnet <= 30 &&
1306 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
1307 {
1308 nBufferBytes = 0;
1309 }
1310 }
1311 /** send data ***/
1312 if((nBufferBytes) && (outmode == NTRIP1)) /*** Ntrip-Version 1.0 ***/
1313 {
1314 int i;
1315 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1316 != nBufferBytes)
1317 {
1318 if(i < 0)
1319 {
1320 if(errno != EAGAIN)
1321 {
1322 perror("WARNING: could not send data to Destination caster");
1323 return;
1324 }
1325 }
1326 else if(i)
1327 {
1328 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
1329 nBufferBytes -= i;
1330 }
1331 }else
1332 {
1333 nBufferBytes = 0;
1334 }
1335 }
1336 /*** Ntrip-Version 2.0 HTTP/1.1 ***/
1337 else if((nBufferBytes) && (outmode == HTTP))
1338 {
1339 int i, nChunkBytes, j = 1;
1340 nChunkBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),"%x\r\n",
1341 nBufferBytes);
1342 send(sock, szSendBuffer, nChunkBytes, MSG_DONTWAIT);
1343 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
1344 != nBufferBytes)
1345 {
1346 if(i < 0)
1347 {
1348 if(errno != EAGAIN)
1349 {
1350 perror("WARNING: could not send data to Destination caster");
1351 return;
1352 }
1353 }
1354 else if(i)
1355 {
1356 while(j>0)
1357 {
1358 j = send(sock, buffer, (size_t)BUFSZ, MSG_DONTWAIT);
1359 }
1360 }
1361 }
1362 else
1363 {
1364 send(sock, "\r\n", strlen("\r\n"), MSG_DONTWAIT);
1365 nBufferBytes = 0;
1366 }
1367 }
1368 /*** Ntrip-Version 2.0 RTSP(TCP) / RTP(UDP) ***/
1369 else if((nBufferBytes) && (outmode == RTSP))
1370 {
1371 time_t ct;
1372 int r;
1373 char rtpbuffer[BUFSZ+12];
1374 int i, j;
1375 gettimeofday(&now, NULL);
1376 /* RTP data packet generation*/
1377 if(isfirstpacket){
1378 rtpseq = rand();
1379 rtptime = rand();
1380 last = now;
1381 isfirstpacket = 0;
1382 }
1383 else
1384 {
1385 ++rtpseq;
1386 sendtimediff = (((now.tv_sec - last.tv_sec)*1000000)
1387 + (now.tv_usec - last.tv_usec));
1388 rtptime += sendtimediff/TIME_RESOLUTION;
1389 }
1390 rtpbuffer[0] = (RTP_VERSION<<6);
1391 /* padding, extension, csrc are empty */
1392 rtpbuffer[1] = 96;
1393 /* marker is empty */
1394 rtpbuffer[2] = rtpseq>>8;
1395 rtpbuffer[3] = rtpseq;
1396 rtpbuffer[4] = rtptime>>24;
1397 rtpbuffer[5] = rtptime>>16;
1398 rtpbuffer[6] = rtptime>>8;
1399 rtpbuffer[7] = rtptime;
1400 rtpbuffer[8] = rtpssrc>>24;
1401 rtpbuffer[9] = rtpssrc>>16;
1402 rtpbuffer[10] = rtpssrc>>8;
1403 rtpbuffer[11] = rtpssrc;
1404 for(j=0; j<nBufferBytes; j++) {rtpbuffer[12+j] = buffer[j];}
1405 last.tv_sec = now.tv_sec;
1406 last.tv_usec = now.tv_usec;
1407 if ((i = sendto(sock, rtpbuffer, 12 + nBufferBytes, 0, pcasterRTP,
1408 length)) != (nBufferBytes + 12))
1409 {
1410 if(i < 0)
1411 {
1412 if(errno != EAGAIN)
1413 {
1414 perror("WARNING: could not send data to Destination caster");
1415 return;
1416 }
1417 }
1418 else if(i)
1419 {
1420 memmove(buffer, buffer+(i-12), (size_t)(nBufferBytes-(i-12)));
1421 nBufferBytes -= i-12;
1422 }
1423 }
1424 else
1425 {
1426 nBufferBytes = 0;
1427 }
1428 ct = time(0);
1429 if(ct-laststate > 15)
1430 {
1431 i = snprintf(buffer, sizeof(buffer),
1432 "GET_PARAMETER rtsp://%s%s/%s RTSP/1.0\r\n"
1433 "CSeq: %d\r\n"
1434 "Session: %d\r\n"
1435 "\r\n",
1436 casterouthost, rtsp_extension, mountpoint, udp_cseq++, rtpseq);
1437 if(i > (int)sizeof(buffer) || i < 0)
1438 {
1439 fprintf(stderr, "Requested data too long\n");
1440 return;
1441 }
1442 else if(send(socket_tcp, buffer, (size_t)i, 0) != i)
1443 {
1444 perror("send");
1445 return;
1446 }
1447 laststate = ct;
1448 }
1449 /* ignore RTSP server replies */
1450 if((r=recv(socket_tcp, buffer, sizeof(buffer), 0)) < 0)
1451 {
1452#ifdef WINDOWSVERSION
1453 if(WSAGetLastError() != WSAEWOULDBLOCK)
1454#else /* WINDOWSVERSION */
1455 if(errno != EAGAIN)
1456#endif /* WINDOWSVERSION */
1457 {
1458 fprintf(stderr, "Control connection closed\n");
1459 return;
1460 }
1461 }
1462 else if(!r)
1463 {
1464 fprintf(stderr, "Control connection read error\n");
1465 return;
1466 }
1467 }
1468 if(send_recv_success == 3) reconnect_sec = 1;
1469 }
1470 return;
1471}
1472
1473
1474/********************************************************************
1475 * openserial
1476 *
1477 * Open the serial port with the given device name and configure it for
1478 * reading NMEA data from a GPS receiver.
1479 *
1480 * Parameters:
1481 * tty : pointer to : A zero-terminated string containing the device
1482 * unsigned char name of the appropriate serial port.
1483 * blocksz : integer : Block size for port I/O (ifndef WINDOWSVERSION)
1484 * baud : integer : Baud rate for port I/O
1485 *
1486 * Return Value:
1487 * The function returns a file descriptor for the opened port if successful.
1488 * The function returns -1 / INVALID_HANDLE_VALUE in the event of an error.
1489 *
1490 * Remarks:
1491 *
1492 ********************************************************************/
1493#ifndef WINDOWSVERSION
1494static int openserial(const char * tty, int blocksz, int baud)
1495{
1496 struct termios termios;
1497
1498/*** opening the serial port ***/
1499 gps_serial = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
1500 if(gps_serial < 0)
1501 {
1502 perror("ERROR: opening serial connection");
1503 return (-1);
1504 }
1505
1506/*** configuring the serial port ***/
1507 if(tcgetattr(gps_serial, &termios) < 0)
1508 {
1509 perror("ERROR: get serial attributes");
1510 return (-1);
1511 }
1512 termios.c_iflag = 0;
1513 termios.c_oflag = 0; /* (ONLRET) */
1514 termios.c_cflag = CS8 | CLOCAL | CREAD;
1515 termios.c_lflag = 0;
1516 {
1517 int cnt;
1518 for(cnt = 0; cnt < NCCS; cnt++)
1519 termios.c_cc[cnt] = -1;
1520 }
1521 termios.c_cc[VMIN] = blocksz;
1522 termios.c_cc[VTIME] = 2;
1523
1524#if (B4800 != 4800)
1525/* Not every system has speed settings equal to absolute speed value. */
1526 switch (baud)
1527 {
1528 case 300:
1529 baud = B300;
1530 break;
1531 case 1200:
1532 baud = B1200;
1533 break;
1534 case 2400:
1535 baud = B2400;
1536 break;
1537 case 4800:
1538 baud = B4800;
1539 break;
1540 case 9600:
1541 baud = B9600;
1542 break;
1543 case 19200:
1544 baud = B19200;
1545 break;
1546 case 38400:
1547 baud = B38400;
1548 break;
1549#ifdef B57600
1550 case 57600:
1551 baud = B57600;
1552 break;
1553#endif
1554#ifdef B115200
1555 case 115200:
1556 baud = B115200;
1557 break;
1558#endif
1559#ifdef B230400
1560 case 230400:
1561 baud = B230400;
1562 break;
1563#endif
1564 default:
1565 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
1566 baud = B19200;
1567 break;
1568 }
1569#endif
1570
1571 if(cfsetispeed(&termios, baud) != 0)
1572 {
1573 perror("ERROR: setting serial speed with cfsetispeed");
1574 return (-1);
1575 }
1576 if(cfsetospeed(&termios, baud) != 0)
1577 {
1578 perror("ERROR: setting serial speed with cfsetospeed");
1579 return (-1);
1580 }
1581 if(tcsetattr(gps_serial, TCSANOW, &termios) < 0)
1582 {
1583 perror("ERROR: setting serial attributes");
1584 return (-1);
1585 }
1586 if(fcntl(gps_serial, F_SETFL, 0) == -1)
1587 {
1588 perror("WARNING: setting blocking inputmode failed");
1589 }
1590 return (gps_serial);
1591}
1592#else
1593static HANDLE openserial(const char * tty, int baud)
1594{
1595 DCB dcb;
1596 COMMTIMEOUTS cmt;
1597 char compath[15] = "";
1598
1599 snprintf(compath, sizeof(compath), "\\\\.\\%s", tty);
1600/*** opening the serial port ***/
1601 gps_serial = CreateFile(compath, GENERIC_READ | GENERIC_WRITE, 0, 0,
1602 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1603 if(gps_serial == INVALID_HANDLE_VALUE)
1604 {
1605 fprintf(stderr, "ERROR: opening serial connection\n");
1606 return (INVALID_HANDLE_VALUE);
1607 }
1608/*** configuring the serial port ***/
1609 FillMemory(&dcb, sizeof(dcb), 0);
1610 dcb.DCBlength = sizeof(dcb);
1611 if(!GetCommState(gps_serial, &dcb))
1612 {
1613 fprintf(stderr, "ERROR: get serial attributes\n");
1614 return (INVALID_HANDLE_VALUE);
1615 }
1616 switch (baud)
1617 {
1618 case 110:
1619 baud = CBR_110;
1620 break;
1621 case 300:
1622 baud = CBR_300;
1623 break;
1624 case 600:
1625 baud = CBR_600;
1626 break;
1627 case 1200:
1628 baud = CBR_1200;
1629 break;
1630 case 2400:
1631 baud = CBR_2400;
1632 break;
1633 case 4800:
1634 baud = CBR_4800;
1635 break;
1636 case 9600:
1637 baud = CBR_9600;
1638 break;
1639 case 14400:
1640 baud = CBR_14400;
1641 break;
1642 case 19200:
1643 baud = CBR_19200;
1644 break;
1645 case 38400:
1646 baud = CBR_38400;
1647 break;
1648 case 56000:
1649 baud = CBR_56000;
1650 break;
1651 case 57600:
1652 baud = CBR_57600;
1653 break;
1654 case 115200:
1655 baud = CBR_115200;
1656 break;
1657 case 128000:
1658 baud = CBR_128000;
1659 break;
1660 case 256000:
1661 baud = CBR_256000;
1662 break;
1663 default:
1664 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
1665 baud = CBR_19200;
1666 break;
1667 }
1668 dcb.BaudRate = baud;
1669 dcb.ByteSize = 8;
1670 dcb.StopBits = ONESTOPBIT;
1671 dcb.Parity = NOPARITY;
1672 if(!GetCommState(gps_serial, &dcb))
1673 {
1674 fprintf(stderr, "ERROR: get serial attributes\n");
1675 return (INVALID_HANDLE_VALUE);
1676 }
1677 FillMemory(&cmt, sizeof(cmt), 0);
1678 cmt.ReadIntervalTimeout = 1000;
1679 cmt.ReadTotalTimeoutMultiplier = 1;
1680 cmt.ReadTotalTimeoutConstant = 0;
1681 if(!SetCommTimeouts(gps_serial, &cmt))
1682 {
1683 fprintf(stderr, "ERROR: set serial timeouts\n");
1684 return (INVALID_HANDLE_VALUE);
1685 }
1686 return (gps_serial);
1687} /* openserial */
1688#endif
1689
1690/********************************************************************
1691* usage
1692*
1693* Send a usage message to standard error and quit the program.
1694*
1695* Parameters:
1696* None.
1697*
1698* Return Value:
1699* The function does not return a value.
1700*
1701* Remarks:
1702*
1703*********************************************************************/
1704#ifdef __GNUC__
1705__attribute__ ((noreturn))
1706#endif /* __GNUC__ */
1707void usage(int rc, char *name)
1708{
1709 fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]\n",
1710 revisionstr, datestr, name);
1711 fprintf(stderr, "PURPOSE\n");
1712 fprintf(stderr, " The purpose of this program is to pick up a GNSS data stream (Input, Source)\n");
1713 fprintf(stderr, " from either\n\n");
1714 fprintf(stderr, " 1. a Serial port, or\n");
1715 fprintf(stderr, " 2. an IP server, or\n");
1716 fprintf(stderr, " 3. a File, or\n");
1717 fprintf(stderr, " 4. a SISNeT Data Server, or\n");
1718 fprintf(stderr, " 5. a UDP server, or\n");
1719 fprintf(stderr, " 6. an NTRIP Version 1.0 Caster\n\n");
1720 fprintf(stderr, " and forward that incoming stream (Output, Destination) to either\n\n");
1721 fprintf(stderr, " - an NTRIP Version 1.0 Caster, or\n");
1722 fprintf(stderr, " - an NTRIP Version 2.0 Caster via TCP/IP or RTSP/RTP.\n\n\n");
1723 fprintf(stderr, "OPTIONS\n");
1724 fprintf(stderr, " -h|? print this help screen\n\n");
1725 fprintf(stderr, " -E <ProxyHost> Proxy server host name or address, required i.e. when\n");
1726 fprintf(stderr, " running the program in a proxy server protected LAN,\n");
1727 fprintf(stderr, " optional\n");
1728 fprintf(stderr, " -F <ProxyPort> Proxy server IP port, required i.e. when running\n");
1729 fprintf(stderr, " the program in a proxy server protected LAN, optional\n");
1730 fprintf(stderr, " -R <maxDelay> Reconnect mechanism with maximum delay between reconnect\n");
1731 fprintf(stderr, " attemts in seconds, default: no reconnect activated,\n");
1732 fprintf(stderr, " optional\n\n");
1733 fprintf(stderr, " -M <InputMode> Sets the input mode (1 = Serial Port, 2 = IP server,\n");
1734 fprintf(stderr, " 3 = File, 4 = SISNeT Data Server, 5 = UDP server, 6 = NTRIP Caster),\n");
1735 fprintf(stderr, " mandatory\n\n");
1736 fprintf(stderr, " <InputMode> = 1 (Serial Port):\n");
1737 fprintf(stderr, " -i <Device> Serial input device, default: /dev/gps, mandatory if\n");
1738 fprintf(stderr, " <InputMode>=1\n");
1739 fprintf(stderr, " -b <BaudRate> Serial input baud rate, default: 19200 bps, mandatory\n");
1740 fprintf(stderr, " if <InputMode>=1\n\n");
1741 fprintf(stderr, " <InputMode> = 2|5 (IP port | UDP port):\n");
1742 fprintf(stderr, " -H <ServerHost> Input host name or address, default: 127.0.0.1,\n");
1743 fprintf(stderr, " mandatory if <InputMode> = 2|5\n");
1744 fprintf(stderr, " -P <ServerPort> Input port, default: 1025, mandatory if <InputMode>= 2|5\n");
1745 fprintf(stderr, " -f <ServerFile> Name of initialization file to be send to server,\n");
1746 fprintf(stderr, " optional\n");
1747 fprintf(stderr, " -x <ServerUser> User ID to access incoming stream, optional\n");
1748 fprintf(stderr, " -y <ServerPass> Password, to access incoming stream, optional\n");
1749 fprintf(stderr, " -B Bind to incoming UDP stream, optional for <InputMode> = 5\n\n");
1750 fprintf(stderr, " <InputMode> = 3 (File):\n");
1751 fprintf(stderr, " -s <File> File name to simulate stream by reading data from (log)\n");
1752 fprintf(stderr, " file, default is /dev/stdin, mandatory for <InputMode> = 3\n\n");
1753 fprintf(stderr, " <InputMode> = 4 (SISNeT Data Server):\n");
1754 fprintf(stderr, " -H <SisnetHost> SISNeT Data Server name or address,\n");
1755 fprintf(stderr, " default: 131.176.49.142, mandatory if <InputMode> = 4\n");
1756 fprintf(stderr, " -P <SisnetPort> SISNeT Data Server port, default: 7777, mandatory if\n");
1757 fprintf(stderr, " <InputMode> = 4\n");
1758 fprintf(stderr, " -u <SisnetUser> SISNeT Data Server user ID, mandatory if <InputMode> = 4\n");
1759 fprintf(stderr, " -l <SisnetPass> SISNeT Data Server password, mandatory if <InputMode> = 4\n");
1760 fprintf(stderr, " -V <SisnetVers> SISNeT Data Server Version number, options are 2.1, 3.0\n");
1761 fprintf(stderr, " or 3.1, default: 3.1, mandatory if <InputMode> = 4\n\n");
1762 fprintf(stderr, " <InputMode> = 6 (NTRIP Version 1.0 Caster):\n");
1763 fprintf(stderr, " -H <SourceHost> Source caster name or address, default: 127.0.0.1,\n");
1764 fprintf(stderr, " mandatory if <InputMode> = 6\n");
1765 fprintf(stderr, " -P <SourcePort> Source caster port, default: 2101, mandatory if\n");
1766 fprintf(stderr, " <InputMode> = 6\n");
1767 fprintf(stderr, " -D <SourceMount> Source caster mountpoint for stream input, mandatory if\n");
1768 fprintf(stderr, " <InputMode> = 6\n");
1769 fprintf(stderr, " -U <SourceUser> Source caster user Id for input stream access, mandatory\n");
1770 fprintf(stderr, " for protected streams if <InputMode> = 6\n");
1771 fprintf(stderr, " -W <SourcePass> Source caster password for input stream access, mandatory\n");
1772 fprintf(stderr, " for protected streams if <InputMode> = 6\n\n");
1773 fprintf(stderr, " -O <OutputMode> Sets output mode for communatation with destination caster\n");
1774 fprintf(stderr, " 1 = http: NTRIP Version 2.0 Caster in TCP/IP mode\n");
1775 fprintf(stderr, " 2 = rtsp: NTRIP Version 2.0 Caster in RTSP/RTP mode\n");
1776 fprintf(stderr, " 3 = ntrip1: NTRIP Version 1.0 Caster\n");
1777 fprintf(stderr, " optional\n\n");
1778 fprintf(stderr, " Defaults to NTRIP1.0, but will change to 2.0 in future versions\n");
1779 fprintf(stderr, " Note that the program automatically falls back from mode rtsp to mode http and\n");
1780 fprintf(stderr, " further to mode ntrip1 if necessary.\n\n");
1781 fprintf(stderr, " -a <DestHost> Destination caster name or address, default: 127.0.0.1,\n");
1782 fprintf(stderr, " mandatory\n");
1783 fprintf(stderr, " -p <DestPort> Destination caster port, default: 2101, mandatory\n");
1784 fprintf(stderr, " -m <DestMount> Destination caster mountpoint for stream upload,\n");
1785 fprintf(stderr, " mandatory\n");
1786 fprintf(stderr, " -n <DestUser> Destination caster user ID for stream upload to\n");
1787 fprintf(stderr, " mountpoint, only for NTRIP Version 2.0 destination\n");
1788 fprintf(stderr, " casters, mandatory\n");
1789 fprintf(stderr, " -c <DestPass> Destination caster password for stream upload to\n");
1790 fprintf(stderr, " mountpoint, mandatory\n");
1791 fprintf(stderr, " -N <STR-record> Sourcetable STR-record\n");
1792 fprintf(stderr, " optional for NTRIP Version 2.0 in RTSP/RTP and TCP/IP mode\n\n");
1793 exit(rc);
1794} /* usage */
1795
1796
1797/********************************************************************/
1798/* signal handling */
1799/********************************************************************/
1800#ifdef __GNUC__
1801static void handle_sigint(int sig __attribute__((__unused__)))
1802#else /* __GNUC__ */
1803static void handle_sigint(int sig)
1804#endif /* __GNUC__ */
1805{
1806 sigint_received = 1;
1807 fprintf(stderr, "WARNING: SIGINT received - ntripserver terminates\n");
1808}
1809
1810#ifndef WINDOWSVERSION
1811#ifdef __GNUC__
1812static void handle_alarm(int sig __attribute__((__unused__)))
1813#else /* __GNUC__ */
1814static void handle_alarm(int sig)
1815#endif /* __GNUC__ */
1816{
1817 sigalarm_received = 1;
1818 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
1819}
1820
1821#ifdef __GNUC__
1822static void handle_sigpipe(int sig __attribute__((__unused__)))
1823#else /* __GNUC__ */
1824static void handle_sigpipe(int sig)
1825#endif /* __GNUC__ */
1826{
1827 sigpipe_received = 1;
1828}
1829#endif /* WINDOWSVERSION */
1830
1831static void setup_signal_handler(int sig, void (*handler)(int))
1832{
1833#if _POSIX_VERSION > 198800L
1834 struct sigaction action;
1835
1836 action.sa_handler = handler;
1837 sigemptyset(&(action.sa_mask));
1838 sigaddset(&(action.sa_mask), sig);
1839 action.sa_flags = 0;
1840 sigaction(sig, &action, 0);
1841#else
1842 signal(sig, handler);
1843#endif
1844 return;
1845} /* setupsignal_handler */
1846
1847
1848/********************************************************************
1849 * base64-encoding *
1850*******************************************************************/
1851static const char encodingTable [64] =
1852{
1853 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
1854 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
1855 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
1856 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
1857};
1858
1859/* does not buffer overrun, but breaks directly after an error */
1860/* returns the number of required bytes */
1861static int encode(char *buf, int size, const char *user, const char *pwd)
1862{
1863 unsigned char inbuf[3];
1864 char *out = buf;
1865 int i, sep = 0, fill = 0, bytes = 0;
1866
1867 while(*user || *pwd)
1868 {
1869 i = 0;
1870 while(i < 3 && *user) inbuf[i++] = *(user++);
1871 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
1872 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
1873 while(i < 3) {inbuf[i++] = 0; ++fill; }
1874 if(out-buf < size-1)
1875 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
1876 if(out-buf < size-1)
1877 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
1878 | ((inbuf [1] & 0xF0) >> 4)];
1879 if(out-buf < size-1)
1880 {
1881 if(fill == 2)
1882 *(out++) = '=';
1883 else
1884 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
1885 | ((inbuf [2] & 0xC0) >> 6)];
1886 }
1887 if(out-buf < size-1)
1888 {
1889 if(fill >= 1)
1890 *(out++) = '=';
1891 else
1892 *(out++) = encodingTable[inbuf [2] & 0x3F];
1893 }
1894 bytes += 4;
1895 }
1896 if(out-buf < size)
1897 *out = 0;
1898 return bytes;
1899}/* base64 Encoding */
1900
1901
1902/********************************************************************
1903 * send message to caster *
1904*********************************************************************/
1905static int send_to_caster(char *input, sockettype socket, int input_size)
1906{
1907 int send_error = 1;
1908
1909 if((send(socket, input, (size_t)input_size, 0)) != input_size)
1910 {
1911 fprintf(stderr, "WARNING: could not send full header to Destination caster\n");
1912 send_error = 0;
1913 }
1914#ifndef NDEBUG
1915 else
1916 {
1917 fprintf(stderr, "\nDestination caster request:\n");
1918 fprintf(stderr, "%s", input);
1919 }
1920#endif
1921 return send_error;
1922}/* send_to_caster */
1923
1924
1925/********************************************************************
1926 * reconnect *
1927*********************************************************************/
1928int reconnect(int rec_sec, int rec_sec_max)
1929{
1930 fprintf(stderr,"reconnect in <%d> seconds\n\n", rec_sec);
1931 rec_sec *= 2;
1932 if (rec_sec > rec_sec_max) rec_sec = rec_sec_max;
1933#ifndef WINDOWSVERSION
1934 sleep(rec_sec);
1935 sigpipe_received = 0;
1936#else
1937 Sleep(rec_sec*1000);
1938#endif
1939 sigalarm_received = 0;
1940 return rec_sec;
1941} /* reconnect */
1942
1943
1944/********************************************************************
1945 * close session *
1946*********************************************************************/
1947static void close_session(const char *caster_addr, const char *mountpoint,
1948int session, char *rtsp_ext, int fallback)
1949{
1950 int size_send_buf;
1951 char send_buf[BUFSZ];
1952
1953 if(!fallback)
1954 {
1955 if((gps_socket != INVALID_SOCKET) &&
1956 ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) ||
1957 (inputmode == CASTER) || (inputmode == SISNET)))
1958 {
1959 if(closesocket(gps_socket) == -1)
1960 {
1961 perror("ERROR: close input device ");
1962 exit(0);
1963 }
1964 else
1965 {
1966 gps_socket = -1;
1967#ifndef NDEBUG
1968 fprintf(stderr, "close input device: successful\n");
1969#endif
1970 }
1971 }
1972 else if((gps_serial != INVALID_HANDLE_VALUE) && (inputmode == SERIAL))
1973 {
1974#ifndef WINDOWSVERSION
1975 if(close(gps_serial) == INVALID_HANDLE_VALUE)
1976 {
1977 perror("ERROR: close input device ");
1978 exit(0);
1979 }
1980#else
1981 if(!CloseHandle(gps_serial))
1982 {
1983 fprintf(stderr, "ERROR: close input device ");
1984 exit(0);
1985 }
1986#endif
1987 else
1988 {
1989 gps_serial = INVALID_HANDLE_VALUE;
1990#ifndef NDEBUG
1991 fprintf(stderr, "close input device: successful\n");
1992#endif
1993 }
1994 }
1995 else if((gps_file != -1) && (inputmode == INFILE))
1996 {
1997 if(close(gps_file) == -1)
1998 {
1999 perror("ERROR: close input device ");
2000 exit(0);
2001 }
2002 else
2003 {
2004 gps_file = -1;
2005#ifndef NDEBUG
2006 fprintf(stderr, "close input device: successful\n");
2007#endif
2008 }
2009 }
2010 }
2011
2012 if(socket_udp != INVALID_SOCKET)
2013 {
2014 if(udp_cseq > 2)
2015 {
2016 size_send_buf = snprintf(send_buf, sizeof(send_buf),
2017 "TEARDOWN rtsp://%s%s/%s RTSP/1.0\r\n"
2018 "CSeq: %d\r\n"
2019 "Session: %d\r\n"
2020 "\r\n",
2021 caster_addr, rtsp_ext, mountpoint, udp_cseq++, session);
2022 if((size_send_buf >= (int)sizeof(send_buf)) || (size_send_buf < 0))
2023 {
2024 fprintf(stderr, "ERROR: Destination caster request to long\n");
2025 exit(0);
2026 }
2027 send_to_caster(send_buf, socket_tcp, size_send_buf); strcpy(send_buf,"");
2028 size_send_buf = recv(socket_tcp, send_buf, sizeof(send_buf), 0);
2029 send_buf[size_send_buf] = '\0';
2030#ifndef NDEBUG
2031 fprintf(stderr, "Destination caster response:\n%s", send_buf);
2032#endif
2033 }
2034 if(closesocket(socket_udp)==-1)
2035 {
2036 perror("ERROR: close udp socket");
2037 exit(0);
2038 }
2039 else
2040 {
2041 socket_udp = -1;
2042#ifndef NDEBUG
2043 fprintf(stderr, "close udp socket: successful\n");
2044#endif
2045 }
2046 }
2047
2048 if(socket_tcp != INVALID_SOCKET)
2049 {
2050 if(closesocket(socket_tcp) == -1)
2051 {
2052 perror("ERROR: close tcp socket");
2053 exit(0);
2054 }
2055 else
2056 {
2057 socket_tcp = -1;
2058#ifndef NDEBUG
2059 fprintf(stderr, "close tcp socket: successful\n");
2060#endif
2061 }
2062 }
2063} /* close_session */
Note: See TracBrowser for help on using the repository browser.