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

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

fixed warnings

File size: 7.7 KB
Line 
1/*
2 Easy example NTRIP client for Linux/Unix.
3 $Id: NtripLinuxClient.c,v 1.11 2005/04/19 11:28:10 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.11 $";
40static char datestr[] = "$Date: 2005/04/19 11:28:10 $";
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 if(getargs(argc, argv, &args))
186 {
187 int i, sockfd, numbytes;
188 char buf[MAXDATASIZE];
189 struct hostent *he;
190 struct sockaddr_in their_addr; /* connector's address information */
191
192 if(!(he=gethostbyname(args.server)))
193 {
194 perror("gethostbyname");
195 exit(1);
196 }
197 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
198 {
199 perror("socket");
200 exit(1);
201 }
202 their_addr.sin_family = AF_INET; /* host byte order */
203 their_addr.sin_port = htons(args.port); /* short, network byte order */
204 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
205 memset(&(their_addr.sin_zero), '\0', 8);
206 if(connect(sockfd, (struct sockaddr *)&their_addr,
207 sizeof(struct sockaddr)) == -1)
208 {
209 perror("connect");
210 exit(1);
211 }
212
213 if(!args.data)
214 {
215 i = snprintf(buf, MAXDATASIZE,
216 "GET / HTTP/1.0\r\n"
217 "User-Agent: %s/%s\r\n"
218#ifdef UNUSED
219 "Accept: */*\r\n"
220 "Connection: close\r\n"
221#endif
222 "\r\n"
223 , AGENTSTRING, revisionstr);
224 }
225 else
226 {
227 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
228 "GET /%s HTTP/1.0\r\n"
229 "User-Agent: %s/%s\r\n"
230#ifdef UNUSED
231 "Accept: */*\r\n"
232 "Connection: close\r\n"
233#endif
234 "Authorization: Basic "
235 , args.data, AGENTSTRING, revisionstr);
236 if(i > MAXDATASIZE-40 && i < 0) /* second check for old glibc */
237 {
238 fprintf(stderr, "Requested data too long\n");
239 exit(1);
240 }
241 i += encode(buf+i, MAXDATASIZE-i-5, args.user, args.password);
242 if(i > MAXDATASIZE-5)
243 {
244 fprintf(stderr, "Username and/or password too long\n");
245 exit(1);
246 }
247 snprintf(buf+i, 5, "\r\n\r\n");
248 i += 5;
249 }
250 if(send(sockfd, buf, i, 0) != i)
251 {
252 perror("send");
253 exit(1);
254 }
255 if(args.data)
256 {
257 int k = 0;
258 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
259 {
260 if(!k)
261 {
262 if(numbytes < 12 || strncmp("ICY 200 OK\r\n", buf, 12))
263 {
264 fprintf(stderr, "Could not get the requested data: ");
265 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
266 {
267 fprintf(stderr, "%c", isprint(buf[k]) ? buf[k] : '.');
268 }
269 fprintf(stderr, "\n");
270 exit(1);
271 }
272 ++k;
273 }
274 else
275 {
276 fwrite(buf, numbytes, 1, stdout);
277 fflush(stdout);
278 }
279 }
280 }
281 else
282 {
283 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
284 {
285 fwrite(buf, numbytes, 1, stdout);
286 }
287 }
288
289 close(sockfd);
290 }
291 return 0;
292}
Note: See TracBrowser for help on using the repository browser.