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

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

updated, renamed

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