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

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

fixed for GNCaster

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