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

Last change on this file since 49 was 49, checked in by stoecker, 18 years ago

some changes detected by using icc compiler

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