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