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

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

added non-buffering

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