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

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

added more version information in help view

File size: 28.5 KB
Line 
1/*
2 * NtripServerLinux.c
3 *
4 * Copyright (c) 2003...2007
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
43/*
44 * Changes - Version 0.7
45 * Sep 22 2003 Steffen Tschirpke <St.Tschirpke@actina.de>
46 * - socket support
47 * - command line option handling
48 * - error handling
49 * - help screen
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
61 * - Modes may be symbolic and not only numeric
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
69 *
70 * Changes - Version 0.12
71 * Jun 07 2005 Dirk Stoecker <soft@dstoecker.de>
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 * May 16 2006 Andrea Stuerze <andrea.stuerze@bkg.bund.de>
80 * - bug fixed in base64_encode-function
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>
92 * - fixed some problems with caster download
93 * - some minor cosmetic changes
94 *
95 * Changes - Version 0.18
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>
106 * - default port changed from 80 to 2101
107 * - fixed illegal memory access
108 * - cleanup of no data alarm timer
109 * - fixed zero byte handling in buffers
110 *
111 */
112
113#include <ctype.h>
114#include <errno.h>
115#include <fcntl.h>
116#include <getopt.h>
117#include <netdb.h>
118#include <signal.h>
119#include <stdio.h>
120#include <stdlib.h>
121#include <string.h>
122#include <unistd.h>
123#include <arpa/inet.h>
124#include <netinet/in.h>
125#include <sys/socket.h>
126#include <sys/termios.h>
127#include <sys/types.h>
128
129#ifndef COMPILEDATE
130#define COMPILEDATE " built " __DATE__
131#endif
132
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
139
140enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
141CASTER = 6, LAST};
142
143#define VERSION "NTRIP NtripServerLinux/0.20"
144#define BUFSZ 1024
145
146/* default socket source */
147#define SERV_HOST_ADDR "localhost"
148#define SERV_TCP_PORT 2101
149
150/* default destination */
151#define NTRIP_CASTER "www.euref-ip.net"
152#define NTRIP_PORT 2101
153
154/* default sisnet source */
155#define SISNET_SERVER "131.176.49.142"
156#define SISNET_PORT 7777
157
158#define ALARMTIME 60
159
160static int ttybaud = 19200;
161static const char *ttyport = "/dev/gps";
162static const char *filepath = "/dev/stdin";
163static enum MODE mode = INFILE;
164static int sisnet = 31;
165static int gpsfd = -1;
166
167/* Forward references */
168static int openserial(const char * tty, int blocksz, int baud);
169static void send_receive_loop(int sock, int fd);
170static void usage(int);
171static int encode(char *buf, int size, const char *user, const char *pwd);
172
173#ifdef __GNUC__
174static __attribute__ ((noreturn)) void sighandler_alarm(
175int sig __attribute__((__unused__)))
176#else /* __GNUC__ */
177static void sighandler_alarm(int sig)
178#endif /* __GNUC__ */
179{
180 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
181 exit(1);
182}
183
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.
192* argv : array of char : Command-line arguments as an array of
193* zero-terminated pointers to strings.
194*
195* Return Value:
196* The function does not return a value (although its return type is int).
197*
198* Remarks:
199*
200*/
201
202int main(int argc, char **argv)
203{
204 int c;
205 int size = 2048; /* for setting send buffer size */
206
207 const char *inhost = 0;
208 const char *outhost = 0;
209 unsigned int outport = 0;
210 unsigned int inport = 0;
211 const char *mountpoint = NULL;
212 const char *password = "";
213 const char *sisnetpassword = "";
214 const char *sisnetuser = "";
215
216 const char *stream_name=0;
217 const char *stream_user=0;
218 const char *stream_password=0;
219
220 const char *initfile = NULL;
221
222 const char *recvrid=0;
223 const char *recvrpwd=0;
224
225 int bindmode = 0;
226 int sock_id;
227 char szSendBuffer[BUFSZ];
228 int nBufferBytes;
229 struct hostent *he;
230 struct sockaddr_in addr;
231
232 signal(SIGALRM,sighandler_alarm);
233 alarm(ALARMTIME);
234 /* get and check program arguments */
235 if(argc <= 1)
236 {
237 usage(2);
238 exit(1);
239 }
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"))
241 != EOF)
242 {
243 switch (c)
244 {
245 case 'M':
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;
252 else mode = atoi(optarg);
253 if((mode == 0) || (mode >= LAST))
254 {
255 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
256 usage(-1);
257 }
258 break;
259 case 'i': /* gps serial ttyport */
260 ttyport = optarg;
261 break;
262 case 'B':
263 bindmode = 1;
264 break;
265 case 'V':
266 if(!strcmp("3.0", optarg)) sisnet = 30;
267 else if(!strcmp("3.1", optarg)) sisnet = 31;
268 else if(!strcmp("2.1", optarg)) sisnet = 21;
269 else
270 {
271 fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
272 usage(-2);
273 }
274 break;
275 case 'b': /* serial ttyin speed */
276 ttybaud = atoi(optarg);
277 if(ttybaud <= 1)
278 {
279 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
280 optarg);
281 usage(1);
282 }
283 break;
284 case 'a': /* http server IP address A.B.C.D */
285 outhost = optarg;
286 break;
287 case 'p': /* http server port */
288 outport = atoi(optarg);
289 if(outport <= 1 || outport > 65535)
290 {
291 fprintf(stderr,
292 "ERROR: can't convert %s to a valid HTTP server port\n", optarg);
293 usage(1);
294 }
295 break;
296 case 'm': /* http server mountpoint */
297 mountpoint = optarg;
298 break;
299 case 's': /* datastream from file */
300 filepath = optarg;
301 break;
302 case 'f':
303 initfile = optarg;
304 break;
305 case 'x':
306 recvrid = optarg;
307 break;
308 case 'y':
309 recvrpwd = optarg;
310 break;
311 case 'u':
312 sisnetuser = optarg;
313 break;
314 case 'l':
315 sisnetpassword = optarg;
316 break;
317 case 'c': /* DestinationCasterPassword */
318 password = optarg;
319 break;
320 case 'H': /* SourceCasterHost */
321 inhost = optarg;
322 break;
323 case 'P': /* SourceCasterPort */
324 inport = atoi(optarg);
325 if(inport <= 1 || inport > 65535)
326 {
327 fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
328 optarg);
329 usage(1);
330 }
331 break;
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;
341 case 'h': /* help */
342 case '?':
343 usage(0);
344 break;
345 default:
346 usage(2);
347 break;
348 }
349 }
350
351 argc -= optind;
352 argv += optind;
353
354 if(argc > 0)
355 {
356 fprintf(stderr, "ERROR: Extra args on command line: ");
357 for(; argc > 0; argc--)
358 {
359 fprintf(stderr, " %s", *argv++);
360 }
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 {
372 fprintf(stderr,
373 "WARNING: Missing password argument - are you really sure?\n");
374 }
375
376 if(stream_name && stream_user && !stream_password)
377 {
378 fprintf(stderr, "WARNING: Missing password argument for download"
379 " - are you really sure?\n");
380 }
381
382 if(!outhost) outhost = NTRIP_CASTER;
383 if(!outport) outport = NTRIP_PORT;
384
385 switch(mode)
386 {
387 case INFILE:
388 {
389 if((gpsfd = open(filepath, O_RDONLY)) < 0)
390 {
391 perror("ERROR: opening input file");
392 exit(1);
393 }
394 /* set blocking mode in case it was not set
395 (seems to be sometimes for fifo's) */
396 fcntl(gpsfd, F_SETFL, 0);
397 printf("file input: file = %s\n", filepath);
398 }
399 break;
400 case SERIAL: /* open serial port */
401 {
402 gpsfd = openserial(ttyport, 1, ttybaud);
403 if(gpsfd < 0)
404 {
405 exit(1);
406 }
407 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
408 }
409 break;
410 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
411 {
412 if(mode == SISNET)
413 {
414 if(!inhost) inhost = SISNET_SERVER;
415 if(!inport) inport = SISNET_PORT;
416 }
417 else if(mode == CASTER)
418 {
419 if(!inport) inport = NTRIP_PORT;
420 if(!inhost) inhost = NTRIP_CASTER;
421 }
422 else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
423 {
424 if(!inport) inport = SERV_TCP_PORT;
425 if(!inhost) inhost = "127.0.0.1";
426 }
427
428 if(!(he = gethostbyname(inhost)))
429 {
430 fprintf(stderr, "ERROR: host %s unknown\n", inhost);
431 usage(-2);
432 }
433
434 if((gpsfd = socket(AF_INET, mode == UDPSOCKET
435 ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
436 {
437 fprintf(stderr, "ERROR: can't create socket\n");
438 exit(1);
439 }
440
441 memset((char *) &addr, 0x00, sizeof(addr));
442 if(!bindmode)
443 memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
444 addr.sin_family = AF_INET;
445 addr.sin_port = htons(inport);
446
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 : "",
453 bindmode ? "binding mode" : "");
454
455 if(bindmode)
456 {
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 {
465 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
466 inet_ntoa(addr.sin_addr), inport);
467 exit(1);
468 }
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,
491 sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
492 if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
493 {
494 fprintf(stderr, "Username and/or password too long\n");
495 exit(1);
496 }
497 szSendBuffer[nBufferBytes++] = '\r';
498 szSendBuffer[nBufferBytes++] = '\n';
499 szSendBuffer[nBufferBytes++] = '\r';
500 szSendBuffer[nBufferBytes++] = '\n';
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
547 if(initfile && mode != SISNET)
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 {
557 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
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 }
576 }
577 if(mode == SISNET)
578 {
579 int i, j;
580 char buffer[1024];
581
582 i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
583 : "AUTH,%s,%s", sisnetuser, sisnetpassword);
584 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
585 {
586 perror("ERROR: sending authentication");
587 exit(1);
588 }
589 i = sisnet >= 30 ? 7 : 5;
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 }
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 }
611 }
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 }
635 break;
636 default:
637 usage(-1);
638 break;
639 }
640
641 /* ----- main part ----- */
642 for(;;)
643 {
644 if(!(he = gethostbyname(outhost)))
645 {
646 fprintf(stderr, "ERROR: host %s unknown\n", outhost);
647 usage(-2);
648 }
649
650 /* create socket */
651 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
652 {
653 fprintf(stderr, "ERROR: could not create socket\n");
654 exit(2);
655 }
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
662 /* connect to caster */
663 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
664 inet_ntoa(addr.sin_addr), outport, mountpoint);
665 if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
666 {
667 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
668 inet_ntoa(addr.sin_addr), outport);
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 */
677 nBufferBytes = sprintf(szSendBuffer, "SOURCE %s /%s\r\nSource-Agent: "
678 VERSION "\r\n\r\n", password, mountpoint);
679 if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
680 {
681 fprintf(stderr, "ERROR: could not send to caster\n");
682 break;
683 }
684 /* check caster's response */
685 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
686 szSendBuffer[nBufferBytes] = '\0';
687 if(!strstr(szSendBuffer, "OK"))
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");
696 break;
697 }
698 printf("connection successfull\n");
699 send_receive_loop(sock_id, gpsfd);
700 }
701 close(sock_id);
702 sleep(5);
703 return 0;
704}
705
706static void send_receive_loop(int sock, int fd)
707{
708 int nodata = 0;
709 char buffer[BUFSZ] = { 0 };
710 char sisnetbackbuffer[200];
711 int nBufferBytes = 0;
712 /* data transmission */
713 printf("transfering data ...\n");
714 while(1)
715 {
716 if(!nodata) alarm(ALARMTIME);
717 else nodata = 0;
718
719 if(!nBufferBytes)
720 {
721 if(mode == SISNET && sisnet <= 30)
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));
729 i = (sisnet >= 30 ? 5 : 3);
730 if((send(gpsfd, "MSG\r\n", i, 0)) != i)
731 {
732 perror("ERROR: sending data request");
733 exit(1);
734 }
735 }
736 /* receiving data */
737 nBufferBytes = read(fd, buffer, sizeof(buffer));
738 if(!nBufferBytes)
739 {
740 printf("WARNING: no data received from input\n");
741 sleep(3);
742 nodata = 1;
743 continue;
744 }
745 else if(nBufferBytes < 0)
746 {
747 perror("ERROR: reading input failed");
748 exit(1);
749 }
750 /* we can compare the whole buffer, as the additional bytes
751 remain unchanged */
752 if(mode == SISNET && sisnet <= 30 &&
753 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
754 {
755 nBufferBytes = 0;
756 }
757 }
758 if(nBufferBytes)
759 {
760 int i;
761 /* send data */
762 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
763 != nBufferBytes)
764 {
765 if(i < 0)
766 {
767 if(errno != EAGAIN)
768 {
769 perror("WARNING: could not send data - retry connection");
770 close(sock);
771 sleep(5);
772 return;
773 }
774 }
775 else if(i)
776 {
777 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
778 nBufferBytes -= i;
779 }
780 }
781 else
782 {
783 nBufferBytes = 0;
784 }
785 }
786 }
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
799 * baud : integer : Baud rate for port I/O
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
809static int openserial(const char * tty, int blocksz, int baud)
810{
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");
818 return (-1);
819 }
820 if(tcgetattr(fd, &termios) < 0)
821 {
822 perror("ERROR: get serial attributes");
823 return (-1);
824 }
825 termios.c_iflag = 0;
826 termios.c_oflag = 0; /* (ONLRET) */
827 termios.c_cflag = CS8 | CLOCAL | CREAD;
828 termios.c_lflag = 0;
829 {
830 int cnt;
831 for(cnt = 0; cnt < NCCS; cnt++)
832 termios.c_cc[cnt] = -1;
833 }
834 termios.c_cc[VMIN] = blocksz;
835 termios.c_cc[VTIME] = 2;
836
837#if (B4800 != 4800)
838 /*
839 * Not every system has speed settings equal to absolute speed value.
840 */
841
842 switch (baud)
843 {
844 case 300:
845 baud = B300;
846 break;
847 case 1200:
848 baud = B1200;
849 break;
850 case 2400:
851 baud = B2400;
852 break;
853 case 4800:
854 baud = B4800;
855 break;
856 case 9600:
857 baud = B9600;
858 break;
859 case 19200:
860 baud = B19200;
861 break;
862 case 38400:
863 baud = B38400;
864 break;
865#ifdef B57600
866 case 57600:
867 baud = B57600;
868 break;
869#endif
870#ifdef B115200
871 case 115200:
872 baud = B115200;
873 break;
874#endif
875#ifdef B230400
876 case 230400:
877 baud = B230400;
878 break;
879#endif
880 default:
881 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
882 baud = B19200;
883 break;
884 }
885#endif
886
887 if(cfsetispeed(&termios, baud) != 0)
888 {
889 perror("ERROR: setting serial speed with cfsetispeed");
890 return (-1);
891 }
892 if(cfsetospeed(&termios, baud) != 0)
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 }
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
924static
925#ifdef __GNUC__
926__attribute__ ((noreturn))
927#endif /* __GNUC__ */
928void usage(int rc)
929{
930 fprintf(stderr, "Usage: " VERSION " [OPTIONS]" COMPILEDATE "\n");
931 fprintf(stderr, " Options are: [-] \n");
932 fprintf(stderr, " -a DestinationCaster name or address (default: %s)\n",
933 NTRIP_CASTER);
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");
937 fprintf(stderr, " -h|? print this help screen\n");
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");
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");
950 fprintf(stderr, " Mode = tcpsocket or udpsocket:\n");
951 fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT);
952 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
953 SERV_HOST_ADDR);
954 fprintf(stderr, " -f initfile send to server\n");
955 fprintf(stderr, " -x receiver id\n");
956 fprintf(stderr, " -y receiver password\n");
957 fprintf(stderr, " -B bindmode: bind to incoming UDP stream\n");
958 fprintf(stderr, " Mode = sisnet:\n");
959 fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT);
960 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
961 SISNET_SERVER);
962 fprintf(stderr, " -u username\n");
963 fprintf(stderr, " -l password\n");
964 fprintf(stderr, " -V version [2.1, 3.0 or 3.1] (default: 3.1)\n");
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");
972 fprintf(stderr, "\n");
973 exit(rc);
974}
975
976static 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 */
985static 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}
Note: See TracBrowser for help on using the repository browser.