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

Last change on this file since 1811 was 1811, checked in by stoecker, 15 years ago

session id is unsigned

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