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

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

added initfile

File size: 16.3 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.9 2005/04/19 11:28:17 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 */
62
63#include <ctype.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <getopt.h>
67#include <netdb.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <unistd.h>
72#include <arpa/inet.h>
73#include <netinet/in.h>
74#include <sys/socket.h>
75#include <sys/termios.h>
76#include <sys/types.h>
77
78#ifndef MSG_DONTWAIT
79#define MSG_DONTWAIT 0 /* prevent compiler errors */
80#endif
81#ifndef O_EXLOCK
82#define O_EXLOCK 0 /* prevent compiler errors */
83#endif
84
85enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3 };
86
87#define VERSION "NTRIP NtripServerLinux/0.10"
88#define BUFSZ 1024
89
90/* default socket source */
91#define SERV_HOST_ADDR "127.0.0.1"
92#define SERV_TCP_PORT 1025
93
94/* default destination */
95#define NTRIP_CASTER "www.euref-ip.net"
96#define NTRIP_PORT 80
97
98int ttybaud = 19200;
99char *ttyport = "/dev/gps";
100char *filepath = "/dev/stdin";
101enum MODE mode = INFILE;
102
103/* Forward references */
104int openserial(u_char * tty, int blocksz, int ttybaud);
105void send_receive_loop(int socket, int fd);
106void usage(int);
107
108/*
109* main
110*
111* Main entry point for the program. Processes command-line arguments and
112* prepares for action.
113*
114* Parameters:
115* argc : integer : Number of command-line arguments.
116* argv : array of char : Command-line arguments as an array of zero-terminated
117* pointers to strings.
118*
119* Return Value:
120* The function does not return a value (although its return type is int).
121*
122* Remarks:
123*
124*/
125
126int main(int argc, char **argv)
127{
128 u_char *ttyin = ttyport;
129 int c, gpsfd = -1;
130 int size = 2048; /* for setting send buffer size */
131
132 unsigned int out_port = 0;
133 unsigned int in_port = 0;
134 char *mountpoint = NULL;
135 char *password = "";
136 char *initfile = NULL;
137 int sock_id;
138 char szSendBuffer[BUFSZ];
139 int nBufferBytes;
140
141 struct hostent *inhost;
142 struct hostent *outhost;
143
144 struct sockaddr_in in_addr;
145 struct sockaddr_in out_addr;
146
147 if(!(outhost = gethostbyname(NTRIP_CASTER)))
148 {
149 fprintf(stderr, "WARNING: host %s unknown\n", NTRIP_CASTER);
150 }
151 else
152 {
153 memset((char *) &out_addr, 0x00, sizeof(out_addr));
154 memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
155 }
156
157 /* get and check program arguments */
158 if(argc <= 1)
159 {
160 usage(2);
161 exit(1);
162 }
163 while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:f:")) != EOF)
164 {
165 switch (c)
166 {
167 case 'M':
168 if(!strcmp(optarg, "serial")) mode = 1;
169 else if(!strcmp(optarg, "tcpsocket")) mode = 2;
170 else if(!strcmp(optarg, "file")) mode = 3;
171 else mode = atoi(optarg);
172 if((mode == 0) || (mode > 3))
173 {
174 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
175 usage(-1);
176 }
177 break;
178 case 'i': /* gps serial ttyin */
179 ttyin = optarg;
180 break;
181 case 'b': /* serial ttyin speed */
182 ttybaud = atoi(optarg);
183 if(ttybaud <= 1)
184 {
185 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg);
186 usage(1);
187 }
188 break;
189 case 'a': /* http server IP address A.B.C.D */
190 outhost = gethostbyname(optarg);
191 if(outhost == NULL)
192 {
193 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
194 usage(-2);
195 }
196 memset((char *) &out_addr, 0x00, sizeof(out_addr));
197 memcpy(&out_addr.sin_addr, outhost->h_addr, outhost->h_length);
198 break;
199 case 'p': /* http server port */
200 out_port = atoi(optarg);
201 if(out_port <= 1)
202 {
203 fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",
204 optarg);
205 usage(1);
206 }
207 break;
208 case 'm': /* http server mountpoint */
209 mountpoint = optarg;
210 break;
211 case 's': /* datastream from file */
212 filepath = optarg;
213 break;
214 case 'f':
215 initfile = optarg;
216 break;
217 case 'c': /* password */
218 password = optarg;
219 break;
220 case 'H': /* host */
221 inhost = gethostbyname(optarg);
222 if(inhost == NULL)
223 {
224 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
225 usage(-2);
226 }
227 memset((char *) &in_addr, 0x00, sizeof(in_addr));
228 memcpy(&in_addr.sin_addr, inhost->h_addr, inhost->h_length);
229 break;
230 case 'P': /* port */
231 in_port = atoi(optarg);
232 if(in_port <= 1)
233 {
234 fprintf(stderr, "ERROR: can't convert %s to a valid receiver port\n",
235 optarg);
236 usage(1);
237 }
238 break;
239 case 'h': /* help */
240 case '?':
241 usage(0);
242 break;
243 default:
244 usage(2);
245 break;
246 }
247
248 if(in_port <= 0)
249 {
250 in_port = SERV_TCP_PORT;
251 }
252 if(out_port <= 0)
253 {
254 out_port = NTRIP_PORT;
255 }
256 }
257
258 argc -= optind;
259 argv += optind;
260
261 if(argc > 0)
262 {
263 fprintf(stderr, "ERROR: Extra args on command line: ");
264 for(; argc > 0; argc--)
265 {
266 fprintf(stderr, " %s", *argv++);
267 }
268 fprintf(stderr, "\n");
269 usage(1); /* never returns */
270 }
271
272 if(mountpoint == NULL)
273 {
274 fprintf(stderr, "ERROR: Missing mountpoint argument\n");
275 exit(1);
276 }
277 if(!password[0])
278 {
279 fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n");
280 }
281
282 switch (mode)
283 {
284 case INFILE:
285 {
286 gpsfd = open(filepath, O_RDONLY);
287 if(!gpsfd)
288 {
289 perror("ERROR: opening input file");
290 exit(1);
291 }
292 /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */
293 fcntl(gpsfd, F_SETFL, 0);
294 printf("file input: file = %s\n", filepath);
295 }
296 break;
297 case SERIAL: /* open serial port */
298 {
299 gpsfd = openserial(ttyin, 1, ttybaud);
300 if(gpsfd < 0)
301 {
302 exit(1);
303 }
304 printf("serial input: device = %s, speed = %d\n", ttyin, ttybaud);
305 }
306 break;
307 case TCPSOCKET:
308 {
309 in_addr.sin_family = AF_INET;
310 in_addr.sin_port = htons(in_port);
311
312 if((gpsfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
313 {
314 fprintf(stderr, "ERROR: can't create socket\n");
315 exit(1);
316 }
317
318 printf("socket input: host = %s, port = %d%s%s\n",
319 inet_ntoa(in_addr.sin_addr), in_port, initfile ? ", initfile = " : "",
320 initfile ? initfile : "");
321
322 if(connect(gpsfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) < 0)
323 {
324 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
325 inet_ntoa(in_addr.sin_addr), in_port);
326 exit(1);
327 }
328 if(initfile)
329 {
330 char buffer[1024];
331 FILE *fh;
332 int i;
333
334 if((fh = fopen(initfile, "r")))
335 {
336 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
337 {
338 if((send(gpsfd, buffer, i, 0)) != i)
339 {
340 perror("ERROR: sending init file");
341 exit(1);
342 }
343 }
344 if(i < 0)
345 {
346 perror("ERROR: reading init file");
347 exit(1);
348 }
349 fclose(fh);
350 }
351 else
352 {
353 fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
354 exit(1);
355 }
356 }
357 }
358 break;
359 default:
360 usage(-1);
361 break;
362 }
363
364 out_addr.sin_family = AF_INET;
365 out_addr.sin_port = htons((u_short) (out_port));
366
367 /* ----- main part ----- */
368 while(1)
369 {
370 /* create socket */
371 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
372 {
373 fprintf(stderr, "ERROR : could not create socket\n");
374 exit(2);
375 }
376 /* connect to caster */
377 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
378 inet_ntoa(out_addr.sin_addr), out_port, mountpoint);
379 if(connect(sock_id, (struct sockaddr *) &out_addr, sizeof(out_addr)) < 0)
380 {
381 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
382 inet_ntoa(out_addr.sin_addr), out_port);
383 close(sock_id);
384 exit(3);
385 }
386
387 /* set socket buffer size */
388 setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
389 sizeof(const char *));
390 /* send message to caster */
391 szSendBuffer[0] = '\0';
392 sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
393 strcat(szSendBuffer, "Source-Agent: ");
394 strcat(szSendBuffer, VERSION);
395 strcat(szSendBuffer, "\r\n");
396 strcat(szSendBuffer, "\r\n");
397 strcat(szSendBuffer, "\0");
398 nBufferBytes = strlen(szSendBuffer);
399 if((send(sock_id, szSendBuffer, nBufferBytes, 0)) != nBufferBytes)
400 {
401 fprintf(stderr, "ERROR: could not send to caster\n");
402 close(sock_id);
403 sleep(5);
404 exit(0);
405 }
406 /* check caster's response */
407 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
408 szSendBuffer[nBufferBytes] = '\0';
409 if(strcmp(szSendBuffer, "OK\r\n"))
410 {
411 char *a;
412 fprintf(stderr, "ERROR: caster's reply is not OK : ");
413 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
414 {
415 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
416 }
417 fprintf(stderr, "\n");
418 close(sock_id);
419 sleep(5);
420 exit(0);
421 }
422 printf("connection succesfull\n");
423 send_receive_loop(sock_id, gpsfd);
424 }
425 exit(0);
426}
427
428void send_receive_loop(int socket, int fd)
429{
430 char buffer[BUFSZ] = { 0 };
431 int nBufferBytes = 0, i;
432 /* data transmission */
433 printf("transfering data ...\n");
434 while(1)
435 {
436 if(!nBufferBytes)
437 {
438 /* receiving data */
439 nBufferBytes = read(fd, buffer, BUFSZ);
440 if(!nBufferBytes)
441 {
442 printf("WARNING: no data received from input\n");
443 continue;
444 }
445 else if(nBufferBytes < 0)
446 {
447 perror("ERROR: reading input failed");
448 exit(1);
449 }
450 }
451 /* send data */
452 if((i = send(socket, buffer, nBufferBytes, MSG_DONTWAIT)) != nBufferBytes)
453 {
454 if(i < 0 && errno != EAGAIN)
455 {
456 perror("WARNING: could not send data - retry connection");
457 close(socket);
458 sleep(5);
459 return;
460 }
461 else if(i)
462 {
463 memmove(buffer, buffer+i, nBufferBytes-i);
464 nBufferBytes -= i;
465 }
466 }
467 else
468 {
469 nBufferBytes = 0;
470 }
471 }
472}
473
474/*
475 * openserial
476 *
477 * Open the serial port with the given device name and configure it for
478 * reading NMEA data from a GPS receiver.
479 *
480 * Parameters:
481 * tty : pointer to : A zero-terminated string containing the device
482 * unsigned char name of the appropriate serial port.
483 * blocksz : integer : Block size for port I/O
484 * ttybaud : integer : Baud rate for port I/O
485 *
486 * Return Value:
487 * The function returns a file descriptor for the opened port if successful.
488 * The function returns -1 in the event of an error.
489 *
490 * Remarks:
491 *
492 */
493
494int openserial(u_char * tty, int blocksz, int ttybaud)
495{
496 int fd;
497 struct termios termios;
498
499 fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
500 if(fd < 0)
501 {
502 perror("ERROR: opening serial connection");
503 return (-1);
504 }
505 if(tcgetattr(fd, &termios) < 0)
506 {
507 perror("ERROR: get serial attributes");
508 return (-1);
509 }
510 termios.c_iflag = 0;
511 termios.c_oflag = 0; /* (ONLRET) */
512 termios.c_cflag = CS8 | CLOCAL | CREAD;
513 termios.c_lflag = 0;
514 {
515 int cnt;
516 for(cnt = 0; cnt < NCCS; cnt++)
517 termios.c_cc[cnt] = -1;
518 }
519 termios.c_cc[VMIN] = blocksz;
520 termios.c_cc[VTIME] = 2;
521
522#if (B4800 != 4800)
523 /*
524 * Not every system has speed settings equal to absolute speed value.
525 */
526
527 switch (ttybaud)
528 {
529 case 300:
530 ttybaud = B300;
531 break;
532 case 1200:
533 ttybaud = B1200;
534 break;
535 case 2400:
536 ttybaud = B2400;
537 break;
538 case 4800:
539 ttybaud = B4800;
540 break;
541 case 9600:
542 ttybaud = B9600;
543 break;
544 case 19200:
545 ttybaud = B19200;
546 break;
547 case 38400:
548 ttybaud = B38400;
549 break;
550#ifdef B57600
551 case 57600:
552 ttybaud = B57600;
553 break;
554#endif
555#ifdef B115200
556 case 115200:
557 ttybaud = B115200;
558 break;
559#endif
560#ifdef B230400
561 case 230400:
562 ttybaud = B230400;
563 break;
564#endif
565 default:
566 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
567 ttybaud = B19200;
568 break;
569 }
570#endif
571
572 if(cfsetispeed(&termios, ttybaud) != 0)
573 {
574 perror("ERROR: setting serial speed with cfsetispeed");
575 return (-1);
576 }
577 if(cfsetospeed(&termios, ttybaud) != 0)
578 {
579 perror("ERROR: setting serial speed with cfsetospeed");
580 return (-1);
581 }
582 if(tcsetattr(fd, TCSANOW, &termios) < 0)
583 {
584 perror("ERROR: setting serial attributes");
585 return (-1);
586 }
587 if(fcntl(fd, F_SETFL, 0) == -1)
588 {
589 perror("WARNING: setting blocking mode failed");
590 }
591 return (fd);
592}
593
594/*
595 * usage
596 *
597 * Send a usage message to standard error and quit the program.
598 *
599 * Parameters:
600 * None.
601 *
602 * Return Value:
603 * The function does not return a value.
604 *
605 * Remarks:
606 *
607 */
608
609void usage(int rc)
610{
611 fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
612 fprintf(stderr, " Options are:\n");
613 fprintf(stderr, " -a caster name or address (default: %s)\n",
614 NTRIP_CASTER);
615 fprintf(stderr, " -p caster port (default: %d)\n", NTRIP_PORT);
616 fprintf(stderr, " -m caster mountpoint\n");
617 fprintf(stderr, " -c password for caster login\n");
618 fprintf(stderr, " -h|? print this help screen\n");
619 fprintf(stderr, " -M <mode> sets the mode\n");
620 fprintf(stderr, " (1=serial, 2=tcpsocket, 3=file)\n");
621 fprintf(stderr, " Mode = file:\n");
622 fprintf(stderr, " -s file, simulate data stream by reading log file\n");
623 fprintf(stderr, " default/current setting is %s\n", filepath);
624 fprintf(stderr, " Mode = serial:\n");
625 fprintf(stderr, " -b baud_rate, sets serial input baud rate\n");
626 fprintf(stderr, " default/current value is %d\n", ttybaud);
627 fprintf(stderr, " -i input_device, sets name of serial input device\n");
628 fprintf(stderr, " default/current value is %s\n", ttyport);
629 fprintf(stderr, " (normally a symbolic link to /dev/tty\?\?)\n");
630 fprintf(stderr, " Mode = tcpsocket:\n");
631 fprintf(stderr, " -P receiver port (default: 1025)\n");
632 fprintf(stderr, " -H hostname of TCP server (default: 127.0.0.1)\n");
633 fprintf(stderr, " -f initfile send to server\n");
634 fprintf(stderr, " \n");
635 exit(rc);
636}
Note: See TracBrowser for help on using the repository browser.