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