[5] | 1 | /*
|
---|
[18] | 2 | * NtripServerLinux.c
|
---|
[5] | 3 | *
|
---|
[14] | 4 | * Copyright (c) 2003...2005
|
---|
[5] | 5 | * German Federal Agency for Cartography and Geodesy (BKG)
|
---|
| 6 | *
|
---|
| 7 | * Developed for Networked Transport of RTCM via Internet Protocol (NTRIP)
|
---|
| 8 | * for streaming GNSS data over the Internet.
|
---|
| 9 | *
|
---|
| 10 | * Designed by Informatik Centrum Dortmund http://www.icd.de
|
---|
| 11 | *
|
---|
| 12 | * NTRIP is currently an experimental technology.
|
---|
| 13 | * The BKG disclaims any liability nor responsibility to any person or
|
---|
| 14 | * entity with respect to any loss or damage caused, or alleged to be
|
---|
| 15 | * caused, directly or indirectly by the use and application of the NTRIP
|
---|
| 16 | * technology.
|
---|
| 17 | *
|
---|
| 18 | * For latest information and updates, access:
|
---|
| 19 | * http://igs.ifag.de/index_ntrip.htm
|
---|
| 20 | *
|
---|
| 21 | * Georg Weber
|
---|
| 22 | * BKG, Frankfurt, Germany, June 2003-06-13
|
---|
| 23 | * E-mail: euref-ip@bkg.bund.de
|
---|
| 24 | *
|
---|
| 25 | * Based on the GNU General Public License published nmead
|
---|
| 26 | *
|
---|
| 27 | * This program is free software; you can redistribute it and/or
|
---|
| 28 | * modify it under the terms of the GNU General Public License
|
---|
| 29 | * as published by the Free Software Foundation; either version 2
|
---|
| 30 | * of the License, or (at your option) any later version.
|
---|
| 31 | *
|
---|
| 32 | * This program is distributed in the hope that it will be useful,
|
---|
| 33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 35 | * GNU General Public License for more details.
|
---|
| 36 | *
|
---|
| 37 | * You should have received a copy of the GNU General Public License
|
---|
| 38 | * along with this program; if not, write to the Free Software
|
---|
| 39 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
---|
| 40 | * USA.
|
---|
| 41 | */
|
---|
| 42 |
|
---|
[34] | 43 | /* $Id: NtripLinuxServer.c,v 1.15 2006/04/27 09:44:27 stoecker Exp $
|
---|
[18] | 44 | * Changes - Version 0.7
|
---|
| 45 | * Sep 22 2003 Steffen Tschirpke <St.Tschirpke@actina.de>
|
---|
[5] | 46 | * - socket support
|
---|
| 47 | * - command line option handling
|
---|
| 48 | * - error handling
|
---|
| 49 | * - help screen
|
---|
[18] | 50 | *
|
---|
| 51 | * Changes - Version 0.9
|
---|
| 52 | * Feb 15 2005 Dirk Stoecker <soft@dstoecker.de>
|
---|
| 53 | * - some minor updates, fixed serial baudrate settings
|
---|
| 54 | *
|
---|
| 55 | * Changes - Version 0.10
|
---|
| 56 | * Apr 05 2005 Dirk Stoecker <soft@dstoecker.de>
|
---|
| 57 | * - some cleanup and miscellaneous fixes
|
---|
| 58 | * - replaced non-working simulate with file input (stdin)
|
---|
| 59 | * - TCP sending now somewhat more stable
|
---|
| 60 | * - cleanup of error handling
|
---|
[24] | 61 | * - Modes may be symbolic and not only numeric
|
---|
[23] | 62 | *
|
---|
| 63 | * Changes - Version 0.11
|
---|
| 64 | * Jun 02 2005 Dirk Stoecker <soft@dstoecker.de>
|
---|
| 65 | * - added SISNeT support
|
---|
| 66 | * - added UDP support
|
---|
| 67 | * - cleanup of host and port handling
|
---|
| 68 | * - added inactivity alarm of 60 seconds
|
---|
[24] | 69 | *
|
---|
| 70 | * Changes - Version 0.12
|
---|
| 71 | * Jun 07 2005 Dirk Stoecker <soft@dstoecker.de>
|
---|
| 72 | * - added UDP bindmode
|
---|
[32] | 73 | *
|
---|
| 74 | * Changes - Version 0.13
|
---|
| 75 | * Apr 25 2006 Andrea Stuerze <andrea.stuerze@bkg.bund.de>
|
---|
| 76 | * - added stream retrieval from caster
|
---|
| 77 | *
|
---|
| 78 | * Changes - Version 0.14
|
---|
| 79 | * Apr 27 2006 Dirk Stoecker <soft@dstoecker.de>
|
---|
| 80 | * - fixed some problems with caster download
|
---|
| 81 | *
|
---|
[5] | 82 | */
|
---|
| 83 |
|
---|
[18] | 84 | #include <ctype.h>
|
---|
| 85 | #include <errno.h>
|
---|
| 86 | #include <fcntl.h>
|
---|
| 87 | #include <getopt.h>
|
---|
| 88 | #include <netdb.h>
|
---|
[23] | 89 | #include <signal.h>
|
---|
[5] | 90 | #include <stdio.h>
|
---|
[18] | 91 | #include <stdlib.h>
|
---|
[5] | 92 | #include <string.h>
|
---|
| 93 | #include <unistd.h>
|
---|
[18] | 94 | #include <arpa/inet.h>
|
---|
| 95 | #include <netinet/in.h>
|
---|
[5] | 96 | #include <sys/socket.h>
|
---|
| 97 | #include <sys/termios.h>
|
---|
[18] | 98 | #include <sys/types.h>
|
---|
[5] | 99 |
|
---|
[18] | 100 | #ifndef MSG_DONTWAIT
|
---|
| 101 | #define MSG_DONTWAIT 0 /* prevent compiler errors */
|
---|
| 102 | #endif
|
---|
| 103 | #ifndef O_EXLOCK
|
---|
| 104 | #define O_EXLOCK 0 /* prevent compiler errors */
|
---|
| 105 | #endif
|
---|
[5] | 106 |
|
---|
[32] | 107 | enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
|
---|
| 108 | CASTER = 6};
|
---|
[5] | 109 |
|
---|
[32] | 110 | #define VERSION "NTRIP NtripServerLinux/0.13"
|
---|
[18] | 111 | #define BUFSZ 1024
|
---|
| 112 |
|
---|
[5] | 113 | /* default socket source */
|
---|
[18] | 114 | #define SERV_HOST_ADDR "127.0.0.1"
|
---|
| 115 | #define SERV_TCP_PORT 1025
|
---|
[5] | 116 |
|
---|
| 117 | /* default destination */
|
---|
[18] | 118 | #define NTRIP_CASTER "www.euref-ip.net"
|
---|
| 119 | #define NTRIP_PORT 80
|
---|
[5] | 120 |
|
---|
[23] | 121 | /* default sisnet source */
|
---|
| 122 | #define SISNET_SERVER "131.176.49.142"
|
---|
| 123 | #define SISNET_PORT 7777
|
---|
| 124 |
|
---|
| 125 | #define ALARMTIME 60
|
---|
| 126 |
|
---|
[22] | 127 | static int ttybaud = 19200;
|
---|
| 128 | static const char *ttyport = "/dev/gps";
|
---|
| 129 | static const char *filepath = "/dev/stdin";
|
---|
| 130 | static enum MODE mode = INFILE;
|
---|
[23] | 131 | static int sisnetv3 = 0;
|
---|
| 132 | static int gpsfd = -1;
|
---|
[5] | 133 |
|
---|
| 134 | /* Forward references */
|
---|
[22] | 135 | static int openserial(const char * tty, int blocksz, int baud);
|
---|
[23] | 136 | static void send_receive_loop(int sock, int fd, int sisnet);
|
---|
[22] | 137 | static void usage(int);
|
---|
[32] | 138 | static int encode(char *buf, int size, const char *user, const char *pwd);
|
---|
[5] | 139 |
|
---|
[30] | 140 | #ifdef __GNUC__
|
---|
| 141 | static __attribute__ ((noreturn)) void sighandler_alarm(
|
---|
| 142 | int sig __attribute__((__unused__)))
|
---|
| 143 | #else /* __GNUC__ */
|
---|
| 144 | static void sighandler_alarm(int sig)
|
---|
| 145 | #endif /* __GNUC__ */
|
---|
[23] | 146 | {
|
---|
| 147 | fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
|
---|
| 148 | exit(1);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[5] | 151 | /*
|
---|
| 152 | * main
|
---|
| 153 | *
|
---|
| 154 | * Main entry point for the program. Processes command-line arguments and
|
---|
| 155 | * prepares for action.
|
---|
| 156 | *
|
---|
| 157 | * Parameters:
|
---|
| 158 | * argc : integer : Number of command-line arguments.
|
---|
[32] | 159 | * argv : array of char : Command-line arguments as an array of
|
---|
| 160 | * zero-terminated pointers to strings.
|
---|
[5] | 161 | *
|
---|
| 162 | * Return Value:
|
---|
| 163 | * The function does not return a value (although its return type is int).
|
---|
| 164 | *
|
---|
| 165 | * Remarks:
|
---|
| 166 | *
|
---|
| 167 | */
|
---|
| 168 |
|
---|
[18] | 169 | int main(int argc, char **argv)
|
---|
[5] | 170 | {
|
---|
[23] | 171 | int c;
|
---|
[18] | 172 | int size = 2048; /* for setting send buffer size */
|
---|
| 173 |
|
---|
[23] | 174 | const char *inhost = 0;
|
---|
| 175 | const char *outhost = 0;
|
---|
| 176 | unsigned int outport = 0;
|
---|
| 177 | unsigned int inport = 0;
|
---|
[22] | 178 | const char *mountpoint = NULL;
|
---|
| 179 | const char *password = "";
|
---|
[23] | 180 | const char *sisnetpassword = "";
|
---|
| 181 | const char *sisnetuser = "";
|
---|
[32] | 182 |
|
---|
| 183 | const char *stream_name=0;
|
---|
| 184 | const char *stream_user=0;
|
---|
| 185 | const char *stream_password=0;
|
---|
| 186 |
|
---|
[22] | 187 | const char *initfile = NULL;
|
---|
[24] | 188 | int bindmode = 0;
|
---|
[18] | 189 | int sock_id;
|
---|
| 190 | char szSendBuffer[BUFSZ];
|
---|
| 191 | int nBufferBytes;
|
---|
[23] | 192 | struct hostent *he;
|
---|
| 193 | struct sockaddr_in addr;
|
---|
[5] | 194 |
|
---|
[23] | 195 | signal(SIGALRM,sighandler_alarm);
|
---|
| 196 | alarm(ALARMTIME);
|
---|
[18] | 197 | /* get and check program arguments */
|
---|
| 198 | if(argc <= 1)
|
---|
| 199 | {
|
---|
| 200 | usage(2);
|
---|
| 201 | exit(1);
|
---|
| 202 | }
|
---|
[32] | 203 | while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:f:l:u:V:D:U:W:B"))
|
---|
| 204 | != EOF)
|
---|
[18] | 205 | {
|
---|
| 206 | switch (c)
|
---|
[5] | 207 | {
|
---|
[18] | 208 | case 'M':
|
---|
[21] | 209 | if(!strcmp(optarg, "serial")) mode = 1;
|
---|
| 210 | else if(!strcmp(optarg, "tcpsocket")) mode = 2;
|
---|
| 211 | else if(!strcmp(optarg, "file")) mode = 3;
|
---|
[23] | 212 | else if(!strcmp(optarg, "sisnet")) mode = 4;
|
---|
| 213 | else if(!strcmp(optarg, "udpsocket")) mode = 5;
|
---|
[32] | 214 | else if(!strcmp(optarg, "caster")) mode = 6;
|
---|
[21] | 215 | else mode = atoi(optarg);
|
---|
[32] | 216 | if((mode == 0) || (mode > 6))
|
---|
[18] | 217 | {
|
---|
| 218 | fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
|
---|
| 219 | usage(-1);
|
---|
| 220 | }
|
---|
| 221 | break;
|
---|
[22] | 222 | case 'i': /* gps serial ttyport */
|
---|
| 223 | ttyport = optarg;
|
---|
[18] | 224 | break;
|
---|
[24] | 225 | case 'B':
|
---|
| 226 | bindmode = 1;
|
---|
| 227 | break;
|
---|
[23] | 228 | case 'V':
|
---|
| 229 | if(!strcmp("3.0", optarg)) sisnetv3 = 1;
|
---|
| 230 | else if(strcmp("2.1", optarg))
|
---|
| 231 | {
|
---|
| 232 | fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
|
---|
| 233 | usage(-2);
|
---|
| 234 | }
|
---|
[18] | 235 | case 'b': /* serial ttyin speed */
|
---|
| 236 | ttybaud = atoi(optarg);
|
---|
| 237 | if(ttybaud <= 1)
|
---|
| 238 | {
|
---|
[32] | 239 | fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
|
---|
| 240 | optarg);
|
---|
[18] | 241 | usage(1);
|
---|
| 242 | }
|
---|
| 243 | break;
|
---|
| 244 | case 'a': /* http server IP address A.B.C.D */
|
---|
[23] | 245 | outhost = optarg;
|
---|
[18] | 246 | break;
|
---|
| 247 | case 'p': /* http server port */
|
---|
[23] | 248 | outport = atoi(optarg);
|
---|
| 249 | if(outport <= 1 || outport > 65535)
|
---|
[18] | 250 | {
|
---|
[32] | 251 | fprintf(stderr,
|
---|
| 252 | "ERROR: can't convert %s to a valid HTTP server port\n", optarg);
|
---|
[18] | 253 | usage(1);
|
---|
| 254 | }
|
---|
| 255 | break;
|
---|
| 256 | case 'm': /* http server mountpoint */
|
---|
| 257 | mountpoint = optarg;
|
---|
| 258 | break;
|
---|
[21] | 259 | case 's': /* datastream from file */
|
---|
[18] | 260 | filepath = optarg;
|
---|
| 261 | break;
|
---|
[21] | 262 | case 'f':
|
---|
| 263 | initfile = optarg;
|
---|
| 264 | break;
|
---|
[23] | 265 | case 'u':
|
---|
| 266 | sisnetuser = optarg;
|
---|
| 267 | break;
|
---|
| 268 | case 'l':
|
---|
| 269 | sisnetpassword = optarg;
|
---|
| 270 | break;
|
---|
[18] | 271 | case 'c': /* password */
|
---|
| 272 | password = optarg;
|
---|
| 273 | break;
|
---|
| 274 | case 'H': /* host */
|
---|
[23] | 275 | inhost = optarg;
|
---|
[18] | 276 | break;
|
---|
| 277 | case 'P': /* port */
|
---|
[23] | 278 | inport = atoi(optarg);
|
---|
| 279 | if(inport <= 1 || inport > 65535)
|
---|
[18] | 280 | {
|
---|
[23] | 281 | fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
|
---|
[18] | 282 | optarg);
|
---|
| 283 | usage(1);
|
---|
| 284 | }
|
---|
| 285 | break;
|
---|
[32] | 286 | case 'D':
|
---|
| 287 | stream_name=optarg; /* desired stream from SourceCaster */
|
---|
| 288 | break;
|
---|
| 289 | case 'U':
|
---|
| 290 | stream_user=optarg; /* username for desired stream */
|
---|
| 291 | break;
|
---|
| 292 | case 'W':
|
---|
| 293 | stream_password=optarg; /* passwd for desired stream */
|
---|
| 294 | break;
|
---|
[18] | 295 | case 'h': /* help */
|
---|
| 296 | case '?':
|
---|
| 297 | usage(0);
|
---|
| 298 | break;
|
---|
| 299 | default:
|
---|
[5] | 300 | usage(2);
|
---|
[18] | 301 | break;
|
---|
[5] | 302 | }
|
---|
[18] | 303 | }
|
---|
| 304 |
|
---|
[5] | 305 | argc -= optind;
|
---|
| 306 | argv += optind;
|
---|
[18] | 307 |
|
---|
| 308 | if(argc > 0)
|
---|
| 309 | {
|
---|
| 310 | fprintf(stderr, "ERROR: Extra args on command line: ");
|
---|
| 311 | for(; argc > 0; argc--)
|
---|
[5] | 312 | {
|
---|
[18] | 313 | fprintf(stderr, " %s", *argv++);
|
---|
[5] | 314 | }
|
---|
[18] | 315 | fprintf(stderr, "\n");
|
---|
| 316 | usage(1); /* never returns */
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | if(mountpoint == NULL)
|
---|
| 320 | {
|
---|
| 321 | fprintf(stderr, "ERROR: Missing mountpoint argument\n");
|
---|
| 322 | exit(1);
|
---|
| 323 | }
|
---|
| 324 | if(!password[0])
|
---|
| 325 | {
|
---|
[32] | 326 | fprintf(stderr,
|
---|
| 327 | "WARNING: Missing password argument - are you really sure?\n");
|
---|
[18] | 328 | }
|
---|
| 329 |
|
---|
[32] | 330 | if(stream_name && (!stream_user || stream_password))
|
---|
| 331 | {
|
---|
| 332 | fprintf(stderr, "WARNING: Missing password argument for download"
|
---|
| 333 | " - are you really sure?\n");
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[23] | 336 | if(!outhost) outhost = NTRIP_CASTER;
|
---|
| 337 | if(!outport) outport = NTRIP_PORT;
|
---|
| 338 |
|
---|
| 339 | switch(mode)
|
---|
[18] | 340 | {
|
---|
| 341 | case INFILE:
|
---|
[5] | 342 | {
|
---|
[18] | 343 | gpsfd = open(filepath, O_RDONLY);
|
---|
| 344 | if(!gpsfd)
|
---|
| 345 | {
|
---|
| 346 | perror("ERROR: opening input file");
|
---|
| 347 | exit(1);
|
---|
| 348 | }
|
---|
[32] | 349 | /* set blocking mode in case it was not set
|
---|
| 350 | (seems to be sometimes for fifo's) */
|
---|
[18] | 351 | fcntl(gpsfd, F_SETFL, 0);
|
---|
| 352 | printf("file input: file = %s\n", filepath);
|
---|
[5] | 353 | }
|
---|
[18] | 354 | break;
|
---|
| 355 | case SERIAL: /* open serial port */
|
---|
[5] | 356 | {
|
---|
[22] | 357 | gpsfd = openserial(ttyport, 1, ttybaud);
|
---|
[18] | 358 | if(gpsfd < 0)
|
---|
| 359 | {
|
---|
| 360 | exit(1);
|
---|
| 361 | }
|
---|
[22] | 362 | printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
|
---|
[5] | 363 | }
|
---|
[18] | 364 | break;
|
---|
[32] | 365 | case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
|
---|
[5] | 366 | {
|
---|
[23] | 367 | if(mode == SISNET)
|
---|
| 368 | {
|
---|
| 369 | if(!inhost) inhost = SISNET_SERVER;
|
---|
| 370 | if(!inport) inport = SISNET_PORT;
|
---|
| 371 | }
|
---|
[32] | 372 | else if(mode == CASTER)
|
---|
[23] | 373 | {
|
---|
[32] | 374 | if(!inport) inport = NTRIP_PORT;
|
---|
| 375 | if(!inhost) inhost = NTRIP_CASTER;
|
---|
| 376 | }
|
---|
| 377 | else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
|
---|
| 378 | {
|
---|
[23] | 379 | if(!inport) inport = SERV_TCP_PORT;
|
---|
| 380 | if(!inhost) inhost = "127.0.0.1";
|
---|
[32] | 381 | }
|
---|
[18] | 382 |
|
---|
[23] | 383 | if(!(he = gethostbyname(inhost)))
|
---|
[5] | 384 | {
|
---|
[23] | 385 | fprintf(stderr, "ERROR: host %s unknown\n", inhost);
|
---|
| 386 | usage(-2);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[32] | 389 | if((gpsfd = socket(AF_INET, mode == UDPSOCKET
|
---|
| 390 | ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
|
---|
[23] | 391 | {
|
---|
[18] | 392 | fprintf(stderr, "ERROR: can't create socket\n");
|
---|
| 393 | exit(1);
|
---|
[5] | 394 | }
|
---|
[18] | 395 |
|
---|
[23] | 396 | memset((char *) &addr, 0x00, sizeof(addr));
|
---|
[24] | 397 | if(!bindmode)
|
---|
| 398 | memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
|
---|
[23] | 399 | addr.sin_family = AF_INET;
|
---|
| 400 | addr.sin_port = htons(inport);
|
---|
| 401 |
|
---|
[32] | 402 | printf("%s input: host = %s, port = %d, %s%s%s%s%s\n",
|
---|
| 403 | mode == CASTER ? "caster" : mode == SISNET ? "sisnet" :
|
---|
| 404 | mode == TCPSOCKET ? "tcp socket" : "udp socket",
|
---|
| 405 | bindmode ? "127.0.0.1" : inet_ntoa(addr.sin_addr),
|
---|
| 406 | inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
|
---|
| 407 | initfile ? ", initfile = " : "", initfile ? initfile : "",
|
---|
| 408 | bindmode ? " binding mode" : "");
|
---|
[18] | 409 |
|
---|
[24] | 410 | if(bindmode)
|
---|
[5] | 411 | {
|
---|
[24] | 412 | if(bind(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
|
---|
| 413 | {
|
---|
| 414 | fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
|
---|
| 415 | exit(1);
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | else if(connect(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
|
---|
| 419 | {
|
---|
[18] | 420 | fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
|
---|
[23] | 421 | inet_ntoa(addr.sin_addr), inport);
|
---|
[18] | 422 | exit(1);
|
---|
[5] | 423 | }
|
---|
[32] | 424 |
|
---|
| 425 | if(stream_name) /* data stream from caster */
|
---|
| 426 | {
|
---|
| 427 | int init = 0;
|
---|
| 428 |
|
---|
| 429 | /* set socket buffer size */
|
---|
| 430 | setsockopt(gpsfd, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
|
---|
| 431 | sizeof(const char *));
|
---|
| 432 | if(stream_user && stream_password)
|
---|
| 433 | {
|
---|
| 434 | /* leave some space for login */
|
---|
| 435 | nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
|
---|
| 436 | "GET /%s HTTP/1.0\r\n"
|
---|
| 437 | "User-Agent: %s\r\n"
|
---|
| 438 | "Authorization: Basic ", stream_name, VERSION);
|
---|
| 439 | /* second check for old glibc */
|
---|
| 440 | if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
|
---|
| 441 | {
|
---|
| 442 | fprintf(stderr, "Requested data too long\n");
|
---|
| 443 | exit(1);
|
---|
| 444 | }
|
---|
| 445 | nBufferBytes += encode(szSendBuffer+nBufferBytes,
|
---|
| 446 | sizeof(szSendBuffer)-nBufferBytes-5, stream_user, stream_password);
|
---|
| 447 | if(nBufferBytes > (int)sizeof(szSendBuffer)-5)
|
---|
| 448 | {
|
---|
| 449 | fprintf(stderr, "Username and/or password too long\n");
|
---|
| 450 | exit(1);
|
---|
| 451 | }
|
---|
| 452 | snprintf(szSendBuffer+nBufferBytes, 5, "\r\n\r\n");
|
---|
| 453 | nBufferBytes += 5;
|
---|
| 454 | }
|
---|
| 455 | else
|
---|
| 456 | {
|
---|
| 457 | nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
|
---|
| 458 | "GET /%s HTTP/1.0\r\n"
|
---|
| 459 | "User-Agent: %s\r\n"
|
---|
| 460 | "\r\n", stream_name, VERSION);
|
---|
| 461 | }
|
---|
| 462 | if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
|
---|
| 463 | != nBufferBytes)
|
---|
| 464 | {
|
---|
| 465 | fprintf(stderr, "ERROR: could not send to caster\n");
|
---|
| 466 | exit(1);
|
---|
| 467 | }
|
---|
| 468 | nBufferBytes = 0;
|
---|
| 469 | /* check caster's response */
|
---|
| 470 | while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
|
---|
| 471 | && (nBufferBytes += recv(gpsfd, szSendBuffer,
|
---|
| 472 | sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
|
---|
| 473 | {
|
---|
| 474 | if(strstr(szSendBuffer, "\r\n"))
|
---|
| 475 | {
|
---|
| 476 | if(!strncmp(szSendBuffer, "ICY 200 OK\r\n", 10))
|
---|
| 477 | init = 1;
|
---|
| 478 | else
|
---|
| 479 | {
|
---|
| 480 | int k;
|
---|
| 481 | fprintf(stderr, "Could not get the requested data: ");
|
---|
| 482 | for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
|
---|
| 483 | && szSendBuffer[k] != '\r'; ++k)
|
---|
| 484 | {
|
---|
| 485 | fprintf(stderr, "%c", isprint(szSendBuffer[k])
|
---|
| 486 | ? szSendBuffer[k] : '.');
|
---|
| 487 | }
|
---|
| 488 | fprintf(stderr, "\n");
|
---|
| 489 | exit(1);
|
---|
| 490 | }
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | if(!init)
|
---|
| 494 | {
|
---|
| 495 | fprintf(stderr, "Could not init caster download.");
|
---|
| 496 | exit(1);
|
---|
| 497 | }
|
---|
| 498 | } /* end data stream from caster */
|
---|
| 499 |
|
---|
[23] | 500 | if(initfile && mode != SISNET)
|
---|
[21] | 501 | {
|
---|
| 502 | char buffer[1024];
|
---|
| 503 | FILE *fh;
|
---|
| 504 | int i;
|
---|
| 505 |
|
---|
| 506 | if((fh = fopen(initfile, "r")))
|
---|
| 507 | {
|
---|
| 508 | while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
|
---|
| 509 | {
|
---|
[22] | 510 | if((send(gpsfd, buffer, (size_t)i, 0)) != i)
|
---|
[21] | 511 | {
|
---|
| 512 | perror("ERROR: sending init file");
|
---|
| 513 | exit(1);
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 | if(i < 0)
|
---|
| 517 | {
|
---|
| 518 | perror("ERROR: reading init file");
|
---|
| 519 | exit(1);
|
---|
| 520 | }
|
---|
| 521 | fclose(fh);
|
---|
| 522 | }
|
---|
| 523 | else
|
---|
| 524 | {
|
---|
| 525 | fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
|
---|
| 526 | exit(1);
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
[5] | 529 | }
|
---|
[23] | 530 | if(mode == SISNET)
|
---|
| 531 | {
|
---|
| 532 | int i, j;
|
---|
| 533 | char buffer[1024];
|
---|
| 534 |
|
---|
[32] | 535 | i = snprintf(buffer, sizeof(buffer), sisnetv3 ? "AUTH,%s,%s\r\n"
|
---|
| 536 | : "AUTH,%s,%s", sisnetuser, sisnetpassword);
|
---|
[23] | 537 | if((send(gpsfd, buffer, (size_t)i, 0)) != i)
|
---|
| 538 | {
|
---|
| 539 | perror("ERROR: sending authentication");
|
---|
| 540 | exit(1);
|
---|
| 541 | }
|
---|
| 542 | i = sisnetv3 ? 7 : 5;
|
---|
| 543 | if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
|
---|
| 544 | {
|
---|
| 545 | fprintf(stderr, "ERROR: SISNeT connect failed:");
|
---|
| 546 | for(i = 0; i < j; ++i)
|
---|
| 547 | {
|
---|
| 548 | if(buffer[i] != '\r' && buffer[i] != '\n')
|
---|
| 549 | {
|
---|
| 550 | fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
| 553 | fprintf(stderr, "\n");
|
---|
| 554 | exit(1);
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
[18] | 557 | break;
|
---|
| 558 | default:
|
---|
| 559 | usage(-1);
|
---|
| 560 | break;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /* ----- main part ----- */
|
---|
[5] | 564 | while(1)
|
---|
[18] | 565 | {
|
---|
[23] | 566 | if(!(he = gethostbyname(outhost)))
|
---|
| 567 | {
|
---|
| 568 | fprintf(stderr, "ERROR: host %s unknown\n", outhost);
|
---|
| 569 | usage(-2);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[18] | 572 | /* create socket */
|
---|
| 573 | if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
---|
[5] | 574 | {
|
---|
[23] | 575 | fprintf(stderr, "ERROR: could not create socket\n");
|
---|
[18] | 576 | exit(2);
|
---|
[5] | 577 | }
|
---|
[23] | 578 |
|
---|
| 579 | memset((char *) &addr, 0x00, sizeof(addr));
|
---|
| 580 | memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
|
---|
| 581 | addr.sin_family = AF_INET;
|
---|
| 582 | addr.sin_port = htons(outport);
|
---|
| 583 |
|
---|
[18] | 584 | /* connect to caster */
|
---|
| 585 | fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
|
---|
[23] | 586 | inet_ntoa(addr.sin_addr), outport, mountpoint);
|
---|
| 587 | if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
|
---|
[18] | 588 | {
|
---|
| 589 | fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
|
---|
[23] | 590 | inet_ntoa(addr.sin_addr), outport);
|
---|
[18] | 591 | close(sock_id);
|
---|
| 592 | exit(3);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /* set socket buffer size */
|
---|
| 596 | setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
|
---|
| 597 | sizeof(const char *));
|
---|
| 598 | /* send message to caster */
|
---|
| 599 | szSendBuffer[0] = '\0';
|
---|
| 600 | sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
|
---|
| 601 | strcat(szSendBuffer, "Source-Agent: ");
|
---|
| 602 | strcat(szSendBuffer, VERSION);
|
---|
| 603 | strcat(szSendBuffer, "\r\n");
|
---|
| 604 | strcat(szSendBuffer, "\r\n");
|
---|
| 605 | strcat(szSendBuffer, "\0");
|
---|
| 606 | nBufferBytes = strlen(szSendBuffer);
|
---|
[22] | 607 | if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
|
---|
[18] | 608 | {
|
---|
| 609 | fprintf(stderr, "ERROR: could not send to caster\n");
|
---|
| 610 | close(sock_id);
|
---|
| 611 | sleep(5);
|
---|
| 612 | exit(0);
|
---|
| 613 | }
|
---|
| 614 | /* check caster's response */
|
---|
| 615 | nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
|
---|
| 616 | szSendBuffer[nBufferBytes] = '\0';
|
---|
| 617 | if(strcmp(szSendBuffer, "OK\r\n"))
|
---|
| 618 | {
|
---|
| 619 | char *a;
|
---|
| 620 | fprintf(stderr, "ERROR: caster's reply is not OK : ");
|
---|
| 621 | for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
|
---|
| 622 | {
|
---|
| 623 | fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
|
---|
| 624 | }
|
---|
| 625 | fprintf(stderr, "\n");
|
---|
| 626 | close(sock_id);
|
---|
| 627 | sleep(5);
|
---|
| 628 | exit(0);
|
---|
| 629 | }
|
---|
[24] | 630 | printf("connection successfull\n");
|
---|
[23] | 631 | send_receive_loop(sock_id, gpsfd, mode == SISNET);
|
---|
[18] | 632 | }
|
---|
[5] | 633 | exit(0);
|
---|
| 634 | }
|
---|
| 635 |
|
---|
[23] | 636 | static void send_receive_loop(int sock, int fd, int sisnet)
|
---|
[5] | 637 | {
|
---|
[18] | 638 | char buffer[BUFSZ] = { 0 };
|
---|
[23] | 639 | char sisnetbackbuffer[200];
|
---|
[30] | 640 | int nBufferBytes = 0;
|
---|
[18] | 641 | /* data transmission */
|
---|
[5] | 642 | printf("transfering data ...\n");
|
---|
| 643 | while(1)
|
---|
[18] | 644 | {
|
---|
[23] | 645 | alarm(ALARMTIME);
|
---|
| 646 |
|
---|
[18] | 647 | if(!nBufferBytes)
|
---|
[5] | 648 | {
|
---|
[23] | 649 | if(sisnet)
|
---|
| 650 | {
|
---|
| 651 | int i;
|
---|
| 652 | /* a somewhat higher rate than 1 second to get really each block */
|
---|
| 653 | /* means we need to skip double blocks sometimes */
|
---|
| 654 | struct timeval tv = {0,700000};
|
---|
| 655 | select(0, 0, 0, 0, &tv);
|
---|
| 656 | memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
|
---|
| 657 | i = (sisnetv3 ? 5 : 3);
|
---|
| 658 | if((send(gpsfd, "MSG\r\n", i, 0)) != i)
|
---|
| 659 | {
|
---|
| 660 | perror("ERROR: sending data request");
|
---|
| 661 | exit(1);
|
---|
| 662 | }
|
---|
| 663 | }
|
---|
[18] | 664 | /* receiving data */
|
---|
[32] | 665 | nBufferBytes = read(fd, buffer, sizeof(buffer));
|
---|
[18] | 666 | if(!nBufferBytes)
|
---|
| 667 | {
|
---|
| 668 | printf("WARNING: no data received from input\n");
|
---|
| 669 | continue;
|
---|
| 670 | }
|
---|
| 671 | else if(nBufferBytes < 0)
|
---|
| 672 | {
|
---|
| 673 | perror("ERROR: reading input failed");
|
---|
| 674 | exit(1);
|
---|
| 675 | }
|
---|
[32] | 676 | /* we can compare the whole buffer, as the additional bytes
|
---|
| 677 | remain unchanged */
|
---|
| 678 | if(sisnet && !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
|
---|
[23] | 679 | {
|
---|
| 680 | nBufferBytes = 0;
|
---|
| 681 | }
|
---|
[5] | 682 | }
|
---|
[23] | 683 | if(nBufferBytes)
|
---|
[18] | 684 | {
|
---|
[30] | 685 | int i;
|
---|
[23] | 686 | /* send data */
|
---|
| 687 | if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
|
---|
[32] | 688 | != nBufferBytes)
|
---|
[18] | 689 | {
|
---|
[34] | 690 | if(i < 0)
|
---|
[23] | 691 | {
|
---|
[34] | 692 | if(errno != EAGAIN)
|
---|
| 693 | {
|
---|
| 694 | perror("WARNING: could not send data - retry connection");
|
---|
| 695 | close(sock);
|
---|
| 696 | sleep(5);
|
---|
| 697 | return;
|
---|
| 698 | }
|
---|
[23] | 699 | }
|
---|
| 700 | else if(i)
|
---|
| 701 | {
|
---|
| 702 | memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
|
---|
| 703 | nBufferBytes -= i;
|
---|
| 704 | }
|
---|
[18] | 705 | }
|
---|
[23] | 706 | else
|
---|
[18] | 707 | {
|
---|
[23] | 708 | nBufferBytes = 0;
|
---|
[18] | 709 | }
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
[5] | 712 | }
|
---|
| 713 |
|
---|
| 714 | /*
|
---|
| 715 | * openserial
|
---|
| 716 | *
|
---|
| 717 | * Open the serial port with the given device name and configure it for
|
---|
| 718 | * reading NMEA data from a GPS receiver.
|
---|
| 719 | *
|
---|
| 720 | * Parameters:
|
---|
| 721 | * tty : pointer to : A zero-terminated string containing the device
|
---|
| 722 | * unsigned char name of the appropriate serial port.
|
---|
| 723 | * blocksz : integer : Block size for port I/O
|
---|
[22] | 724 | * baud : integer : Baud rate for port I/O
|
---|
[5] | 725 | *
|
---|
| 726 | * Return Value:
|
---|
| 727 | * The function returns a file descriptor for the opened port if successful.
|
---|
| 728 | * The function returns -1 in the event of an error.
|
---|
| 729 | *
|
---|
| 730 | * Remarks:
|
---|
| 731 | *
|
---|
| 732 | */
|
---|
| 733 |
|
---|
[22] | 734 | static int openserial(const char * tty, int blocksz, int baud)
|
---|
[5] | 735 | {
|
---|
[18] | 736 | int fd;
|
---|
| 737 | struct termios termios;
|
---|
| 738 |
|
---|
| 739 | fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
|
---|
| 740 | if(fd < 0)
|
---|
| 741 | {
|
---|
| 742 | perror("ERROR: opening serial connection");
|
---|
[5] | 743 | return (-1);
|
---|
| 744 | }
|
---|
[18] | 745 | if(tcgetattr(fd, &termios) < 0)
|
---|
| 746 | {
|
---|
| 747 | perror("ERROR: get serial attributes");
|
---|
[5] | 748 | return (-1);
|
---|
| 749 | }
|
---|
| 750 | termios.c_iflag = 0;
|
---|
[18] | 751 | termios.c_oflag = 0; /* (ONLRET) */
|
---|
[5] | 752 | termios.c_cflag = CS8 | CLOCAL | CREAD;
|
---|
| 753 | termios.c_lflag = 0;
|
---|
| 754 | {
|
---|
[18] | 755 | int cnt;
|
---|
| 756 | for(cnt = 0; cnt < NCCS; cnt++)
|
---|
[5] | 757 | termios.c_cc[cnt] = -1;
|
---|
| 758 | }
|
---|
| 759 | termios.c_cc[VMIN] = blocksz;
|
---|
| 760 | termios.c_cc[VTIME] = 2;
|
---|
[18] | 761 |
|
---|
[5] | 762 | #if (B4800 != 4800)
|
---|
| 763 | /*
|
---|
[11] | 764 | * Not every system has speed settings equal to absolute speed value.
|
---|
[5] | 765 | */
|
---|
[18] | 766 |
|
---|
[22] | 767 | switch (baud)
|
---|
[18] | 768 | {
|
---|
| 769 | case 300:
|
---|
[22] | 770 | baud = B300;
|
---|
[18] | 771 | break;
|
---|
| 772 | case 1200:
|
---|
[22] | 773 | baud = B1200;
|
---|
[18] | 774 | break;
|
---|
| 775 | case 2400:
|
---|
[22] | 776 | baud = B2400;
|
---|
[18] | 777 | break;
|
---|
| 778 | case 4800:
|
---|
[22] | 779 | baud = B4800;
|
---|
[18] | 780 | break;
|
---|
| 781 | case 9600:
|
---|
[22] | 782 | baud = B9600;
|
---|
[18] | 783 | break;
|
---|
| 784 | case 19200:
|
---|
[22] | 785 | baud = B19200;
|
---|
[18] | 786 | break;
|
---|
| 787 | case 38400:
|
---|
[22] | 788 | baud = B38400;
|
---|
[18] | 789 | break;
|
---|
[11] | 790 | #ifdef B57600
|
---|
[18] | 791 | case 57600:
|
---|
[22] | 792 | baud = B57600;
|
---|
[18] | 793 | break;
|
---|
[11] | 794 | #endif
|
---|
| 795 | #ifdef B115200
|
---|
[18] | 796 | case 115200:
|
---|
[22] | 797 | baud = B115200;
|
---|
[18] | 798 | break;
|
---|
[11] | 799 | #endif
|
---|
| 800 | #ifdef B230400
|
---|
[18] | 801 | case 230400:
|
---|
[22] | 802 | baud = B230400;
|
---|
[18] | 803 | break;
|
---|
[11] | 804 | #endif
|
---|
[18] | 805 | default:
|
---|
| 806 | fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
|
---|
[22] | 807 | baud = B19200;
|
---|
[18] | 808 | break;
|
---|
| 809 | }
|
---|
[5] | 810 | #endif
|
---|
[18] | 811 |
|
---|
[22] | 812 | if(cfsetispeed(&termios, baud) != 0)
|
---|
[18] | 813 | {
|
---|
| 814 | perror("ERROR: setting serial speed with cfsetispeed");
|
---|
| 815 | return (-1);
|
---|
| 816 | }
|
---|
[22] | 817 | if(cfsetospeed(&termios, baud) != 0)
|
---|
[18] | 818 | {
|
---|
| 819 | perror("ERROR: setting serial speed with cfsetospeed");
|
---|
| 820 | return (-1);
|
---|
| 821 | }
|
---|
| 822 | if(tcsetattr(fd, TCSANOW, &termios) < 0)
|
---|
| 823 | {
|
---|
| 824 | perror("ERROR: setting serial attributes");
|
---|
| 825 | return (-1);
|
---|
| 826 | }
|
---|
| 827 | if(fcntl(fd, F_SETFL, 0) == -1)
|
---|
| 828 | {
|
---|
| 829 | perror("WARNING: setting blocking mode failed");
|
---|
| 830 | }
|
---|
[5] | 831 | return (fd);
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 | /*
|
---|
| 835 | * usage
|
---|
| 836 | *
|
---|
| 837 | * Send a usage message to standard error and quit the program.
|
---|
| 838 | *
|
---|
| 839 | * Parameters:
|
---|
| 840 | * None.
|
---|
| 841 | *
|
---|
| 842 | * Return Value:
|
---|
| 843 | * The function does not return a value.
|
---|
| 844 | *
|
---|
| 845 | * Remarks:
|
---|
| 846 | *
|
---|
| 847 | */
|
---|
| 848 |
|
---|
[30] | 849 | static
|
---|
| 850 | #ifdef __GNUC__
|
---|
| 851 | __attribute__ ((noreturn))
|
---|
| 852 | #endif /* __GNUC__ */
|
---|
| 853 | void usage(int rc)
|
---|
[5] | 854 | {
|
---|
[18] | 855 | fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
|
---|
[32] | 856 | fprintf(stderr, " Options are: [-] \n");
|
---|
| 857 | fprintf(stderr, " -a DestinationCaster name or address (default: %s)\n",
|
---|
[18] | 858 | NTRIP_CASTER);
|
---|
[32] | 859 | fprintf(stderr, " -p DestinationCaster port (default: %d)\n", NTRIP_PORT);
|
---|
| 860 | fprintf(stderr, " -m DestinationCaster mountpoint\n");
|
---|
| 861 | fprintf(stderr, " -c DestinationCaster password\n");
|
---|
[18] | 862 | fprintf(stderr, " -h|? print this help screen\n");
|
---|
[32] | 863 | fprintf(stderr, " -M <mode> sets the input mode\n");
|
---|
| 864 | fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file, 4=sisnet"
|
---|
| 865 | ", 5=udpsocket, 6=caster)\n");
|
---|
[18] | 866 | fprintf(stderr, " Mode = file:\n");
|
---|
| 867 | fprintf(stderr, " -s file, simulate data stream by reading log file\n");
|
---|
| 868 | fprintf(stderr, " default/current setting is %s\n", filepath);
|
---|
| 869 | fprintf(stderr, " Mode = serial:\n");
|
---|
| 870 | fprintf(stderr, " -b baud_rate, sets serial input baud rate\n");
|
---|
| 871 | fprintf(stderr, " default/current value is %d\n", ttybaud);
|
---|
| 872 | fprintf(stderr, " -i input_device, sets name of serial input device\n");
|
---|
| 873 | fprintf(stderr, " default/current value is %s\n", ttyport);
|
---|
| 874 | fprintf(stderr, " (normally a symbolic link to /dev/tty\?\?)\n");
|
---|
[23] | 875 | fprintf(stderr, " Mode = tcpsocket or udpsocket:\n");
|
---|
| 876 | fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT);
|
---|
[32] | 877 | fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
|
---|
| 878 | SERV_HOST_ADDR);
|
---|
[21] | 879 | fprintf(stderr, " -f initfile send to server\n");
|
---|
[32] | 880 | fprintf(stderr, " -B bindmode: bind to incoming UDP stream\n");
|
---|
[23] | 881 | fprintf(stderr, " Mode = sisnet:\n");
|
---|
| 882 | fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT);
|
---|
[32] | 883 | fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
|
---|
| 884 | SISNET_SERVER);
|
---|
[23] | 885 | fprintf(stderr, " -u username\n");
|
---|
| 886 | fprintf(stderr, " -l password\n");
|
---|
| 887 | fprintf(stderr, " -V version [2.1 or 3.0] (default: 2.1)\n");
|
---|
[32] | 888 | fprintf(stderr, " Mode = caster:\n");
|
---|
| 889 | fprintf(stderr, " -P SourceCaster port (default: %d)\n", NTRIP_PORT);
|
---|
| 890 | fprintf(stderr, " -H SourceCaster hostname (default: %s)\n",
|
---|
| 891 | NTRIP_CASTER);
|
---|
| 892 | fprintf(stderr, " -D SourceCaster mountpoint\n");
|
---|
| 893 | fprintf(stderr, " -U SourceCaster mountpoint username\n");
|
---|
| 894 | fprintf(stderr, " -W SourceCaster mountpoint password\n");
|
---|
[23] | 895 | fprintf(stderr, "\n");
|
---|
[18] | 896 | exit(rc);
|
---|
[5] | 897 | }
|
---|
[32] | 898 |
|
---|
| 899 | static const char encodingTable [64] = {
|
---|
| 900 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
|
---|
| 901 | 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
|
---|
| 902 | 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
|
---|
| 903 | 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
|
---|
| 904 | };
|
---|
| 905 |
|
---|
| 906 | /* does not buffer overrun, but breaks directly after an error */
|
---|
| 907 | /* returns the number of required bytes */
|
---|
| 908 | static int encode(char *buf, int size, const char *user, const char *pwd)
|
---|
| 909 | {
|
---|
| 910 | unsigned char inbuf[3];
|
---|
| 911 | char *out = buf;
|
---|
| 912 | int i, sep = 0, fill = 0, bytes = 0;
|
---|
| 913 |
|
---|
| 914 | while(*user || *pwd)
|
---|
| 915 | {
|
---|
| 916 | i = 0;
|
---|
| 917 | while(i < 3 && *user) inbuf[i++] = *(user++);
|
---|
| 918 | if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
|
---|
| 919 | while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
|
---|
| 920 | while(i < 3) {inbuf[i++] = 0; ++fill; }
|
---|
| 921 | if(out-buf < size-1)
|
---|
| 922 | *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
|
---|
| 923 | if(out-buf < size-1)
|
---|
| 924 | *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
|
---|
| 925 | | ((inbuf [1] & 0xF0) >> 4)];
|
---|
| 926 | if(out-buf < size-1)
|
---|
| 927 | {
|
---|
| 928 | if(fill == 2)
|
---|
| 929 | *(out++) = '=';
|
---|
| 930 | else
|
---|
| 931 | *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
|
---|
| 932 | | ((inbuf [2] & 0xC0) >> 6)];
|
---|
| 933 | }
|
---|
| 934 | if(out-buf < size-1)
|
---|
| 935 | {
|
---|
| 936 | if(fill >= 1)
|
---|
| 937 | *(out++) = '=';
|
---|
| 938 | else
|
---|
| 939 | *(out++) = encodingTable[inbuf [2] & 0x3F];
|
---|
| 940 | }
|
---|
| 941 | bytes += 4;
|
---|
| 942 | }
|
---|
| 943 | if(out-buf < size)
|
---|
| 944 | *out = 0;
|
---|
| 945 | return bytes;
|
---|
| 946 | }
|
---|