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
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 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
135
136enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
137CASTER = 6, LAST};
138
139#define VERSION "NTRIP NtripServerLinux/0.20"
140#define BUFSZ 1024
141
142/* default socket source */
143#define SERV_HOST_ADDR "localhost"
144#define SERV_TCP_PORT 2101
145
146/* default destination */
147#define NTRIP_CASTER "www.euref-ip.net"
148#define NTRIP_PORT 2101
149
150/* default sisnet source */
151#define SISNET_SERVER "131.176.49.142"
152#define SISNET_PORT 7777
153
154#define ALARMTIME 60
155
156static int ttybaud = 19200;
157static const char *ttyport = "/dev/gps";
158static const char *filepath = "/dev/stdin";
159static enum MODE mode = INFILE;
160static int sisnet = 31;
161static int gpsfd = -1;
162
163/* Forward references */
164static int openserial(const char * tty, int blocksz, int baud);
165static void send_receive_loop(int sock, int fd);
166static void usage(int);
167static int encode(char *buf, int size, const char *user, const char *pwd);
168
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__ */
175{
176 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
177 exit(1);
178}
179
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.
188* argv : array of char : Command-line arguments as an array of
189* zero-terminated pointers to strings.
190*
191* Return Value:
192* The function does not return a value (although its return type is int).
193*
194* Remarks:
195*
196*/
197
198int main(int argc, char **argv)
199{
200 int c;
201 int size = 2048; /* for setting send buffer size */
202
203 const char *inhost = 0;
204 const char *outhost = 0;
205 unsigned int outport = 0;
206 unsigned int inport = 0;
207 const char *mountpoint = NULL;
208 const char *password = "";
209 const char *sisnetpassword = "";
210 const char *sisnetuser = "";
211
212 const char *stream_name=0;
213 const char *stream_user=0;
214 const char *stream_password=0;
215
216 const char *initfile = NULL;
217
218 const char *recvrid=0;
219 const char *recvrpwd=0;
220
221 int bindmode = 0;
222 int sock_id;
223 char szSendBuffer[BUFSZ];
224 int nBufferBytes;
225 struct hostent *he;
226 struct sockaddr_in addr;
227
228 signal(SIGALRM,sighandler_alarm);
229 alarm(ALARMTIME);
230 /* get and check program arguments */
231 if(argc <= 1)
232 {
233 usage(2);
234 exit(1);
235 }
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"))
237 != EOF)
238 {
239 switch (c)
240 {
241 case 'M':
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;
248 else mode = atoi(optarg);
249 if((mode == 0) || (mode >= LAST))
250 {
251 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
252 usage(-1);
253 }
254 break;
255 case 'i': /* gps serial ttyport */
256 ttyport = optarg;
257 break;
258 case 'B':
259 bindmode = 1;
260 break;
261 case 'V':
262 if(!strcmp("3.0", optarg)) sisnet = 30;
263 else if(!strcmp("3.1", optarg)) sisnet = 31;
264 else if(!strcmp("2.1", optarg)) sisnet = 21;
265 else
266 {
267 fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
268 usage(-2);
269 }
270 break;
271 case 'b': /* serial ttyin speed */
272 ttybaud = atoi(optarg);
273 if(ttybaud <= 1)
274 {
275 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
276 optarg);
277 usage(1);
278 }
279 break;
280 case 'a': /* http server IP address A.B.C.D */
281 outhost = optarg;
282 break;
283 case 'p': /* http server port */
284 outport = atoi(optarg);
285 if(outport <= 1 || outport > 65535)
286 {
287 fprintf(stderr,
288 "ERROR: can't convert %s to a valid HTTP server port\n", optarg);
289 usage(1);
290 }
291 break;
292 case 'm': /* http server mountpoint */
293 mountpoint = optarg;
294 break;
295 case 's': /* datastream from file */
296 filepath = optarg;
297 break;
298 case 'f':
299 initfile = optarg;
300 break;
301 case 'x':
302 recvrid = optarg;
303 break;
304 case 'y':
305 recvrpwd = optarg;
306 break;
307 case 'u':
308 sisnetuser = optarg;
309 break;
310 case 'l':
311 sisnetpassword = optarg;
312 break;
313 case 'c': /* DestinationCasterPassword */
314 password = optarg;
315 break;
316 case 'H': /* SourceCasterHost */
317 inhost = optarg;
318 break;
319 case 'P': /* SourceCasterPort */
320 inport = atoi(optarg);
321 if(inport <= 1 || inport > 65535)
322 {
323 fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
324 optarg);
325 usage(1);
326 }
327 break;
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;
337 case 'h': /* help */
338 case '?':
339 usage(0);
340 break;
341 default:
342 usage(2);
343 break;
344 }
345 }
346
347 argc -= optind;
348 argv += optind;
349
350 if(argc > 0)
351 {
352 fprintf(stderr, "ERROR: Extra args on command line: ");
353 for(; argc > 0; argc--)
354 {
355 fprintf(stderr, " %s", *argv++);
356 }
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 {
368 fprintf(stderr,
369 "WARNING: Missing password argument - are you really sure?\n");
370 }
371
372 if(stream_name && stream_user && !stream_password)
373 {
374 fprintf(stderr, "WARNING: Missing password argument for download"
375 " - are you really sure?\n");
376 }
377
378 if(!outhost) outhost = NTRIP_CASTER;
379 if(!outport) outport = NTRIP_PORT;
380
381 switch(mode)
382 {
383 case INFILE:
384 {
385 if((gpsfd = open(filepath, O_RDONLY)) < 0)
386 {
387 perror("ERROR: opening input file");
388 exit(1);
389 }
390 /* set blocking mode in case it was not set
391 (seems to be sometimes for fifo's) */
392 fcntl(gpsfd, F_SETFL, 0);
393 printf("file input: file = %s\n", filepath);
394 }
395 break;
396 case SERIAL: /* open serial port */
397 {
398 gpsfd = openserial(ttyport, 1, ttybaud);
399 if(gpsfd < 0)
400 {
401 exit(1);
402 }
403 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
404 }
405 break;
406 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
407 {
408 if(mode == SISNET)
409 {
410 if(!inhost) inhost = SISNET_SERVER;
411 if(!inport) inport = SISNET_PORT;
412 }
413 else if(mode == CASTER)
414 {
415 if(!inport) inport = NTRIP_PORT;
416 if(!inhost) inhost = NTRIP_CASTER;
417 }
418 else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
419 {
420 if(!inport) inport = SERV_TCP_PORT;
421 if(!inhost) inhost = "127.0.0.1";
422 }
423
424 if(!(he = gethostbyname(inhost)))
425 {
426 fprintf(stderr, "ERROR: host %s unknown\n", inhost);
427 usage(-2);
428 }
429
430 if((gpsfd = socket(AF_INET, mode == UDPSOCKET
431 ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
432 {
433 fprintf(stderr, "ERROR: can't create socket\n");
434 exit(1);
435 }
436
437 memset((char *) &addr, 0x00, sizeof(addr));
438 if(!bindmode)
439 memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
440 addr.sin_family = AF_INET;
441 addr.sin_port = htons(inport);
442
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 : "",
449 bindmode ? "binding mode" : "");
450
451 if(bindmode)
452 {
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 {
461 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
462 inet_ntoa(addr.sin_addr), inport);
463 exit(1);
464 }
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,
487 sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
488 if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
489 {
490 fprintf(stderr, "Username and/or password too long\n");
491 exit(1);
492 }
493 szSendBuffer[nBufferBytes++] = '\r';
494 szSendBuffer[nBufferBytes++] = '\n';
495 szSendBuffer[nBufferBytes++] = '\r';
496 szSendBuffer[nBufferBytes++] = '\n';
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
543 if(initfile && mode != SISNET)
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 {
553 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
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 }
572 }
573 if(mode == SISNET)
574 {
575 int i, j;
576 char buffer[1024];
577
578 i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
579 : "AUTH,%s,%s", sisnetuser, sisnetpassword);
580 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
581 {
582 perror("ERROR: sending authentication");
583 exit(1);
584 }
585 i = sisnet >= 30 ? 7 : 5;
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 }
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 }
607 }
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 }
631 break;
632 default:
633 usage(-1);
634 break;
635 }
636
637 /* ----- main part ----- */
638 for(;;)
639 {
640 if(!(he = gethostbyname(outhost)))
641 {
642 fprintf(stderr, "ERROR: host %s unknown\n", outhost);
643 usage(-2);
644 }
645
646 /* create socket */
647 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
648 {
649 fprintf(stderr, "ERROR: could not create socket\n");
650 exit(2);
651 }
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
658 /* connect to caster */
659 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
660 inet_ntoa(addr.sin_addr), outport, mountpoint);
661 if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
662 {
663 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
664 inet_ntoa(addr.sin_addr), outport);
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 */
673 nBufferBytes = sprintf(szSendBuffer, "SOURCE %s /%s\r\nSource-Agent: "
674 VERSION "\r\n\r\n", password, mountpoint);
675 if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
676 {
677 fprintf(stderr, "ERROR: could not send to caster\n");
678 break;
679 }
680 /* check caster's response */
681 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
682 szSendBuffer[nBufferBytes] = '\0';
683 if(!strstr(szSendBuffer, "OK"))
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");
692 break;
693 }
694 printf("connection successfull\n");
695 send_receive_loop(sock_id, gpsfd);
696 }
697 close(sock_id);
698 sleep(5);
699 return 0;
700}
701
702static void send_receive_loop(int sock, int fd)
703{
704 int nodata = 0;
705 char buffer[BUFSZ] = { 0 };
706 char sisnetbackbuffer[200];
707 int nBufferBytes = 0;
708 /* data transmission */
709 printf("transfering data ...\n");
710 while(1)
711 {
712 if(!nodata) alarm(ALARMTIME);
713 else nodata = 0;
714
715 if(!nBufferBytes)
716 {
717 if(mode == SISNET && sisnet <= 30)
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));
725 i = (sisnet >= 30 ? 5 : 3);
726 if((send(gpsfd, "MSG\r\n", i, 0)) != i)
727 {
728 perror("ERROR: sending data request");
729 exit(1);
730 }
731 }
732 /* receiving data */
733 nBufferBytes = read(fd, buffer, sizeof(buffer));
734 if(!nBufferBytes)
735 {
736 printf("WARNING: no data received from input\n");
737 sleep(3);
738 nodata = 1;
739 continue;
740 }
741 else if(nBufferBytes < 0)
742 {
743 perror("ERROR: reading input failed");
744 exit(1);
745 }
746 /* we can compare the whole buffer, as the additional bytes
747 remain unchanged */
748 if(mode == SISNET && sisnet <= 30 &&
749 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
750 {
751 nBufferBytes = 0;
752 }
753 }
754 if(nBufferBytes)
755 {
756 int i;
757 /* send data */
758 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
759 != nBufferBytes)
760 {
761 if(i < 0)
762 {
763 if(errno != EAGAIN)
764 {
765 perror("WARNING: could not send data - retry connection");
766 close(sock);
767 sleep(5);
768 return;
769 }
770 }
771 else if(i)
772 {
773 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
774 nBufferBytes -= i;
775 }
776 }
777 else
778 {
779 nBufferBytes = 0;
780 }
781 }
782 }
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
795 * baud : integer : Baud rate for port I/O
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
805static int openserial(const char * tty, int blocksz, int baud)
806{
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");
814 return (-1);
815 }
816 if(tcgetattr(fd, &termios) < 0)
817 {
818 perror("ERROR: get serial attributes");
819 return (-1);
820 }
821 termios.c_iflag = 0;
822 termios.c_oflag = 0; /* (ONLRET) */
823 termios.c_cflag = CS8 | CLOCAL | CREAD;
824 termios.c_lflag = 0;
825 {
826 int cnt;
827 for(cnt = 0; cnt < NCCS; cnt++)
828 termios.c_cc[cnt] = -1;
829 }
830 termios.c_cc[VMIN] = blocksz;
831 termios.c_cc[VTIME] = 2;
832
833#if (B4800 != 4800)
834 /*
835 * Not every system has speed settings equal to absolute speed value.
836 */
837
838 switch (baud)
839 {
840 case 300:
841 baud = B300;
842 break;
843 case 1200:
844 baud = B1200;
845 break;
846 case 2400:
847 baud = B2400;
848 break;
849 case 4800:
850 baud = B4800;
851 break;
852 case 9600:
853 baud = B9600;
854 break;
855 case 19200:
856 baud = B19200;
857 break;
858 case 38400:
859 baud = B38400;
860 break;
861#ifdef B57600
862 case 57600:
863 baud = B57600;
864 break;
865#endif
866#ifdef B115200
867 case 115200:
868 baud = B115200;
869 break;
870#endif
871#ifdef B230400
872 case 230400:
873 baud = B230400;
874 break;
875#endif
876 default:
877 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
878 baud = B19200;
879 break;
880 }
881#endif
882
883 if(cfsetispeed(&termios, baud) != 0)
884 {
885 perror("ERROR: setting serial speed with cfsetispeed");
886 return (-1);
887 }
888 if(cfsetospeed(&termios, baud) != 0)
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 }
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
920static
921#ifdef __GNUC__
922__attribute__ ((noreturn))
923#endif /* __GNUC__ */
924void usage(int rc)
925{
926 fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
927 fprintf(stderr, " Options are: [-] \n");
928 fprintf(stderr, " -a DestinationCaster name or address (default: %s)\n",
929 NTRIP_CASTER);
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");
933 fprintf(stderr, " -h|? print this help screen\n");
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");
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");
946 fprintf(stderr, " Mode = tcpsocket or udpsocket:\n");
947 fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT);
948 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
949 SERV_HOST_ADDR);
950 fprintf(stderr, " -f initfile send to server\n");
951 fprintf(stderr, " -x receiver id\n");
952 fprintf(stderr, " -y receiver password\n");
953 fprintf(stderr, " -B bindmode: bind to incoming UDP stream\n");
954 fprintf(stderr, " Mode = sisnet:\n");
955 fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT);
956 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
957 SISNET_SERVER);
958 fprintf(stderr, " -u username\n");
959 fprintf(stderr, " -l password\n");
960 fprintf(stderr, " -V version [2.1, 3.0 or 3.1] (default: 3.1)\n");
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");
968 fprintf(stderr, "\n");
969 exit(rc);
970}
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.