1 | /*
|
---|
2 | Easy example NTRIP client for Linux/Unix.
|
---|
3 | $Id: NtripLinuxClient.c,v 1.15 2005/12/06 16:50:26 euronav 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 <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <errno.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <netdb.h>
|
---|
30 | #include <sys/types.h>
|
---|
31 | #include <netinet/in.h>
|
---|
32 | #include <sys/socket.h>
|
---|
33 |
|
---|
34 | /* The string, which is send as agent in HTTP request */
|
---|
35 | #define AGENTSTRING "NTRIP NtripLinuxClient"
|
---|
36 |
|
---|
37 | #define MAXDATASIZE 1000 /* max number of bytes we can get at once */
|
---|
38 |
|
---|
39 | /* CVS revision and version */
|
---|
40 | static char revisionstr[] = "$Revision: 1.15 $";
|
---|
41 | static char datestr[] = "$Date: 2005/12/06 16:50:26 $";
|
---|
42 |
|
---|
43 | struct Args
|
---|
44 | {
|
---|
45 | const char *server;
|
---|
46 | int port;
|
---|
47 | const char *user;
|
---|
48 | const char *password;
|
---|
49 | const char *data;
|
---|
50 | };
|
---|
51 |
|
---|
52 | /* option parsing */
|
---|
53 | #ifdef NO_LONG_OPTS
|
---|
54 | #define LONG_OPT(a)
|
---|
55 | #else
|
---|
56 | #define LONG_OPT(a) a
|
---|
57 | static struct option opts[] = {
|
---|
58 | { "data", required_argument, 0, 'd'},
|
---|
59 | { "server", required_argument, 0, 's'},
|
---|
60 | { "password", required_argument, 0, 'p'},
|
---|
61 | { "port", required_argument, 0, 'r'},
|
---|
62 | { "user", required_argument, 0, 'u'},
|
---|
63 | { "help", no_argument, 0, 'h'},
|
---|
64 | {0,0,0,0}};
|
---|
65 | #endif
|
---|
66 | #define ARGOPT "d:hp:r:s:u:"
|
---|
67 |
|
---|
68 | static int getargs(int argc, char **argv, struct Args *args)
|
---|
69 | {
|
---|
70 | int res = 1;
|
---|
71 | int getoptr;
|
---|
72 | char *a;
|
---|
73 | int i = 0, help = 0;
|
---|
74 | char *t;
|
---|
75 |
|
---|
76 | args->server = "www.euref-ip.net";
|
---|
77 | args->port = 80;
|
---|
78 | args->user = "";
|
---|
79 | args->password = "";
|
---|
80 | args->data = 0;
|
---|
81 | help = 0;
|
---|
82 |
|
---|
83 | do
|
---|
84 | {
|
---|
85 | #ifdef NO_LONG_OPTS
|
---|
86 | switch((getoptr = getopt(argc, argv, ARGOPT)))
|
---|
87 | #else
|
---|
88 | switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
|
---|
89 | #endif
|
---|
90 | {
|
---|
91 | case 's': args->server = optarg; break;
|
---|
92 | case 'u': args->user = optarg; break;
|
---|
93 | case 'p': args->password = optarg; break;
|
---|
94 | case 'd': args->data = optarg; break;
|
---|
95 | case 'h': help=1; break;
|
---|
96 | case 'r':
|
---|
97 | args->port = strtoul(optarg, &t, 10);
|
---|
98 | if((t && *t) || args->port < 1 || args->port > 65535)
|
---|
99 | res = 0;
|
---|
100 | break;
|
---|
101 | case -1: break;
|
---|
102 | }
|
---|
103 | } while(getoptr != -1 || !res);
|
---|
104 |
|
---|
105 | for(a = revisionstr+11; *a && *a != ' '; ++a)
|
---|
106 | revisionstr[i++] = *a;
|
---|
107 | revisionstr[i] = 0;
|
---|
108 | datestr[0] = datestr[7];
|
---|
109 | datestr[1] = datestr[8];
|
---|
110 | datestr[2] = datestr[9];
|
---|
111 | datestr[3] = datestr[10];
|
---|
112 | datestr[5] = datestr[12];
|
---|
113 | datestr[6] = datestr[13];
|
---|
114 | datestr[8] = datestr[15];
|
---|
115 | datestr[9] = datestr[16];
|
---|
116 | datestr[4] = datestr[7] = '-';
|
---|
117 | datestr[10] = 0;
|
---|
118 |
|
---|
119 | if(!res || help)
|
---|
120 | {
|
---|
121 | fprintf(stderr, "Version %s (%s) GPL\nUsage: %s -s server -u user ...\n"
|
---|
122 | " -d " LONG_OPT("--data ") "the requested data set\n"
|
---|
123 | " -s " LONG_OPT("--server ") "the server name or address\n"
|
---|
124 | " -p " LONG_OPT("--password ") "the login password\n"
|
---|
125 | " -r " LONG_OPT("--port ") "the server port number (default 80)\n"
|
---|
126 | " -u " LONG_OPT("--user ") "the user name\n"
|
---|
127 | , revisionstr, datestr, argv[0]);
|
---|
128 | exit(1);
|
---|
129 | }
|
---|
130 | return res;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static const char encodingTable [64] = {
|
---|
134 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
|
---|
135 | 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
|
---|
136 | 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
|
---|
137 | 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
|
---|
138 | };
|
---|
139 |
|
---|
140 | /* does not buffer overrun, but breaks directly after an error */
|
---|
141 | /* returns the number of required bytes */
|
---|
142 | static int encode(char *buf, int size, const char *user, const char *pwd)
|
---|
143 | {
|
---|
144 | unsigned char inbuf[3];
|
---|
145 | char *out = buf;
|
---|
146 | int i, sep = 0, fill = 0, bytes = 0;
|
---|
147 |
|
---|
148 | while(*user || *pwd)
|
---|
149 | {
|
---|
150 | i = 0;
|
---|
151 | while(i < 3 && *user) inbuf[i++] = *(user++);
|
---|
152 | if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
|
---|
153 | while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
|
---|
154 | while(i < 3) {inbuf[i++] = 0; ++fill; }
|
---|
155 | if(out-buf < size-1)
|
---|
156 | *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
|
---|
157 | if(out-buf < size-1)
|
---|
158 | *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
|
---|
159 | | ((inbuf [1] & 0xF0) >> 4)];
|
---|
160 | if(out-buf < size-1)
|
---|
161 | {
|
---|
162 | if(fill == 2)
|
---|
163 | *(out++) = '=';
|
---|
164 | else
|
---|
165 | *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
|
---|
166 | | ((inbuf [2] & 0xC0) >> 6)];
|
---|
167 | }
|
---|
168 | if(out-buf < size-1)
|
---|
169 | {
|
---|
170 | if(fill >= 1)
|
---|
171 | *(out++) = '=';
|
---|
172 | else
|
---|
173 | *(out++) = encodingTable[inbuf [2] & 0x3F];
|
---|
174 | }
|
---|
175 | bytes += 4;
|
---|
176 | }
|
---|
177 | if(out-buf < size)
|
---|
178 | *out = 0;
|
---|
179 | return bytes;
|
---|
180 | }
|
---|
181 |
|
---|
182 | int main(int argc, char **argv)
|
---|
183 | {
|
---|
184 | struct Args args;
|
---|
185 |
|
---|
186 | setbuf(stdout, 0);
|
---|
187 | setbuf(stdin, 0);
|
---|
188 | setbuf(stderr, 0);
|
---|
189 |
|
---|
190 | if(getargs(argc, argv, &args))
|
---|
191 | {
|
---|
192 | int i, sockfd, numbytes;
|
---|
193 | char buf[MAXDATASIZE];
|
---|
194 | struct hostent *he;
|
---|
195 | struct sockaddr_in their_addr; /* connector's address information */
|
---|
196 |
|
---|
197 | if(!(he=gethostbyname(args.server)))
|
---|
198 | {
|
---|
199 | perror("gethostbyname");
|
---|
200 | exit(1);
|
---|
201 | }
|
---|
202 | if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
---|
203 | {
|
---|
204 | perror("socket");
|
---|
205 | exit(1);
|
---|
206 | }
|
---|
207 | their_addr.sin_family = AF_INET; /* host byte order */
|
---|
208 | their_addr.sin_port = htons(args.port); /* short, network byte order */
|
---|
209 | their_addr.sin_addr = *((struct in_addr *)he->h_addr);
|
---|
210 | memset(&(their_addr.sin_zero), '\0', 8);
|
---|
211 | if(connect(sockfd, (struct sockaddr *)&their_addr,
|
---|
212 | sizeof(struct sockaddr)) == -1)
|
---|
213 | {
|
---|
214 | perror("connect");
|
---|
215 | exit(1);
|
---|
216 | }
|
---|
217 |
|
---|
218 | if(!args.data)
|
---|
219 | {
|
---|
220 | i = snprintf(buf, MAXDATASIZE,
|
---|
221 | "GET / HTTP/1.0\r\n"
|
---|
222 | "User-Agent: %s/%s\r\n"
|
---|
223 | #ifdef UNUSED
|
---|
224 | "Accept: */*\r\n"
|
---|
225 | "Connection: close\r\n"
|
---|
226 | #endif
|
---|
227 | "\r\n"
|
---|
228 | , AGENTSTRING, revisionstr);
|
---|
229 | }
|
---|
230 | else
|
---|
231 | {
|
---|
232 | i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
|
---|
233 | "GET /%s HTTP/1.0\r\n"
|
---|
234 | "User-Agent: %s/%s\r\n"
|
---|
235 | #ifdef UNUSED
|
---|
236 | "Accept: */*\r\n"
|
---|
237 | "Connection: close\r\n"
|
---|
238 | #endif
|
---|
239 | "Authorization: Basic "
|
---|
240 | , args.data, AGENTSTRING, revisionstr);
|
---|
241 | if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
|
---|
242 | {
|
---|
243 | fprintf(stderr, "Requested data too long\n");
|
---|
244 | exit(1);
|
---|
245 | }
|
---|
246 | i += encode(buf+i, MAXDATASIZE-i-5, args.user, args.password);
|
---|
247 | if(i > MAXDATASIZE-5)
|
---|
248 | {
|
---|
249 | fprintf(stderr, "Username and/or password too long\n");
|
---|
250 | exit(1);
|
---|
251 | }
|
---|
252 | snprintf(buf+i, 5, "\r\n\r\n");
|
---|
253 | i += 5;
|
---|
254 | }
|
---|
255 | if(send(sockfd, buf, (size_t)i, 0) != i)
|
---|
256 | {
|
---|
257 | perror("send");
|
---|
258 | exit(1);
|
---|
259 | }
|
---|
260 | if(args.data)
|
---|
261 | {
|
---|
262 | int k = 0;
|
---|
263 | while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
|
---|
264 | {
|
---|
265 | if(!k)
|
---|
266 | {
|
---|
267 | if(numbytes < 12 || strncmp("ICY 200 OK\r\n", buf, 12))
|
---|
268 | {
|
---|
269 | fprintf(stderr, "Could not get the requested data: ");
|
---|
270 | for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
|
---|
271 | {
|
---|
272 | fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
|
---|
273 | }
|
---|
274 | fprintf(stderr, "\n");
|
---|
275 | exit(1);
|
---|
276 | }
|
---|
277 | ++k;
|
---|
278 | }
|
---|
279 | else
|
---|
280 | {
|
---|
281 | fwrite(buf, (size_t)numbytes, 1, stdout);
|
---|
282 | fflush(stdout);
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|
286 | else
|
---|
287 | {
|
---|
288 | while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
|
---|
289 | {
|
---|
290 | fwrite(buf, (size_t)numbytes, 1, stdout);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | close(sockfd);
|
---|
295 | }
|
---|
296 | return 0;
|
---|
297 | }
|
---|