[483] | 1 | /*
|
---|
[484] | 2 | Easy example NTRIP client for POSIX.
|
---|
[579] | 3 | $Id: ntripclient.c,v 1.35 2007/10/26 14:56:47 stuerze Exp $
|
---|
[483] | 4 | Copyright (C) 2003-2005 by Dirk Stoecker <soft@dstoecker.de>
|
---|
| 5 |
|
---|
| 6 | This program is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program; if not, write to the Free Software
|
---|
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 19 | or read http://www.gnu.org/licenses/gpl.txt
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #include <ctype.h>
|
---|
| 23 | #include <getopt.h>
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <stdlib.h>
|
---|
| 26 | #include <errno.h>
|
---|
| 27 | #include <string.h>
|
---|
| 28 | #include <time.h>
|
---|
| 29 |
|
---|
[579] | 30 | #ifdef WINDOWSVERSION
|
---|
| 31 | #include <winsock.h>
|
---|
| 32 | typedef SOCKET sockettype;
|
---|
| 33 | typedef u_long in_addr_t;
|
---|
| 34 | typedef size_t socklen_t;
|
---|
| 35 | #else
|
---|
| 36 | typedef int sockettype;
|
---|
| 37 | #include <signal.h>
|
---|
| 38 | #include <fcntl.h>
|
---|
| 39 | #include <unistd.h>
|
---|
| 40 | #include <arpa/inet.h>
|
---|
| 41 | #include <sys/socket.h>
|
---|
| 42 | #include <netinet/in.h>
|
---|
| 43 | #include <netdb.h>
|
---|
| 44 |
|
---|
| 45 | #define closesocket(sock) close(sock)
|
---|
| 46 | #define ALARMTIME (2*60)
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
[483] | 49 | #ifndef COMPILEDATE
|
---|
| 50 | #define COMPILEDATE " built " __DATE__
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | /* The string, which is send as agent in HTTP request */
|
---|
[484] | 54 | #define AGENTSTRING "NTRIP NtripClientPOSIX"
|
---|
[483] | 55 |
|
---|
| 56 | #define MAXDATASIZE 1000 /* max number of bytes we can get at once */
|
---|
| 57 |
|
---|
| 58 | /* CVS revision and version */
|
---|
[579] | 59 | static char revisionstr[] = "$Revision: 1.35 $";
|
---|
| 60 | static char datestr[] = "$Date: 2007/10/26 14:56:47 $";
|
---|
[483] | 61 |
|
---|
[494] | 62 | enum MODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, AUTO = 4, END };
|
---|
| 63 |
|
---|
[483] | 64 | struct Args
|
---|
| 65 | {
|
---|
| 66 | const char *server;
|
---|
[494] | 67 | const char *port;
|
---|
[483] | 68 | const char *user;
|
---|
[494] | 69 | const char *proxyhost;
|
---|
| 70 | const char *proxyport;
|
---|
[483] | 71 | const char *password;
|
---|
| 72 | const char *nmea;
|
---|
| 73 | const char *data;
|
---|
| 74 | int bitrate;
|
---|
[494] | 75 | int mode;
|
---|
[483] | 76 | };
|
---|
| 77 |
|
---|
| 78 | /* option parsing */
|
---|
| 79 | #ifdef NO_LONG_OPTS
|
---|
| 80 | #define LONG_OPT(a)
|
---|
| 81 | #else
|
---|
| 82 | #define LONG_OPT(a) a
|
---|
| 83 | static struct option opts[] = {
|
---|
| 84 | { "bitrate", no_argument, 0, 'b'},
|
---|
| 85 | { "data", required_argument, 0, 'd'},
|
---|
[550] | 86 | { "mountpoint", required_argument, 0, 'm'},
|
---|
[483] | 87 | { "server", required_argument, 0, 's'},
|
---|
| 88 | { "password", required_argument, 0, 'p'},
|
---|
| 89 | { "port", required_argument, 0, 'r'},
|
---|
[494] | 90 | { "proxyport", required_argument, 0, 'R'},
|
---|
| 91 | { "proxyhost", required_argument, 0, 'S'},
|
---|
[483] | 92 | { "user", required_argument, 0, 'u'},
|
---|
| 93 | { "nmea", required_argument, 0, 'n'},
|
---|
[501] | 94 | { "mode", required_argument, 0, 'M'},
|
---|
[483] | 95 | { "help", no_argument, 0, 'h'},
|
---|
| 96 | {0,0,0,0}};
|
---|
| 97 | #endif
|
---|
[550] | 98 | #define ARGOPT "-d:m:bhp:r:s:u:n:S:R:M:"
|
---|
[483] | 99 |
|
---|
[579] | 100 | int stop = 0;
|
---|
| 101 | #ifndef WINDOWSVERSION
|
---|
[483] | 102 | #ifdef __GNUC__
|
---|
| 103 | static __attribute__ ((noreturn)) void sighandler_alarm(
|
---|
| 104 | int sig __attribute__((__unused__)))
|
---|
| 105 | #else /* __GNUC__ */
|
---|
| 106 | static void sighandler_alarm(int sig)
|
---|
| 107 | #endif /* __GNUC__ */
|
---|
| 108 | {
|
---|
| 109 | fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
|
---|
| 110 | exit(1);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[494] | 113 | #ifdef __GNUC__
|
---|
| 114 | static void sighandler_int(int sig __attribute__((__unused__)))
|
---|
| 115 | #else /* __GNUC__ */
|
---|
| 116 | static void sighandler_alarm(int sig)
|
---|
| 117 | #endif /* __GNUC__ */
|
---|
| 118 | {
|
---|
| 119 | alarm(2);
|
---|
| 120 | stop = 1;
|
---|
| 121 | }
|
---|
[579] | 122 | #endif /* WINDOWSVERSION */
|
---|
[494] | 123 |
|
---|
[500] | 124 | static const char *encodeurl(const char *req)
|
---|
| 125 | {
|
---|
| 126 | char *h = "0123456789abcdef";
|
---|
| 127 | static char buf[128];
|
---|
| 128 | char *urlenc = buf;
|
---|
[501] | 129 | char *bufend = buf + sizeof(buf) - 3;
|
---|
[500] | 130 |
|
---|
[501] | 131 | while(*req && urlenc < bufend)
|
---|
[500] | 132 | {
|
---|
| 133 | if(isalnum(*req)
|
---|
| 134 | || *req == '-' || *req == '_' || *req == '.')
|
---|
| 135 | *urlenc++ = *req++;
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
| 138 | *urlenc++ = '%';
|
---|
| 139 | *urlenc++ = h[*req >> 4];
|
---|
| 140 | *urlenc++ = h[*req & 0x0f];
|
---|
[579] | 141 | req++;
|
---|
[500] | 142 | }
|
---|
[501] | 143 | }
|
---|
| 144 | *urlenc = 0;
|
---|
[500] | 145 | return buf;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[483] | 148 | static const char *geturl(const char *url, struct Args *args)
|
---|
| 149 | {
|
---|
| 150 | static char buf[1000];
|
---|
| 151 | static char *Buffer = buf;
|
---|
| 152 | static char *Bufend = buf+sizeof(buf);
|
---|
[500] | 153 | char *h = "0123456789abcdef";
|
---|
[483] | 154 |
|
---|
| 155 | if(strncmp("ntrip:", url, 6))
|
---|
| 156 | return "URL must start with 'ntrip:'.";
|
---|
| 157 | url += 6; /* skip ntrip: */
|
---|
| 158 |
|
---|
| 159 | if(*url != '@' && *url != '/')
|
---|
| 160 | {
|
---|
| 161 | /* scan for mountpoint */
|
---|
| 162 | args->data = Buffer;
|
---|
[500] | 163 | if(*url != '?')
|
---|
| 164 | {
|
---|
| 165 | while(*url && *url != '@' && *url != ';' && *url != '/' && Buffer != Bufend)
|
---|
| 166 | *(Buffer++) = *(url++);
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
| 170 | while(*url && *url != '@' && *url != '/' && Buffer != Bufend)
|
---|
| 171 | {
|
---|
| 172 | if(isalnum(*url) || *url == '-' || *url == '_' || *url == '.')
|
---|
| 173 | *Buffer++ = *url++;
|
---|
| 174 | else
|
---|
| 175 | {
|
---|
| 176 | *Buffer++ = '%';
|
---|
| 177 | *Buffer++ = h[*url >> 4];
|
---|
| 178 | *Buffer++ = h[*url & 0x0f];
|
---|
[579] | 179 | url++;
|
---|
[500] | 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
[483] | 183 | if(Buffer == args->data)
|
---|
| 184 | return "Mountpoint required.";
|
---|
| 185 | else if(Buffer >= Bufend-1)
|
---|
| 186 | return "Parsing buffer too short.";
|
---|
| 187 | *(Buffer++) = 0;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | if(*url == '/') /* username and password */
|
---|
| 191 | {
|
---|
| 192 | ++url;
|
---|
| 193 | args->user = Buffer;
|
---|
| 194 | while(*url && *url != '@' && *url != ';' && *url != ':' && Buffer != Bufend)
|
---|
| 195 | *(Buffer++) = *(url++);
|
---|
| 196 | if(Buffer == args->user)
|
---|
| 197 | return "Username cannot be empty.";
|
---|
| 198 | else if(Buffer >= Bufend-1)
|
---|
| 199 | return "Parsing buffer too short.";
|
---|
| 200 | *(Buffer++) = 0;
|
---|
| 201 |
|
---|
| 202 | if(*url == ':') ++url;
|
---|
| 203 |
|
---|
| 204 | args->password = Buffer;
|
---|
| 205 | while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
|
---|
| 206 | *(Buffer++) = *(url++);
|
---|
| 207 | if(Buffer == args->password)
|
---|
| 208 | return "Password cannot be empty.";
|
---|
| 209 | else if(Buffer >= Bufend-1)
|
---|
| 210 | return "Parsing buffer too short.";
|
---|
| 211 | *(Buffer++) = 0;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if(*url == '@') /* server */
|
---|
| 215 | {
|
---|
| 216 | ++url;
|
---|
[494] | 217 | if(*url != '@' && *url != ':')
|
---|
| 218 | {
|
---|
| 219 | args->server = Buffer;
|
---|
| 220 | while(*url && *url != '@' && *url != ':' && *url != ';' && Buffer != Bufend)
|
---|
| 221 | *(Buffer++) = *(url++);
|
---|
| 222 | if(Buffer == args->server)
|
---|
| 223 | return "Servername cannot be empty.";
|
---|
| 224 | else if(Buffer >= Bufend-1)
|
---|
| 225 | return "Parsing buffer too short.";
|
---|
| 226 | *(Buffer++) = 0;
|
---|
| 227 | }
|
---|
[483] | 228 |
|
---|
| 229 | if(*url == ':')
|
---|
| 230 | {
|
---|
[494] | 231 | ++url;
|
---|
| 232 | args->port = Buffer;
|
---|
| 233 | while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
|
---|
| 234 | *(Buffer++) = *(url++);
|
---|
| 235 | if(Buffer == args->port)
|
---|
| 236 | return "Port cannot be empty.";
|
---|
| 237 | else if(Buffer >= Bufend-1)
|
---|
| 238 | return "Parsing buffer too short.";
|
---|
| 239 | *(Buffer++) = 0;
|
---|
[483] | 240 | }
|
---|
[494] | 241 |
|
---|
| 242 | if(*url == '@') /* proxy */
|
---|
| 243 | {
|
---|
| 244 | ++url;
|
---|
| 245 | args->proxyhost = Buffer;
|
---|
| 246 | while(*url && *url != ':' && *url != ';' && Buffer != Bufend)
|
---|
| 247 | *(Buffer++) = *(url++);
|
---|
| 248 | if(Buffer == args->proxyhost)
|
---|
| 249 | return "Proxy servername cannot be empty.";
|
---|
| 250 | else if(Buffer >= Bufend-1)
|
---|
| 251 | return "Parsing buffer too short.";
|
---|
| 252 | *(Buffer++) = 0;
|
---|
| 253 |
|
---|
| 254 | if(*url == ':')
|
---|
| 255 | {
|
---|
| 256 | ++url;
|
---|
| 257 | args->proxyport = Buffer;
|
---|
| 258 | while(*url && *url != ';' && Buffer != Bufend)
|
---|
| 259 | *(Buffer++) = *(url++);
|
---|
| 260 | if(Buffer == args->proxyport)
|
---|
| 261 | return "Proxy port cannot be empty.";
|
---|
| 262 | else if(Buffer >= Bufend-1)
|
---|
| 263 | return "Parsing buffer too short.";
|
---|
| 264 | *(Buffer++) = 0;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
[483] | 267 | }
|
---|
| 268 | if(*url == ';') /* NMEA */
|
---|
| 269 | {
|
---|
| 270 | args->nmea = ++url;
|
---|
| 271 | while(*url)
|
---|
| 272 | ++url;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | return *url ? "Garbage at end of server string." : 0;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | static int getargs(int argc, char **argv, struct Args *args)
|
---|
| 279 | {
|
---|
| 280 | int res = 1;
|
---|
| 281 | int getoptr;
|
---|
| 282 | char *a;
|
---|
| 283 | int i = 0, help = 0;
|
---|
| 284 |
|
---|
| 285 | args->server = "www.euref-ip.net";
|
---|
[494] | 286 | args->port = "2101";
|
---|
[483] | 287 | args->user = "";
|
---|
| 288 | args->password = "";
|
---|
| 289 | args->nmea = 0;
|
---|
| 290 | args->data = 0;
|
---|
| 291 | args->bitrate = 0;
|
---|
[494] | 292 | args->proxyhost = 0;
|
---|
| 293 | args->proxyport = "2101";
|
---|
| 294 | args->mode = AUTO;
|
---|
[483] | 295 | help = 0;
|
---|
| 296 |
|
---|
| 297 | do
|
---|
| 298 | {
|
---|
| 299 | #ifdef NO_LONG_OPTS
|
---|
| 300 | switch((getoptr = getopt(argc, argv, ARGOPT)))
|
---|
| 301 | #else
|
---|
| 302 | switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
|
---|
| 303 | #endif
|
---|
| 304 | {
|
---|
| 305 | case 's': args->server = optarg; break;
|
---|
| 306 | case 'u': args->user = optarg; break;
|
---|
| 307 | case 'p': args->password = optarg; break;
|
---|
[500] | 308 | case 'd':
|
---|
[550] | 309 | case 'm':
|
---|
[501] | 310 | if(optarg && *optarg == '?')
|
---|
[500] | 311 | args->data = encodeurl(optarg);
|
---|
| 312 | else
|
---|
| 313 | args->data = optarg;
|
---|
| 314 | break;
|
---|
[483] | 315 | case 'n': args->nmea = optarg; break;
|
---|
| 316 | case 'b': args->bitrate = 1; break;
|
---|
| 317 | case 'h': help=1; break;
|
---|
[494] | 318 | case 'r': args->port = optarg; break;
|
---|
| 319 | case 'S': args->proxyhost = optarg; break;
|
---|
| 320 | case 'R': args->proxyport = optarg; break;
|
---|
| 321 | case 'M':
|
---|
| 322 | args->mode = 0;
|
---|
| 323 | if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
|
---|
| 324 | args->mode = NTRIP1;
|
---|
| 325 | else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
|
---|
| 326 | args->mode = HTTP;
|
---|
| 327 | else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
|
---|
| 328 | args->mode = RTSP;
|
---|
| 329 | else if(!strcmp(optarg,"a") || !strcmp(optarg,"auto"))
|
---|
| 330 | args->mode = AUTO;
|
---|
| 331 | else args->mode = atoi(optarg);
|
---|
| 332 | if((args->mode == 0) || (args->mode >= END))
|
---|
| 333 | {
|
---|
| 334 | fprintf(stderr, "Mode %s unknown\n", optarg);
|
---|
| 335 | res = 0;
|
---|
| 336 | }
|
---|
| 337 | break;
|
---|
[483] | 338 | case 1:
|
---|
| 339 | {
|
---|
| 340 | const char *err;
|
---|
| 341 | if((err = geturl(optarg, args)))
|
---|
| 342 | {
|
---|
| 343 | fprintf(stderr, "%s\n\n", err);
|
---|
| 344 | res = 0;
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | break;
|
---|
| 348 | case -1: break;
|
---|
| 349 | }
|
---|
| 350 | } while(getoptr != -1 && res);
|
---|
| 351 |
|
---|
| 352 | for(a = revisionstr+11; *a && *a != ' '; ++a)
|
---|
| 353 | revisionstr[i++] = *a;
|
---|
| 354 | revisionstr[i] = 0;
|
---|
| 355 | datestr[0] = datestr[7];
|
---|
| 356 | datestr[1] = datestr[8];
|
---|
| 357 | datestr[2] = datestr[9];
|
---|
| 358 | datestr[3] = datestr[10];
|
---|
| 359 | datestr[5] = datestr[12];
|
---|
| 360 | datestr[6] = datestr[13];
|
---|
| 361 | datestr[8] = datestr[15];
|
---|
| 362 | datestr[9] = datestr[16];
|
---|
| 363 | datestr[4] = datestr[7] = '-';
|
---|
| 364 | datestr[10] = 0;
|
---|
| 365 |
|
---|
| 366 | if(!res || help)
|
---|
| 367 | {
|
---|
| 368 | fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s -s server -u user ...\n"
|
---|
[550] | 369 | " -m " LONG_OPT("--mountpoint ") "the requested data set or sourcetable filtering criteria\n"
|
---|
| 370 | " -s " LONG_OPT("--server ") "the server name or address\n"
|
---|
| 371 | " -p " LONG_OPT("--password ") "the login password\n"
|
---|
| 372 | " -r " LONG_OPT("--port ") "the server port number (default 2101)\n"
|
---|
| 373 | " -u " LONG_OPT("--user ") "the user name\n"
|
---|
| 374 | " -n " LONG_OPT("--nmea ") "NMEA string for sending to server\n"
|
---|
| 375 | " -b " LONG_OPT("--bitrate ") "output bitrate\n"
|
---|
| 376 | " -S " LONG_OPT("--proxyhost ") "proxy name or address\n"
|
---|
| 377 | " -R " LONG_OPT("--proxyport ") "proxy port, optional (default 2101)\n"
|
---|
| 378 | " -M " LONG_OPT("--mode ") "mode for data request\n"
|
---|
[494] | 379 | " Valid modes are:\n"
|
---|
| 380 | " 1, h, http NTRIP Version 2.0 Caster in TCP/IP mode\n"
|
---|
| 381 | " 2, r, rtsp NTRIP Version 2.0 Caster in RTSP/RTP mode\n"
|
---|
| 382 | " 3, n, ntrip1 NTRIP Version 1.0 Caster\n"
|
---|
| 383 | " 4, a, auto automatic detection (default)\n"
|
---|
[550] | 384 | "or using an URL:\n%s ntrip:mountpoint[/user[:password]][@[server][:port][@proxyhost[:proxyport]]][;nmea]\n"
|
---|
[483] | 385 | , revisionstr, datestr, argv[0], argv[0]);
|
---|
| 386 | exit(1);
|
---|
| 387 | }
|
---|
| 388 | return res;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | static const char encodingTable [64] = {
|
---|
| 392 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
|
---|
| 393 | 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
|
---|
| 394 | 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
|
---|
| 395 | 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
|
---|
| 396 | };
|
---|
| 397 |
|
---|
| 398 | /* does not buffer overrun, but breaks directly after an error */
|
---|
| 399 | /* returns the number of required bytes */
|
---|
| 400 | static int encode(char *buf, int size, const char *user, const char *pwd)
|
---|
| 401 | {
|
---|
| 402 | unsigned char inbuf[3];
|
---|
| 403 | char *out = buf;
|
---|
| 404 | int i, sep = 0, fill = 0, bytes = 0;
|
---|
| 405 |
|
---|
| 406 | while(*user || *pwd)
|
---|
| 407 | {
|
---|
| 408 | i = 0;
|
---|
| 409 | while(i < 3 && *user) inbuf[i++] = *(user++);
|
---|
| 410 | if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
|
---|
| 411 | while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
|
---|
| 412 | while(i < 3) {inbuf[i++] = 0; ++fill; }
|
---|
| 413 | if(out-buf < size-1)
|
---|
| 414 | *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
|
---|
| 415 | if(out-buf < size-1)
|
---|
| 416 | *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
|
---|
| 417 | | ((inbuf [1] & 0xF0) >> 4)];
|
---|
| 418 | if(out-buf < size-1)
|
---|
| 419 | {
|
---|
| 420 | if(fill == 2)
|
---|
| 421 | *(out++) = '=';
|
---|
| 422 | else
|
---|
| 423 | *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
|
---|
| 424 | | ((inbuf [2] & 0xC0) >> 6)];
|
---|
| 425 | }
|
---|
| 426 | if(out-buf < size-1)
|
---|
| 427 | {
|
---|
| 428 | if(fill >= 1)
|
---|
| 429 | *(out++) = '=';
|
---|
| 430 | else
|
---|
| 431 | *(out++) = encodingTable[inbuf [2] & 0x3F];
|
---|
| 432 | }
|
---|
| 433 | bytes += 4;
|
---|
| 434 | }
|
---|
| 435 | if(out-buf < size)
|
---|
| 436 | *out = 0;
|
---|
| 437 | return bytes;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | int main(int argc, char **argv)
|
---|
| 441 | {
|
---|
| 442 | struct Args args;
|
---|
| 443 |
|
---|
| 444 | setbuf(stdout, 0);
|
---|
| 445 | setbuf(stdin, 0);
|
---|
| 446 | setbuf(stderr, 0);
|
---|
[579] | 447 | #ifndef WINDOWSVERSION
|
---|
[483] | 448 | signal(SIGALRM,sighandler_alarm);
|
---|
[494] | 449 | signal(SIGINT,sighandler_int);
|
---|
[483] | 450 | alarm(ALARMTIME);
|
---|
[579] | 451 | #else
|
---|
| 452 | WSADATA wsaData;
|
---|
| 453 | if(WSAStartup(MAKEWORD(1,1),&wsaData))
|
---|
| 454 | {
|
---|
| 455 | fprintf(stderr, "Could not init network access.\n");
|
---|
| 456 | return 20;
|
---|
| 457 | }
|
---|
| 458 | #endif
|
---|
[483] | 459 |
|
---|
| 460 | if(getargs(argc, argv, &args))
|
---|
| 461 | {
|
---|
[494] | 462 | int sleeptime = 0;
|
---|
| 463 | do
|
---|
[483] | 464 | {
|
---|
[579] | 465 | sockettype sockfd;
|
---|
| 466 | int numbytes;
|
---|
[494] | 467 | char buf[MAXDATASIZE];
|
---|
| 468 | struct sockaddr_in their_addr; /* connector's address information */
|
---|
| 469 | struct hostent *he;
|
---|
| 470 | struct servent *se;
|
---|
| 471 | const char *server, *port, *proxyserver = 0;
|
---|
| 472 | char proxyport[6];
|
---|
| 473 | char *b;
|
---|
| 474 | long i;
|
---|
| 475 | if(sleeptime)
|
---|
| 476 | {
|
---|
[579] | 477 | #ifdef WINDOWSVERSION
|
---|
| 478 | Sleep(sleeptime*1000);
|
---|
| 479 | #else
|
---|
[494] | 480 | sleep(sleeptime);
|
---|
[579] | 481 | #endif
|
---|
[494] | 482 | sleeptime += 2;
|
---|
| 483 | }
|
---|
| 484 | else
|
---|
| 485 | {
|
---|
| 486 | sleeptime = 1;
|
---|
| 487 | }
|
---|
[579] | 488 | #ifndef WINDOWSVERSION
|
---|
[494] | 489 | alarm(ALARMTIME);
|
---|
[579] | 490 | #endif
|
---|
[494] | 491 | if(args.proxyhost)
|
---|
| 492 | {
|
---|
| 493 | int p;
|
---|
| 494 | if((i = strtol(args.port, &b, 10)) && (!b || !*b))
|
---|
| 495 | p = i;
|
---|
| 496 | else if(!(se = getservbyname(args.port, 0)))
|
---|
| 497 | {
|
---|
| 498 | fprintf(stderr, "Can't resolve port %s.", args.port);
|
---|
| 499 | exit(1);
|
---|
| 500 | }
|
---|
| 501 | else
|
---|
| 502 | {
|
---|
| 503 | p = ntohs(se->s_port);
|
---|
| 504 | }
|
---|
| 505 | snprintf(proxyport, sizeof(proxyport), "%d", p);
|
---|
| 506 | port = args.proxyport;
|
---|
| 507 | proxyserver = args.server;
|
---|
| 508 | server = args.proxyhost;
|
---|
| 509 | }
|
---|
| 510 | else
|
---|
| 511 | {
|
---|
| 512 | server = args.server;
|
---|
| 513 | port = args.port;
|
---|
| 514 | }
|
---|
| 515 | memset(&their_addr, 0, sizeof(struct sockaddr_in));
|
---|
| 516 | if((i = strtol(port, &b, 10)) && (!b || !*b))
|
---|
| 517 | their_addr.sin_port = htons(i);
|
---|
| 518 | else if(!(se = getservbyname(port, 0)))
|
---|
[483] | 519 | {
|
---|
[494] | 520 | fprintf(stderr, "Can't resolve port %s.", port);
|
---|
[483] | 521 | exit(1);
|
---|
| 522 | }
|
---|
[494] | 523 | else
|
---|
[483] | 524 | {
|
---|
[494] | 525 | their_addr.sin_port = se->s_port;
|
---|
| 526 | }
|
---|
| 527 | if(!(he=gethostbyname(server)))
|
---|
| 528 | {
|
---|
| 529 | fprintf(stderr, "Server name lookup failed for '%s'.\n", server);
|
---|
[483] | 530 | exit(1);
|
---|
| 531 | }
|
---|
[494] | 532 | if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
---|
[483] | 533 | {
|
---|
[494] | 534 | perror("socket");
|
---|
| 535 | exit(1);
|
---|
| 536 | }
|
---|
| 537 | their_addr.sin_family = AF_INET;
|
---|
| 538 | their_addr.sin_addr = *((struct in_addr *)he->h_addr);
|
---|
[500] | 539 |
|
---|
| 540 | if(args.data && *args.data != '%' && args.mode == RTSP)
|
---|
[494] | 541 | {
|
---|
| 542 | struct sockaddr_in local;
|
---|
| 543 | int sockudp, localport;
|
---|
| 544 | int cseq = 1;
|
---|
| 545 | socklen_t len;
|
---|
| 546 |
|
---|
| 547 | if((sockudp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
---|
| 548 | {
|
---|
| 549 | perror("socket");
|
---|
| 550 | exit(1);
|
---|
| 551 | }
|
---|
| 552 | /* fill structure with local address information for UDP */
|
---|
| 553 | memset(&local, 0, sizeof(local));
|
---|
| 554 | local.sin_family = AF_INET;
|
---|
| 555 | local.sin_port = htons(0);
|
---|
| 556 | local.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
| 557 | len = sizeof(local);
|
---|
| 558 | /* bind() in order to get a random RTP client_port */
|
---|
| 559 | if((bind(sockudp, (struct sockaddr *)&local, len)) < 0)
|
---|
| 560 | {
|
---|
| 561 | perror("bind");
|
---|
| 562 | exit(1);
|
---|
| 563 | }
|
---|
| 564 | if((getsockname(sockudp, (struct sockaddr*)&local, &len)) != -1)
|
---|
| 565 | {
|
---|
| 566 | localport = ntohs(local.sin_port);
|
---|
| 567 | }
|
---|
[483] | 568 | else
|
---|
| 569 | {
|
---|
[494] | 570 | perror("local access failed");
|
---|
[483] | 571 | exit(1);
|
---|
| 572 | }
|
---|
[494] | 573 | if(connect(sockfd, (struct sockaddr *)&their_addr,
|
---|
| 574 | sizeof(struct sockaddr)) == -1)
|
---|
[483] | 575 | {
|
---|
[494] | 576 | perror("connect");
|
---|
| 577 | exit(1);
|
---|
| 578 | }
|
---|
| 579 | i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
|
---|
| 580 | "SETUP rtsp://%s%s%s/%s RTSP/1.0\r\n"
|
---|
| 581 | "CSeq: %d\r\n"
|
---|
| 582 | "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
| 583 | "Ntrip-Component: Ntripclient\r\n"
|
---|
| 584 | "User-Agent: %s/%s\r\n"
|
---|
| 585 | "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
|
---|
| 586 | "Authorization: Basic ",
|
---|
| 587 | args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
|
---|
| 588 | args.data, cseq++, AGENTSTRING, revisionstr, localport);
|
---|
| 589 | if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
|
---|
| 590 | {
|
---|
| 591 | fprintf(stderr, "Requested data too long\n");
|
---|
| 592 | exit(1);
|
---|
| 593 | }
|
---|
| 594 | i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
|
---|
| 595 | if(i > MAXDATASIZE-4)
|
---|
| 596 | {
|
---|
| 597 | fprintf(stderr, "Username and/or password too long\n");
|
---|
| 598 | exit(1);
|
---|
| 599 | }
|
---|
| 600 | buf[i++] = '\r';
|
---|
| 601 | buf[i++] = '\n';
|
---|
| 602 | buf[i++] = '\r';
|
---|
| 603 | buf[i++] = '\n';
|
---|
| 604 | if(args.nmea)
|
---|
| 605 | {
|
---|
| 606 | int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
|
---|
| 607 | if(j >= 0 && j < MAXDATASIZE-i)
|
---|
| 608 | i += j;
|
---|
| 609 | else
|
---|
[483] | 610 | {
|
---|
[494] | 611 | fprintf(stderr, "NMEA string too long\n");
|
---|
| 612 | exit(1);
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
| 615 | if(send(sockfd, buf, (size_t)i, 0) != i)
|
---|
| 616 | {
|
---|
| 617 | perror("send");
|
---|
| 618 | exit(1);
|
---|
| 619 | }
|
---|
| 620 | if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
|
---|
| 621 | {
|
---|
| 622 | if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
|
---|
| 623 | {
|
---|
| 624 | int serverport = 0, session = 0;
|
---|
| 625 | const char *portcheck = "server_port=";
|
---|
| 626 | const char *sessioncheck = "session: ";
|
---|
| 627 | int l = strlen(portcheck)-1;
|
---|
| 628 | int j=0;
|
---|
| 629 | for(i = 0; j != l && i < numbytes-l; ++i)
|
---|
[483] | 630 | {
|
---|
[494] | 631 | for(j = 0; j < l && tolower(buf[i+j]) == portcheck[j]; ++j)
|
---|
| 632 | ;
|
---|
[483] | 633 | }
|
---|
[494] | 634 | if(i == numbytes-l)
|
---|
| 635 | {
|
---|
| 636 | fprintf(stderr, "No server port number found\n");
|
---|
| 637 | exit(1);
|
---|
| 638 | }
|
---|
| 639 | else
|
---|
| 640 | {
|
---|
| 641 | i+=l;
|
---|
| 642 | while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
|
---|
| 643 | serverport = serverport * 10 + buf[i++]-'0';
|
---|
| 644 | if(buf[i] != '\r' && buf[i] != ';')
|
---|
| 645 | {
|
---|
| 646 | fprintf(stderr, "Could not extract server port\n");
|
---|
| 647 | exit(1);
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 | l = strlen(sessioncheck)-1;
|
---|
| 651 | j=0;
|
---|
| 652 | for(i = 0; j != l && i < numbytes-l; ++i)
|
---|
| 653 | {
|
---|
| 654 | for(j = 0; j < l && tolower(buf[i+j]) == sessioncheck[j]; ++j)
|
---|
| 655 | ;
|
---|
| 656 | }
|
---|
| 657 | if(i == numbytes-l)
|
---|
| 658 | {
|
---|
| 659 | fprintf(stderr, "No session number found\n");
|
---|
| 660 | exit(1);
|
---|
| 661 | }
|
---|
| 662 | else
|
---|
| 663 | {
|
---|
| 664 | i+=l;
|
---|
| 665 | while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
|
---|
| 666 | session = session * 10 + buf[i++]-'0';
|
---|
| 667 | if(buf[i] != '\r')
|
---|
| 668 | {
|
---|
| 669 | fprintf(stderr, "Could not extract session number\n");
|
---|
| 670 | exit(1);
|
---|
| 671 | }
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | i = snprintf(buf, MAXDATASIZE,
|
---|
| 675 | "PLAY rtsp://%s%s%s/%s RTSP/1.0\r\n"
|
---|
| 676 | "CSeq: %d\r\n"
|
---|
| 677 | "Session: %d\r\n"
|
---|
| 678 | "\r\n",
|
---|
| 679 | args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
|
---|
| 680 | args.data, cseq++, session);
|
---|
| 681 |
|
---|
| 682 | if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
|
---|
| 683 | {
|
---|
| 684 | fprintf(stderr, "Requested data too long\n");
|
---|
| 685 | exit(1);
|
---|
| 686 | }
|
---|
| 687 | if(send(sockfd, buf, (size_t)i, 0) != i)
|
---|
| 688 | {
|
---|
| 689 | perror("send");
|
---|
| 690 | exit(1);
|
---|
| 691 | }
|
---|
| 692 | if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
|
---|
| 693 | {
|
---|
| 694 | if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
|
---|
| 695 | {
|
---|
| 696 | struct sockaddr_in addrRTP;
|
---|
| 697 | /* fill structure with caster address information for UDP */
|
---|
| 698 | memset(&addrRTP, 0, sizeof(addrRTP));
|
---|
| 699 | addrRTP.sin_family = AF_INET;
|
---|
| 700 | addrRTP.sin_port = htons(serverport);
|
---|
| 701 | their_addr.sin_addr = *((struct in_addr *)he->h_addr);
|
---|
| 702 | len = sizeof(addrRTP);
|
---|
| 703 | int ts = 0;
|
---|
| 704 | int sn = 0;
|
---|
| 705 | int ssrc = 0;
|
---|
| 706 | int init = 0;
|
---|
| 707 | int u, v, w;
|
---|
| 708 | while(!stop && (i = recvfrom(sockudp, buf, 1526, 0,
|
---|
| 709 | (struct sockaddr*) &addrRTP, &len)) > 0)
|
---|
| 710 | {
|
---|
[579] | 711 | #ifndef WINDOWSVERSION
|
---|
[494] | 712 | alarm(ALARMTIME);
|
---|
[579] | 713 | #endif
|
---|
[494] | 714 | if(i >= 12+1 && (unsigned char)buf[0] == (2 << 6) && buf[1] == 0x60)
|
---|
| 715 | {
|
---|
| 716 | u= ((unsigned char)buf[2]<<8)+(unsigned char)buf[3];
|
---|
| 717 | v = ((unsigned char)buf[4]<<24)+((unsigned char)buf[5]<<16)
|
---|
| 718 | +((unsigned char)buf[6]<<8)+(unsigned char)buf[7];
|
---|
| 719 | w = ((unsigned char)buf[8]<<24)+((unsigned char)buf[9]<<16)
|
---|
| 720 | +((unsigned char)buf[10]<<8)+(unsigned char)buf[11];
|
---|
| 721 |
|
---|
| 722 | if(init)
|
---|
| 723 | {
|
---|
| 724 | if(u < -30000 && sn > 30000) sn -= 0xFFFF;
|
---|
| 725 | if(ssrc != w || ts > v)
|
---|
| 726 | {
|
---|
| 727 | fprintf(stderr, "Illegal UDP data received.\n");
|
---|
| 728 | exit(1);
|
---|
| 729 | }
|
---|
| 730 | if(u > sn) /* don't show out-of-order packets */
|
---|
| 731 | fwrite(buf+12, (size_t)i-12, 1, stdout);
|
---|
| 732 | }
|
---|
| 733 | sn = u; ts = v; ssrc = w; init = 1;
|
---|
| 734 | }
|
---|
| 735 | else
|
---|
| 736 | {
|
---|
| 737 | fprintf(stderr, "Illegal UDP header.\n");
|
---|
| 738 | exit(1);
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 | i = snprintf(buf, MAXDATASIZE,
|
---|
| 743 | "TEARDOWN rtsp://%s%s%s/%s RTSP/1.0\r\n"
|
---|
| 744 | "CSeq: %d\r\n"
|
---|
| 745 | "Session: %d\r\n"
|
---|
| 746 | "\r\n",
|
---|
| 747 | args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
|
---|
| 748 | args.data, cseq++, session);
|
---|
| 749 |
|
---|
| 750 | if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
|
---|
| 751 | {
|
---|
| 752 | fprintf(stderr, "Requested data too long\n");
|
---|
| 753 | exit(1);
|
---|
| 754 | }
|
---|
| 755 | if(send(sockfd, buf, (size_t)i, 0) != i)
|
---|
| 756 | {
|
---|
| 757 | perror("send");
|
---|
| 758 | exit(1);
|
---|
| 759 | }
|
---|
| 760 | }
|
---|
| 761 | else
|
---|
| 762 | {
|
---|
| 763 | fprintf(stderr, "Could not start data stream.\n");
|
---|
| 764 | exit(1);
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 | else
|
---|
| 768 | {
|
---|
| 769 | fprintf(stderr, "Could not setup initial control connection.\n");
|
---|
[483] | 770 | exit(1);
|
---|
| 771 | }
|
---|
| 772 | }
|
---|
| 773 | else
|
---|
| 774 | {
|
---|
[494] | 775 | perror("recv");
|
---|
| 776 | exit(1);
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
| 779 | else
|
---|
| 780 | {
|
---|
| 781 | if(connect(sockfd, (struct sockaddr *)&their_addr,
|
---|
| 782 | sizeof(struct sockaddr)) == -1)
|
---|
| 783 | {
|
---|
| 784 | perror("connect");
|
---|
| 785 | exit(1);
|
---|
| 786 | }
|
---|
| 787 | if(!args.data)
|
---|
| 788 | {
|
---|
| 789 | i = snprintf(buf, MAXDATASIZE,
|
---|
| 790 | "GET %s%s%s%s/ HTTP/1.0\r\n"
|
---|
| 791 | "Host: %s\r\n%s"
|
---|
| 792 | "User-Agent: %s/%s\r\n"
|
---|
| 793 | "\r\n"
|
---|
| 794 | , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
|
---|
| 795 | proxyserver ? ":" : "", proxyserver ? proxyport : "",
|
---|
| 796 | args.server, args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
|
---|
| 797 | AGENTSTRING, revisionstr);
|
---|
| 798 | }
|
---|
| 799 | else
|
---|
| 800 | {
|
---|
| 801 | i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
|
---|
| 802 | "GET %s%s%s%s/%s HTTP/1.0\r\n"
|
---|
| 803 | "Host: %s\r\n%s"
|
---|
| 804 | "User-Agent: %s/%s\r\n"
|
---|
| 805 | "Authorization: Basic "
|
---|
| 806 | , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
|
---|
| 807 | proxyserver ? ":" : "", proxyserver ? proxyport : "",
|
---|
| 808 | args.data, args.server,
|
---|
| 809 | args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
|
---|
| 810 | AGENTSTRING, revisionstr);
|
---|
| 811 | if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
|
---|
[483] | 812 | {
|
---|
[494] | 813 | fprintf(stderr, "Requested data too long\n");
|
---|
| 814 | exit(1);
|
---|
[483] | 815 | }
|
---|
[494] | 816 | i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
|
---|
| 817 | if(i > MAXDATASIZE-4)
|
---|
[483] | 818 | {
|
---|
[494] | 819 | fprintf(stderr, "Username and/or password too long\n");
|
---|
| 820 | exit(1);
|
---|
| 821 | }
|
---|
| 822 | buf[i++] = '\r';
|
---|
| 823 | buf[i++] = '\n';
|
---|
| 824 | buf[i++] = '\r';
|
---|
| 825 | buf[i++] = '\n';
|
---|
| 826 | if(args.nmea)
|
---|
| 827 | {
|
---|
| 828 | int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
|
---|
| 829 | if(j >= 0 && j < MAXDATASIZE-i)
|
---|
| 830 | i += j;
|
---|
| 831 | else
|
---|
[483] | 832 | {
|
---|
[494] | 833 | fprintf(stderr, "NMEA string too long\n");
|
---|
| 834 | exit(1);
|
---|
[483] | 835 | }
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
[494] | 838 | if(send(sockfd, buf, (size_t)i, 0) != i)
|
---|
| 839 | {
|
---|
| 840 | perror("send");
|
---|
| 841 | exit(1);
|
---|
| 842 | }
|
---|
[500] | 843 | if(args.data && *args.data != '%')
|
---|
[494] | 844 | {
|
---|
| 845 | int k = 0;
|
---|
| 846 | int chunkymode = 0;
|
---|
| 847 | int starttime = time(0);
|
---|
| 848 | int lastout = starttime;
|
---|
| 849 | int totalbytes = 0;
|
---|
| 850 | int chunksize = 0;
|
---|
| 851 |
|
---|
[579] | 852 | while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
|
---|
[494] | 853 | {
|
---|
[579] | 854 | #ifndef WINDOWSVERSION
|
---|
[494] | 855 | alarm(ALARMTIME);
|
---|
[579] | 856 | #endif
|
---|
[494] | 857 | if(!k)
|
---|
| 858 | {
|
---|
| 859 | if(numbytes > 17 && (!strncmp(buf, "HTTP/1.1 200 OK\r\n", 17)
|
---|
| 860 | || !strncmp(buf, "HTTP/1.0 200 OK\r\n", 17)))
|
---|
[579] | 861 | {
|
---|
[494] | 862 | const char *datacheck = "Content-Type: gnss/data\r\n";
|
---|
| 863 | const char *chunkycheck = "Transfer-Encoding: chunked\r\n";
|
---|
| 864 | int l = strlen(datacheck)-1;
|
---|
| 865 | int j=0;
|
---|
| 866 | for(i = 0; j != l && i < numbytes-l; ++i)
|
---|
| 867 | {
|
---|
| 868 | for(j = 0; j < l && buf[i+j] == datacheck[j]; ++j)
|
---|
| 869 | ;
|
---|
| 870 | }
|
---|
| 871 | if(i == numbytes-l)
|
---|
| 872 | {
|
---|
| 873 | fprintf(stderr, "No 'Content-Type: gnss/data' found\n");
|
---|
| 874 | exit(1);
|
---|
| 875 | }
|
---|
| 876 | l = strlen(chunkycheck)-1;
|
---|
| 877 | j=0;
|
---|
| 878 | for(i = 0; j != l && i < numbytes-l; ++i)
|
---|
| 879 | {
|
---|
| 880 | for(j = 0; j < l && buf[i+j] == chunkycheck[j]; ++j)
|
---|
| 881 | ;
|
---|
| 882 | }
|
---|
| 883 | if(i < numbytes-l)
|
---|
| 884 | chunkymode = 1;
|
---|
| 885 | }
|
---|
| 886 | else if(numbytes < 12 || strncmp("ICY 200 OK\r\n", buf, 12))
|
---|
| 887 | {
|
---|
| 888 | fprintf(stderr, "Could not get the requested data: ");
|
---|
| 889 | for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
|
---|
| 890 | {
|
---|
| 891 | fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
|
---|
| 892 | }
|
---|
| 893 | fprintf(stderr, "\n");
|
---|
| 894 | exit(1);
|
---|
| 895 | }
|
---|
| 896 | else if(args.mode != NTRIP1)
|
---|
| 897 | {
|
---|
| 898 | fprintf(stderr, "NTRIP version 2 HTTP connection failed%s.\n",
|
---|
| 899 | args.mode == AUTO ? ", falling back to NTRIP1" : "");
|
---|
| 900 | if(args.mode == HTTP)
|
---|
| 901 | exit(1);
|
---|
| 902 | }
|
---|
| 903 | ++k;
|
---|
| 904 | }
|
---|
| 905 | else
|
---|
| 906 | {
|
---|
| 907 | sleeptime = 0;
|
---|
| 908 | if(chunkymode)
|
---|
| 909 | {
|
---|
| 910 | int stop = 0;
|
---|
| 911 | int pos = 0;
|
---|
| 912 | while(!stop && pos < numbytes)
|
---|
| 913 | {
|
---|
| 914 | switch(chunkymode)
|
---|
| 915 | {
|
---|
| 916 | case 1: /* reading number starts */
|
---|
| 917 | chunksize = 0;
|
---|
| 918 | ++chunkymode; /* no break */
|
---|
| 919 | case 2: /* during reading number */
|
---|
| 920 | i = buf[pos++];
|
---|
| 921 | if(i >= '0' && i <= '9') chunksize = chunksize*16+i-'0';
|
---|
| 922 | else if(i >= 'a' && i <= 'f') chunksize = chunksize*16+i-'a'+10;
|
---|
| 923 | else if(i >= 'A' && i <= 'F') chunksize = chunksize*16+i-'A'+10;
|
---|
| 924 | else if(i == '\r') ++chunkymode;
|
---|
| 925 | else stop = 1;
|
---|
| 926 | break;
|
---|
| 927 | case 3: /* scanning for return */
|
---|
| 928 | if(buf[pos++] == '\n') chunkymode = chunksize ? 4 : 1;
|
---|
| 929 | else stop = 1;
|
---|
| 930 | break;
|
---|
| 931 | case 4: /* output data */
|
---|
| 932 | i = numbytes-pos;
|
---|
| 933 | if(i > chunksize) i = chunksize;
|
---|
| 934 | fwrite(buf+pos, (size_t)i, 1, stdout);
|
---|
| 935 | totalbytes += i;
|
---|
| 936 | chunksize -= i;
|
---|
| 937 | pos += i;
|
---|
| 938 | if(!chunksize)
|
---|
| 939 | chunkymode = 1;
|
---|
| 940 | break;
|
---|
| 941 | }
|
---|
| 942 | }
|
---|
| 943 | if(stop)
|
---|
| 944 | {
|
---|
| 945 | fprintf(stderr, "Error in chunky transfer encoding\n");
|
---|
| 946 | break;
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 | else
|
---|
| 950 | {
|
---|
| 951 | totalbytes += numbytes;
|
---|
| 952 | fwrite(buf, (size_t)numbytes, 1, stdout);
|
---|
| 953 | }
|
---|
| 954 | fflush(stdout);
|
---|
| 955 | if(totalbytes < 0) /* overflow */
|
---|
| 956 | {
|
---|
| 957 | totalbytes = 0;
|
---|
| 958 | starttime = time(0);
|
---|
| 959 | lastout = starttime;
|
---|
| 960 | }
|
---|
| 961 | if(args.bitrate)
|
---|
| 962 | {
|
---|
| 963 | int t = time(0);
|
---|
| 964 | if(t > lastout + 60)
|
---|
| 965 | {
|
---|
| 966 | lastout = t;
|
---|
| 967 | fprintf(stderr, "Bitrate is %dbyte/s (%d seconds accumulated).\n",
|
---|
| 968 | totalbytes/(t-starttime), t-starttime);
|
---|
| 969 | }
|
---|
| 970 | }
|
---|
| 971 | }
|
---|
| 972 | }
|
---|
| 973 | }
|
---|
| 974 | else
|
---|
| 975 | {
|
---|
| 976 | sleeptime = 0;
|
---|
| 977 | while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
|
---|
| 978 | {
|
---|
[579] | 979 | #ifndef WINDOWSVERSION
|
---|
[494] | 980 | alarm(ALARMTIME);
|
---|
[579] | 981 | #endif
|
---|
[494] | 982 | fwrite(buf, (size_t)numbytes, 1, stdout);
|
---|
| 983 | }
|
---|
| 984 | }
|
---|
[579] | 985 | closesocket(sockfd);
|
---|
[483] | 986 | }
|
---|
[500] | 987 | } while(args.data && *args.data != '%' && !stop);
|
---|
[483] | 988 | }
|
---|
| 989 | return 0;
|
---|
| 990 | }
|
---|