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

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

better output

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