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