source: ntrip/trunk/ntripclient/ntripclient.c@ 10319

Last change on this file since 10319 was 9404, checked in by stoecker, 4 years ago

drop the deprecated www prefix

File size: 58.8 KB
Line 
1/*
2 NTRIP client for POSIX.
3 $Id: ntripclient.c,v 1.51 2009/09/11 09:49:19 stoecker Exp $
4 Copyright (C) 2003-2008 by Dirk Stöcker <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
30#include "serial.c"
31
32#ifdef WINDOWSVERSION
33 #include <winsock.h>
34 typedef SOCKET sockettype;
35 typedef u_long in_addr_t;
36 typedef size_t socklen_t;
37 void myperror(char *s)
38 {
39 fprintf(stderr, "%s: %d\n", s, WSAGetLastError());
40 }
41#else
42 typedef int sockettype;
43 #include <signal.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netdb.h>
50
51 #define closesocket(sock) close(sock)
52 #define ALARMTIME (2*60)
53 #define myperror perror
54#endif
55
56#ifndef COMPILEDATE
57#define COMPILEDATE " built " __DATE__
58#endif
59
60/* The string, which is send as agent in HTTP request */
61#define AGENTSTRING "NTRIP NtripClientPOSIX"
62#define TIME_RESOLUTION 125
63
64#define MAXDATASIZE 1000 /* max number of bytes we can get at once */
65
66/* CVS revision and version */
67static char revisionstr[] = "$Revision: 1.51 $";
68static char datestr[] = "$Date: 2009/09/11 09:49:19 $";
69
70enum MODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, AUTO = 4, UDP = 5, END };
71
72struct Args
73{
74 const char *server;
75 const char *port;
76 const char *user;
77 const char *proxyhost;
78 const char *proxyport;
79 const char *password;
80 const char *nmea;
81 const char *data;
82 int bitrate;
83 int mode;
84
85 int udpport;
86 int initudp;
87 enum SerialBaud baud;
88 enum SerialDatabits databits;
89 enum SerialStopbits stopbits;
90 enum SerialParity parity;
91 enum SerialProtocol protocol;
92 const char *serdevice;
93 const char *serlogfile;
94};
95
96/* option parsing */
97#ifdef NO_LONG_OPTS
98#define LONG_OPT(a)
99#else
100#define LONG_OPT(a) a
101static struct option opts[] = {
102{ "bitrate", no_argument, 0, 'b'},
103{ "data", required_argument, 0, 'd'}, /* compatibility */
104{ "mountpoint", required_argument, 0, 'm'},
105{ "initudp", no_argument, 0, 'I'},
106{ "udpport", required_argument, 0, 'P'},
107{ "server", required_argument, 0, 's'},
108{ "password", required_argument, 0, 'p'},
109{ "port", required_argument, 0, 'r'},
110{ "proxyport", required_argument, 0, 'R'},
111{ "proxyhost", required_argument, 0, 'S'},
112{ "user", required_argument, 0, 'u'},
113{ "nmea", required_argument, 0, 'n'},
114{ "mode", required_argument, 0, 'M'},
115{ "serdevice", required_argument, 0, 'D'},
116{ "baud", required_argument, 0, 'B'},
117{ "stopbits", required_argument, 0, 'T'},
118{ "protocol", required_argument, 0, 'C'},
119{ "parity", required_argument, 0, 'Y'},
120{ "databits", required_argument, 0, 'A'},
121{ "serlogfile", required_argument, 0, 'l'},
122{ "help", no_argument, 0, 'h'},
123{0,0,0,0}};
124#endif
125#define ARGOPT "-d:m:bhp:r:s:u:n:S:R:M:IP:D:B:T:C:Y:A:l:"
126
127int stop = 0;
128#ifndef WINDOWSVERSION
129int sigstop = 0;
130#ifdef __GNUC__
131static __attribute__ ((noreturn)) void sighandler_alarm(
132int sig __attribute__((__unused__)))
133#else /* __GNUC__ */
134static void sighandler_alarm(int sig)
135#endif /* __GNUC__ */
136{
137 if(!sigstop)
138 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
139 else
140 fprintf(stderr, "ERROR: user break\n");
141 exit(1);
142}
143
144#ifdef __GNUC__
145static void sighandler_int(int sig __attribute__((__unused__)))
146#else /* __GNUC__ */
147static void sighandler_alarm(int sig)
148#endif /* __GNUC__ */
149{
150 sigstop = 1;
151 alarm(2);
152 stop = 1;
153}
154#endif /* WINDOWSVERSION */
155
156static const char *encodeurl(const char *req)
157{
158 char *h = "0123456789abcdef";
159 static char buf[128];
160 char *urlenc = buf;
161 char *bufend = buf + sizeof(buf) - 3;
162
163 while(*req && urlenc < bufend)
164 {
165 if(isalnum(*req)
166 || *req == '-' || *req == '_' || *req == '.')
167 *urlenc++ = *req++;
168 else
169 {
170 *urlenc++ = '%';
171 *urlenc++ = h[*req >> 4];
172 *urlenc++ = h[*req & 0x0f];
173 req++;
174 }
175 }
176 *urlenc = 0;
177 return buf;
178}
179
180static const char *geturl(const char *url, struct Args *args)
181{
182 static char buf[1000];
183 static char *Buffer = buf;
184 static char *Bufend = buf+sizeof(buf);
185 char *h = "0123456789abcdef";
186
187 if(strncmp("ntrip:", url, 6))
188 return "URL must start with 'ntrip:'.";
189 url += 6; /* skip ntrip: */
190
191 if(*url != '@' && *url != '/')
192 {
193 /* scan for mountpoint */
194 args->data = Buffer;
195 if(*url != '?')
196 {
197 while(*url && *url != '@' && *url != ';' && *url != '/' && Buffer != Bufend)
198 *(Buffer++) = *(url++);
199 }
200 else
201 {
202 while(*url && *url != '@' && *url != '/' && Buffer != Bufend)
203 {
204 if(isalnum(*url) || *url == '-' || *url == '_' || *url == '.')
205 *Buffer++ = *url++;
206 else
207 {
208 *Buffer++ = '%';
209 *Buffer++ = h[*url >> 4];
210 *Buffer++ = h[*url & 0x0f];
211 url++;
212 }
213 }
214 }
215 if(Buffer == args->data)
216 return "Mountpoint required.";
217 else if(Buffer >= Bufend-1)
218 return "Parsing buffer too short.";
219 *(Buffer++) = 0;
220 }
221
222 if(*url == '/') /* username and password */
223 {
224 ++url;
225 args->user = Buffer;
226 while(*url && *url != '@' && *url != ';' && *url != ':' && Buffer != Bufend)
227 *(Buffer++) = *(url++);
228 if(Buffer == args->user)
229 return "Username cannot be empty.";
230 else if(Buffer >= Bufend-1)
231 return "Parsing buffer too short.";
232 *(Buffer++) = 0;
233
234 if(*url == ':') ++url;
235
236 args->password = Buffer;
237 while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
238 *(Buffer++) = *(url++);
239 if(Buffer == args->password)
240 return "Password cannot be empty.";
241 else if(Buffer >= Bufend-1)
242 return "Parsing buffer too short.";
243 *(Buffer++) = 0;
244 }
245
246 if(*url == '@') /* server */
247 {
248 ++url;
249 if(*url != '@' && *url != ':')
250 {
251 args->server = Buffer;
252 while(*url && *url != '@' && *url != ':' && *url != ';' && Buffer != Bufend)
253 *(Buffer++) = *(url++);
254 if(Buffer == args->server)
255 return "Servername cannot be empty.";
256 else if(Buffer >= Bufend-1)
257 return "Parsing buffer too short.";
258 *(Buffer++) = 0;
259 }
260
261 if(*url == ':')
262 {
263 ++url;
264 args->port = Buffer;
265 while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
266 *(Buffer++) = *(url++);
267 if(Buffer == args->port)
268 return "Port cannot be empty.";
269 else if(Buffer >= Bufend-1)
270 return "Parsing buffer too short.";
271 *(Buffer++) = 0;
272 }
273
274 if(*url == '@') /* proxy */
275 {
276 ++url;
277 args->proxyhost = Buffer;
278 while(*url && *url != ':' && *url != ';' && Buffer != Bufend)
279 *(Buffer++) = *(url++);
280 if(Buffer == args->proxyhost)
281 return "Proxy servername cannot be empty.";
282 else if(Buffer >= Bufend-1)
283 return "Parsing buffer too short.";
284 *(Buffer++) = 0;
285
286 if(*url == ':')
287 {
288 ++url;
289 args->proxyport = Buffer;
290 while(*url && *url != ';' && Buffer != Bufend)
291 *(Buffer++) = *(url++);
292 if(Buffer == args->proxyport)
293 return "Proxy port cannot be empty.";
294 else if(Buffer >= Bufend-1)
295 return "Parsing buffer too short.";
296 *(Buffer++) = 0;
297 }
298 }
299 }
300 if(*url == ';') /* NMEA */
301 {
302 args->nmea = ++url;
303 while(*url)
304 ++url;
305 }
306
307 return *url ? "Garbage at end of server string." : 0;
308}
309
310static int getargs(int argc, char **argv, struct Args *args)
311{
312 int res = 1;
313 int getoptr;
314 char *a;
315 int i = 0, help = 0;
316
317 args->server = "euref-ip.net";
318 args->port = "2101";
319 args->user = "";
320 args->password = "";
321 args->nmea = 0;
322 args->data = 0;
323 args->bitrate = 0;
324 args->proxyhost = 0;
325 args->proxyport = "2101";
326 args->mode = AUTO;
327 args->initudp = 0;
328 args->udpport = 0;
329 args->protocol = SPAPROTOCOL_NONE;
330 args->parity = SPAPARITY_NONE;
331 args->stopbits = SPASTOPBITS_1;
332 args->databits = SPADATABITS_8;
333 args->baud = SPABAUD_9600;
334 args->serdevice = 0;
335 args->serlogfile = 0;
336 help = 0;
337
338 do
339 {
340#ifdef NO_LONG_OPTS
341 switch((getoptr = getopt(argc, argv, ARGOPT)))
342#else
343 switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
344#endif
345 {
346 case 's': args->server = optarg; break;
347 case 'u': args->user = optarg; break;
348 case 'p': args->password = optarg; break;
349 case 'd': /* legacy option, may get removed in future */
350 fprintf(stderr, "Option -d or --data is deprecated. Use -m instead.\n");
351 case 'm':
352 if(optarg && *optarg == '?')
353 args->data = encodeurl(optarg);
354 else
355 args->data = optarg;
356 break;
357 case 'B':
358 {
359 int i = strtol(optarg, 0, 10);
360
361 switch(i)
362 {
363 case 50: args->baud = SPABAUD_50; break;
364 case 110: args->baud = SPABAUD_110; break;
365 case 300: args->baud = SPABAUD_300; break;
366 case 600: args->baud = SPABAUD_600; break;
367 case 1200: args->baud = SPABAUD_1200; break;
368 case 2400: args->baud = SPABAUD_2400; break;
369 case 4800: args->baud = SPABAUD_4800; break;
370 case 9600: args->baud = SPABAUD_9600; break;
371 case 19200: args->baud = SPABAUD_19200; break;
372 case 38400: args->baud = SPABAUD_38400; break;
373 case 57600: args->baud = SPABAUD_57600; break;
374 case 115200: args->baud = SPABAUD_115200; break;
375 default:
376 fprintf(stderr, "Baudrate '%s' unknown\n", optarg);
377 res = 0;
378 break;
379 }
380 }
381 break;
382 case 'T':
383 if(!strcmp(optarg, "1")) args->stopbits = SPASTOPBITS_1;
384 else if(!strcmp(optarg, "2")) args->stopbits = SPASTOPBITS_2;
385 else
386 {
387 fprintf(stderr, "Stopbits '%s' unknown\n", optarg);
388 res = 0;
389 }
390 break;
391 case 'A':
392 if(!strcmp(optarg, "5")) args->databits = SPADATABITS_5;
393 else if(!strcmp(optarg, "6")) args->databits = SPADATABITS_6;
394 else if(!strcmp(optarg, "7")) args->databits = SPADATABITS_7;
395 else if(!strcmp(optarg, "8")) args->databits = SPADATABITS_8;
396 else
397 {
398 fprintf(stderr, "Databits '%s' unknown\n", optarg);
399 res = 0;
400 }
401 break;
402 case 'C':
403 {
404 int i = 0;
405 args->protocol = SerialGetProtocol(optarg, &i);
406 if(!i)
407 {
408 fprintf(stderr, "Protocol '%s' unknown\n", optarg);
409 res = 0;
410 }
411 }
412 break;
413 case 'Y':
414 {
415 int i = 0;
416 args->parity = SerialGetParity(optarg, &i);
417 if(!i)
418 {
419 fprintf(stderr, "Parity '%s' unknown\n", optarg);
420 res = 0;
421 }
422 }
423 break;
424 case 'D': args->serdevice = optarg; break;
425 case 'l': args->serlogfile = optarg; break;
426 case 'I': args->initudp = 1; break;
427 case 'P': args->udpport = strtol(optarg, 0, 10); break;
428 case 'n': args->nmea = optarg; break;
429 case 'b': args->bitrate = 1; break;
430 case 'h': help=1; break;
431 case 'r': args->port = optarg; break;
432 case 'S': args->proxyhost = optarg; break;
433 case 'R': args->proxyport = optarg; break;
434 case 'M':
435 args->mode = 0;
436 if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
437 args->mode = NTRIP1;
438 else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
439 args->mode = HTTP;
440 else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
441 args->mode = RTSP;
442 else if(!strcmp(optarg,"u") || !strcmp(optarg,"udp"))
443 args->mode = UDP;
444 else if(!strcmp(optarg,"a") || !strcmp(optarg,"auto"))
445 args->mode = AUTO;
446 else args->mode = atoi(optarg);
447 if((args->mode == 0) || (args->mode >= END))
448 {
449 fprintf(stderr, "Mode %s unknown\n", optarg);
450 res = 0;
451 }
452 break;
453 case 1:
454 {
455 const char *err;
456 if((err = geturl(optarg, args)))
457 {
458 fprintf(stderr, "%s\n\n", err);
459 res = 0;
460 }
461 }
462 break;
463 case -1: break;
464 }
465 } while(getoptr != -1 && res);
466
467 for(a = revisionstr+11; *a && *a != ' '; ++a)
468 revisionstr[i++] = *a;
469 revisionstr[i] = 0;
470 datestr[0] = datestr[7];
471 datestr[1] = datestr[8];
472 datestr[2] = datestr[9];
473 datestr[3] = datestr[10];
474 datestr[5] = datestr[12];
475 datestr[6] = datestr[13];
476 datestr[8] = datestr[15];
477 datestr[9] = datestr[16];
478 datestr[4] = datestr[7] = '-';
479 datestr[10] = 0;
480
481 if(!res || help)
482 {
483 fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s -s server -u user ...\n"
484 " -m " LONG_OPT("--mountpoint ") "the requested data set or sourcetable filtering criteria\n"
485 " -s " LONG_OPT("--server ") "the server name or address\n"
486 " -p " LONG_OPT("--password ") "the login password\n"
487 " -r " LONG_OPT("--port ") "the server port number (default 2101)\n"
488 " -u " LONG_OPT("--user ") "the user name\n"
489 " -M " LONG_OPT("--mode ") "mode for data request\n"
490 " Valid modes are:\n"
491 " 1, h, http NTRIP Version 2.0 Caster in TCP/IP mode\n"
492 " 2, r, rtsp NTRIP Version 2.0 Caster in RTSP/RTP mode\n"
493 " 3, n, ntrip1 NTRIP Version 1.0 Caster\n"
494 " 4, a, auto automatic detection (default)\n"
495 " 5, u, udp NTRIP Version 2.0 Caster in UDP mode\n"
496 "or using an URL:\n%s ntrip:mountpoint[/user[:password]][@[server][:port][@proxyhost[:proxyport]]][;nmea]\n"
497 "\nExpert options:\n"
498 " -n " LONG_OPT("--nmea ") "NMEA string for sending to server\n"
499 " -b " LONG_OPT("--bitrate ") "output bitrate\n"
500 " -I " LONG_OPT("--initudp ") "send initial UDP packet for firewall handling\n"
501 " -P " LONG_OPT("--udpport ") "set the local UDP port\n"
502 " -S " LONG_OPT("--proxyhost ") "proxy name or address\n"
503 " -R " LONG_OPT("--proxyport ") "proxy port, optional (default 2101)\n"
504 "\nSerial input/output:\n"
505 " -D " LONG_OPT("--serdevice ") "serial device for output\n"
506 " -B " LONG_OPT("--baud ") "baudrate for serial device\n"
507 " -T " LONG_OPT("--stopbits ") "stopbits for serial device\n"
508 " -C " LONG_OPT("--protocol ") "protocol for serial device\n"
509 " -Y " LONG_OPT("--parity ") "parity for serial device\n"
510 " -A " LONG_OPT("--databits ") "databits for serial device\n"
511 " -l " LONG_OPT("--serlogfile ") "logfile for serial data\n"
512 , revisionstr, datestr, argv[0], argv[0]);
513 exit(1);
514 }
515 return res;
516}
517
518static const char encodingTable [64] = {
519 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
520 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
521 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
522 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
523};
524
525/* does not buffer overrun, but breaks directly after an error */
526/* returns the number of required bytes */
527static int encode(char *buf, int size, const char *user, const char *pwd)
528{
529 unsigned char inbuf[3];
530 char *out = buf;
531 int i, sep = 0, fill = 0, bytes = 0;
532
533 while(*user || *pwd)
534 {
535 i = 0;
536 while(i < 3 && *user) inbuf[i++] = *(user++);
537 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
538 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
539 while(i < 3) {inbuf[i++] = 0; ++fill; }
540 if(out-buf < size-1)
541 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
542 if(out-buf < size-1)
543 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
544 | ((inbuf [1] & 0xF0) >> 4)];
545 if(out-buf < size-1)
546 {
547 if(fill == 2)
548 *(out++) = '=';
549 else
550 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
551 | ((inbuf [2] & 0xC0) >> 6)];
552 }
553 if(out-buf < size-1)
554 {
555 if(fill >= 1)
556 *(out++) = '=';
557 else
558 *(out++) = encodingTable[inbuf [2] & 0x3F];
559 }
560 bytes += 4;
561 }
562 if(out-buf < size)
563 *out = 0;
564 return bytes;
565}
566
567int main(int argc, char **argv)
568{
569 struct Args args;
570
571 setbuf(stdout, 0);
572 setbuf(stdin, 0);
573 setbuf(stderr, 0);
574#ifndef WINDOWSVERSION
575 signal(SIGALRM,sighandler_alarm);
576 signal(SIGINT,sighandler_int);
577 alarm(ALARMTIME);
578#else
579 WSADATA wsaData;
580 if(WSAStartup(MAKEWORD(1,1),&wsaData))
581 {
582 fprintf(stderr, "Could not init network access.\n");
583 return 20;
584 }
585#endif
586
587 if(getargs(argc, argv, &args))
588 {
589 struct serial sx;
590 FILE *ser = 0;
591 char nmeabuffer[200] = "$GPGGA,"; /* our start string */
592 size_t nmeabufpos = 0;
593 size_t nmeastarpos = 0;
594 int sleeptime = 0;
595 if(args.serdevice)
596 {
597 const char *e = SerialInit(&sx, args.serdevice, args.baud,
598 args.stopbits, args.protocol, args.parity, args.databits, 1);
599 if(e)
600 {
601 fprintf(stderr, "%s\n", e);
602 return 20;
603 }
604 if(args.serlogfile)
605 {
606 if(!(ser = fopen(args.serlogfile, "a+")))
607 {
608 SerialFree(&sx);
609 fprintf(stderr, "Could not open serial logfile.\n");
610 return 20;
611 }
612 }
613 }
614 do
615 {
616 int error = 0;
617 sockettype sockfd = 0;
618 int numbytes;
619 char buf[MAXDATASIZE];
620 struct sockaddr_in their_addr; /* connector's address information */
621 struct hostent *he;
622 struct servent *se;
623 const char *server, *port, *proxyserver = 0;
624 char proxyport[6];
625 char *b;
626 long i;
627 if(sleeptime)
628 {
629#ifdef WINDOWSVERSION
630 Sleep(sleeptime*1000);
631#else
632 sleep(sleeptime);
633#endif
634 sleeptime += 2;
635 }
636 else
637 {
638 sleeptime = 1;
639 }
640#ifndef WINDOWSVERSION
641 alarm(ALARMTIME);
642#endif
643 if(args.proxyhost)
644 {
645 int p;
646 if((i = strtol(args.port, &b, 10)) && (!b || !*b))
647 p = i;
648 else if(!(se = getservbyname(args.port, 0)))
649 {
650 fprintf(stderr, "Can't resolve port %s.", args.port);
651 stop = 1;
652 }
653 else
654 {
655 p = ntohs(se->s_port);
656 }
657 if(!stop && !error)
658 {
659 snprintf(proxyport, sizeof(proxyport), "%d", p);
660 port = args.proxyport;
661 proxyserver = args.server;
662 server = args.proxyhost;
663 }
664 }
665 else
666 {
667 server = args.server;
668 port = args.port;
669 }
670 if(!stop && !error)
671 {
672 memset(&their_addr, 0, sizeof(struct sockaddr_in));
673 if((i = strtol(port, &b, 10)) && (!b || !*b))
674 their_addr.sin_port = htons(i);
675 else if(!(se = getservbyname(port, 0)))
676 {
677 fprintf(stderr, "Can't resolve port %s.", port);
678 stop = 1;
679 }
680 else
681 {
682 their_addr.sin_port = se->s_port;
683 }
684 if(!stop && !error)
685 {
686 if(!(he=gethostbyname(server)))
687 {
688 fprintf(stderr, "Server name lookup failed for '%s'.\n", server);
689 error = 1;
690 }
691 else if((sockfd = socket(AF_INET, (args.mode == UDP ? SOCK_DGRAM :
692 SOCK_STREAM), 0)) == -1)
693 {
694 myperror("socket");
695 error = 1;
696 }
697 else
698 {
699 their_addr.sin_family = AF_INET;
700 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
701 }
702 }
703 }
704 if(!stop && !error)
705 {
706 if(args.mode == UDP)
707 {
708 unsigned int session;
709 int tim, seq, init;
710 char rtpbuf[1526];
711 int i=12, j;
712
713 init = time(0);
714 srand(init);
715 session = rand();
716 tim = rand();
717 seq = rand();
718
719 rtpbuf[0] = (2<<6);
720 /* padding, extension, csrc are empty */
721 rtpbuf[1] = 97;
722 /* marker is empty */
723 rtpbuf[2] = (seq>>8)&0xFF;
724 rtpbuf[3] = (seq)&0xFF;
725 rtpbuf[4] = (tim>>24)&0xFF;
726 rtpbuf[5] = (tim>>16)&0xFF;
727 rtpbuf[6] = (tim>>8)&0xFF;
728 rtpbuf[7] = (tim)&0xFF;
729 /* sequence and timestamp are empty */
730 rtpbuf[8] = (session>>24)&0xFF;
731 rtpbuf[9] = (session>>16)&0xFF;
732 rtpbuf[10] = (session>>8)&0xFF;
733 rtpbuf[11] = (session)&0xFF;
734 ++seq;
735
736 j = snprintf(rtpbuf+i, sizeof(rtpbuf)-i-40, /* leave some space for login */
737 "GET /%s HTTP/1.1\r\n"
738 "Host: %s\r\n"
739 "Ntrip-Version: Ntrip/2.0\r\n"
740 "User-Agent: %s/%s\r\n"
741 "%s%s%s"
742 "Connection: close%s",
743 args.data ? args.data : "", args.server, AGENTSTRING, revisionstr,
744 args.nmea ? "Ntrip-GGA: " : "", args.nmea ? args.nmea : "",
745 args.nmea ? "\r\n" : "",
746 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
747 i += j;
748 if(i > (int)sizeof(rtpbuf)-40 || j < 0) /* second check for old glibc */
749 {
750 fprintf(stderr, "Requested data too long\n");
751 stop = 1;
752 }
753 else
754 {
755 i += encode(rtpbuf+i, sizeof(rtpbuf)-i-4, args.user, args.password);
756 if(i > (int)sizeof(rtpbuf)-4)
757 {
758 fprintf(stderr, "Username and/or password too long\n");
759 stop = 1;
760 }
761 else
762 {
763 struct sockaddr_in local;
764 socklen_t len;
765
766 rtpbuf[i++] = '\r';
767 rtpbuf[i++] = '\n';
768 rtpbuf[i++] = '\r';
769 rtpbuf[i++] = '\n';
770
771
772 /* fill structure with local address information for UDP */
773 memset(&local, 0, sizeof(local));
774 local.sin_family = AF_INET;
775 local.sin_port = htons(args.udpport);
776 local.sin_addr.s_addr = htonl(INADDR_ANY);
777 len = sizeof(local);
778
779 /* bind() in order to get a random RTP client_port */
780 if((bind(sockfd, (struct sockaddr *)&local, len)) < 0)
781 {
782 myperror("bind");
783 error = 1;
784 }
785 else if(connect(sockfd, (struct sockaddr *)&their_addr,
786 sizeof(struct sockaddr)) == -1)
787 {
788 myperror("connect");
789 error = 1;
790 }
791 else if(send(sockfd, rtpbuf, i, 0) != i)
792 {
793 myperror("Could not send UDP packet");
794 stop = 1;
795 }
796 else
797 {
798 if((numbytes=recv(sockfd, rtpbuf, sizeof(rtpbuf)-1, 0)) > 0)
799 {
800 int sn = 0x10000, ts=0;
801 /* we don't expect message longer than 1513, so we cut the last
802 byte for security reasons to prevent buffer overrun */
803 rtpbuf[numbytes] = 0;
804 if(numbytes > 17+12 &&
805 (!strncmp(rtpbuf+12, "HTTP/1.1 200 OK\r\n", 17) ||
806 !strncmp(rtpbuf+12, "HTTP/1.0 200 OK\r\n", 17)))
807 {
808 const char *sessioncheck = "session: ";
809 const char *datacheck = "Content-Type: gnss/data\r\n";
810 const char *sourcetablecheck = "Content-Type: gnss/sourcetable\r\n";
811 const char *contentlengthcheck = "Content-Length: ";
812 const char *httpresponseend = "\r\n\r\n";
813 int contentlength = 0, httpresponselength = 0;
814 /* datacheck */
815 int l = strlen(datacheck)-1;
816 int j=0;
817 for(i = 12; j != l && i < numbytes-l; ++i)
818 {
819 for(j = 0; j < l && rtpbuf[i+j] == datacheck[j]; ++j)
820 ;
821 }
822 if(i != numbytes-l)
823 {
824 /* check for Session */
825 l = strlen(sessioncheck)-1;
826 j=0;
827 for(i = 12; j != l && i < numbytes-l; ++i)
828 {
829 for(j = 0; j < l && tolower(rtpbuf[i+j]) == sessioncheck[j]; ++j)
830 ;
831 }
832 if(i != numbytes-l) /* found a session number */
833 {
834 i+=l;
835 session = 0;
836 while(i < numbytes && rtpbuf[i] >= '0' && rtpbuf[i] <= '9')
837 session = session * 10 + rtpbuf[i++]-'0';
838 if(rtpbuf[i] != '\r')
839 {
840 fprintf(stderr, "Could not extract session number\n");
841 stop = 1;
842 }
843 }
844 }
845 else
846 {
847 /* sourcetablecheck */
848 l = strlen(sourcetablecheck)-1;
849 j=0;
850 for(i = 12; j != l && i < numbytes-l; ++i)
851 {
852 for(j = 0; j < l && rtpbuf[i+j] == sourcetablecheck[j]; ++j)
853 ;
854 }
855 if(i == numbytes-l)
856 {
857 fprintf(stderr, "No 'Content-Type: gnss/data' or"
858 " 'Content-Type: gnss/sourcetable' found\n");
859 error = 1;
860 }
861 else
862 {
863 /* check for http response end */
864 l = strlen(httpresponseend)-1;
865 j=0;
866 for(i = 12; j != l && i < numbytes-l; ++i)
867 {
868 for(j = 0; j < l && rtpbuf[i+j] == httpresponseend[j]; ++j)
869 ;
870 }
871 if(i != numbytes-l) /* found http response end */
872 {
873 httpresponselength = i+3-12;
874 }
875 /* check for content length */
876 l = strlen(contentlengthcheck)-1;
877 j=0;
878 for(i = 12; j != l && i < numbytes-l; ++i)
879 {
880 for(j = 0; j < l && rtpbuf[i+j] == contentlengthcheck[j]; ++j)
881 ;
882 }
883 if(i != numbytes-l) /* found content length */
884 {
885 i+=l;
886 contentlength = 0;
887 while(i < numbytes && rtpbuf[i] >= '0' && rtpbuf[i] <= '9')
888 contentlength = contentlength * 10 + rtpbuf[i++]-'0';
889 if(rtpbuf[i] == '\r')
890 {
891 contentlength += httpresponselength;
892 do
893 {
894 fwrite(rtpbuf+12, (size_t)numbytes-12, 1, stdout);
895 if((contentlength -= (numbytes-12)) == 0)
896 {
897 stop = 1;
898 }
899 else
900 {
901 numbytes = recv(sockfd, rtpbuf, sizeof(rtpbuf), 0);
902 }
903 }while((numbytes >12) && (!stop));
904 }
905 else
906 {
907 fprintf(stderr, "Could not extract content length\n");
908 stop = 1;
909 }
910 }
911 }
912 }
913 }
914 else
915 {
916 int k;
917 fprintf(stderr, "Could not get the requested data: ");
918 for(k = 12; k < numbytes && rtpbuf[k] != '\n' && rtpbuf[k] != '\r'; ++k)
919 {
920 fprintf(stderr, "%c", isprint(rtpbuf[k]) ? rtpbuf[k] : '.');
921 }
922 fprintf(stderr, "\n");
923 error = 1;
924 }
925 while(!stop && !error)
926 {
927 struct timeval tv = {1,0};
928 fd_set fdr;
929 fd_set fde;
930
931 FD_ZERO(&fdr);
932 FD_ZERO(&fde);
933 FD_SET(sockfd, &fdr);
934 FD_SET(sockfd, &fde);
935 if(select(sockfd+1,&fdr,0,&fde,&tv) < 0)
936 {
937 fprintf(stderr, "Select problem.\n");
938 error = 1;
939 continue;
940 }
941 i = recv(sockfd, rtpbuf, sizeof(rtpbuf), 0);
942#ifndef WINDOWSVERSION
943 alarm(ALARMTIME);
944#endif
945 if(i >= 12 && (unsigned char)rtpbuf[0] == (2 << 6)
946 && rtpbuf[1] >= 96 && rtpbuf[1] <= 98)
947 {
948 time_t ct;
949 int u,v;
950 unsigned int w;
951 u = ((unsigned char)rtpbuf[2]<<8)+(unsigned char)rtpbuf[3];
952 v = ((unsigned char)rtpbuf[4]<<24)+((unsigned char)rtpbuf[5]<<16)
953 +((unsigned char)rtpbuf[6]<<8)+(unsigned char)rtpbuf[7];
954 w = ((unsigned char)rtpbuf[8]<<24)+((unsigned char)rtpbuf[9]<<16)
955 +((unsigned char)rtpbuf[10]<<8)+(unsigned char)rtpbuf[11];
956
957 if(sn == 0x10000) {sn = u-1;ts=v-1;}
958 else if(u < -30000 && sn > 30000) sn -= 0xFFFF;
959 if(session != w || ts > v)
960 {
961 fprintf(stderr, "Illegal UDP data received.\n");
962 continue;
963 }
964 else if(u > sn) /* don't show out-of-order packets */
965 {
966 if(rtpbuf[1] == 98)
967 {
968 fprintf(stderr, "Connection closed.\n");
969 error = 1;
970 continue;
971 }
972 else if((rtpbuf[1] == 96) && (i>12))
973 {
974 fwrite(rtpbuf+12, (size_t)i-12, 1, stdout);
975 }
976 }
977 sn = u; ts = v;
978
979 /* Keep Alive */
980 ct = time(0);
981 if(ct-init > 15)
982 {
983 tim += (ct-init)*1000000/TIME_RESOLUTION;
984 rtpbuf[0] = (2<<6);
985 /* padding, extension, csrc are empty */
986 rtpbuf[1] = 96;
987 /* marker is empty */
988 rtpbuf[2] = (seq>>8)&0xFF;
989 rtpbuf[3] = (seq)&0xFF;
990 rtpbuf[4] = (tim>>24)&0xFF;
991 rtpbuf[5] = (tim>>16)&0xFF;
992 rtpbuf[6] = (tim>>8)&0xFF;
993 rtpbuf[7] = (tim)&0xFF;
994 /* sequence and timestamp are empty */
995 rtpbuf[8] = (session>>24)&0xFF;
996 rtpbuf[9] = (session>>16)&0xFF;
997 rtpbuf[10] = (session>>8)&0xFF;
998 rtpbuf[11] = (session)&0xFF;
999 ++seq;
1000 init = ct;
1001
1002 if(send(sockfd, rtpbuf, 12, 0) != 12)
1003 {
1004 myperror("send");
1005 error = 1;
1006 }
1007 }
1008 }
1009 else if(i >= 0)
1010 {
1011 fprintf(stderr, "Illegal UDP header.\n");
1012 continue;
1013 }
1014 }
1015 }
1016 /* send connection close always to allow nice session closing */
1017 tim += (time(0)-init)*1000000/TIME_RESOLUTION;
1018 rtpbuf[0] = (2<<6);
1019 /* padding, extension, csrc are empty */
1020 rtpbuf[1] = 98;
1021 /* marker is empty */
1022 rtpbuf[2] = (seq>>8)&0xFF;
1023 rtpbuf[3] = (seq)&0xFF;
1024 rtpbuf[4] = (tim>>24)&0xFF;
1025 rtpbuf[5] = (tim>>16)&0xFF;
1026 rtpbuf[6] = (tim>>8)&0xFF;
1027 rtpbuf[7] = (tim)&0xFF;
1028 /* sequence and timestamp are empty */
1029 rtpbuf[8] = (session>>24)&0xFF;
1030 rtpbuf[9] = (session>>16)&0xFF;
1031 rtpbuf[10] = (session>>8)&0xFF;
1032 rtpbuf[11] = (session)&0xFF;
1033
1034 send(sockfd, rtpbuf, 12, 0); /* cleanup */
1035 }
1036 }
1037 }
1038 }
1039 else if(args.data && *args.data != '%' && args.mode == RTSP)
1040 {
1041 struct sockaddr_in local;
1042 sockettype sockudp = 0;
1043 int localport;
1044 int cseq = 1;
1045 socklen_t len;
1046
1047 if((sockudp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1048 {
1049 myperror("socket");
1050 error = 1;
1051 }
1052 if(!stop && !error)
1053 {
1054 /* fill structure with local address information for UDP */
1055 memset(&local, 0, sizeof(local));
1056 local.sin_family = AF_INET;
1057 local.sin_port = htons(args.udpport);
1058 local.sin_addr.s_addr = htonl(INADDR_ANY);
1059 len = sizeof(local);
1060 /* bind() in order to get a random RTP client_port */
1061 if((bind(sockudp, (struct sockaddr *)&local, len)) < 0)
1062 {
1063 myperror("bind");
1064 error = 1;
1065 }
1066 else if((getsockname(sockudp, (struct sockaddr*)&local, &len)) == -1)
1067 {
1068 myperror("local access failed");
1069 error = 1;
1070 }
1071 else if(connect(sockfd, (struct sockaddr *)&their_addr,
1072 sizeof(struct sockaddr)) == -1)
1073 {
1074 myperror("connect");
1075 error = 1;
1076 }
1077 localport = ntohs(local.sin_port);
1078 }
1079 if(!stop && !error)
1080 {
1081 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
1082 "SETUP rtsp://%s%s%s/%s RTSP/1.0\r\n"
1083 "CSeq: %d\r\n"
1084 "Ntrip-Version: Ntrip/2.0\r\n"
1085 "Ntrip-Component: Ntripclient\r\n"
1086 "User-Agent: %s/%s\r\n"
1087 "%s%s%s"
1088 "Transport: RTP/GNSS;unicast;client_port=%u%s",
1089 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1090 args.data, cseq++, AGENTSTRING, revisionstr,
1091 args.nmea ? "Ntrip-GGA: " : "", args.nmea ? args.nmea : "",
1092 args.nmea ? "\r\n" : "",
1093 localport,
1094 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1095 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1096 {
1097 fprintf(stderr, "Requested data too long\n");
1098 stop = 1;
1099 }
1100 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1101 if(i > MAXDATASIZE-4)
1102 {
1103 fprintf(stderr, "Username and/or password too long\n");
1104 stop = 1;
1105 }
1106 buf[i++] = '\r';
1107 buf[i++] = '\n';
1108 buf[i++] = '\r';
1109 buf[i++] = '\n';
1110 }
1111 if(!stop && !error)
1112 {
1113 if(send(sockfd, buf, (size_t)i, 0) != i)
1114 {
1115 myperror("send");
1116 error = 1;
1117 }
1118 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
1119 {
1120 myperror("recv");
1121 error = 1;
1122 }
1123 else if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1124 {
1125 int serverport = 0, session = 0;
1126 const char *portcheck = "server_port=";
1127 const char *sessioncheck = "session: ";
1128 int l = strlen(portcheck)-1;
1129 int j=0;
1130 for(i = 0; j != l && i < numbytes-l; ++i)
1131 {
1132 for(j = 0; j < l && tolower(buf[i+j]) == portcheck[j]; ++j)
1133 ;
1134 }
1135 if(i == numbytes-l)
1136 {
1137 fprintf(stderr, "No server port number found\n");
1138 stop = 1;
1139 }
1140 else
1141 {
1142 i+=l;
1143 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1144 serverport = serverport * 10 + buf[i++]-'0';
1145 if(buf[i] != '\r' && buf[i] != ';')
1146 {
1147 fprintf(stderr, "Could not extract server port\n");
1148 stop = 1;
1149 }
1150 }
1151 if(!stop && !error)
1152 {
1153 l = strlen(sessioncheck)-1;
1154 j=0;
1155 for(i = 0; j != l && i < numbytes-l; ++i)
1156 {
1157 for(j = 0; j < l && tolower(buf[i+j]) == sessioncheck[j]; ++j)
1158 ;
1159 }
1160 if(i == numbytes-l)
1161 {
1162 fprintf(stderr, "No session number found\n");
1163 stop = 1;
1164 }
1165 else
1166 {
1167 i+=l;
1168 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1169 session = session * 10 + buf[i++]-'0';
1170 if(buf[i] != '\r')
1171 {
1172 fprintf(stderr, "Could not extract session number\n");
1173 stop = 1;
1174 }
1175 }
1176 }
1177 if(!stop && !error && args.initudp)
1178 {
1179 printf("Sending initial UDP packet\n");
1180 struct sockaddr_in casterRTP;
1181 char rtpbuffer[12];
1182 int i;
1183 rtpbuffer[0] = (2<<6);
1184 /* padding, extension, csrc are empty */
1185 rtpbuffer[1] = 96;
1186 /* marker is empty */
1187 rtpbuffer[2] = 0;
1188 rtpbuffer[3] = 0;
1189 rtpbuffer[4] = 0;
1190 rtpbuffer[5] = 0;
1191 rtpbuffer[6] = 0;
1192 rtpbuffer[7] = 0;
1193 /* sequence and timestamp are empty */
1194 rtpbuffer[8] = (session>>24)&0xFF;
1195 rtpbuffer[9] = (session>>16)&0xFF;
1196 rtpbuffer[10] = (session>>8)&0xFF;
1197 rtpbuffer[11] = (session)&0xFF;
1198 /* fill structure with caster address information for UDP */
1199 memset(&casterRTP, 0, sizeof(casterRTP));
1200 casterRTP.sin_family = AF_INET;
1201 casterRTP.sin_port = htons(serverport);
1202 casterRTP.sin_addr = *((struct in_addr *)he->h_addr);
1203
1204 if((i = sendto(sockudp, rtpbuffer, 12, 0,
1205 (struct sockaddr *) &casterRTP, sizeof(casterRTP))) != 12)
1206 myperror("WARNING: could not send initial UDP packet");
1207 }
1208 if(!stop && !error)
1209 {
1210 i = snprintf(buf, MAXDATASIZE,
1211 "PLAY rtsp://%s%s%s/%s RTSP/1.0\r\n"
1212 "CSeq: %d\r\n"
1213 "Session: %u\r\n"
1214 "\r\n",
1215 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1216 args.data, cseq++, session);
1217
1218 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1219 {
1220 fprintf(stderr, "Requested data too long\n");
1221 stop=1;
1222 }
1223 else if(send(sockfd, buf, (size_t)i, 0) != i)
1224 {
1225 myperror("send");
1226 error = 1;
1227 }
1228 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
1229 {
1230 if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1231 {
1232 int ts = 0, sn = 0;
1233 time_t init = 0;
1234 struct sockaddr_in addrRTP;
1235#ifdef WINDOWSVERSION
1236 u_long blockmode = 1;
1237 if(ioctlsocket(sockudp, FIONBIO, &blockmode)
1238 || ioctlsocket(sockfd, FIONBIO, &blockmode))
1239#else /* WINDOWSVERSION */
1240 if(fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0
1241 || fcntl(sockudp, F_SETFL, O_NONBLOCK) < 0)
1242#endif /* WINDOWSVERSION */
1243 {
1244 fprintf(stderr, "Could not set nonblocking mode\n");
1245 error = 1;
1246 }
1247
1248 /* fill structure with caster address information for UDP */
1249 memset(&addrRTP, 0, sizeof(addrRTP));
1250 addrRTP.sin_family = AF_INET;
1251 addrRTP.sin_port = htons(serverport);
1252 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
1253 len = sizeof(addrRTP);
1254
1255 while(!stop && !error)
1256 {
1257 char rtpbuffer[1526];
1258 struct timeval tv = {1,0};
1259 fd_set fdr;
1260 fd_set fde;
1261 int r;
1262
1263 FD_ZERO(&fdr);
1264 FD_ZERO(&fde);
1265 FD_SET(sockudp, &fdr);
1266 FD_SET(sockfd, &fdr);
1267 FD_SET(sockudp, &fde);
1268 FD_SET(sockfd, &fde);
1269 if(select((sockudp>sockfd?sockudp:sockfd)+1,
1270 &fdr,0,&fde,&tv) < 0)
1271 {
1272 fprintf(stderr, "Select problem.\n");
1273 error = 1;
1274 continue;
1275 }
1276 i = recvfrom(sockudp, rtpbuffer, sizeof(rtpbuffer), 0,
1277 (struct sockaddr*) &addrRTP, &len);
1278#ifndef WINDOWSVERSION
1279 alarm(ALARMTIME);
1280#endif
1281 if(i >= 12+1 && (unsigned char)rtpbuffer[0] == (2 << 6) && rtpbuffer[1] == 0x60)
1282 {
1283 int u,v,w;
1284 u = ((unsigned char)rtpbuffer[2]<<8)+(unsigned char)rtpbuffer[3];
1285 v = ((unsigned char)rtpbuffer[4]<<24)+((unsigned char)rtpbuffer[5]<<16)
1286 +((unsigned char)rtpbuffer[6]<<8)+(unsigned char)rtpbuffer[7];
1287 w = ((unsigned char)rtpbuffer[8]<<24)+((unsigned char)rtpbuffer[9]<<16)
1288 +((unsigned char)rtpbuffer[10]<<8)+(unsigned char)rtpbuffer[11];
1289
1290 if(init)
1291 {
1292 time_t ct;
1293 if(u < -30000 && sn > 30000) sn -= 0xFFFF;
1294 if(session != w || ts > v)
1295 {
1296 fprintf(stderr, "Illegal UDP data received.\n");
1297 continue;
1298 }
1299 else if(u > sn) /* don't show out-of-order packets */
1300 fwrite(rtpbuffer+12, (size_t)i-12, 1, stdout);
1301 ct = time(0);
1302 if(ct-init > 15)
1303 {
1304 i = snprintf(buf, MAXDATASIZE,
1305 "GET_PARAMETER rtsp://%s%s%s/%s RTSP/1.0\r\n"
1306 "CSeq: %d\r\n"
1307 "Session: %u\r\n"
1308 "\r\n",
1309 args.server, proxyserver ? ":" : "", proxyserver
1310 ? args.port : "", args.data, cseq++, session);
1311 if(i > MAXDATASIZE || i < 0)
1312 {
1313 fprintf(stderr, "Requested data too long\n");
1314 stop = 1;
1315 }
1316 else if(send(sockfd, buf, (size_t)i, 0) != i)
1317 {
1318 myperror("send");
1319 error = 1;
1320 }
1321 init = ct;
1322 }
1323 }
1324 else
1325 {
1326 init = time(0);
1327 }
1328 sn = u; ts = v;
1329 }
1330 else if(i >= 0)
1331 {
1332 fprintf(stderr, "Illegal UDP header.\n");
1333 continue;
1334 }
1335 /* ignore RTSP server replies */
1336 if((r=recv(sockfd, buf, MAXDATASIZE-1, 0)) < 0)
1337 {
1338#ifdef WINDOWSVERSION
1339 if(WSAGetLastError() != WSAEWOULDBLOCK)
1340#else /* WINDOWSVERSION */
1341 if(errno != EAGAIN)
1342#endif /* WINDOWSVERSION */
1343 {
1344 fprintf(stderr, "Control connection closed\n");
1345 error = 1;
1346 }
1347 }
1348 else if(!r)
1349 {
1350 fprintf(stderr, "Control connection read error\n");
1351 error = 1;
1352 }
1353 }
1354 }
1355 i = snprintf(buf, MAXDATASIZE,
1356 "TEARDOWN rtsp://%s%s%s/%s RTSP/1.0\r\n"
1357 "CSeq: %d\r\n"
1358 "Session: %u\r\n"
1359 "\r\n",
1360 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1361 args.data, cseq++, session);
1362
1363 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1364 {
1365 fprintf(stderr, "Requested data too long\n");
1366 stop = 1;
1367 }
1368 else if(send(sockfd, buf, (size_t)i, 0) != i)
1369 {
1370 myperror("send");
1371 error = 1;
1372 }
1373 }
1374 else
1375 {
1376 fprintf(stderr, "Could not start data stream.\n");
1377 error = 1;
1378 }
1379 }
1380 }
1381 else
1382 {
1383 fprintf(stderr, "Could not setup initial control connection.\n");
1384 error = 1;
1385 }
1386 if(sockudp)
1387 closesocket(sockudp);
1388 }
1389 }
1390 else
1391 {
1392 if(connect(sockfd, (struct sockaddr *)&their_addr,
1393 sizeof(struct sockaddr)) == -1)
1394 {
1395 myperror("connect");
1396 error = 1;
1397 }
1398 if(!stop && !error)
1399 {
1400 if(!args.data)
1401 {
1402 i = snprintf(buf, MAXDATASIZE,
1403 "GET %s%s%s%s/ HTTP/1.1\r\n"
1404 "Host: %s\r\n%s"
1405 "User-Agent: %s/%s\r\n"
1406 "Connection: close\r\n"
1407 "\r\n"
1408 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1409 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1410 args.server, args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1411 AGENTSTRING, revisionstr);
1412 }
1413 else
1414 {
1415 const char *nmeahead = (args.nmea && args.mode == HTTP) ? args.nmea : 0;
1416
1417 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
1418 "GET %s%s%s%s/%s HTTP/1.1\r\n"
1419 "Host: %s\r\n%s"
1420 "User-Agent: %s/%s\r\n"
1421 "%s%s%s"
1422 "Connection: close%s"
1423 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1424 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1425 args.data, args.server,
1426 args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1427 AGENTSTRING, revisionstr,
1428 nmeahead ? "Ntrip-GGA: " : "", nmeahead ? nmeahead : "",
1429 nmeahead ? "\r\n" : "",
1430 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1431 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1432 {
1433 fprintf(stderr, "Requested data too long\n");
1434 stop = 1;
1435 }
1436 else
1437 {
1438 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1439 if(i > MAXDATASIZE-4)
1440 {
1441 fprintf(stderr, "Username and/or password too long\n");
1442 stop = 1;
1443 }
1444 else
1445 {
1446 buf[i++] = '\r';
1447 buf[i++] = '\n';
1448 buf[i++] = '\r';
1449 buf[i++] = '\n';
1450 if(args.nmea && !nmeahead)
1451 {
1452 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
1453 if(j >= 0 && j < MAXDATASIZE-i)
1454 i += j;
1455 else
1456 {
1457 fprintf(stderr, "NMEA string too long\n");
1458 stop = 1;
1459 }
1460 }
1461 }
1462 }
1463 }
1464 }
1465 if(!stop && !error)
1466 {
1467 if(send(sockfd, buf, (size_t)i, 0) != i)
1468 {
1469 myperror("send");
1470 error = 1;
1471 }
1472 else if(args.data && *args.data != '%')
1473 {
1474 int k = 0;
1475 int chunkymode = 0;
1476 int starttime = time(0);
1477 int lastout = starttime;
1478 int totalbytes = 0;
1479 int chunksize = 0;
1480
1481 while(!stop && !error &&
1482 (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1483 {
1484#ifndef WINDOWSVERSION
1485 alarm(ALARMTIME);
1486#endif
1487 if(!k)
1488 {
1489 buf[numbytes] = 0; /* latest end mark for strstr */
1490 if( numbytes > 17 &&
1491 !strstr(buf, "ICY 200 OK") && /* case 'proxy & ntrip 1.0 caster' */
1492 (!strncmp(buf, "HTTP/1.1 200 OK\r\n", 17) ||
1493 !strncmp(buf, "HTTP/1.0 200 OK\r\n", 17)) )
1494 {
1495 const char *datacheck = "Content-Type: gnss/data\r\n";
1496 const char *chunkycheck = "Transfer-Encoding: chunked\r\n";
1497 int l = strlen(datacheck)-1;
1498 int j=0;
1499 for(i = 0; j != l && i < numbytes-l; ++i)
1500 {
1501 for(j = 0; j < l && buf[i+j] == datacheck[j]; ++j)
1502 ;
1503 }
1504 if(i == numbytes-l)
1505 {
1506 fprintf(stderr, "No 'Content-Type: gnss/data' found\n");
1507 error = 1;
1508 }
1509 l = strlen(chunkycheck)-1;
1510 j=0;
1511 for(i = 0; j != l && i < numbytes-l; ++i)
1512 {
1513 for(j = 0; j < l && buf[i+j] == chunkycheck[j]; ++j)
1514 ;
1515 }
1516 if(i < numbytes-l)
1517 chunkymode = 1;
1518 }
1519 else if(!strstr(buf, "ICY 200 OK"))
1520 {
1521 fprintf(stderr, "Could not get the requested data: ");
1522 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
1523 {
1524 fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
1525 }
1526 fprintf(stderr, "\n");
1527 error = 1;
1528 }
1529 else if(args.mode != NTRIP1)
1530 {
1531 fprintf(stderr, "NTRIP version 2 HTTP connection failed%s.\n",
1532 args.mode == AUTO ? ", falling back to NTRIP1" : "");
1533 if(args.mode == HTTP)
1534 stop = 1;
1535 }
1536 k = 1;
1537 if(args.mode == NTRIP1)
1538 continue; /* skip old headers for NTRIP1 */
1539 else
1540 {
1541 char *ep = strstr(buf, "\r\n\r\n");
1542 if(!ep || ep+4 == buf+numbytes)
1543 continue;
1544 ep += 4;
1545 memmove(buf, ep, numbytes-(ep-buf));
1546 numbytes -= (ep-buf);
1547 }
1548 }
1549 sleeptime = 0;
1550 if(chunkymode)
1551 {
1552 int cstop = 0;
1553 int pos = 0;
1554 while(!stop && !cstop && !error && pos < numbytes)
1555 {
1556 switch(chunkymode)
1557 {
1558 case 1: /* reading number starts */
1559 chunksize = 0;
1560 ++chunkymode; /* no break */
1561 case 2: /* during reading number */
1562 i = buf[pos++];
1563 if(i >= '0' && i <= '9') chunksize = chunksize*16+i-'0';
1564 else if(i >= 'a' && i <= 'f') chunksize = chunksize*16+i-'a'+10;
1565 else if(i >= 'A' && i <= 'F') chunksize = chunksize*16+i-'A'+10;
1566 else if(i == '\r') ++chunkymode;
1567 else if(i == ';') chunkymode = 5;
1568 else cstop = 1;
1569 break;
1570 case 3: /* scanning for return */
1571 if(buf[pos++] == '\n') chunkymode = chunksize ? 4 : 1;
1572 else cstop = 1;
1573 break;
1574 case 4: /* output data */
1575 i = numbytes-pos;
1576 if(i > chunksize) i = chunksize;
1577 if(args.serdevice)
1578 {
1579 int ofs = 0;
1580 while(i > ofs && !cstop && !stop && !error)
1581 {
1582 int j = SerialWrite(&sx, buf+pos+ofs, i-ofs);
1583 if(j < 0)
1584 {
1585 fprintf(stderr, "Could not access serial device\n");
1586 stop = 1;
1587 }
1588 else
1589 ofs += j;
1590 }
1591 }
1592 else
1593 fwrite(buf+pos, (size_t)i, 1, stdout);
1594 totalbytes += i;
1595 chunksize -= i;
1596 pos += i;
1597 if(!chunksize)
1598 chunkymode = 1;
1599 break;
1600 case 5:
1601 if(i == '\r') chunkymode = 3;
1602 break;
1603 }
1604 }
1605 if(cstop)
1606 {
1607 fprintf(stderr, "Error in chunky transfer encoding\n");
1608 error = 1;
1609 }
1610 }
1611 else
1612 {
1613 totalbytes += numbytes;
1614 if(args.serdevice)
1615 {
1616 int ofs = 0;
1617 while(numbytes > ofs && !stop)
1618 {
1619 int i = SerialWrite(&sx, buf+ofs, numbytes-ofs);
1620 if(i < 0)
1621 {
1622 fprintf(stderr, "Could not access serial device\n");
1623 stop = 1;
1624 }
1625 else
1626 ofs += i;
1627 }
1628 }
1629 else
1630 fwrite(buf, (size_t)numbytes, 1, stdout);
1631 }
1632 fflush(stdout);
1633 if(totalbytes < 0) /* overflow */
1634 {
1635 totalbytes = 0;
1636 starttime = time(0);
1637 lastout = starttime;
1638 }
1639 if(args.serdevice && !stop)
1640 {
1641 int doloop = 1;
1642 while(doloop && !stop)
1643 {
1644 int i = SerialRead(&sx, buf, 200);
1645 if(i < 0)
1646 {
1647 fprintf(stderr, "Could not access serial device\n");
1648 stop = 1;
1649 }
1650 else
1651 {
1652 int j = 0;
1653 if(i < 200) doloop = 0;
1654 fwrite(buf, i, 1, stdout);
1655 if(ser)
1656 fwrite(buf, i, 1, ser);
1657 while(j < i)
1658 {
1659 if(nmeabufpos < 6)
1660 {
1661 if(nmeabuffer[nmeabufpos] != buf[j])
1662 {
1663 if(nmeabufpos) nmeabufpos = 0;
1664 else ++j;
1665 }
1666 else
1667 {
1668 nmeastarpos = 0;
1669 ++j; ++nmeabufpos;
1670 }
1671 }
1672 else if((nmeastarpos && nmeabufpos == nmeastarpos + 3)
1673 || buf[j] == '\r' || buf[j] == '\n')
1674 {
1675 doloop = 0;
1676 nmeabuffer[nmeabufpos++] = '\r';
1677 nmeabuffer[nmeabufpos++] = '\n';
1678 if(send(sockfd, nmeabuffer, nmeabufpos, 0)
1679 != (int)nmeabufpos)
1680 {
1681 fprintf(stderr, "Could not send NMEA\n");
1682 error = 1;
1683 }
1684 nmeabufpos = 0;
1685 }
1686 else if(nmeabufpos > sizeof(nmeabuffer)-10 ||
1687 buf[j] == '$')
1688 nmeabufpos = 0;
1689 else
1690 {
1691 if(buf[j] == '*') nmeastarpos = nmeabufpos;
1692 nmeabuffer[nmeabufpos++] = buf[j++];
1693 }
1694 }
1695 }
1696 }
1697 }
1698 if(args.bitrate)
1699 {
1700 int t = time(0);
1701 if(t > lastout + 60)
1702 {
1703 lastout = t;
1704 fprintf(stderr, "Bitrate is %dbyte/s (%d seconds accumulated).\n",
1705 totalbytes/(t-starttime), t-starttime);
1706 }
1707 }
1708 }
1709 }
1710 else
1711 {
1712 sleeptime = 0;
1713 while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1714 {
1715 #ifndef WINDOWSVERSION
1716 alarm(ALARMTIME);
1717 #endif
1718 fwrite(buf, (size_t)numbytes, 1, stdout);
1719 }
1720 }
1721 }
1722 }
1723 }
1724 if(sockfd)
1725 closesocket(sockfd);
1726 sleep(10);
1727 } while(args.data && *args.data != '%' && !stop);
1728 if(args.serdevice)
1729 {
1730 SerialFree(&sx);
1731 }
1732 if(ser)
1733 fclose(ser);
1734 }
1735 return 0;
1736}
Note: See TracBrowser for help on using the repository browser.