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

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

switched to port 2101

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