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