forked from nunojpg/ntripserver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ntripserver.c
2263 lines (2159 loc) · 72 KB
/
ntripserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: ntripserver.c,v 1.51 2010/01/22 08:36:59 stoecker Exp $
*
* Copyright (c) 2003...2007
* German Federal Agency for Cartography and Geodesy (BKG)
*
* Developed for Networked Transport of RTCM via Internet Protocol (NTRIP)
* for streaming GNSS data over the Internet.
*
* Designed by Informatik Centrum Dortmund http://www.icd.de
*
* The BKG disclaims any liability nor responsibility to any person or
* entity with respect to any loss or damage caused, or alleged to be
* caused, directly or indirectly by the use and application of the NTRIP
* technology.
*
* For latest information and updates, access:
* http://igs.bkg.bund.de/index_ntrip_down.htm
*
* BKG, Frankfurt, Germany, February 2007
* E-mail: [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* CVS revision and version */
static char revisionstr[] = "$Revision: 1.51 $";
static char datestr[] = "$Date: 2010/01/22 08:36:59 $";
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#ifdef WINDOWSVERSION
#include <winsock2.h>
#include <io.h>
#include <sys/stat.h>
#include <windows.h>
typedef SOCKET sockettype;
typedef u_long in_addr_t;
typedef size_t socklen_t;
typedef u_short uint16_t;
#else
typedef int sockettype;
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/termios.h>
#define closesocket(sock) close(sock)
#define INVALID_HANDLE_VALUE -1
#define INVALID_SOCKET -1
#endif
#ifndef COMPILEDATE
#define COMPILEDATE " built " __DATE__
#endif
#define ALARMTIME (2*60)
#ifndef MSG_DONTWAIT
#define MSG_DONTWAIT 0 /* prevent compiler errors */
#endif
#ifndef O_EXLOCK
#define O_EXLOCK 0 /* prevent compiler errors */
#endif
enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
CASTER = 6, LAST };
enum OUTMODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, UDP = 4, END };
#define AGENTSTRING "NTRIP NtripServerPOSIX"
#define BUFSZ 1024
#define SZ 64
/* default socket source */
#define SERV_HOST_ADDR "localhost"
#define SERV_TCP_PORT 2101
/* default destination */
#define NTRIP_CASTER "www.euref-ip.net"
#define NTRIP_PORT 2101
#define SISNET_SERVER "131.176.49.142"
#define SISNET_PORT 7777
#define RTP_VERSION 2
#define TIME_RESOLUTION 125
static int ttybaud = 19200;
#ifndef WINDOWSVERSION
static const char *ttyport = "/dev/gps";
#else
static const char *ttyport = "COM1";
#endif
static const char *filepath = "/dev/stdin";
static enum MODE inputmode = INFILE;
static int sisnet = 31;
static int gps_file = -1;
static sockettype gps_socket = INVALID_SOCKET;
static sockettype socket_tcp = INVALID_SOCKET;
static sockettype socket_udp = INVALID_SOCKET;
#ifndef WINDOWSVERSION
static int gps_serial = INVALID_HANDLE_VALUE;
static int sigpipe_received = 0;
#else
HANDLE gps_serial = INVALID_HANDLE_VALUE;
#endif
static int sigalarm_received = 0;
static int sigint_received = 0;
static int reconnect_sec = 1;
static const char * casterouthost = NTRIP_CASTER;
static char rtsp_extension[SZ] = "";
static const char * mountpoint = NULL;
static int udp_cseq = 1;
static int udp_tim, udp_seq, udp_init;
/* Forward references */
static void send_receive_loop(sockettype sock, int outmode,
struct sockaddr * pcasterRTP, socklen_t length, unsigned int rtpssrc);
static void usage(int, char *);
static int encode(char *buf, int size, const char *user, const char *pwd);
static int send_to_caster(char *input, sockettype socket, int input_size);
static void close_session(const char *caster_addr, const char *mountpoint,
int session, char *rtsp_ext, int fallback);
static int reconnect(int rec_sec, int rec_sec_max);
static void handle_sigint(int sig);
static void setup_signal_handler(int sig, void (*handler)(int));
#ifndef WINDOWSVERSION
static int openserial(const char * tty, int blocksz, int baud);
static void handle_sigpipe(int sig);
static void handle_alarm(int sig);
#else
static HANDLE openserial(const char * tty, int baud);
#endif
/*
* main
*
* Main entry point for the program. Processes command-line arguments and
* prepares for action.
*
* Parameters:
* argc : integer : Number of command-line arguments.
* argv : array of char : Command-line arguments as an array of
* zero-terminated pointers to strings.
*
* Return Value:
* The function does not return a value (although its return type is int).
*
* Remarks:
*
*/
int main(int argc, char **argv)
{
int c;
int size = 2048; /* for setting send buffer size */
struct sockaddr_in caster;
const char * proxyhost = "";
unsigned int proxyport = 0;
/*** INPUT ***/
const char * casterinhost = 0;
unsigned int casterinport = 0;
const char * inhost = 0;
unsigned int inport = 0;
char get_extension[SZ] = "";
struct hostent * he;
const char * sisnetpassword = "";
const char * sisnetuser = "";
const char * stream_name = 0;
const char * stream_user = 0;
const char * stream_password = 0;
const char * recvrid= 0;
const char * recvrpwd = 0;
const char * initfile = NULL;
int bindmode = 0;
/*** OUTPUT ***/
unsigned int casteroutport = NTRIP_PORT;
const char * outhost = 0;
unsigned int outport = 0;
char post_extension[SZ] = "";
const char * ntrip_str = "";
const char * user = "";
const char * password = "";
int outputmode = NTRIP1;
struct sockaddr_in casterRTP;
struct sockaddr_in local;
int client_port = 0;
int server_port = 0;
unsigned int session = 0;
socklen_t len = 0;
int i = 0;
char szSendBuffer[BUFSZ];
char authorization[SZ];
int nBufferBytes = 0;
char * dlim = " \r\n=";
char * token;
char * tok_buf[BUFSZ];
int reconnect_sec_max = 0;
setbuf(stdout, 0);
setbuf(stdin, 0);
setbuf(stderr, 0);
{
char *a;
int i = 0;
for(a = revisionstr+11; *a && *a != ' '; ++a)
revisionstr[i++] = *a;
revisionstr[i] = 0;
datestr[0] = datestr[7];
datestr[1] = datestr[8];
datestr[2] = datestr[9];
datestr[3] = datestr[10];
datestr[5] = datestr[12];
datestr[6] = datestr[13];
datestr[8] = datestr[15];
datestr[9] = datestr[16];
datestr[4] = datestr[7] = '-';
datestr[10] = 0;
}
/* setup signal handler for CTRL+C */
setup_signal_handler(SIGINT, handle_sigint);
#ifndef WINDOWSVERSION
/* setup signal handler for boken pipe */
setup_signal_handler(SIGPIPE, handle_sigpipe);
/* setup signal handler for timeout */
setup_signal_handler(SIGALRM, handle_alarm);
alarm(ALARMTIME);
#else
/* winsock initialization */
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData))
{
fprintf(stderr, "Could not init network access.\n");
return 20;
}
#endif
/* get and check program arguments */
if(argc <= 1)
{
usage(2, argv[0]);
exit(1);
}
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:O:E:F:R:N:n:B")) != EOF)
{
switch (c)
{
case 'M': /*** InputMode ***/
if(!strcmp(optarg, "serial")) inputmode = SERIAL;
else if(!strcmp(optarg, "tcpsocket")) inputmode = TCPSOCKET;
else if(!strcmp(optarg, "file")) inputmode = INFILE;
else if(!strcmp(optarg, "sisnet")) inputmode = SISNET;
else if(!strcmp(optarg, "udpsocket")) inputmode = UDPSOCKET;
else if(!strcmp(optarg, "caster")) inputmode = CASTER;
else inputmode = atoi(optarg);
if((inputmode == 0) || (inputmode >= LAST))
{
fprintf(stderr, "ERROR: can't convert <%s> to a valid InputMode\n",
optarg);
usage(-1, argv[0]);
}
break;
case 'i': /* serial input device */
ttyport = optarg;
break;
case 'B': /* bind to incoming UDP stream */
bindmode = 1;
break;
case 'V': /* Sisnet data server version number */
if(!strcmp("3.0", optarg)) sisnet = 30;
else if(!strcmp("3.1", optarg)) sisnet = 31;
else if(!strcmp("2.1", optarg)) sisnet = 21;
else
{
fprintf(stderr, "ERROR: unknown SISNeT version <%s>\n", optarg);
usage(-2, argv[0]);
}
break;
case 'b': /* serial input baud rate */
ttybaud = atoi(optarg);
if(ttybaud <= 1)
{
fprintf(stderr, "ERROR: can't convert <%s> to valid serial baud rate\n",
optarg);
usage(1, argv[0]);
}
break;
case 'a': /* Destination caster address */
casterouthost = optarg;
break;
case 'p': /* Destination caster port */
casteroutport = atoi(optarg);
if(casteroutport <= 1 || casteroutport > 65535)
{
fprintf(stderr,
"ERROR: can't convert <%s> to a valid HTTP server port\n", optarg);
usage(1, argv[0]);
}
break;
case 'm': /* Destination caster mountpoint for stream upload */
mountpoint = optarg;
break;
case 's': /* File name for input data simulation from file */
filepath = optarg;
break;
case 'f': /* name of an initialization file */
initfile = optarg;
break;
case 'x': /* user ID to access incoming stream */
recvrid = optarg;
break;
case 'y': /* password to access incoming stream */
recvrpwd = optarg;
break;
case 'u': /* Sisnet data server user ID */
sisnetuser = optarg;
break;
case 'l': /* Sisnet data server password */
sisnetpassword = optarg;
break;
case 'c': /* DestinationCaster password for stream upload to mountpoint */
password = optarg;
break;
case 'H': /* Input host address*/
casterinhost = optarg;
break;
case 'P': /* Input port */
casterinport = atoi(optarg);
if(casterinport <= 1 || casterinport > 65535)
{
fprintf(stderr, "ERROR: can't convert <%s> to a valid port number\n",
optarg);
usage(1, argv[0]);
}
break;
case 'D': /* Source caster mountpoint for stream input */
stream_name = optarg;
break;
case 'U': /* Source caster user ID for input stream access */
stream_user = optarg;
break;
case 'W': /* Source caster password for input stream access */
stream_password = optarg;
break;
case 'E': /* Proxy Server */
proxyhost = optarg;
break;
case 'F': /* Proxy port */
proxyport = atoi(optarg);
break;
case 'R': /* maximum delay between reconnect attempts in seconds */
reconnect_sec_max = atoi(optarg);
break;
case 'O': /* OutputMode */
outputmode = 0;
if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
outputmode = NTRIP1;
else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
outputmode = HTTP;
else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
outputmode = RTSP;
else if(!strcmp(optarg,"u") || !strcmp(optarg,"udp"))
outputmode = UDP;
else outputmode = atoi(optarg);
if((outputmode == 0) || (outputmode >= END))
{
fprintf(stderr, "ERROR: can't convert <%s> to a valid OutputMode\n",
optarg);
usage(-1, argv[0]);
}
break;
case 'n': /* Destination caster user ID for stream upload to mountpoint */
user = optarg;
break;
case 'N': /* Ntrip-STR, optional for Ntrip Version 2.0 */
ntrip_str = optarg;
break;
case 'h': /* print help screen */
case '?':
usage(0, argv[0]);
break;
default:
usage(2, argv[0]);
break;
}
}
argc -= optind;
argv += optind;
/*** argument analysis ***/
if(argc > 0)
{
fprintf(stderr, "ERROR: Extra args on command line: ");
for(; argc > 0; argc--)
{
fprintf(stderr, " %s", *argv++);
}
fprintf(stderr, "\n");
usage(1, argv[0]); /* never returns */
}
if((reconnect_sec_max > 0) && (reconnect_sec_max < 256))
{
fprintf(stderr,
"WARNING: maximum delay between reconnect attemts changed from %d to 256 seconds\n"
, reconnect_sec_max);
reconnect_sec_max = 256;
}
if(!mountpoint)
{
fprintf(stderr, "ERROR: Missing mountpoint argument for stream upload\n");
exit(1);
}
if(!password[0])
{
fprintf(stderr, "WARNING: Missing password argument for stream upload - "
"are you really sure?\n");
}
else
{
nBufferBytes += encode(authorization, sizeof(authorization), user,
password);
if(nBufferBytes > (int)sizeof(authorization))
{
fprintf(stderr, "ERROR: user ID and/or password too long: %d (%d)\n"
" user ID: %s \npassword: <%s>\n",
nBufferBytes, (int)sizeof(authorization), user, password);
exit(1);
}
}
if(stream_name && stream_user && !stream_password)
{
fprintf(stderr, "WARNING: Missing password argument for stream download"
" - are you really sure?\n");
}
/*** proxy server handling ***/
if(*proxyhost)
{
outhost = inhost = proxyhost;
outport = inport = proxyport;
i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d",
casterouthost, casteroutport);
if((i > SZ) || (i < 0))
{
fprintf(stderr, "ERROR: Destination caster name/port to long - "
"length = %d (max: %d)\n", i, SZ);
exit(0);
}
else
{
strncpy(post_extension, szSendBuffer, (size_t)i);
strcpy(szSendBuffer, "");
i = snprintf(szSendBuffer, sizeof(szSendBuffer),":%d", casteroutport);
strncpy(rtsp_extension, szSendBuffer, SZ);
strcpy(szSendBuffer,""); i = 0;
}
i = snprintf(szSendBuffer, sizeof(szSendBuffer),"http://%s:%d", casterinhost, casterinport);
if((i > SZ) || (i < 0))
{
fprintf(stderr,"ERROR: Destination caster name/port to long - length = %d (max: %d)\n", i, SZ);
exit(0);
}
else
{
strncpy(get_extension, szSendBuffer, (size_t)i);
strcpy(szSendBuffer, "");
i = 0;
}
}
else
{
outhost = casterouthost; outport = casteroutport;
inhost = casterinhost; inport = casterinport;
}
while(inputmode != LAST)
{
int input_init = 1;
if(sigint_received) break;
/*** InputMode handling ***/
switch(inputmode)
{
case INFILE:
{
if((gps_file = open(filepath, O_RDONLY)) < 0)
{
perror("ERROR: opening input file");
exit(1);
}
#ifndef WINDOWSVERSION
/* set blocking inputmode in case it was not set
(seems to be sometimes for fifo's) */
fcntl(gps_file, F_SETFL, 0);
#endif
printf("file input: file = %s\n", filepath);
}
break;
case SERIAL: /* open serial port */
{
#ifndef WINDOWSVERSION
gps_serial = openserial(ttyport, 1, ttybaud);
#else
gps_serial = openserial(ttyport, ttybaud);
#endif
if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
if(initfile)
{
char buffer[1024];
FILE *fh;
int i;
if((fh = fopen(initfile, "r")))
{
while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
{
#ifndef WINDOWSVERSION
if((write(gps_serial, buffer, i)) != i)
{
perror("WARNING: sending init file");
input_init = 0;
break;
}
#else
DWORD nWrite = -1;
if(!WriteFile(gps_serial, buffer, sizeof(buffer), &nWrite, NULL))
{
fprintf(stderr,"ERROR: sending init file \n");
input_init = 0;
break;
}
i = (int)nWrite;
#endif
}
if(i < 0)
{
perror("ERROR: reading init file");
reconnect_sec_max = 0;
input_init = 0;
break;
}
fclose(fh);
}
else
{
fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
reconnect_sec_max = 0;
input_init = 0;
break;
}
}
}
break;
case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
{
if(inputmode == SISNET)
{
if(!inhost) inhost = SISNET_SERVER;
if(!inport) inport = SISNET_PORT;
}
else if(inputmode == CASTER)
{
if(!inport) inport = NTRIP_PORT;
if(!inhost) inhost = NTRIP_CASTER;
}
else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
{
if(!inport) inport = SERV_TCP_PORT;
if(!inhost) inhost = SERV_HOST_ADDR;
}
if(!(he = gethostbyname(inhost)))
{
fprintf(stderr, "ERROR: Input host <%s> unknown\n", inhost);
usage(-2, argv[0]);
}
if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
{
fprintf(stderr,
"ERROR: can't create socket for incoming data stream\n");
exit(1);
}
memset((char *) &caster, 0x00, sizeof(caster));
if(!bindmode)
memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
caster.sin_family = AF_INET;
caster.sin_port = htons(inport);
fprintf(stderr, "%s input: host = %s, port = %d, %s%s%s%s%s\n",
inputmode == CASTER ? "caster" : inputmode == SISNET ? "sisnet" :
inputmode == TCPSOCKET ? "tcp socket" : "udp socket",
bindmode ? "127.0.0.1" : inet_ntoa(caster.sin_addr),
inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
initfile ? ", initfile = " : "", initfile ? initfile : "",
bindmode ? "binding mode" : "");
if(bindmode)
{
if(bind(gps_socket, (struct sockaddr *) &caster, sizeof(caster)) < 0)
{
fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
reconnect_sec_max = 0;
input_init = 0;
break;
}
} /* connect to input-caster or proxy server*/
else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
{
fprintf(stderr, "WARNING: can't connect input to %s at port %d\n",
inet_ntoa(caster.sin_addr), inport);
input_init = 0;
break;
}
if(stream_name) /* input from Ntrip Version 1.0 caster*/
{
int init = 0;
/* set socket buffer size */
setsockopt(gps_socket, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
sizeof(const char *));
if(stream_user && stream_password)
{
/* leave some space for login */
nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
"GET %s/%s HTTP/1.0\r\n"
"User-Agent: %s/%s\r\n"
"Connection: close\r\n"
"Authorization: Basic ", get_extension, stream_name,
AGENTSTRING, revisionstr);
/* second check for old glibc */
if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
{
fprintf(stderr, "ERROR: Source caster request too long\n");
input_init = 0;
reconnect_sec_max =0;
break;
}
nBufferBytes += encode(szSendBuffer+nBufferBytes,
sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
{
fprintf(stderr,
"ERROR: Source caster user ID and/or password too long\n");
input_init = 0;
reconnect_sec_max =0;
break;
}
szSendBuffer[nBufferBytes++] = '\r';
szSendBuffer[nBufferBytes++] = '\n';
szSendBuffer[nBufferBytes++] = '\r';
szSendBuffer[nBufferBytes++] = '\n';
}
else
{
nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
"GET %s/%s HTTP/1.0\r\n"
"User-Agent: %s/%s\r\n"
"Connection: close\r\n"
"\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
}
if((send(gps_socket, szSendBuffer, (size_t)nBufferBytes, 0))
!= nBufferBytes)
{
fprintf(stderr, "WARNING: could not send Source caster request\n");
input_init = 0;
break;
}
nBufferBytes = 0;
/* check Source caster's response */
while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
&& (nBufferBytes += recv(gps_socket, szSendBuffer,
sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
{
if(strstr(szSendBuffer, "\r\n"))
{
if(!strstr(szSendBuffer, "ICY 200 OK"))
{
int k;
fprintf(stderr,
"ERROR: could not get requested data from Source caster: ");
for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
&& szSendBuffer[k] != '\r'; ++k)
{
fprintf(stderr, "%c", isprint(szSendBuffer[k])
? szSendBuffer[k] : '.');
}
fprintf(stderr, "\n");
if(!strstr(szSendBuffer, "SOURCETABLE 200 OK"))
{
reconnect_sec_max =0;
}
input_init = 0;
break;
}
else init = 1;
}
}
} /* end input from Ntrip Version 1.0 caster */
if(initfile && inputmode != SISNET)
{
char buffer[1024];
FILE *fh;
int i;
if((fh = fopen(initfile, "r")))
{
while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
{
if((send(gps_socket, buffer, (size_t)i, 0)) != i)
{
perror("WARNING: sending init file");
input_init = 0;
break;
}
}
if(i < 0)
{
perror("ERROR: reading init file");
reconnect_sec_max = 0;
input_init = 0;
break;
}
fclose(fh);
}
else
{
fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
reconnect_sec_max = 0;
input_init = 0;
break;
}
}
}
if(inputmode == SISNET)
{
int i, j;
char buffer[1024];
i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
: "AUTH,%s,%s", sisnetuser, sisnetpassword);
if((send(gps_socket, buffer, (size_t)i, 0)) != i)
{
perror("WARNING: sending authentication for SISNeT data server");
input_init = 0;
break;
}
i = sisnet >= 30 ? 7 : 5;
if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
{
fprintf(stderr, "WARNING: SISNeT connect failed:");
for(i = 0; i < j; ++i)
{
if(buffer[i] != '\r' && buffer[i] != '\n')
{
fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
}
}
fprintf(stderr, "\n");
input_init = 0;
break;
}
if(sisnet >= 31)
{
if((send(gps_socket, "START\r\n", 7, 0)) != i)
{
perror("WARNING: sending Sisnet start command");
input_init = 0;
break;
}
}
}
/*** receiver authentication ***/
if (recvrid && recvrpwd && ((inputmode == TCPSOCKET)
|| (inputmode == UDPSOCKET)))
{
if (strlen(recvrid) > (BUFSZ-3))
{
fprintf(stderr, "ERROR: Receiver ID too long\n");
reconnect_sec_max = 0;
input_init = 0;
break;
}
else
{
fprintf(stderr, "Sending user ID for receiver...\n");
nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
strcpy(szSendBuffer, recvrid);
strcat(szSendBuffer,"\r\n");
if(send(gps_socket,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
{
perror("WARNING: sending user ID for receiver");
input_init = 0;
break;
}
}
if (strlen(recvrpwd) > (BUFSZ-3))
{
fprintf(stderr, "ERROR: Receiver password too long\n");
reconnect_sec_max = 0;
input_init = 0;
break;
}
else
{
fprintf(stderr, "Sending user password for receiver...\n");
nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
strcpy(szSendBuffer, recvrpwd);
strcat(szSendBuffer,"\r\n");
if(send(gps_socket, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT) < 0)
{
perror("WARNING: sending user password for receiver");
input_init = 0;
break;
}
}
}
break;
default:
usage(-1, argv[0]);
break;
}
/* ----- main part ----- */
int output_init = 1, fallback = 0;
while((input_init) && (output_init))
{
#ifndef WINDOWSVERSION
if((sigalarm_received) || (sigint_received) || (sigpipe_received)) break;
#else
if((sigalarm_received) || (sigint_received)) break;
#endif
if(!(he = gethostbyname(outhost)))
{
fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
outhost);
close_session(casterouthost, mountpoint, session, rtsp_extension, 0);
usage(-2, argv[0]);
}
/* create socket */
if((socket_tcp = socket(AF_INET, (outputmode == UDP ? SOCK_DGRAM
: SOCK_STREAM), 0)) == INVALID_SOCKET)
{
perror("ERROR: tcp socket");
reconnect_sec_max = 0;
break;
}
memset((char *) &caster, 0x00, sizeof(caster));
memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
caster.sin_family = AF_INET;
caster.sin_port = htons(outport);
/* connect to Destination caster or Proxy server*/
fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s"
", mode = %s\n\n", inet_ntoa(caster.sin_addr), outport, mountpoint,
outputmode == NTRIP1 ? "ntrip1" : outputmode == HTTP ? "http" :
outputmode == UDP ? "udp" : "rtsp");
if(connect(socket_tcp, (struct sockaddr *) &caster, sizeof(caster)) < 0)
{
fprintf(stderr, "WARNING: can't connect output to %s at port %d\n",
inet_ntoa(caster.sin_addr), outport);
break;
}
/*** OutputMode handling ***/
switch(outputmode)
{
case UDP:
{
unsigned int session;
char rtpbuf[1526];
int i=12, j;
udp_init = time(0);
srand(udp_init);
session = rand();
udp_tim = rand();
udp_seq = rand();
rtpbuf[0] = (2<<6);
/* padding, extension, csrc are empty */
rtpbuf[1] = 97;
/* marker is empty */
rtpbuf[2] = (udp_seq>>8)&0xFF;
rtpbuf[3] = (udp_seq)&0xFF;
rtpbuf[4] = (udp_tim>>24)&0xFF;
rtpbuf[5] = (udp_tim>>16)&0xFF;
rtpbuf[6] = (udp_tim>>8)&0xFF;
rtpbuf[7] = (udp_tim)&0xFF;
/* sequence and timestamp are empty */
rtpbuf[8] = (session>>24)&0xFF;
rtpbuf[9] = (session>>16)&0xFF;
rtpbuf[10] = (session>>8)&0xFF;
rtpbuf[11] = (session)&0xFF;
++udp_seq;
j = snprintf(rtpbuf+i, sizeof(rtpbuf)-i-40, /* leave some space for login */
"POST /%s HTTP/1.1\r\n"
"Host: %s\r\n"
"Ntrip-Version: Ntrip/2.0\r\n"
"User-Agent: %s/%s\r\n"
"Authorization: Basic %s%s%s\r\n"
"Connection: close\r\n"
"Transfer-Encoding: chunked\r\n\r\n",
mountpoint, casterouthost, AGENTSTRING,
revisionstr, authorization, ntrip_str ? (outputmode == NTRIP1 ? "\r\nSTR: " : "\r\nNtrip-STR: ") : "",
ntrip_str);
i += j;
if(i > (int)sizeof(rtpbuf)-40 || j < 0) /* second check for old glibc */
{
fprintf(stderr, "Requested data too long\n");
reconnect_sec_max = 0;
output_init = 0;
break;
}
else
{
rtpbuf[i++] = '\r';
rtpbuf[i++] = '\n';
rtpbuf[i++] = '\r';
rtpbuf[i++] = '\n';
if(send(socket_tcp, rtpbuf, i, 0) != i)
{
perror("Could not send UDP packet");
reconnect_sec_max = 0;
output_init = 0;
break;
}
else
{
int stop = 0;
int numbytes;
if((numbytes=recv(socket_tcp, rtpbuf, sizeof(rtpbuf)-1, 0)) > 0)
{
/* we don't expect message longer than 1513, so we cut the last
byte for security reasons to prevent buffer overrun */
rtpbuf[numbytes] = 0;
if(numbytes > 17+12 &&
(!strncmp(rtpbuf+12, "HTTP/1.1 200 OK\r\n", 17) ||
!strncmp(rtpbuf+12, "HTTP/1.0 200 OK\r\n", 17)))
{
const char *sessioncheck = "session: ";
int l = strlen(sessioncheck)-1;