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