source: ntrip/trunk/ntripclient/NtripLinuxClient.c@ 362

Last change on this file since 362 was 362, checked in by stoecker, 17 years ago

added NMEA

File size: 11.7 KB
Line 
1/*
2 Easy example NTRIP client for Linux/Unix.
3 $Id: NtripLinuxClient.c,v 1.23 2006/11/29 10:43:15 stoecker Exp $
4 Copyright (C) 2003-2005 by Dirk Stoecker <soft@dstoecker.de>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 or read http://www.gnu.org/licenses/gpl.txt
20*/
21
22#include <ctype.h>
23#include <getopt.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <errno.h>
29#include <string.h>
30#include <netdb.h>
31#include <sys/types.h>
32#include <netinet/in.h>
33#include <sys/socket.h>
34#include <time.h>
35
36/* The string, which is send as agent in HTTP request */
37#define AGENTSTRING "NTRIP NtripLinuxClient"
38
39#define MAXDATASIZE 1000 /* max number of bytes we can get at once */
40#define ALARMTIME (2*60)
41
42/* CVS revision and version */
43static char revisionstr[] = "$Revision: 1.23 $";
44static char datestr[] = "$Date: 2006/11/29 10:43:15 $";
45
46struct Args
47{
48 const char *server;
49 int port;
50 const char *user;
51 const char *password;
52 const char *nmea;
53 const char *data;
54 int bitrate;
55};
56
57/* option parsing */
58#ifdef NO_LONG_OPTS
59#define LONG_OPT(a)
60#else
61#define LONG_OPT(a) a
62static struct option opts[] = {
63{ "bitrate", no_argument, 0, 'b'},
64{ "data", required_argument, 0, 'd'},
65{ "server", required_argument, 0, 's'},
66{ "password", required_argument, 0, 'p'},
67{ "port", required_argument, 0, 'r'},
68{ "user", required_argument, 0, 'u'},
69{ "nmea", required_argument, 0, 'n'},
70{ "help", no_argument, 0, 'h'},
71{0,0,0,0}};
72#endif
73#define ARGOPT "-d:bhp:r:s:u:n:"
74
75#ifdef __GNUC__
76static __attribute__ ((noreturn)) void sighandler_alarm(
77int sig __attribute__((__unused__)))
78#else /* __GNUC__ */
79static void sighandler_alarm(int sig)
80#endif /* __GNUC__ */
81{
82 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
83 exit(1);
84}
85
86static const char *geturl(const char *url, struct Args *args)
87{
88 static char buf[1000];
89 static char *Buffer = buf;
90 static char *Bufend = buf+sizeof(buf);
91
92 if(strncmp("ntrip:", url, 6))
93 return "URL must start with 'ntrip:'.";
94 url += 6; /* skip ntrip: */
95
96 if(*url != '@' && *url != '/')
97 {
98 /* scan for mountpoint */
99 args->data = Buffer;
100 while(*url && *url != '@' && *url != ';' &&*url != '/' && Buffer != Bufend)
101 *(Buffer++) = *(url++);
102 if(Buffer == args->data)
103 return "Mountpoint required.";
104 else if(Buffer >= Bufend-1)
105 return "Parsing buffer too short.";
106 *(Buffer++) = 0;
107 }
108
109 if(*url == '/') /* username and password */
110 {
111 ++url;
112 args->user = Buffer;
113 while(*url && *url != '@' && *url != ';' && *url != ':' && Buffer != Bufend)
114 *(Buffer++) = *(url++);
115 if(Buffer == args->user)
116 return "Username cannot be empty.";
117 else if(Buffer >= Bufend-1)
118 return "Parsing buffer too short.";
119 *(Buffer++) = 0;
120
121 if(*url == ':') ++url;
122
123 args->password = Buffer;
124 while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
125 *(Buffer++) = *(url++);
126 if(Buffer == args->password)
127 return "Password cannot be empty.";
128 else if(Buffer >= Bufend-1)
129 return "Parsing buffer too short.";
130 *(Buffer++) = 0;
131 }
132
133 if(*url == '@') /* server */
134 {
135 ++url;
136 args->server = Buffer;
137 while(*url && *url != ':' && *url != ';' && Buffer != Bufend)
138 *(Buffer++) = *(url++);
139 if(Buffer == args->server)
140 return "Servername cannot be empty.";
141 else if(Buffer >= Bufend-1)
142 return "Parsing buffer too short.";
143 *(Buffer++) = 0;
144
145 if(*url == ':')
146 {
147 char *s2 = 0;
148 args->port = strtol(++url, &s2, 10);
149 if((*s2 && *s2 != ';') || args->port <= 0 || args->port > 0xFFFF)
150 return "Illegal port number.";
151 url = s2;
152 }
153 }
154 if(*url == ';') /* NMEA */
155 {
156 args->nmea = ++url;
157 while(*url)
158 ++url;
159 }
160
161 return *url ? "Garbage at end of server string." : 0;
162}
163
164static int getargs(int argc, char **argv, struct Args *args)
165{
166 int res = 1;
167 int getoptr;
168 char *a;
169 int i = 0, help = 0;
170 char *t;
171
172 args->server = "www.euref-ip.net";
173 args->port = 2101;
174 args->user = "";
175 args->password = "";
176 args->nmea = 0;
177 args->data = 0;
178 args->bitrate = 0;
179 help = 0;
180
181 do
182 {
183#ifdef NO_LONG_OPTS
184 switch((getoptr = getopt(argc, argv, ARGOPT)))
185#else
186 switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
187#endif
188 {
189 case 's': args->server = optarg; break;
190 case 'u': args->user = optarg; break;
191 case 'p': args->password = optarg; break;
192 case 'd': args->data = optarg; break;
193 case 'n': args->nmea = optarg; break;
194 case 'b': args->bitrate = 1; break;
195 case 'h': help=1; break;
196 case 1:
197 {
198 const char *err;
199 if((err = geturl(optarg, args)))
200 {
201 fprintf(stderr, "%s\n\n", err);
202 res = 0;
203 }
204 }
205 break;
206 case 'r':
207 args->port = strtoul(optarg, &t, 10);
208 if((t && *t) || args->port < 1 || args->port > 65535)
209 res = 0;
210 break;
211 case -1: break;
212 }
213 } while(getoptr != -1 && res);
214
215 for(a = revisionstr+11; *a && *a != ' '; ++a)
216 revisionstr[i++] = *a;
217 revisionstr[i] = 0;
218 datestr[0] = datestr[7];
219 datestr[1] = datestr[8];
220 datestr[2] = datestr[9];
221 datestr[3] = datestr[10];
222 datestr[5] = datestr[12];
223 datestr[6] = datestr[13];
224 datestr[8] = datestr[15];
225 datestr[9] = datestr[16];
226 datestr[4] = datestr[7] = '-';
227 datestr[10] = 0;
228
229 if(!res || help)
230 {
231 fprintf(stderr, "Version %s (%s) GPL\nUsage:\n%s -s server -u user ...\n"
232 " -d " LONG_OPT("--data ") "the requested data set\n"
233 " -s " LONG_OPT("--server ") "the server name or address\n"
234 " -p " LONG_OPT("--password ") "the login password\n"
235 " -r " LONG_OPT("--port ") "the server port number (default 2101)\n"
236 " -u " LONG_OPT("--user ") "the user name\n"
237 " -b " LONG_OPT("--bitrate ") "output bitrate\n"
238 "or using an URL:\n%s ntrip:mountpoint[/username[:password]][@server[:port]]\n"
239 , revisionstr, datestr, argv[0], argv[0]);
240 exit(1);
241 }
242 return res;
243}
244
245static const char encodingTable [64] = {
246 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
247 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
248 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
249 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
250};
251
252/* does not buffer overrun, but breaks directly after an error */
253/* returns the number of required bytes */
254static int encode(char *buf, int size, const char *user, const char *pwd)
255{
256 unsigned char inbuf[3];
257 char *out = buf;
258 int i, sep = 0, fill = 0, bytes = 0;
259
260 while(*user || *pwd)
261 {
262 i = 0;
263 while(i < 3 && *user) inbuf[i++] = *(user++);
264 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
265 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
266 while(i < 3) {inbuf[i++] = 0; ++fill; }
267 if(out-buf < size-1)
268 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
269 if(out-buf < size-1)
270 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
271 | ((inbuf [1] & 0xF0) >> 4)];
272 if(out-buf < size-1)
273 {
274 if(fill == 2)
275 *(out++) = '=';
276 else
277 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
278 | ((inbuf [2] & 0xC0) >> 6)];
279 }
280 if(out-buf < size-1)
281 {
282 if(fill >= 1)
283 *(out++) = '=';
284 else
285 *(out++) = encodingTable[inbuf [2] & 0x3F];
286 }
287 bytes += 4;
288 }
289 if(out-buf < size)
290 *out = 0;
291 return bytes;
292}
293
294int main(int argc, char **argv)
295{
296 struct Args args;
297
298 setbuf(stdout, 0);
299 setbuf(stdin, 0);
300 setbuf(stderr, 0);
301 signal(SIGALRM,sighandler_alarm);
302 alarm(ALARMTIME);
303
304 if(getargs(argc, argv, &args))
305 {
306 int i, sockfd, numbytes;
307 char buf[MAXDATASIZE];
308 struct hostent *he;
309 struct sockaddr_in their_addr; /* connector's address information */
310
311 if(!(he=gethostbyname(args.server)))
312 {
313 fprintf(stderr, "Server name lookup failed for '%s'.\n", args.server);
314 exit(1);
315 }
316 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
317 {
318 perror("socket");
319 exit(1);
320 }
321 their_addr.sin_family = AF_INET; /* host byte order */
322 their_addr.sin_port = htons(args.port); /* short, network byte order */
323 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
324 memset(&(their_addr.sin_zero), '\0', 8);
325 if(connect(sockfd, (struct sockaddr *)&their_addr,
326 sizeof(struct sockaddr)) == -1)
327 {
328 perror("connect");
329 exit(1);
330 }
331
332 if(!args.data)
333 {
334 i = snprintf(buf, MAXDATASIZE,
335 "GET / HTTP/1.0\r\n"
336 "User-Agent: %s/%s\r\n"
337#ifdef UNUSED
338 "Accept: */*\r\n"
339 "Connection: close\r\n"
340#endif
341 "\r\n"
342 , AGENTSTRING, revisionstr);
343 }
344 else
345 {
346 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
347 "GET /%s HTTP/1.0\r\n"
348 "User-Agent: %s/%s\r\n"
349#ifdef UNUSED
350 "Accept: */*\r\n"
351 "Connection: close\r\n"
352#endif
353 "Authorization: Basic "
354 , args.data, AGENTSTRING, revisionstr);
355 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
356 {
357 fprintf(stderr, "Requested data too long\n");
358 exit(1);
359 }
360 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
361 if(i > MAXDATASIZE-4)
362 {
363 fprintf(stderr, "Username and/or password too long\n");
364 exit(1);
365 }
366 buf[i++] = '\r';
367 buf[i++] = '\n';
368 buf[i++] = '\r';
369 buf[i++] = '\n';
370 if(args.nmea)
371 {
372 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
373 if(j >= 0 && i < MAXDATASIZE-i)
374 i += j;
375 else
376 {
377 fprintf(stderr, "NMEA string too long\n");
378 exit(1);
379 }
380 }
381 }
382 if(send(sockfd, buf, (size_t)i, 0) != i)
383 {
384 perror("send");
385 exit(1);
386 }
387 if(args.data)
388 {
389 int k = 0;
390 int starttime = time(0);
391 int lastout = starttime;
392 int totalbytes = 0;
393
394 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
395 {
396 alarm(ALARMTIME);
397 if(!k)
398 {
399 if(numbytes < 12 || strncmp("ICY 200 OK\r\n", buf, 12))
400 {
401 fprintf(stderr, "Could not get the requested data: ");
402 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
403 {
404 fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
405 }
406 fprintf(stderr, "\n");
407 exit(1);
408 }
409 ++k;
410 }
411 else
412 {
413 totalbytes += numbytes;
414 if(totalbytes < 0) /* overflow */
415 {
416 totalbytes = 0;
417 starttime = time(0);
418 lastout = starttime;
419 }
420 fwrite(buf, (size_t)numbytes, 1, stdout);
421 fflush(stdout);
422 if(args.bitrate)
423 {
424 int t = time(0);
425 if(t > lastout + 60)
426 {
427 lastout = t;
428 fprintf(stderr, "Bitrate is %d/s (%d seconds accumulated).\n",
429 totalbytes/(t-starttime), t-starttime);
430 }
431 }
432 }
433 }
434 }
435 else
436 {
437 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
438 {
439 fwrite(buf, (size_t)numbytes, 1, stdout);
440 }
441 }
442
443 close(sockfd);
444 }
445 return 0;
446}
Note: See TracBrowser for help on using the repository browser.