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

Last change on this file since 22 was 22, checked in by stoecker, 19 years ago

fixed warnings

File size: 16.4 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.10 2005/04/27 08:32:43 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
98static int ttybaud = 19200;
99static const char *ttyport = "/dev/gps";
100static const char *filepath = "/dev/stdin";
101static enum MODE mode = INFILE;
102
103/* Forward references */
104static int openserial(const char * tty, int blocksz, int baud);
105static void send_receive_loop(int sock, int fd);
106static void 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 int c, gpsfd = -1;
129 int size = 2048; /* for setting send buffer size */
130
131 unsigned int out_port = 0;
132 unsigned int in_port = 0;
133 const char *mountpoint = NULL;
134 const char *password = "";
135 const char *initfile = NULL;
136 int sock_id;
137 char szSendBuffer[BUFSZ];
138 int nBufferBytes;
139
140 struct hostent *inhost;
141 struct hostent *outhost;
142
143 struct sockaddr_in in_addr;
144 struct sockaddr_in out_addr;
145
146 if(!(outhost = gethostbyname(NTRIP_CASTER)))
147 {
148 fprintf(stderr, "WARNING: host %s unknown\n", NTRIP_CASTER);
149 }
150 else
151 {
152 memset((char *) &out_addr, 0x00, sizeof(out_addr));
153 memcpy(&out_addr.sin_addr, outhost->h_addr, (size_t)outhost->h_length);
154 }
155
156 /* get and check program arguments */
157 if(argc <= 1)
158 {
159 usage(2);
160 exit(1);
161 }
162 while((c = getopt(argc, argv, "M:i:h:b:p:s:a:m:c:H:P:f:")) != EOF)
163 {
164 switch (c)
165 {
166 case 'M':
167 if(!strcmp(optarg, "serial")) mode = 1;
168 else if(!strcmp(optarg, "tcpsocket")) mode = 2;
169 else if(!strcmp(optarg, "file")) mode = 3;
170 else mode = atoi(optarg);
171 if((mode == 0) || (mode > 3))
172 {
173 fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
174 usage(-1);
175 }
176 break;
177 case 'i': /* gps serial ttyport */
178 ttyport = optarg;
179 break;
180 case 'b': /* serial ttyin speed */
181 ttybaud = atoi(optarg);
182 if(ttybaud <= 1)
183 {
184 fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n", optarg);
185 usage(1);
186 }
187 break;
188 case 'a': /* http server IP address A.B.C.D */
189 outhost = gethostbyname(optarg);
190 if(outhost == NULL)
191 {
192 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
193 usage(-2);
194 }
195 memset((char *) &out_addr, 0x00, sizeof(out_addr));
196 memcpy(&out_addr.sin_addr, outhost->h_addr, (size_t)outhost->h_length);
197 break;
198 case 'p': /* http server port */
199 out_port = atoi(optarg);
200 if(out_port <= 1)
201 {
202 fprintf(stderr, "ERROR: can't convert %s to a valid HTTP server port\n",
203 optarg);
204 usage(1);
205 }
206 break;
207 case 'm': /* http server mountpoint */
208 mountpoint = optarg;
209 break;
210 case 's': /* datastream from file */
211 filepath = optarg;
212 break;
213 case 'f':
214 initfile = optarg;
215 break;
216 case 'c': /* password */
217 password = optarg;
218 break;
219 case 'H': /* host */
220 inhost = gethostbyname(optarg);
221 if(inhost == NULL)
222 {
223 fprintf(stderr, "ERROR: host %s unknown\n", optarg);
224 usage(-2);
225 }
226 memset((char *) &in_addr, 0x00, sizeof(in_addr));
227 memcpy(&in_addr.sin_addr, inhost->h_addr, (size_t)inhost->h_length);
228 break;
229 case 'P': /* port */
230 in_port = atoi(optarg);
231 if(in_port <= 1)
232 {
233 fprintf(stderr, "ERROR: can't convert %s to a valid receiver port\n",
234 optarg);
235 usage(1);
236 }
237 break;
238 case 'h': /* help */
239 case '?':
240 usage(0);
241 break;
242 default:
243 usage(2);
244 break;
245 }
246
247 if(in_port <= 0)
248 {
249 in_port = SERV_TCP_PORT;
250 }
251 if(out_port <= 0)
252 {
253 out_port = NTRIP_PORT;
254 }
255 }
256
257 argc -= optind;
258 argv += optind;
259
260 if(argc > 0)
261 {
262 fprintf(stderr, "ERROR: Extra args on command line: ");
263 for(; argc > 0; argc--)
264 {
265 fprintf(stderr, " %s", *argv++);
266 }
267 fprintf(stderr, "\n");
268 usage(1); /* never returns */
269 }
270
271 if(mountpoint == NULL)
272 {
273 fprintf(stderr, "ERROR: Missing mountpoint argument\n");
274 exit(1);
275 }
276 if(!password[0])
277 {
278 fprintf(stderr, "WARNING: Missing password argument - are you really sure?\n");
279 }
280
281 switch (mode)
282 {
283 case INFILE:
284 {
285 gpsfd = open(filepath, O_RDONLY);
286 if(!gpsfd)
287 {
288 perror("ERROR: opening input file");
289 exit(1);
290 }
291 /* set blocking mode in case it was not set (seems to be sometimes for fifo's) */
292 fcntl(gpsfd, F_SETFL, 0);
293 printf("file input: file = %s\n", filepath);
294 }
295 break;
296 case SERIAL: /* open serial port */
297 {
298 gpsfd = openserial(ttyport, 1, ttybaud);
299 if(gpsfd < 0)
300 {
301 exit(1);
302 }
303 printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
304 }
305 break;
306 case TCPSOCKET:
307 {
308 in_addr.sin_family = AF_INET;
309 in_addr.sin_port = htons(in_port);
310
311 if((gpsfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
312 {
313 fprintf(stderr, "ERROR: can't create socket\n");
314 exit(1);
315 }
316
317 printf("socket input: host = %s, port = %d%s%s\n",
318 inet_ntoa(in_addr.sin_addr), in_port, initfile ? ", initfile = " : "",
319 initfile ? initfile : "");
320
321 if(connect(gpsfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) < 0)
322 {
323 fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
324 inet_ntoa(in_addr.sin_addr), in_port);
325 exit(1);
326 }
327 if(initfile)
328 {
329 char buffer[1024];
330 FILE *fh;
331 int i;
332
333 if((fh = fopen(initfile, "r")))
334 {
335 while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
336 {
337 if((send(gpsfd, buffer, (size_t)i, 0)) != i)
338 {
339 perror("ERROR: sending init file");
340 exit(1);
341 }
342 }
343 if(i < 0)
344 {
345 perror("ERROR: reading init file");
346 exit(1);
347 }
348 fclose(fh);
349 }
350 else
351 {
352 fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
353 exit(1);
354 }
355 }
356 }
357 break;
358 default:
359 usage(-1);
360 break;
361 }
362
363 out_addr.sin_family = AF_INET;
364 out_addr.sin_port = htons((u_short) (out_port));
365
366 /* ----- main part ----- */
367 while(1)
368 {
369 /* create socket */
370 if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
371 {
372 fprintf(stderr, "ERROR : could not create socket\n");
373 exit(2);
374 }
375 /* connect to caster */
376 fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
377 inet_ntoa(out_addr.sin_addr), out_port, mountpoint);
378 if(connect(sock_id, (struct sockaddr *) &out_addr, sizeof(out_addr)) < 0)
379 {
380 fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
381 inet_ntoa(out_addr.sin_addr), out_port);
382 close(sock_id);
383 exit(3);
384 }
385
386 /* set socket buffer size */
387 setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
388 sizeof(const char *));
389 /* send message to caster */
390 szSendBuffer[0] = '\0';
391 sprintf(szSendBuffer, "SOURCE %s /%s\r\n", password, mountpoint);
392 strcat(szSendBuffer, "Source-Agent: ");
393 strcat(szSendBuffer, VERSION);
394 strcat(szSendBuffer, "\r\n");
395 strcat(szSendBuffer, "\r\n");
396 strcat(szSendBuffer, "\0");
397 nBufferBytes = strlen(szSendBuffer);
398 if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
399 {
400 fprintf(stderr, "ERROR: could not send to caster\n");
401 close(sock_id);
402 sleep(5);
403 exit(0);
404 }
405 /* check caster's response */
406 nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
407 szSendBuffer[nBufferBytes] = '\0';
408 if(strcmp(szSendBuffer, "OK\r\n"))
409 {
410 char *a;
411 fprintf(stderr, "ERROR: caster's reply is not OK : ");
412 for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
413 {
414 fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
415 }
416 fprintf(stderr, "\n");
417 close(sock_id);
418 sleep(5);
419 exit(0);
420 }
421 printf("connection succesfull\n");
422 send_receive_loop(sock_id, gpsfd);
423 }
424 exit(0);
425}
426
427static void send_receive_loop(int sock, int fd)
428{
429 char buffer[BUFSZ] = { 0 };
430 int nBufferBytes = 0, i;
431 /* data transmission */
432 printf("transfering data ...\n");
433 while(1)
434 {
435 if(!nBufferBytes)
436 {
437 /* receiving data */
438 nBufferBytes = read(fd, buffer, BUFSZ);
439 if(!nBufferBytes)
440 {
441 printf("WARNING: no data received from input\n");
442 continue;
443 }
444 else if(nBufferBytes < 0)
445 {
446 perror("ERROR: reading input failed");
447 exit(1);
448 }
449 }
450 /* send data */
451 if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
452 != nBufferBytes)
453 {
454 if(i < 0 && errno != EAGAIN)
455 {
456 perror("WARNING: could not send data - retry connection");
457 close(sock);
458 sleep(5);
459 return;
460 }
461 else if(i)
462 {
463 memmove(buffer, buffer+i, (size_t)(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 * baud : 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
494static int openserial(const char * tty, int blocksz, int baud)
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 (baud)
528 {
529 case 300:
530 baud = B300;
531 break;
532 case 1200:
533 baud = B1200;
534 break;
535 case 2400:
536 baud = B2400;
537 break;
538 case 4800:
539 baud = B4800;
540 break;
541 case 9600:
542 baud = B9600;
543 break;
544 case 19200:
545 baud = B19200;
546 break;
547 case 38400:
548 baud = B38400;
549 break;
550#ifdef B57600
551 case 57600:
552 baud = B57600;
553 break;
554#endif
555#ifdef B115200
556 case 115200:
557 baud = B115200;
558 break;
559#endif
560#ifdef B230400
561 case 230400:
562 baud = B230400;
563 break;
564#endif
565 default:
566 fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
567 baud = B19200;
568 break;
569 }
570#endif
571
572 if(cfsetispeed(&termios, baud) != 0)
573 {
574 perror("ERROR: setting serial speed with cfsetispeed");
575 return (-1);
576 }
577 if(cfsetospeed(&termios, baud) != 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
609static void 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.