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

Last change on this file since 466 was 466, checked in by stoecker, 15 years ago

updated release stuff

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