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

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

added URL style

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