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

Last change on this file since 339 was 339, checked in by stoecker, 16 years ago

fixed illegal zero byte

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