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

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

changed NTRIP default server

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