- Timestamp:
- Apr 27, 2006, 11:44:27 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ntripserver/NtripLinuxServer.c
r30 r32 41 41 */ 42 42 43 /* $Id: NtripLinuxServer.c,v 1.1 3 2005/06/07 14:47:52stoecker Exp $43 /* $Id: NtripLinuxServer.c,v 1.12 2005/06/02 09:33:11 stoecker Exp $ 44 44 * Changes - Version 0.7 45 45 * Sep 22 2003 Steffen Tschirpke <St.Tschirpke@actina.de> … … 71 71 * Jun 07 2005 Dirk Stoecker <soft@dstoecker.de> 72 72 * - added UDP bindmode 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 * 73 82 */ 74 83 … … 96 105 #endif 97 106 98 enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5 }; 99 100 #define VERSION "NTRIP NtripServerLinux/0.12" 107 enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5, 108 CASTER = 6}; 109 110 #define VERSION "NTRIP NtripServerLinux/0.13" 101 111 #define BUFSZ 1024 102 112 … … 126 136 static void send_receive_loop(int sock, int fd, int sisnet); 127 137 static void usage(int); 138 static int encode(char *buf, int size, const char *user, const char *pwd); 128 139 129 140 #ifdef __GNUC__ … … 146 157 * Parameters: 147 158 * argc : integer : Number of command-line arguments. 148 * argv : array of char : Command-line arguments as an array of zero-terminated149 * pointers to strings.159 * argv : array of char : Command-line arguments as an array of 160 * zero-terminated pointers to strings. 150 161 * 151 162 * Return Value: … … 169 180 const char *sisnetpassword = ""; 170 181 const char *sisnetuser = ""; 182 183 const char *stream_name=0; 184 const char *stream_user=0; 185 const char *stream_password=0; 186 171 187 const char *initfile = NULL; 172 188 int bindmode = 0; … … 185 201 exit(1); 186 202 } 187 while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:f:l:u:V:B")) != EOF) 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) 188 205 { 189 206 switch (c) … … 195 212 else if(!strcmp(optarg, "sisnet")) mode = 4; 196 213 else if(!strcmp(optarg, "udpsocket")) mode = 5; 214 else if(!strcmp(optarg, "caster")) mode = 6; 197 215 else mode = atoi(optarg); 198 if((mode == 0) || (mode > 5))216 if((mode == 0) || (mode > 6)) 199 217 { 200 218 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg); … … 219 237 if(ttybaud <= 1) 220 238 { 221 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg); 239 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", 240 optarg); 222 241 usage(1); 223 242 } … … 230 249 if(outport <= 1 || outport > 65535) 231 250 { 232 fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",233 optarg);251 fprintf(stderr, 252 "ERROR: can't convert %s to a valid HTTP server port\n", optarg); 234 253 usage(1); 235 254 } … … 265 284 } 266 285 break; 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; 267 295 case 'h': /* help */ 268 296 case '?': … … 296 324 if(!password[0]) 297 325 { 298 fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n"); 326 fprintf(stderr, 327 "WARNING: Missing password argument - are you really sure?\n"); 328 } 329 330 if(stream_name && (!stream_user || stream_password)) 331 { 332 fprintf(stderr, "WARNING: Missing password argument for download" 333 " - are you really sure?\n"); 299 334 } 300 335 … … 312 347 exit(1); 313 348 } 314 /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */ 349 /* set blocking mode in case it was not set 350 (seems to be sometimes for fifo's) */ 315 351 fcntl(gpsfd, F_SETFL, 0); 316 352 printf("file input: file = %s\n", filepath); … … 327 363 } 328 364 break; 329 case TCPSOCKET: case UDPSOCKET: case SISNET: 365 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER: 330 366 { 331 367 if(mode == SISNET) … … 334 370 if(!inport) inport = SISNET_PORT; 335 371 } 336 else 372 else if(mode == CASTER) 373 { 374 if(!inport) inport = NTRIP_PORT; 375 if(!inhost) inhost = NTRIP_CASTER; 376 } 377 else if((mode == TCPSOCKET) || (mode == UDPSOCKET)) 337 378 { 338 379 if(!inport) inport = SERV_TCP_PORT; 339 380 if(!inhost) inhost = "127.0.0.1"; 340 } 381 } 341 382 342 383 if(!(he = gethostbyname(inhost))) … … 346 387 } 347 388 348 if((gpsfd = socket(AF_INET, mode == UDPSOCKET ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0) 389 if((gpsfd = socket(AF_INET, mode == UDPSOCKET 390 ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0) 349 391 { 350 392 fprintf(stderr, "ERROR: can't create socket\n"); … … 358 400 addr.sin_port = htons(inport); 359 401 360 printf("%s input: host = %s, port = %d%s%s%s\n", mode == SISNET ? "sisnet" 361 : mode == TCPSOCKET ? "tcp socket" : "udp socket", 362 bindmode ? "127.0.0.1" : inet_ntoa(addr.sin_addr), inport, 363 initfile ? ", initfile = " : "", 364 initfile ? initfile : "", bindmode ? " binding mode" : ""); 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" : ""); 365 409 366 410 if(bindmode) … … 378 422 exit(1); 379 423 } 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 380 500 if(initfile && mode != SISNET) 381 501 { … … 413 533 char buffer[1024]; 414 534 415 i = snprintf(buffer, sizeof(buffer), sisnetv3 ? "AUTH,%s,%s\r\n" : "AUTH,%s,%s",416 sisnetuser,sisnetpassword);535 i = snprintf(buffer, sizeof(buffer), sisnetv3 ? "AUTH,%s,%s\r\n" 536 : "AUTH,%s,%s", sisnetuser, sisnetpassword); 417 537 if((send(gpsfd, buffer, (size_t)i, 0)) != i) 418 538 { … … 543 663 } 544 664 /* receiving data */ 545 nBufferBytes = read(fd, buffer, BUFSZ);665 nBufferBytes = read(fd, buffer, sizeof(buffer)); 546 666 if(!nBufferBytes) 547 667 { … … 554 674 exit(1); 555 675 } 556 /* we can compare the whole buffer, as the additional bytes remain unchanged */ 557 if(!memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer))) 676 /* we can compare the whole buffer, as the additional bytes 677 remain unchanged */ 678 if(sisnet && !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer))) 558 679 { 559 680 nBufferBytes = 0; … … 565 686 /* send data */ 566 687 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT)) 567 != nBufferBytes)688 != nBufferBytes) 568 689 { 569 690 if(i < 0 && errno != EAGAIN) … … 730 851 { 731 852 fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION); 732 fprintf(stderr, " Options are: \n");733 fprintf(stderr, " -a caster name or address (default: %s)\n",853 fprintf(stderr, " Options are: [-] \n"); 854 fprintf(stderr, " -a DestinationCaster name or address (default: %s)\n", 734 855 NTRIP_CASTER); 735 fprintf(stderr, " -p caster port (default: %d)\n", NTRIP_PORT);736 fprintf(stderr, " -m caster mountpoint\n");737 fprintf(stderr, " -c password for caster login\n");856 fprintf(stderr, " -p DestinationCaster port (default: %d)\n", NTRIP_PORT); 857 fprintf(stderr, " -m DestinationCaster mountpoint\n"); 858 fprintf(stderr, " -c DestinationCaster password\n"); 738 859 fprintf(stderr, " -h|? print this help screen\n"); 739 fprintf(stderr, " -M <mode> sets the mode\n"); 740 fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file, 4=sisnet, 5=udpsocket)\n"); 860 fprintf(stderr, " -M <mode> sets the input mode\n"); 861 fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file, 4=sisnet" 862 ", 5=udpsocket, 6=caster)\n"); 741 863 fprintf(stderr, " Mode = file:\n"); 742 864 fprintf(stderr, " -s file, simulate data stream by reading log file\n"); … … 750 872 fprintf(stderr, " Mode = tcpsocket or udpsocket:\n"); 751 873 fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT); 752 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", SERV_HOST_ADDR); 874 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", 875 SERV_HOST_ADDR); 753 876 fprintf(stderr, " -f initfile send to server\n"); 754 fprintf(stderr, " -B bindmode (bind to incoming UDP stream)\n");877 fprintf(stderr, " -B bindmode: bind to incoming UDP stream\n"); 755 878 fprintf(stderr, " Mode = sisnet:\n"); 756 879 fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT); 757 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", SISNET_SERVER); 880 fprintf(stderr, " -H hostname of TCP server (default: %s)\n", 881 SISNET_SERVER); 758 882 fprintf(stderr, " -u username\n"); 759 883 fprintf(stderr, " -l password\n"); 760 884 fprintf(stderr, " -V version [2.1 or 3.0] (default: 2.1)\n"); 885 fprintf(stderr, " Mode = caster:\n"); 886 fprintf(stderr, " -P SourceCaster port (default: %d)\n", NTRIP_PORT); 887 fprintf(stderr, " -H SourceCaster hostname (default: %s)\n", 888 NTRIP_CASTER); 889 fprintf(stderr, " -D SourceCaster mountpoint\n"); 890 fprintf(stderr, " -U SourceCaster mountpoint username\n"); 891 fprintf(stderr, " -W SourceCaster mountpoint password\n"); 761 892 fprintf(stderr, "\n"); 762 893 exit(rc); 763 894 } 895 896 static const char encodingTable [64] = { 897 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 898 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 899 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 900 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' 901 }; 902 903 /* does not buffer overrun, but breaks directly after an error */ 904 /* returns the number of required bytes */ 905 static int encode(char *buf, int size, const char *user, const char *pwd) 906 { 907 unsigned char inbuf[3]; 908 char *out = buf; 909 int i, sep = 0, fill = 0, bytes = 0; 910 911 while(*user || *pwd) 912 { 913 i = 0; 914 while(i < 3 && *user) inbuf[i++] = *(user++); 915 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; } 916 while(i < 3 && *pwd) inbuf[i++] = *(pwd++); 917 while(i < 3) {inbuf[i++] = 0; ++fill; } 918 if(out-buf < size-1) 919 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2]; 920 if(out-buf < size-1) 921 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4) 922 | ((inbuf [1] & 0xF0) >> 4)]; 923 if(out-buf < size-1) 924 { 925 if(fill == 2) 926 *(out++) = '='; 927 else 928 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2) 929 | ((inbuf [2] & 0xC0) >> 6)]; 930 } 931 if(out-buf < size-1) 932 { 933 if(fill >= 1) 934 *(out++) = '='; 935 else 936 *(out++) = encodingTable[inbuf [2] & 0x3F]; 937 } 938 bytes += 4; 939 } 940 if(out-buf < size) 941 *out = 0; 942 return bytes; 943 }
Note:
See TracChangeset
for help on using the changeset viewer.