source: ntrip/trunk/ntripserver/NtripLinuxServer.c@ 398

Last change on this file since 398 was 398, checked in by stoecker, 17 years ago

updated version of BKG

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