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

Last change on this file since 1747 was 1569, checked in by stoecker, 16 years ago

added Plain UDP mode

File size: 55.3 KB
Line 
1/*
2 NTRIP client for POSIX.
3 $Id: ntripclient.c,v 1.46 2008/11/04 15:35:03 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.46 $";
68static char datestr[] = "$Date: 2008/11/04 15:35:03 $";
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 int session, tim, seq, init;
709 char rtpbuf[1526];
710 int i=12, j;
711
712 init = time(0);
713 srand(init);
714 session = rand();
715 tim = rand();
716 seq = rand();
717
718 rtpbuf[0] = (2<<6);
719 /* padding, extension, csrc are empty */
720 rtpbuf[1] = 97;
721 /* marker is empty */
722 rtpbuf[2] = (seq>>8)&0xFF;
723 rtpbuf[3] = (seq)&0xFF;
724 rtpbuf[4] = (tim>>24)&0xFF;
725 rtpbuf[5] = (tim>>16)&0xFF;
726 rtpbuf[6] = (tim>>8)&0xFF;
727 rtpbuf[7] = (tim)&0xFF;
728 /* sequence and timestamp are empty */
729 rtpbuf[8] = (session>>24)&0xFF;
730 rtpbuf[9] = (session>>16)&0xFF;
731 rtpbuf[10] = (session>>8)&0xFF;
732 rtpbuf[11] = (session)&0xFF;
733 ++seq;
734
735 j = snprintf(rtpbuf+i, sizeof(rtpbuf)-i-40, /* leave some space for login */
736 "GET /%s HTTP/1.1\r\n"
737 "Host: %s\r\n"
738 "Ntrip-Version: Ntrip/2.0\r\n"
739 "User-Agent: %s/%s\r\n"
740 "%s%s%s"
741 "Connection: close%s",
742 args.data ? args.data : "", args.server, AGENTSTRING, revisionstr,
743 args.nmea ? "Ntrip-GGA: " : "", args.nmea ? args.nmea : "",
744 args.nmea ? "\r\n" : "",
745 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
746 i += j;
747 if(i > (int)sizeof(rtpbuf)-40 || j < 0) /* second check for old glibc */
748 {
749 fprintf(stderr, "Requested data too long\n");
750 stop = 1;
751 }
752 else
753 {
754 i += encode(rtpbuf+i, sizeof(rtpbuf)-i-4, args.user, args.password);
755 if(i > (int)sizeof(rtpbuf)-4)
756 {
757 fprintf(stderr, "Username and/or password too long\n");
758 stop = 1;
759 }
760 else
761 {
762 struct sockaddr_in local;
763 socklen_t len;
764
765 rtpbuf[i++] = '\r';
766 rtpbuf[i++] = '\n';
767 rtpbuf[i++] = '\r';
768 rtpbuf[i++] = '\n';
769
770
771 /* fill structure with local address information for UDP */
772 memset(&local, 0, sizeof(local));
773 local.sin_family = AF_INET;
774 local.sin_port = htons(args.udpport);
775 local.sin_addr.s_addr = htonl(INADDR_ANY);
776 len = sizeof(local);
777
778 /* bind() in order to get a random RTP client_port */
779 if((bind(sockfd, (struct sockaddr *)&local, len)) < 0)
780 {
781 myperror("bind");
782 error = 1;
783 }
784 else if(connect(sockfd, (struct sockaddr *)&their_addr,
785 sizeof(struct sockaddr)) == -1)
786 {
787 myperror("connect");
788 error = 1;
789 }
790 else if(send(sockfd, rtpbuf, i, 0) != i)
791 {
792 myperror("Could not send UDP packet");
793 stop = 1;
794 }
795 else
796 {
797 if((numbytes=recv(sockfd, rtpbuf, sizeof(rtpbuf)-1, 0)) > 0)
798 {
799 int sn = 0x10000, ts=0;
800 /* we don't expect message longer than 1513, so we cut the last
801 byte for security reasons to prevent buffer overrun */
802 rtpbuf[numbytes] = 0;
803 if(numbytes > 17+12 &&
804 (!strncmp(rtpbuf+12, "HTTP/1.1 200 OK\r\n", 17) ||
805 !strncmp(rtpbuf+12, "HTTP/1.0 200 OK\r\n", 17)))
806 {
807 const char *sessioncheck = "session: ";
808 const char *datacheck = "Content-Type: gnss/data\r\n";
809 int l = strlen(datacheck)-1;
810 int j=0;
811 for(i = 12; j != l && i < numbytes-l; ++i)
812 {
813 for(j = 0; j < l && rtpbuf[i+j] == datacheck[j]; ++j)
814 ;
815 }
816 if(i == numbytes-l)
817 {
818 fprintf(stderr, "No 'Content-Type: gnss/data' found\n");
819 error = 1;
820 }
821 /* check for Session */
822 l = strlen(sessioncheck)-1;
823 j=0;
824 for(i = 12; j != l && i < numbytes-l; ++i)
825 {
826 for(j = 0; j < l && tolower(rtpbuf[i+j]) == sessioncheck[j]; ++j)
827 ;
828 }
829 if(i != numbytes-l) /* found a session number */
830 {
831 i+=l;
832 session = 0;
833 while(i < numbytes && rtpbuf[i] >= '0' && rtpbuf[i] <= '9')
834 session = session * 10 + rtpbuf[i++]-'0';
835 if(rtpbuf[i] != '\r')
836 {
837 fprintf(stderr, "Could not extract session number\n");
838 stop = 1;
839 }
840 }
841 }
842 else
843 {
844 int k;
845 fprintf(stderr, "Could not get the requested data: ");
846 for(k = 12; k < numbytes && rtpbuf[k] != '\n' && rtpbuf[k] != '\r'; ++k)
847 {
848 fprintf(stderr, "%c", isprint(rtpbuf[k]) ? rtpbuf[k] : '.');
849 }
850 fprintf(stderr, "\n");
851 error = 1;
852 }
853 while(!stop && !error)
854 {
855 struct timeval tv = {1,0};
856 fd_set fdr;
857 fd_set fde;
858
859 FD_ZERO(&fdr);
860 FD_ZERO(&fde);
861 FD_SET(sockfd, &fdr);
862 FD_SET(sockfd, &fde);
863 if(select(sockfd+1,&fdr,0,&fde,&tv) < 0)
864 {
865 fprintf(stderr, "Select problem.\n");
866 error = 1;
867 continue;
868 }
869 i = recv(sockfd, rtpbuf, sizeof(rtpbuf), 0);
870#ifndef WINDOWSVERSION
871 alarm(ALARMTIME);
872#endif
873 if(i >= 12+1 && (unsigned char)rtpbuf[0] == (2 << 6)
874 && rtpbuf[1] >= 96 && rtpbuf[1] <= 98)
875 {
876 time_t ct;
877 int u,v,w;
878 u = ((unsigned char)rtpbuf[2]<<8)+(unsigned char)rtpbuf[3];
879 v = ((unsigned char)rtpbuf[4]<<24)+((unsigned char)rtpbuf[5]<<16)
880 +((unsigned char)rtpbuf[6]<<8)+(unsigned char)rtpbuf[7];
881 w = ((unsigned char)rtpbuf[8]<<24)+((unsigned char)rtpbuf[9]<<16)
882 +((unsigned char)rtpbuf[10]<<8)+(unsigned char)rtpbuf[11];
883
884 if(sn == 0x10000) {sn = u-1;ts=v-1;}
885 else if(u < -30000 && sn > 30000) sn -= 0xFFFF;
886 if(session != w || ts > v)
887 {
888 fprintf(stderr, "Illegal UDP data received.\n");
889 continue;
890 }
891 else if(u > sn) /* don't show out-of-order packets */
892 {
893 if(rtpbuf[1] == 98)
894 {
895 fprintf(stderr, "Connection closed.\n");
896 error = 1;
897 continue;
898 }
899 else if(rtpbuf[1] == 96)
900 {
901 fwrite(rtpbuf+12, (size_t)i-12, 1, stdout);
902 }
903 }
904 sn = u; ts = v;
905
906 /* Keep Alive */
907 ct = time(0);
908 if(ct-init > 15)
909 {
910 tim += (ct-init)*1000000/TIME_RESOLUTION;
911 rtpbuf[0] = (2<<6);
912 /* padding, extension, csrc are empty */
913 rtpbuf[1] = 96;
914 /* marker is empty */
915 rtpbuf[2] = (seq>>8)&0xFF;
916 rtpbuf[3] = (seq)&0xFF;
917 rtpbuf[4] = (tim>>24)&0xFF;
918 rtpbuf[5] = (tim>>16)&0xFF;
919 rtpbuf[6] = (tim>>8)&0xFF;
920 rtpbuf[7] = (tim)&0xFF;
921 /* sequence and timestamp are empty */
922 rtpbuf[8] = (session>>24)&0xFF;
923 rtpbuf[9] = (session>>16)&0xFF;
924 rtpbuf[10] = (session>>8)&0xFF;
925 rtpbuf[11] = (session)&0xFF;
926 ++seq;
927 init = ct;
928
929 if(send(sockfd, rtpbuf, 12, 0) != 12)
930 {
931 myperror("send");
932 error = 1;
933 }
934 }
935 }
936 else if(i >= 0)
937 {
938 fprintf(stderr, "Illegal UDP header.\n");
939 continue;
940 }
941 }
942 }
943 /* send connection close always to allow nice session closing */
944 tim += (time(0)-init)*1000000/TIME_RESOLUTION;
945 rtpbuf[0] = (2<<6);
946 /* padding, extension, csrc are empty */
947 rtpbuf[1] = 98;
948 /* marker is empty */
949 rtpbuf[2] = (seq>>8)&0xFF;
950 rtpbuf[3] = (seq)&0xFF;
951 rtpbuf[4] = (tim>>24)&0xFF;
952 rtpbuf[5] = (tim>>16)&0xFF;
953 rtpbuf[6] = (tim>>8)&0xFF;
954 rtpbuf[7] = (tim)&0xFF;
955 /* sequence and timestamp are empty */
956 rtpbuf[8] = (session>>24)&0xFF;
957 rtpbuf[9] = (session>>16)&0xFF;
958 rtpbuf[10] = (session>>8)&0xFF;
959 rtpbuf[11] = (session)&0xFF;
960
961 send(sockfd, rtpbuf, 12, 0); /* cleanup */
962 }
963 }
964 }
965 }
966 else if(args.data && *args.data != '%' && args.mode == RTSP)
967 {
968 struct sockaddr_in local;
969 sockettype sockudp = 0;
970 int localport;
971 int cseq = 1;
972 socklen_t len;
973
974 if((sockudp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
975 {
976 myperror("socket");
977 error = 1;
978 }
979 if(!stop && !error)
980 {
981 /* fill structure with local address information for UDP */
982 memset(&local, 0, sizeof(local));
983 local.sin_family = AF_INET;
984 local.sin_port = htons(args.udpport);
985 local.sin_addr.s_addr = htonl(INADDR_ANY);
986 len = sizeof(local);
987 /* bind() in order to get a random RTP client_port */
988 if((bind(sockudp, (struct sockaddr *)&local, len)) < 0)
989 {
990 myperror("bind");
991 error = 1;
992 }
993 else if((getsockname(sockudp, (struct sockaddr*)&local, &len)) == -1)
994 {
995 myperror("local access failed");
996 error = 1;
997 }
998 else if(connect(sockfd, (struct sockaddr *)&their_addr,
999 sizeof(struct sockaddr)) == -1)
1000 {
1001 myperror("connect");
1002 error = 1;
1003 }
1004 localport = ntohs(local.sin_port);
1005 }
1006 if(!stop && !error)
1007 {
1008 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
1009 "SETUP rtsp://%s%s%s/%s RTSP/1.0\r\n"
1010 "CSeq: %d\r\n"
1011 "Ntrip-Version: Ntrip/2.0\r\n"
1012 "Ntrip-Component: Ntripclient\r\n"
1013 "User-Agent: %s/%s\r\n"
1014 "Transport: RTP/GNSS;unicast;client_port=%u%s",
1015 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1016 args.data, cseq++, AGENTSTRING, revisionstr, localport,
1017 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1018 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1019 {
1020 fprintf(stderr, "Requested data too long\n");
1021 stop = 1;
1022 }
1023 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1024 if(i > MAXDATASIZE-4)
1025 {
1026 fprintf(stderr, "Username and/or password too long\n");
1027 stop = 1;
1028 }
1029 buf[i++] = '\r';
1030 buf[i++] = '\n';
1031 buf[i++] = '\r';
1032 buf[i++] = '\n';
1033 if(args.nmea)
1034 {
1035 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
1036 if(j >= 0 && j < MAXDATASIZE-i)
1037 i += j;
1038 else
1039 {
1040 fprintf(stderr, "NMEA string too long\n");
1041 stop = 1;
1042 }
1043 }
1044 }
1045 if(!stop && !error)
1046 {
1047 if(send(sockfd, buf, (size_t)i, 0) != i)
1048 {
1049 myperror("send");
1050 error = 1;
1051 }
1052 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
1053 {
1054 myperror("recv");
1055 error = 1;
1056 }
1057 else if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1058 {
1059 int serverport = 0, session = 0;
1060 const char *portcheck = "server_port=";
1061 const char *sessioncheck = "session: ";
1062 int l = strlen(portcheck)-1;
1063 int j=0;
1064 for(i = 0; j != l && i < numbytes-l; ++i)
1065 {
1066 for(j = 0; j < l && tolower(buf[i+j]) == portcheck[j]; ++j)
1067 ;
1068 }
1069 if(i == numbytes-l)
1070 {
1071 fprintf(stderr, "No server port number found\n");
1072 stop = 1;
1073 }
1074 else
1075 {
1076 i+=l;
1077 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1078 serverport = serverport * 10 + buf[i++]-'0';
1079 if(buf[i] != '\r' && buf[i] != ';')
1080 {
1081 fprintf(stderr, "Could not extract server port\n");
1082 stop = 1;
1083 }
1084 }
1085 if(!stop && !error)
1086 {
1087 l = strlen(sessioncheck)-1;
1088 j=0;
1089 for(i = 0; j != l && i < numbytes-l; ++i)
1090 {
1091 for(j = 0; j < l && tolower(buf[i+j]) == sessioncheck[j]; ++j)
1092 ;
1093 }
1094 if(i == numbytes-l)
1095 {
1096 fprintf(stderr, "No session number found\n");
1097 stop = 1;
1098 }
1099 else
1100 {
1101 i+=l;
1102 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
1103 session = session * 10 + buf[i++]-'0';
1104 if(buf[i] != '\r')
1105 {
1106 fprintf(stderr, "Could not extract session number\n");
1107 stop = 1;
1108 }
1109 }
1110 }
1111 if(!stop && !error && args.initudp)
1112 {
1113 printf("Sending initial UDP packet\n");
1114 struct sockaddr_in casterRTP;
1115 char rtpbuffer[12];
1116 int i;
1117 rtpbuffer[0] = (2<<6);
1118 /* padding, extension, csrc are empty */
1119 rtpbuffer[1] = 96;
1120 /* marker is empty */
1121 rtpbuffer[2] = 0;
1122 rtpbuffer[3] = 0;
1123 rtpbuffer[4] = 0;
1124 rtpbuffer[5] = 0;
1125 rtpbuffer[6] = 0;
1126 rtpbuffer[7] = 0;
1127 /* sequence and timestamp are empty */
1128 rtpbuffer[8] = (session>>24)&0xFF;
1129 rtpbuffer[9] = (session>>16)&0xFF;
1130 rtpbuffer[10] = (session>>8)&0xFF;
1131 rtpbuffer[11] = (session)&0xFF;
1132 /* fill structure with caster address information for UDP */
1133 memset(&casterRTP, 0, sizeof(casterRTP));
1134 casterRTP.sin_family = AF_INET;
1135 casterRTP.sin_port = htons(serverport);
1136 casterRTP.sin_addr = *((struct in_addr *)he->h_addr);
1137
1138 if((i = sendto(sockudp, rtpbuffer, 12, 0,
1139 (struct sockaddr *) &casterRTP, sizeof(casterRTP))) != 12)
1140 myperror("WARNING: could not send initial UDP packet");
1141 }
1142 if(!stop && !error)
1143 {
1144 i = snprintf(buf, MAXDATASIZE,
1145 "PLAY rtsp://%s%s%s/%s RTSP/1.0\r\n"
1146 "CSeq: %d\r\n"
1147 "Session: %d\r\n"
1148 "\r\n",
1149 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1150 args.data, cseq++, session);
1151
1152 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1153 {
1154 fprintf(stderr, "Requested data too long\n");
1155 stop=1;
1156 }
1157 else if(send(sockfd, buf, (size_t)i, 0) != i)
1158 {
1159 myperror("send");
1160 error = 1;
1161 }
1162 else if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
1163 {
1164 if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
1165 {
1166 int ts = 0, sn = 0;
1167 time_t init = 0;
1168 struct sockaddr_in addrRTP;
1169#ifdef WINDOWSVERSION
1170 u_long blockmode = 1;
1171 if(ioctlsocket(sockudp, FIONBIO, &blockmode)
1172 || ioctlsocket(sockfd, FIONBIO, &blockmode))
1173#else /* WINDOWSVERSION */
1174 if(fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0
1175 || fcntl(sockudp, F_SETFL, O_NONBLOCK) < 0)
1176#endif /* WINDOWSVERSION */
1177 {
1178 fprintf(stderr, "Could not set nonblocking mode\n");
1179 error = 1;
1180 }
1181
1182 /* fill structure with caster address information for UDP */
1183 memset(&addrRTP, 0, sizeof(addrRTP));
1184 addrRTP.sin_family = AF_INET;
1185 addrRTP.sin_port = htons(serverport);
1186 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
1187 len = sizeof(addrRTP);
1188
1189 while(!stop && !error)
1190 {
1191 char rtpbuffer[1526];
1192 struct timeval tv = {1,0};
1193 fd_set fdr;
1194 fd_set fde;
1195 int r;
1196
1197 FD_ZERO(&fdr);
1198 FD_ZERO(&fde);
1199 FD_SET(sockudp, &fdr);
1200 FD_SET(sockfd, &fdr);
1201 FD_SET(sockudp, &fde);
1202 FD_SET(sockfd, &fde);
1203 if(select((sockudp>sockfd?sockudp:sockfd)+1,
1204 &fdr,0,&fde,&tv) < 0)
1205 {
1206 fprintf(stderr, "Select problem.\n");
1207 error = 1;
1208 continue;
1209 }
1210 i = recvfrom(sockudp, rtpbuffer, sizeof(rtpbuffer), 0,
1211 (struct sockaddr*) &addrRTP, &len);
1212#ifndef WINDOWSVERSION
1213 alarm(ALARMTIME);
1214#endif
1215 if(i >= 12+1 && (unsigned char)rtpbuffer[0] == (2 << 6) && rtpbuffer[1] == 0x60)
1216 {
1217 int u,v,w;
1218 u = ((unsigned char)rtpbuffer[2]<<8)+(unsigned char)rtpbuffer[3];
1219 v = ((unsigned char)rtpbuffer[4]<<24)+((unsigned char)rtpbuffer[5]<<16)
1220 +((unsigned char)rtpbuffer[6]<<8)+(unsigned char)rtpbuffer[7];
1221 w = ((unsigned char)rtpbuffer[8]<<24)+((unsigned char)rtpbuffer[9]<<16)
1222 +((unsigned char)rtpbuffer[10]<<8)+(unsigned char)rtpbuffer[11];
1223
1224 if(init)
1225 {
1226 time_t ct;
1227 if(u < -30000 && sn > 30000) sn -= 0xFFFF;
1228 if(session != w || ts > v)
1229 {
1230 fprintf(stderr, "Illegal UDP data received.\n");
1231 continue;
1232 }
1233 else if(u > sn) /* don't show out-of-order packets */
1234 fwrite(rtpbuffer+12, (size_t)i-12, 1, stdout);
1235 ct = time(0);
1236 if(ct-init > 15)
1237 {
1238 i = snprintf(buf, MAXDATASIZE,
1239 "GET_PARAMETER rtsp://%s%s%s/%s RTSP/1.0\r\n"
1240 "CSeq: %d\r\n"
1241 "Session: %d\r\n"
1242 "\r\n",
1243 args.server, proxyserver ? ":" : "", proxyserver
1244 ? args.port : "", args.data, cseq++, session);
1245 if(i > MAXDATASIZE || i < 0)
1246 {
1247 fprintf(stderr, "Requested data too long\n");
1248 stop = 1;
1249 }
1250 else if(send(sockfd, buf, (size_t)i, 0) != i)
1251 {
1252 myperror("send");
1253 error = 1;
1254 }
1255 init = ct;
1256 }
1257 }
1258 else
1259 {
1260 init = time(0);
1261 }
1262 sn = u; ts = v;
1263 }
1264 else if(i >= 0)
1265 {
1266 fprintf(stderr, "Illegal UDP header.\n");
1267 continue;
1268 }
1269 /* ignore RTSP server replies */
1270 if((r=recv(sockfd, buf, MAXDATASIZE-1, 0)) < 0)
1271 {
1272#ifdef WINDOWSVERSION
1273 if(WSAGetLastError() != WSAEWOULDBLOCK)
1274#else /* WINDOWSVERSION */
1275 if(errno != EAGAIN)
1276#endif /* WINDOWSVERSION */
1277 {
1278 fprintf(stderr, "Control connection closed\n");
1279 error = 1;
1280 }
1281 }
1282 else if(!r)
1283 {
1284 fprintf(stderr, "Control connection read error\n");
1285 error = 1;
1286 }
1287 }
1288 }
1289 i = snprintf(buf, MAXDATASIZE,
1290 "TEARDOWN rtsp://%s%s%s/%s RTSP/1.0\r\n"
1291 "CSeq: %d\r\n"
1292 "Session: %d\r\n"
1293 "\r\n",
1294 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
1295 args.data, cseq++, session);
1296
1297 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
1298 {
1299 fprintf(stderr, "Requested data too long\n");
1300 stop = 1;
1301 }
1302 else if(send(sockfd, buf, (size_t)i, 0) != i)
1303 {
1304 myperror("send");
1305 error = 1;
1306 }
1307 }
1308 else
1309 {
1310 fprintf(stderr, "Could not start data stream.\n");
1311 error = 1;
1312 }
1313 }
1314 }
1315 else
1316 {
1317 fprintf(stderr, "Could not setup initial control connection.\n");
1318 error = 1;
1319 }
1320 if(sockudp)
1321 closesocket(sockudp);
1322 }
1323 }
1324 else
1325 {
1326 if(connect(sockfd, (struct sockaddr *)&their_addr,
1327 sizeof(struct sockaddr)) == -1)
1328 {
1329 myperror("connect");
1330 error = 1;
1331 }
1332 if(!stop && !error)
1333 {
1334 if(!args.data)
1335 {
1336 i = snprintf(buf, MAXDATASIZE,
1337 "GET %s%s%s%s/ HTTP/1.1\r\n"
1338 "Host: %s\r\n%s"
1339 "User-Agent: %s/%s\r\n"
1340 "Connection: close\r\n"
1341 "\r\n"
1342 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1343 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1344 args.server, args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1345 AGENTSTRING, revisionstr);
1346 }
1347 else
1348 {
1349 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
1350 "GET %s%s%s%s/%s HTTP/1.1\r\n"
1351 "Host: %s\r\n%s"
1352 "User-Agent: %s/%s\r\n"
1353 "Connection: close%s"
1354 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
1355 proxyserver ? ":" : "", proxyserver ? proxyport : "",
1356 args.data, args.server,
1357 args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
1358 AGENTSTRING, revisionstr,
1359 (*args.user || *args.password) ? "\r\nAuthorization: Basic " : "");
1360 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
1361 {
1362 fprintf(stderr, "Requested data too long\n");
1363 stop = 1;
1364 }
1365 else
1366 {
1367 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
1368 if(i > MAXDATASIZE-4)
1369 {
1370 fprintf(stderr, "Username and/or password too long\n");
1371 stop = 1;
1372 }
1373 else
1374 {
1375 buf[i++] = '\r';
1376 buf[i++] = '\n';
1377 buf[i++] = '\r';
1378 buf[i++] = '\n';
1379 if(args.nmea)
1380 {
1381 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
1382 if(j >= 0 && j < MAXDATASIZE-i)
1383 i += j;
1384 else
1385 {
1386 fprintf(stderr, "NMEA string too long\n");
1387 stop = 1;
1388 }
1389 }
1390 }
1391 }
1392 }
1393 }
1394 if(!stop && !error)
1395 {
1396 if(send(sockfd, buf, (size_t)i, 0) != i)
1397 {
1398 myperror("send");
1399 error = 1;
1400 }
1401 else if(args.data && *args.data != '%')
1402 {
1403 int k = 0;
1404 int chunkymode = 0;
1405 int starttime = time(0);
1406 int lastout = starttime;
1407 int totalbytes = 0;
1408 int chunksize = 0;
1409
1410 while(!stop && !error &&
1411 (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1412 {
1413#ifndef WINDOWSVERSION
1414 alarm(ALARMTIME);
1415#endif
1416 if(!k)
1417 {
1418 if( numbytes > 17 &&
1419 !strstr(buf, "ICY 200 OK") && /* case 'proxy & ntrip 1.0 caster' */
1420 (!strncmp(buf, "HTTP/1.1 200 OK\r\n", 17) ||
1421 !strncmp(buf, "HTTP/1.0 200 OK\r\n", 17)) )
1422 {
1423 const char *datacheck = "Content-Type: gnss/data\r\n";
1424 const char *chunkycheck = "Transfer-Encoding: chunked\r\n";
1425 int l = strlen(datacheck)-1;
1426 int j=0;
1427 for(i = 0; j != l && i < numbytes-l; ++i)
1428 {
1429 for(j = 0; j < l && buf[i+j] == datacheck[j]; ++j)
1430 ;
1431 }
1432 if(i == numbytes-l)
1433 {
1434 fprintf(stderr, "No 'Content-Type: gnss/data' found\n");
1435 error = 1;
1436 }
1437 l = strlen(chunkycheck)-1;
1438 j=0;
1439 for(i = 0; j != l && i < numbytes-l; ++i)
1440 {
1441 for(j = 0; j < l && buf[i+j] == chunkycheck[j]; ++j)
1442 ;
1443 }
1444 if(i < numbytes-l)
1445 chunkymode = 1;
1446 }
1447 else if(!strstr(buf, "ICY 200 OK"))
1448 {
1449 fprintf(stderr, "Could not get the requested data: ");
1450 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
1451 {
1452 fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
1453 }
1454 fprintf(stderr, "\n");
1455 error = 1;
1456 }
1457 else if(args.mode != NTRIP1)
1458 {
1459 fprintf(stderr, "NTRIP version 2 HTTP connection failed%s.\n",
1460 args.mode == AUTO ? ", falling back to NTRIP1" : "");
1461 if(args.mode == HTTP)
1462 stop = 1;
1463 }
1464 ++k;
1465 }
1466 else
1467 {
1468 sleeptime = 0;
1469 if(chunkymode)
1470 {
1471 int cstop = 0;
1472 int pos = 0;
1473 while(!stop && !cstop && !error && pos < numbytes)
1474 {
1475 switch(chunkymode)
1476 {
1477 case 1: /* reading number starts */
1478 chunksize = 0;
1479 ++chunkymode; /* no break */
1480 case 2: /* during reading number */
1481 i = buf[pos++];
1482 if(i >= '0' && i <= '9') chunksize = chunksize*16+i-'0';
1483 else if(i >= 'a' && i <= 'f') chunksize = chunksize*16+i-'a'+10;
1484 else if(i >= 'A' && i <= 'F') chunksize = chunksize*16+i-'A'+10;
1485 else if(i == '\r') ++chunkymode;
1486 else if(i == ';') chunkymode = 5;
1487 else cstop = 1;
1488 break;
1489 case 3: /* scanning for return */
1490 if(buf[pos++] == '\n') chunkymode = chunksize ? 4 : 1;
1491 else cstop = 1;
1492 break;
1493 case 4: /* output data */
1494 i = numbytes-pos;
1495 if(i > chunksize) i = chunksize;
1496 if(args.serdevice)
1497 {
1498 int ofs = 0;
1499 while(i > ofs && !cstop && !stop && !error)
1500 {
1501 int j = SerialWrite(&sx, buf+pos+ofs, i-ofs);
1502 if(j < 0)
1503 {
1504 fprintf(stderr, "Could not access serial device\n");
1505 stop = 1;
1506 }
1507 else
1508 ofs += j;
1509 }
1510 }
1511 else
1512 fwrite(buf+pos, (size_t)i, 1, stdout);
1513 totalbytes += i;
1514 chunksize -= i;
1515 pos += i;
1516 if(!chunksize)
1517 chunkymode = 1;
1518 break;
1519 case 5:
1520 if(i == '\r') chunkymode = 3;
1521 break;
1522 }
1523 }
1524 if(cstop)
1525 {
1526 fprintf(stderr, "Error in chunky transfer encoding\n");
1527 error = 1;
1528 }
1529 }
1530 else
1531 {
1532 totalbytes += numbytes;
1533 if(args.serdevice)
1534 {
1535 int ofs = 0;
1536 while(numbytes > ofs && !stop)
1537 {
1538 int i = SerialWrite(&sx, buf+ofs, numbytes-ofs);
1539 if(i < 0)
1540 {
1541 fprintf(stderr, "Could not access serial device\n");
1542 stop = 1;
1543 }
1544 else
1545 ofs += i;
1546 }
1547 }
1548 else
1549 fwrite(buf, (size_t)numbytes, 1, stdout);
1550 }
1551 fflush(stdout);
1552 if(totalbytes < 0) /* overflow */
1553 {
1554 totalbytes = 0;
1555 starttime = time(0);
1556 lastout = starttime;
1557 }
1558 if(args.serdevice && !stop)
1559 {
1560 int doloop = 1;
1561 while(doloop && !stop)
1562 {
1563 int i = SerialRead(&sx, buf, 200);
1564 if(i < 0)
1565 {
1566 fprintf(stderr, "Could not access serial device\n");
1567 stop = 1;
1568 }
1569 else
1570 {
1571 int j = 0;
1572 if(i < 200) doloop = 0;
1573 fwrite(buf, i, 1, stdout);
1574 if(ser)
1575 fwrite(buf, i, 1, ser);
1576 while(j < i)
1577 {
1578 if(nmeabufpos < 6)
1579 {
1580 if(nmeabuffer[nmeabufpos] != buf[j])
1581 {
1582 if(nmeabufpos) nmeabufpos = 0;
1583 else ++j;
1584 }
1585 else
1586 {
1587 nmeastarpos = 0;
1588 ++j; ++nmeabufpos;
1589 }
1590 }
1591 else if((nmeastarpos && nmeabufpos == nmeastarpos + 3)
1592 || buf[j] == '\r' || buf[j] == '\n')
1593 {
1594 doloop = 0;
1595 nmeabuffer[nmeabufpos++] = '\r';
1596 nmeabuffer[nmeabufpos++] = '\n';
1597 if(send(sockfd, nmeabuffer, nmeabufpos, 0)
1598 != (int)nmeabufpos)
1599 {
1600 fprintf(stderr, "Could not send NMEA\n");
1601 error = 1;
1602 }
1603 nmeabufpos = 0;
1604 }
1605 else if(nmeabufpos > sizeof(nmeabuffer)-10 ||
1606 buf[j] == '$')
1607 nmeabufpos = 0;
1608 else
1609 {
1610 if(buf[j] == '*') nmeastarpos = nmeabufpos;
1611 nmeabuffer[nmeabufpos++] = buf[j++];
1612 }
1613 }
1614 }
1615 }
1616 }
1617 if(args.bitrate)
1618 {
1619 int t = time(0);
1620 if(t > lastout + 60)
1621 {
1622 lastout = t;
1623 fprintf(stderr, "Bitrate is %dbyte/s (%d seconds accumulated).\n",
1624 totalbytes/(t-starttime), t-starttime);
1625 }
1626 }
1627 }
1628 }
1629 }
1630 else
1631 {
1632 sleeptime = 0;
1633 while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
1634 {
1635 #ifndef WINDOWSVERSION
1636 alarm(ALARMTIME);
1637 #endif
1638 fwrite(buf, (size_t)numbytes, 1, stdout);
1639 }
1640 }
1641 }
1642 }
1643 }
1644 if(sockfd)
1645 closesocket(sockfd);
1646 } while(args.data && *args.data != '%' && !stop);
1647 if(args.serdevice)
1648 {
1649 SerialFree(&sx);
1650 }
1651 if(ser)
1652 fclose(ser);
1653 }
1654 return 0;
1655}
Note: See TracBrowser for help on using the repository browser.