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

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

updated release stuff

File size: 27.0 KB
RevLine 
[5]1/*
[466]2 * $Id: NtripLinuxClient.c,v 1.27 2007/05/16 14:16:21 stoecker Exp $
[5]3 *
[398]4 * Copyright (c) 2003...2007
[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 * 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
[466]42/* CVS revision and version */
43static char revisionstr[] = "$Revision: 1.27 $";
44static char datestr[] = "$Date: 2007/05/16 14:16:21 $";
[5]45
[18]46#include <ctype.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <getopt.h>
50#include <netdb.h>
[23]51#include <signal.h>
[5]52#include <stdio.h>
[18]53#include <stdlib.h>
[5]54#include <string.h>
55#include <unistd.h>
[18]56#include <arpa/inet.h>
57#include <netinet/in.h>
[5]58#include <sys/socket.h>
59#include <sys/termios.h>
[18]60#include <sys/types.h>
[5]61
[439]62#ifndef COMPILEDATE
63#define COMPILEDATE " built " __DATE__
64#endif
65
[18]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
[5]72
[32]73enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
[49]74CASTER = 6, LAST};
[5]75
[466]76#define AGENTSTRING "NTRIP NtripServerLinux"
[18]77#define BUFSZ 1024
78
[5]79/* default socket source */
[326]80#define SERV_HOST_ADDR "localhost"
81#define SERV_TCP_PORT 2101
[5]82
83/* default destination */
[18]84#define NTRIP_CASTER "www.euref-ip.net"
[398]85#define NTRIP_PORT 2101
[5]86
[23]87/* default sisnet source */
88#define SISNET_SERVER "131.176.49.142"
89#define SISNET_PORT 7777
90
91#define ALARMTIME 60
92
[22]93static int ttybaud = 19200;
94static const char *ttyport = "/dev/gps";
95static const char *filepath = "/dev/stdin";
96static enum MODE mode = INFILE;
[45]97static int sisnet = 31;
[23]98static int gpsfd = -1;
[5]99
100/* Forward references */
[22]101static int openserial(const char * tty, int blocksz, int baud);
[45]102static void send_receive_loop(int sock, int fd);
[466]103static void usage(int, char*);
[32]104static int encode(char *buf, int size, const char *user, const char *pwd);
[5]105
[30]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__ */
[23]112{
113 fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
114 exit(1);
115}
116
[5]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.
[32]125* argv : array of char : Command-line arguments as an array of
126* zero-terminated pointers to strings.
[5]127*
128* Return Value:
129* The function does not return a value (although its return type is int).
130*
131* Remarks:
132*
133*/
134
[18]135int main(int argc, char **argv)
[5]136{
[23]137 int c;
[18]138 int size = 2048; /* for setting send buffer size */
139
[23]140 const char *inhost = 0;
141 const char *outhost = 0;
142 unsigned int outport = 0;
143 unsigned int inport = 0;
[22]144 const char *mountpoint = NULL;
145 const char *password = "";
[23]146 const char *sisnetpassword = "";
147 const char *sisnetuser = "";
[32]148
149 const char *stream_name=0;
150 const char *stream_user=0;
151 const char *stream_password=0;
152
[22]153 const char *initfile = NULL;
[398]154
155 const char *recvrid=0;
156 const char *recvrpwd=0;
157
[24]158 int bindmode = 0;
[18]159 int sock_id;
160 char szSendBuffer[BUFSZ];
161 int nBufferBytes;
[23]162 struct hostent *he;
163 struct sockaddr_in addr;
[5]164
[466]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
[23]185 signal(SIGALRM,sighandler_alarm);
186 alarm(ALARMTIME);
[18]187 /* get and check program arguments */
188 if(argc <= 1)
189 {
[466]190 usage(2, argv[0]);
[18]191 exit(1);
192 }
[398]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"))
[32]194 != EOF)
[18]195 {
196 switch (c)
[5]197 {
[18]198 case 'M':
[49]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;
[21]205 else mode = atoi(optarg);
[49]206 if((mode == 0) || (mode >= LAST))
[18]207 {
208 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
[466]209 usage(-1, argv[0]);
[18]210 }
211 break;
[22]212 case 'i': /* gps serial ttyport */
213 ttyport = optarg;
[18]214 break;
[24]215 case 'B':
216 bindmode = 1;
217 break;
[23]218 case 'V':
[45]219 if(!strcmp("3.0", optarg)) sisnet = 30;
220 else if(!strcmp("3.1", optarg)) sisnet = 31;
[48]221 else if(!strcmp("2.1", optarg)) sisnet = 21;
[45]222 else
[23]223 {
224 fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
[466]225 usage(-2, argv[0]);
[23]226 }
[45]227 break;
[18]228 case 'b': /* serial ttyin speed */
229 ttybaud = atoi(optarg);
230 if(ttybaud <= 1)
231 {
[32]232 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
233 optarg);
[466]234 usage(1, argv[0]);
[18]235 }
236 break;
237 case 'a': /* http server IP address A.B.C.D */
[23]238 outhost = optarg;
[18]239 break;
240 case 'p': /* http server port */
[23]241 outport = atoi(optarg);
242 if(outport <= 1 || outport > 65535)
[18]243 {
[32]244 fprintf(stderr,
245 "ERROR: can't convert %s to a valid HTTP server port\n", optarg);
[466]246 usage(1, argv[0]);
[18]247 }
248 break;
249 case 'm': /* http server mountpoint */
250 mountpoint = optarg;
251 break;
[21]252 case 's': /* datastream from file */
[18]253 filepath = optarg;
254 break;
[21]255 case 'f':
256 initfile = optarg;
257 break;
[398]258 case 'x':
259 recvrid = optarg;
260 break;
261 case 'y':
262 recvrpwd = optarg;
263 break;
[23]264 case 'u':
265 sisnetuser = optarg;
266 break;
267 case 'l':
268 sisnetpassword = optarg;
269 break;
[398]270 case 'c': /* DestinationCasterPassword */
[18]271 password = optarg;
272 break;
[398]273 case 'H': /* SourceCasterHost */
[23]274 inhost = optarg;
[18]275 break;
[398]276 case 'P': /* SourceCasterPort */
[23]277 inport = atoi(optarg);
278 if(inport <= 1 || inport > 65535)
[18]279 {
[23]280 fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
[18]281 optarg);
[466]282 usage(1, argv[0]);
[18]283 }
284 break;
[32]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;
[18]294 case 'h': /* help */
295 case '?':
[466]296 usage(0, argv[0]);
[18]297 break;
298 default:
[466]299 usage(2, argv[0]);
[18]300 break;
[5]301 }
[18]302 }
303
[5]304 argc -= optind;
305 argv += optind;
[18]306
307 if(argc > 0)
308 {
309 fprintf(stderr, "ERROR: Extra args on command line: ");
310 for(; argc > 0; argc--)
[5]311 {
[18]312 fprintf(stderr, " %s", *argv++);
[5]313 }
[18]314 fprintf(stderr, "\n");
[466]315 usage(1, argv[0]); /* never returns */
[18]316 }
317
318 if(mountpoint == NULL)
319 {
320 fprintf(stderr, "ERROR: Missing mountpoint argument\n");
321 exit(1);
322 }
323 if(!password[0])
324 {
[32]325 fprintf(stderr,
326 "WARNING: Missing password argument - are you really sure?\n");
[18]327 }
328
[47]329 if(stream_name && stream_user && !stream_password)
[32]330 {
331 fprintf(stderr, "WARNING: Missing password argument for download"
332 " - are you really sure?\n");
333 }
334
[23]335 if(!outhost) outhost = NTRIP_CASTER;
336 if(!outport) outport = NTRIP_PORT;
337
338 switch(mode)
[18]339 {
340 case INFILE:
[5]341 {
[45]342 if((gpsfd = open(filepath, O_RDONLY)) < 0)
[18]343 {
344 perror("ERROR: opening input file");
345 exit(1);
346 }
[32]347 /* set blocking mode in case it was not set
348 (seems to be sometimes for fifo's) */
[18]349 fcntl(gpsfd, F_SETFL, 0);
350 printf("file input: file = %s\n", filepath);
[5]351 }
[18]352 break;
353 case SERIAL: /* open serial port */
[5]354 {
[22]355 gpsfd = openserial(ttyport, 1, ttybaud);
[18]356 if(gpsfd < 0)
357 {
358 exit(1);
359 }
[22]360 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
[5]361 }
[18]362 break;
[32]363 case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
[5]364 {
[23]365 if(mode == SISNET)
366 {
367 if(!inhost) inhost = SISNET_SERVER;
368 if(!inport) inport = SISNET_PORT;
369 }
[32]370 else if(mode == CASTER)
[23]371 {
[32]372 if(!inport) inport = NTRIP_PORT;
373 if(!inhost) inhost = NTRIP_CASTER;
374 }
375 else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
376 {
[23]377 if(!inport) inport = SERV_TCP_PORT;
378 if(!inhost) inhost = "127.0.0.1";
[32]379 }
[18]380
[23]381 if(!(he = gethostbyname(inhost)))
[5]382 {
[23]383 fprintf(stderr, "ERROR: host %s unknown\n", inhost);
[466]384 usage(-2, argv[0]);
[23]385 }
386
[32]387 if((gpsfd = socket(AF_INET, mode == UDPSOCKET
388 ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
[23]389 {
[18]390 fprintf(stderr, "ERROR: can't create socket\n");
391 exit(1);
[5]392 }
[18]393
[23]394 memset((char *) &addr, 0x00, sizeof(addr));
[24]395 if(!bindmode)
396 memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
[23]397 addr.sin_family = AF_INET;
398 addr.sin_port = htons(inport);
399
[32]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 : "",
[45]406 bindmode ? "binding mode" : "");
[18]407
[24]408 if(bindmode)
[5]409 {
[24]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 {
[18]418 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
[23]419 inet_ntoa(addr.sin_addr), inport);
[18]420 exit(1);
[5]421 }
[32]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"
[466]435 "User-Agent: %s/%s\r\n"
436 "Authorization: Basic ", stream_name, AGENTSTRING, revisionstr);
[32]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,
[339]444 sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
445 if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
[32]446 {
447 fprintf(stderr, "Username and/or password too long\n");
448 exit(1);
449 }
[339]450 szSendBuffer[nBufferBytes++] = '\r';
451 szSendBuffer[nBufferBytes++] = '\n';
452 szSendBuffer[nBufferBytes++] = '\r';
453 szSendBuffer[nBufferBytes++] = '\n';
[32]454 }
455 else
456 {
457 nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
458 "GET /%s HTTP/1.0\r\n"
[466]459 "User-Agent: %s/%s\r\n"
460 "\r\n", stream_name, AGENTSTRING, revisionstr);
[32]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
[23]500 if(initfile && mode != SISNET)
[21]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 {
[22]510 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
[21]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 }
[5]529 }
[23]530 if(mode == SISNET)
531 {
532 int i, j;
533 char buffer[1024];
534
[45]535 i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
[32]536 : "AUTH,%s,%s", sisnetuser, sisnetpassword);
[23]537 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
538 {
539 perror("ERROR: sending authentication");
540 exit(1);
541 }
[45]542 i = sisnet >= 30 ? 7 : 5;
[23]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 }
[45]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 }
[23]564 }
[398]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 }
[18]588 break;
589 default:
[466]590 usage(-1, argv[0]);
[18]591 break;
592 }
593
594 /* ----- main part ----- */
[50]595 for(;;)
[18]596 {
[23]597 if(!(he = gethostbyname(outhost)))
598 {
599 fprintf(stderr, "ERROR: host %s unknown\n", outhost);
[466]600 usage(-2, argv[0]);
[23]601 }
602
[18]603 /* create socket */
604 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
[5]605 {
[23]606 fprintf(stderr, "ERROR: could not create socket\n");
[18]607 exit(2);
[5]608 }
[23]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
[18]615 /* connect to caster */
616 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
[23]617 inet_ntoa(addr.sin_addr), outport, mountpoint);
618 if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
[18]619 {
620 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
[23]621 inet_ntoa(addr.sin_addr), outport);
[18]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 */
[339]630 nBufferBytes = sprintf(szSendBuffer, "SOURCE %s /%s\r\nSource-Agent: "
[466]631 "%s/%s\r\n\r\n", password, mountpoint, AGENTSTRING, revisionstr);
[22]632 if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
[18]633 {
634 fprintf(stderr, "ERROR: could not send to caster\n");
[50]635 break;
[18]636 }
637 /* check caster's response */
638 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
639 szSendBuffer[nBufferBytes] = '\0';
[45]640 if(!strstr(szSendBuffer, "OK"))
[18]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");
[50]649 break;
[18]650 }
[24]651 printf("connection successfull\n");
[45]652 send_receive_loop(sock_id, gpsfd);
[18]653 }
[50]654 close(sock_id);
655 sleep(5);
656 return 0;
[5]657}
658
[45]659static void send_receive_loop(int sock, int fd)
[5]660{
[51]661 int nodata = 0;
[18]662 char buffer[BUFSZ] = { 0 };
[23]663 char sisnetbackbuffer[200];
[30]664 int nBufferBytes = 0;
[18]665 /* data transmission */
[5]666 printf("transfering data ...\n");
667 while(1)
[18]668 {
[51]669 if(!nodata) alarm(ALARMTIME);
670 else nodata = 0;
[23]671
[18]672 if(!nBufferBytes)
[5]673 {
[45]674 if(mode == SISNET && sisnet <= 30)
[23]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));
[45]682 i = (sisnet >= 30 ? 5 : 3);
[23]683 if((send(gpsfd, "MSG\r\n", i, 0)) != i)
684 {
685 perror("ERROR: sending data request");
686 exit(1);
687 }
688 }
[18]689 /* receiving data */
[32]690 nBufferBytes = read(fd, buffer, sizeof(buffer));
[18]691 if(!nBufferBytes)
692 {
693 printf("WARNING: no data received from input\n");
[398]694 sleep(3);
[51]695 nodata = 1;
[18]696 continue;
697 }
698 else if(nBufferBytes < 0)
699 {
700 perror("ERROR: reading input failed");
701 exit(1);
702 }
[32]703 /* we can compare the whole buffer, as the additional bytes
704 remain unchanged */
[46]705 if(mode == SISNET && sisnet <= 30 &&
706 !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
[23]707 {
708 nBufferBytes = 0;
709 }
[5]710 }
[23]711 if(nBufferBytes)
[18]712 {
[30]713 int i;
[23]714 /* send data */
715 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
[32]716 != nBufferBytes)
[18]717 {
[34]718 if(i < 0)
[23]719 {
[34]720 if(errno != EAGAIN)
721 {
722 perror("WARNING: could not send data - retry connection");
723 close(sock);
724 sleep(5);
725 return;
726 }
[23]727 }
728 else if(i)
729 {
730 memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
731 nBufferBytes -= i;
732 }
[18]733 }
[23]734 else
[18]735 {
[23]736 nBufferBytes = 0;
[18]737 }
738 }
739 }
[5]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
[22]752 * baud : integer : Baud rate for port I/O
[5]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
[22]762static int openserial(const char * tty, int blocksz, int baud)
[5]763{
[18]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");
[5]771 return (-1);
772 }
[18]773 if(tcgetattr(fd, &termios) < 0)
774 {
775 perror("ERROR: get serial attributes");
[5]776 return (-1);
777 }
778 termios.c_iflag = 0;
[18]779 termios.c_oflag = 0; /* (ONLRET) */
[5]780 termios.c_cflag = CS8 | CLOCAL | CREAD;
781 termios.c_lflag = 0;
782 {
[18]783 int cnt;
784 for(cnt = 0; cnt < NCCS; cnt++)
[5]785 termios.c_cc[cnt] = -1;
786 }
787 termios.c_cc[VMIN] = blocksz;
788 termios.c_cc[VTIME] = 2;
[18]789
[5]790#if (B4800 != 4800)
791 /*
[11]792 * Not every system has speed settings equal to absolute speed value.
[5]793 */
[18]794
[22]795 switch (baud)
[18]796 {
797 case 300:
[22]798 baud = B300;
[18]799 break;
800 case 1200:
[22]801 baud = B1200;
[18]802 break;
803 case 2400:
[22]804 baud = B2400;
[18]805 break;
806 case 4800:
[22]807 baud = B4800;
[18]808 break;
809 case 9600:
[22]810 baud = B9600;
[18]811 break;
812 case 19200:
[22]813 baud = B19200;
[18]814 break;
815 case 38400:
[22]816 baud = B38400;
[18]817 break;
[11]818#ifdef B57600
[18]819 case 57600:
[22]820 baud = B57600;
[18]821 break;
[11]822#endif
823#ifdef B115200
[18]824 case 115200:
[22]825 baud = B115200;
[18]826 break;
[11]827#endif
828#ifdef B230400
[18]829 case 230400:
[22]830 baud = B230400;
[18]831 break;
[11]832#endif
[18]833 default:
834 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
[22]835 baud = B19200;
[18]836 break;
837 }
[5]838#endif
[18]839
[22]840 if(cfsetispeed(&termios, baud) != 0)
[18]841 {
842 perror("ERROR: setting serial speed with cfsetispeed");
843 return (-1);
844 }
[22]845 if(cfsetospeed(&termios, baud) != 0)
[18]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 }
[5]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
[30]877static
878#ifdef __GNUC__
879__attribute__ ((noreturn))
880#endif /* __GNUC__ */
[466]881void usage(int rc, char *name)
[5]882{
[466]883 fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]",
884 revisionstr, datestr, name);
[32]885 fprintf(stderr, " Options are: [-] \n");
886 fprintf(stderr, " -a DestinationCaster name or address (default: %s)\n",
[18]887 NTRIP_CASTER);
[32]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");
[18]891 fprintf(stderr, " -h|? print this help screen\n");
[32]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");
[18]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");
[23]904 fprintf(stderr, " Mode = tcpsocket or udpsocket:\n");
905 fprintf(stderr, " -P receiver port (default: %d)\n", SERV_TCP_PORT);
[32]906 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
907 SERV_HOST_ADDR);
[21]908 fprintf(stderr, " -f initfile send to server\n");
[398]909 fprintf(stderr, " -x receiver id\n");
910 fprintf(stderr, " -y receiver password\n");
[32]911 fprintf(stderr, " -B bindmode: bind to incoming UDP stream\n");
[23]912 fprintf(stderr, " Mode = sisnet:\n");
913 fprintf(stderr, " -P receiver port (default: %d)\n", SISNET_PORT);
[32]914 fprintf(stderr, " -H hostname of TCP server (default: %s)\n",
915 SISNET_SERVER);
[23]916 fprintf(stderr, " -u username\n");
917 fprintf(stderr, " -l password\n");
[45]918 fprintf(stderr, " -V version [2.1, 3.0 or 3.1] (default: 3.1)\n");
[32]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");
[23]926 fprintf(stderr, "\n");
[18]927 exit(rc);
[5]928}
[32]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.