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