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

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

removed DOS returns, change dmain server

File size: 7.6 KB
Line 
1/*
2 Easy example NTRIP client for Linux/Unix.
3 Copyright (C) 2003 by Dirk Stoecker <stoecker@epost.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 or read http://www.gnu.org/licenses/gpl.txt
19*/
20
21/* Version history
22 Please always keep revision history and the two related strings up to date!
23 1.1 2003-02-24 stoecker initial version
24 1.2 2003-02-25 stoecker fixed agent string
25*/
26
27#include <getopt.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <errno.h>
32#include <string.h>
33#include <netdb.h>
34#include <sys/types.h>
35#include <netinet/in.h>
36#include <sys/socket.h>
37
38/* The string, which is send as agent in HTTP request */
39#define AGENTSTRING "NTRIP NtripLinuxClient"
40
41#define MAXDATASIZE 1000 /* max number of bytes we can get at once */
42char buf[MAXDATASIZE];
43
44/* CVS revision and version */
45static char revisionstr[] = "$Revision: 1.2 $";
46static char datestr[] = "$Date: 2003/10/21 07:25:01 $";
47
48struct Args
49{
50 char *server;
51 int port;
52 char *user;
53 char *password;
54 char *data;
55};
56
57/* option parsing */
58#ifdef NO_LONG_OPTS
59#define LONG_OPT(a)
60#else
61#define LONG_OPT(a) a
62static struct option opts[] = {
63{ "data", required_argument, 0, 'd'},
64{ "server", required_argument, 0, 's'},
65{ "password", required_argument, 0, 'p'},
66{ "port", required_argument, 0, 'r'},
67{ "user", required_argument, 0, 'u'},
68{ "help", no_argument, 0, 'h'},
69{0,0,0,0}};
70#endif
71#define ARGOPT "d:hp:r:s:u:"
72
73static int getargs(int argc, char **argv, struct Args *args)
74{
75 int res = 1;
76 int getoptr;
77 char *a;
78 int i = 0, help = 0;
79 char *t;
80
81 args->server = "213.20.169.236";
82 args->port = 80;
83 args->user = "";
84 args->password = "";
85 args->data = 0;
86 help = 0;
87
88 do
89 {
90#ifdef NO_LONG_OPTS
91 switch((getoptr = getopt(argc, argv, ARGOPT)))
92#else
93 switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
94#endif
95 {
96 case 's': args->server = optarg; break;
97 case 'u': args->user = optarg; break;
98 case 'p': args->password = optarg; break;
99 case 'd': args->data = optarg; break;
100 case 'h': help=1; break;
101 case 'r':
102 args->port = strtoul(optarg, &t, 10);
103 if((t && *t) || args->port < 1 || args->port > 65535)
104 res = 0;
105 break;
106 case -1: break;
107 }
108 } while(getoptr != -1 || !res);
109
110 for(a = revisionstr+11; *a && *a != ' '; ++a)
111 revisionstr[i++] = *a;
112 revisionstr[i] = 0;
113 datestr[0] = datestr[7];
114 datestr[1] = datestr[8];
115 datestr[2] = datestr[9];
116 datestr[3] = datestr[10];
117 datestr[5] = datestr[12];
118 datestr[6] = datestr[13];
119 datestr[8] = datestr[15];
120 datestr[9] = datestr[16];
121 datestr[4] = datestr[7] = '-';
122 datestr[10] = 0;
123
124 if(!res || help)
125 {
126 fprintf(stderr, "Version %s (%s) GPL\nUsage: %s -s server -u user ...\n"
127 " -d " LONG_OPT("--data ") "the requested data set\n"
128 " -s " LONG_OPT("--server ") "the server name or address\n"
129 " -p " LONG_OPT("--password ") "the login password\n"
130 " -r " LONG_OPT("--port ") "the server port number (default 80)\n"
131 " -u " LONG_OPT("--user ") "the user name\n"
132 , revisionstr, datestr, argv[0]);
133 exit(1);
134 }
135 return res;
136}
137
138static char encodingTable [64] = {
139 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
140 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
141 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
142 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
143};
144
145/* does not buffer overrun, but breaks directly after an error */
146/* returns the number of required bytes */
147static int encode(char *buf, int size, char *user, char *pwd)
148{
149 unsigned char inbuf[3];
150 char *out = buf;
151 int i, sep = 0, fill = 0, bytes = 0;
152
153 while(*user || *pwd)
154 {
155 i = 0;
156 while(i < 3 && *user) inbuf[i++] = *(user++);
157 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
158 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
159 while(i < 3) {inbuf[i++] = 0; ++fill; }
160 if(out-buf < size-1)
161 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
162 if(out-buf < size-1)
163 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
164 | ((inbuf [1] & 0xF0) >> 4)];
165 if(out-buf < size-1)
166 {
167 if(fill == 2)
168 *(out++) = '=';
169 else
170 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
171 | ((inbuf [2] & 0xC0) >> 6)];
172 }
173 if(out-buf < size-1)
174 {
175 if(fill >= 1)
176 *(out++) = '=';
177 else
178 *(out++) = encodingTable[inbuf [2] & 0x3F];
179 }
180 bytes += 4;
181 }
182 if(out-buf < size)
183 *out = 0;
184 return bytes;
185}
186
187int main(int argc, char **argv)
188{
189 struct Args args;
190
191 if(getargs(argc, argv, &args))
192 {
193 int i, sockfd, numbytes;
194 char buf[MAXDATASIZE];
195 struct hostent *he;
196 struct sockaddr_in their_addr; /* connector's address information */
197
198 if(!(he=gethostbyname(args.server)))
199 {
200 perror("gethostbyname");
201 exit(1);
202 }
203 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
204 {
205 perror("socket");
206 exit(1);
207 }
208 their_addr.sin_family = AF_INET; /* host byte order */
209 their_addr.sin_port = htons(args.port); /* short, network byte order */
210 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
211 memset(&(their_addr.sin_zero), '\0', 8);
212 if(connect(sockfd, (struct sockaddr *)&their_addr,
213 sizeof(struct sockaddr)) == -1)
214 {
215 perror("connect");
216 exit(1);
217 }
218
219 if(!args.data)
220 {
221 i = snprintf(buf, MAXDATASIZE,
222 "GET / HTTP/1.0\r\n"
223 "User-Agent: %s/%s\r\n"
224// "Accept: */*\r\n"
225// "Connection: close\r\n"
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// "Accept: */*\r\n"
235// "Connection: close\r\n"
236 "Authorization: Basic "
237 , args.data, AGENTSTRING, revisionstr);
238 if(i > MAXDATASIZE-40 && i < 0) /* second check for old glibc */
239 {
240 fprintf(stderr, "Requested data too long\n");
241 exit(1);
242 }
243 i += encode(buf+i, MAXDATASIZE-i-5, args.user, args.password);
244 if(i > MAXDATASIZE-5)
245 {
246 fprintf(stderr, "Username and/or password too long\n");
247 exit(1);
248 }
249 snprintf(buf+i, 5, "\r\n\r\n");
250 i += 5;
251 }
252 if(send(sockfd, buf, i, 0) != i)
253 {
254 perror("send");
255 exit(1);
256 }
257 if(args.data)
258 {
259 int k = 0;
260 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
261 {
262 if(!k)
263 {
264 if(numbytes != 12 || strncmp("ICY 200 OK\r\n", buf, 12))
265 {
266 fprintf(stderr, "Could not get the requested data\n");
267 exit(1);
268 }
269 ++k;
270 }
271 else
272 fwrite(buf, numbytes, 1, stdout);
273 }
274 }
275 else
276 {
277 while((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
278 {
279 fwrite(buf, numbytes, 1, stdout);
280 if(!strncmp("ENDSOURCETABLE\r\n", buf+numbytes-16, 16))
281 break;
282 }
283 }
284
285 close(sockfd);
286 }
287 return 0;
288}
Note: See TracBrowser for help on using the repository browser.