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

Last change on this file since 42 was 42, checked in by stoecker, 18 years ago

fixed error text

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