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

Last change on this file since 1830 was 1830, checked in by stuerze, 15 years ago

looong-winded changes regarding source table request via plain RTP

File size: 58.6 KB
Line 
1/*
2 NTRIP client for POSIX.
3 $Id: ntripclient.c,v 1.48 2009/04/27 09:49:54 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.48 $";
68static char datestr[] = "$Date: 2009/04/27 09:49:54 $";
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 = "www.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 "Transport: RTP/GNSS;unicast;client_port=%u%s",
1088 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1089 args.data, cseq++, AGENTSTRING, revisionstr, localport,
1090 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1091 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1092 {
1093 fprintf(stderr, "Requested data too long\n");
1094 stop = 1;
1095 }
1096 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1097 if(i > MAXDATASIZE-4)
1098 {
1099 fprintf(stderr, "Username and/or password too long\n");
1100 stop = 1;
1101 }
1102 buf[i++] = '\r';
1103 buf[i++] = '\n';
1104 buf[i++] = '\r';
1105 buf[i++] = '\n';
1106 if(args.nmea)
1107 {
1108 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
1109 if(j >= 0 && j < MAXDATASIZE-i)
1110 i += j;
1111 else
1112 {
1113 fprintf(stderr, "NMEA string too long\n");
1114 stop = 1;
1115 }
1116 }
1117 }
1118 if(!stop && !error)
1119 {
1120 if(send(sockfd, buf, (size_t)i, 0) != i)
1121 {
1122 myperror("send");
1123 error = 1;
1124 }
1125 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
1126 {
1127 myperror("recv");
1128 error = 1;
1129 }
1130 else if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1131 {
1132 int serverport = 0, session = 0;
1133 const char *portcheck = "server_port=";
1134 const char *sessioncheck = "session: ";
1135 int l = strlen(portcheck)-1;
1136 int j=0;
1137 for(i = 0; j != l && i < numbytes-l; ++i)
1138 {
1139 for(j = 0; j < l && tolower(buf[i+j]) == portcheck[j]; ++j)
1140 ;
1141 }
1142 if(i == numbytes-l)
1143 {
1144 fprintf(stderr, "No server port number found\n");
1145 stop = 1;
1146 }
1147 else
1148 {
1149 i+=l;
1150 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1151 serverport = serverport * 10 + buf[i++]-'0';
1152 if(buf[i] != '\r' && buf[i] != ';')
1153 {
1154 fprintf(stderr, "Could not extract server port\n");
1155 stop = 1;
1156 }
1157 }
1158 if(!stop && !error)
1159 {
1160 l = strlen(sessioncheck)-1;
1161 j=0;
1162 for(i = 0; j != l && i < numbytes-l; ++i)
1163 {
1164 for(j = 0; j < l && tolower(buf[i+j]) == sessioncheck[j]; ++j)
1165 ;
1166 }
1167 if(i == numbytes-l)
1168 {
1169 fprintf(stderr, "No session number found\n");
1170 stop = 1;
1171 }
1172 else
1173 {
1174 i+=l;
1175 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1176 session = session * 10 + buf[i++]-'0';
1177 if(buf[i] != '\r')
1178 {
1179 fprintf(stderr, "Could not extract session number\n");
1180 stop = 1;
1181 }
1182 }
1183 }
1184 if(!stop && !error && args.initudp)
1185 {
1186 printf("Sending initial UDP packet\n");
1187 struct sockaddr_in casterRTP;
1188 char rtpbuffer[12];
1189 int i;
1190 rtpbuffer[0] = (2<<6);
1191 /* padding, extension, csrc are empty */
1192 rtpbuffer[1] = 96;
1193 /* marker is empty */
1194 rtpbuffer[2] = 0;
1195 rtpbuffer[3] = 0;
1196 rtpbuffer[4] = 0;
1197 rtpbuffer[5] = 0;
1198 rtpbuffer[6] = 0;
1199 rtpbuffer[7] = 0;
1200 /* sequence and timestamp are empty */
1201 rtpbuffer[8] = (session>>24)&0xFF;
1202 rtpbuffer[9] = (session>>16)&0xFF;
1203 rtpbuffer[10] = (session>>8)&0xFF;
1204 rtpbuffer[11] = (session)&0xFF;
1205 /* fill structure with caster address information for UDP */
1206 memset(&casterRTP, 0, sizeof(casterRTP));
1207 casterRTP.sin_family = AF_INET;
1208 casterRTP.sin_port = htons(serverport);
1209 casterRTP.sin_addr = *((struct in_addr *)he->h_addr);
1210
1211 if((i = sendto(sockudp, rtpbuffer, 12, 0,
1212 (struct sockaddr *) &casterRTP, sizeof(casterRTP))) != 12)
1213 myperror("WARNING: could not send initial UDP packet");
1214 }
1215 if(!stop && !error)
1216 {
1217 i = snprintf(buf, MAXDATASIZE,
1218 "PLAY rtsp://%s%s%s/%s RTSP/1.0\r\n"
1219 "CSeq: %d\r\n"
1220 "Session: %u\r\n"
1221 "\r\n",
1222 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1223 args.data, cseq++, session);
1224
1225 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1226 {
1227 fprintf(stderr, "Requested data too long\n");
1228 stop=1;
1229 }
1230 else if(send(sockfd, buf, (size_t)i, 0) != i)
1231 {
1232 myperror("send");
1233 error = 1;
1234 }
1235 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
1236 {
1237 if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1238 {
1239 int ts = 0, sn = 0;
1240 time_t init = 0;
1241 struct sockaddr_in addrRTP;
1242#ifdef WINDOWSVERSION
1243 u_long blockmode = 1;
1244 if(ioctlsocket(sockudp, FIONBIO, &blockmode)
1245 || ioctlsocket(sockfd, FIONBIO, &blockmode))
1246#else /* WINDOWSVERSION */
1247 if(fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0
1248 || fcntl(sockudp, F_SETFL, O_NONBLOCK) < 0)
1249#endif /* WINDOWSVERSION */
1250 {
1251 fprintf(stderr, "Could not set nonblocking mode\n");
1252 error = 1;
1253 }
1254
1255 /* fill structure with caster address information for UDP */
1256 memset(&addrRTP, 0, sizeof(addrRTP));
1257 addrRTP.sin_family = AF_INET;
1258 addrRTP.sin_port = htons(serverport);
1259 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
1260 len = sizeof(addrRTP);
1261
1262 while(!stop && !error)
1263 {
1264 char rtpbuffer[1526];
1265 struct timeval tv = {1,0};
1266 fd_set fdr;
1267 fd_set fde;
1268 int r;
1269
1270 FD_ZERO(&fdr);
1271 FD_ZERO(&fde);
1272 FD_SET(sockudp, &fdr);
1273 FD_SET(sockfd, &fdr);
1274 FD_SET(sockudp, &fde);
1275 FD_SET(sockfd, &fde);
1276 if(select((sockudp>sockfd?sockudp:sockfd)+1,
1277 &fdr,0,&fde,&tv) < 0)
1278 {
1279 fprintf(stderr, "Select problem.\n");
1280 error = 1;
1281 continue;
1282 }
1283 i = recvfrom(sockudp, rtpbuffer, sizeof(rtpbuffer), 0,
1284 (struct sockaddr*) &addrRTP, &len);
1285#ifndef WINDOWSVERSION
1286 alarm(ALARMTIME);
1287#endif
1288 if(i >= 12+1 && (unsigned char)rtpbuffer[0] == (2 << 6) && rtpbuffer[1] == 0x60)
1289 {
1290 int u,v,w;
1291 u = ((unsigned char)rtpbuffer[2]<<8)+(unsigned char)rtpbuffer[3];
1292 v = ((unsigned char)rtpbuffer[4]<<24)+((unsigned char)rtpbuffer[5]<<16)
1293 +((unsigned char)rtpbuffer[6]<<8)+(unsigned char)rtpbuffer[7];
1294 w = ((unsigned char)rtpbuffer[8]<<24)+((unsigned char)rtpbuffer[9]<<16)
1295 +((unsigned char)rtpbuffer[10]<<8)+(unsigned char)rtpbuffer[11];
1296
1297 if(init)
1298 {
1299 time_t ct;
1300 if(u < -30000 && sn > 30000) sn -= 0xFFFF;
1301 if(session != w || ts > v)
1302 {
1303 fprintf(stderr, "Illegal UDP data received.\n");
1304 continue;
1305 }
1306 else if(u > sn) /* don't show out-of-order packets */
1307 fwrite(rtpbuffer+12, (size_t)i-12, 1, stdout);
1308 ct = time(0);
1309 if(ct-init > 15)
1310 {
1311 i = snprintf(buf, MAXDATASIZE,
1312 "GET_PARAMETER rtsp://%s%s%s/%s RTSP/1.0\r\n"
1313 "CSeq: %d\r\n"
1314 "Session: %u\r\n"
1315 "\r\n",
1316 args.server, proxyserver ? ":" : "", proxyserver
1317 ? args.port : "", args.data, cseq++, session);
1318 if(i > MAXDATASIZE || i < 0)
1319 {
1320 fprintf(stderr, "Requested data too long\n");
1321 stop = 1;
1322 }
1323 else if(send(sockfd, buf, (size_t)i, 0) != i)
1324 {
1325 myperror("send");
1326 error = 1;
1327 }
1328 init = ct;
1329 }
1330 }
1331 else
1332 {
1333 init = time(0);
1334 }
1335 sn = u; ts = v;
1336 }
1337 else if(i >= 0)
1338 {
1339 fprintf(stderr, "Illegal UDP header.\n");
1340 continue;
1341 }
1342 /* ignore RTSP server replies */
1343 if((r=recv(sockfd, buf, MAXDATASIZE-1, 0)) < 0)
1344 {
1345#ifdef WINDOWSVERSION
1346 if(WSAGetLastError() != WSAEWOULDBLOCK)
1347#else /* WINDOWSVERSION */
1348 if(errno != EAGAIN)
1349#endif /* WINDOWSVERSION */
1350 {
1351 fprintf(stderr, "Control connection closed\n");
1352 error = 1;
1353 }
1354 }
1355 else if(!r)
1356 {
1357 fprintf(stderr, "Control connection read error\n");
1358 error = 1;
1359 }
1360 }
1361 }
1362 i = snprintf(buf, MAXDATASIZE,
1363 "TEARDOWN rtsp://%s%s%s/%s RTSP/1.0\r\n"
1364 "CSeq: %d\r\n"
1365 "Session: %u\r\n"
1366 "\r\n",
1367 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1368 args.data, cseq++, session);
1369
1370 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1371 {
1372 fprintf(stderr, "Requested data too long\n");
1373 stop = 1;
1374 }
1375 else if(send(sockfd, buf, (size_t)i, 0) != i)
1376 {
1377 myperror("send");
1378 error = 1;
1379 }
1380 }
1381 else
1382 {
1383 fprintf(stderr, "Could not start data stream.\n");
1384 error = 1;
1385 }
1386 }
1387 }
1388 else
1389 {
1390 fprintf(stderr, "Could not setup initial control connection.\n");
1391 error = 1;
1392 }
1393 if(sockudp)
1394 closesocket(sockudp);
1395 }
1396 }
1397 else
1398 {
1399 if(connect(sockfd, (struct sockaddr *)&their_addr,
1400 sizeof(struct sockaddr)) == -1)
1401 {
1402 myperror("connect");
1403 error = 1;
1404 }
1405 if(!stop && !error)
1406 {
1407 if(!args.data)
1408 {
1409 i = snprintf(buf, MAXDATASIZE,
1410 "GET %s%s%s%s/ HTTP/1.1\r\n"
1411 "Host: %s\r\n%s"
1412 "User-Agent: %s/%s\r\n"
1413 "Connection: close\r\n"
1414 "\r\n"
1415 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1416 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1417 args.server, args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1418 AGENTSTRING, revisionstr);
1419 }
1420 else
1421 {
1422 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
1423 "GET %s%s%s%s/%s HTTP/1.1\r\n"
1424 "Host: %s\r\n%s"
1425 "User-Agent: %s/%s\r\n"
1426 "Connection: close%s"
1427 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1428 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1429 args.data, args.server,
1430 args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1431 AGENTSTRING, revisionstr,
1432 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1433 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1434 {
1435 fprintf(stderr, "Requested data too long\n");
1436 stop = 1;
1437 }
1438 else
1439 {
1440 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1441 if(i > MAXDATASIZE-4)
1442 {
1443 fprintf(stderr, "Username and/or password too long\n");
1444 stop = 1;
1445 }
1446 else
1447 {
1448 buf[i++] = '\r';
1449 buf[i++] = '\n';
1450 buf[i++] = '\r';
1451 buf[i++] = '\n';
1452 if(args.nmea)
1453 {
1454 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
1455 if(j >= 0 && j < MAXDATASIZE-i)
1456 i += j;
1457 else
1458 {
1459 fprintf(stderr, "NMEA string too long\n");
1460 stop = 1;
1461 }
1462 }
1463 }
1464 }
1465 }
1466 }
1467 if(!stop && !error)
1468 {
1469 if(send(sockfd, buf, (size_t)i, 0) != i)
1470 {
1471 myperror("send");
1472 error = 1;
1473 }
1474 else if(args.data && *args.data != '%')
1475 {
1476 int k = 0;
1477 int chunkymode = 0;
1478 int starttime = time(0);
1479 int lastout = starttime;
1480 int totalbytes = 0;
1481 int chunksize = 0;
1482
1483 while(!stop && !error &&
1484 (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1485 {
1486#ifndef WINDOWSVERSION
1487 alarm(ALARMTIME);
1488#endif
1489 if(!k)
1490 {
1491 if( numbytes > 17 &&
1492 !strstr(buf, "ICY 200 OK") && /* case 'proxy & ntrip 1.0 caster' */
1493 (!strncmp(buf, "HTTP/1.1 200 OK\r\n", 17) ||
1494 !strncmp(buf, "HTTP/1.0 200 OK\r\n", 17)) )
1495 {
1496 const char *datacheck = "Content-Type: gnss/data\r\n";
1497 const char *chunkycheck = "Transfer-Encoding: chunked\r\n";
1498 int l = strlen(datacheck)-1;
1499 int j=0;
1500 for(i = 0; j != l && i < numbytes-l; ++i)
1501 {
1502 for(j = 0; j < l && buf[i+j] == datacheck[j]; ++j)
1503 ;
1504 }
1505 if(i == numbytes-l)
1506 {
1507 fprintf(stderr, "No 'Content-Type: gnss/data' found\n");
1508 error = 1;
1509 }
1510 l = strlen(chunkycheck)-1;
1511 j=0;
1512 for(i = 0; j != l && i < numbytes-l; ++i)
1513 {
1514 for(j = 0; j < l && buf[i+j] == chunkycheck[j]; ++j)
1515 ;
1516 }
1517 if(i < numbytes-l)
1518 chunkymode = 1;
1519 }
1520 else if(!strstr(buf, "ICY 200 OK"))
1521 {
1522 fprintf(stderr, "Could not get the requested data: ");
1523 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
1524 {
1525 fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
1526 }
1527 fprintf(stderr, "\n");
1528 error = 1;
1529 }
1530 else if(args.mode != NTRIP1)
1531 {
1532 fprintf(stderr, "NTRIP version 2 HTTP connection failed%s.\n",
1533 args.mode == AUTO ? ", falling back to NTRIP1" : "");
1534 if(args.mode == HTTP)
1535 stop = 1;
1536 }
1537 ++k;
1538 }
1539 else
1540 {
1541 sleeptime = 0;
1542 if(chunkymode)
1543 {
1544 int cstop = 0;
1545 int pos = 0;
1546 while(!stop && !cstop && !error && pos < numbytes)
1547 {
1548 switch(chunkymode)
1549 {
1550 case 1: /* reading number starts */
1551 chunksize = 0;
1552 ++chunkymode; /* no break */
1553 case 2: /* during reading number */
1554 i = buf[pos++];
1555 if(i >= '0' && i <= '9') chunksize = chunksize*16+i-'0';
1556 else if(i >= 'a' && i <= 'f') chunksize = chunksize*16+i-'a'+10;
1557 else if(i >= 'A' && i <= 'F') chunksize = chunksize*16+i-'A'+10;
1558 else if(i == '\r') ++chunkymode;
1559 else if(i == ';') chunkymode = 5;
1560 else cstop = 1;
1561 break;
1562 case 3: /* scanning for return */
1563 if(buf[pos++] == '\n') chunkymode = chunksize ? 4 : 1;
1564 else cstop = 1;
1565 break;
1566 case 4: /* output data */
1567 i = numbytes-pos;
1568 if(i > chunksize) i = chunksize;
1569 if(args.serdevice)
1570 {
1571 int ofs = 0;
1572 while(i > ofs && !cstop && !stop && !error)
1573 {
1574 int j = SerialWrite(&sx, buf+pos+ofs, i-ofs);
1575 if(j < 0)
1576 {
1577 fprintf(stderr, "Could not access serial device\n");
1578 stop = 1;
1579 }
1580 else
1581 ofs += j;
1582 }
1583 }
1584 else
1585 fwrite(buf+pos, (size_t)i, 1, stdout);
1586 totalbytes += i;
1587 chunksize -= i;
1588 pos += i;
1589 if(!chunksize)
1590 chunkymode = 1;
1591 break;
1592 case 5:
1593 if(i == '\r') chunkymode = 3;
1594 break;
1595 }
1596 }
1597 if(cstop)
1598 {
1599 fprintf(stderr, "Error in chunky transfer encoding\n");
1600 error = 1;
1601 }
1602 }
1603 else
1604 {
1605 totalbytes += numbytes;
1606 if(args.serdevice)
1607 {
1608 int ofs = 0;
1609 while(numbytes > ofs && !stop)
1610 {
1611 int i = SerialWrite(&sx, buf+ofs, numbytes-ofs);
1612 if(i < 0)
1613 {
1614 fprintf(stderr, "Could not access serial device\n");
1615 stop = 1;
1616 }
1617 else
1618 ofs += i;
1619 }
1620 }
1621 else
1622 fwrite(buf, (size_t)numbytes, 1, stdout);
1623 }
1624 fflush(stdout);
1625 if(totalbytes < 0) /* overflow */
1626 {
1627 totalbytes = 0;
1628 starttime = time(0);
1629 lastout = starttime;
1630 }
1631 if(args.serdevice && !stop)
1632 {
1633 int doloop = 1;
1634 while(doloop && !stop)
1635 {
1636 int i = SerialRead(&sx, buf, 200);
1637 if(i < 0)
1638 {
1639 fprintf(stderr, "Could not access serial device\n");
1640 stop = 1;
1641 }
1642 else
1643 {
1644 int j = 0;
1645 if(i < 200) doloop = 0;
1646 fwrite(buf, i, 1, stdout);
1647 if(ser)
1648 fwrite(buf, i, 1, ser);
1649 while(j < i)
1650 {
1651 if(nmeabufpos < 6)
1652 {
1653 if(nmeabuffer[nmeabufpos] != buf[j])
1654 {
1655 if(nmeabufpos) nmeabufpos = 0;
1656 else ++j;
1657 }
1658 else
1659 {
1660 nmeastarpos = 0;
1661 ++j; ++nmeabufpos;
1662 }
1663 }
1664 else if((nmeastarpos && nmeabufpos == nmeastarpos + 3)
1665 || buf[j] == '\r' || buf[j] == '\n')
1666 {
1667 doloop = 0;
1668 nmeabuffer[nmeabufpos++] = '\r';
1669 nmeabuffer[nmeabufpos++] = '\n';
1670 if(send(sockfd, nmeabuffer, nmeabufpos, 0)
1671 != (int)nmeabufpos)
1672 {
1673 fprintf(stderr, "Could not send NMEA\n");
1674 error = 1;
1675 }
1676 nmeabufpos = 0;
1677 }
1678 else if(nmeabufpos > sizeof(nmeabuffer)-10 ||
1679 buf[j] == '$')
1680 nmeabufpos = 0;
1681 else
1682 {
1683 if(buf[j] == '*') nmeastarpos = nmeabufpos;
1684 nmeabuffer[nmeabufpos++] = buf[j++];
1685 }
1686 }
1687 }
1688 }
1689 }
1690 if(args.bitrate)
1691 {
1692 int t = time(0);
1693 if(t > lastout + 60)
1694 {
1695 lastout = t;
1696 fprintf(stderr, "Bitrate is %dbyte/s (%d seconds accumulated).\n",
1697 totalbytes/(t-starttime), t-starttime);
1698 }
1699 }
1700 }
1701 }
1702 }
1703 else
1704 {
1705 sleeptime = 0;
1706 while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1707 {
1708 #ifndef WINDOWSVERSION
1709 alarm(ALARMTIME);
1710 #endif
1711 fwrite(buf, (size_t)numbytes, 1, stdout);
1712 }
1713 }
1714 }
1715 }
1716 }
1717 if(sockfd)
1718 closesocket(sockfd);
1719 } while(args.data && *args.data != '%' && !stop);
1720 if(args.serdevice)
1721 {
1722 SerialFree(&sx);
1723 }
1724 if(ser)
1725 fclose(ser);
1726 }
1727 return 0;
1728}
Note: See TracBrowser for help on using the repository browser.