Changeset 1570 in ntrip for trunk/ntripserver


Ignore:
Timestamp:
Feb 10, 2009, 1:20:09 PM (15 years ago)
Author:
stoecker
Message:

added plain UDP mode

Location:
trunk/ntripserver
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ntripserver/makefile

    r653 r1570  
    11#!/usr/bin/make
    2 # $Id: makefile,v 1.7 2007/08/30 14:57:34 stuerze Exp $
     2# $Id: makefile,v 1.8 2008/01/04 15:22:44 stuerze Exp $
    33
    44ifdef windir
    55CC   = gcc
    6 OPTS = -Wall -W -O3 -DWINDOWSVERSION
     6OPTS = -Wall -W -DWINDOWSVERSION
    77LIBS = -lwsock32
    88else
    9 OPTS = -Wall -W -O3
     9OPTS = -Wall -W
    1010endif
    1111
    1212ntripserver: ntripserver.c
    13         $(CC) $(OPTS) $? -DNDEBUG -o $@ $(LIBS)
     13        $(CC) $(OPTS) $? -O3 -DNDEBUG -o $@ $(LIBS)
    1414
    1515debug: ntripserver.c
    16         $(CC) $(OPTS) $? -o ntripserver $(LIBS)
     16        $(CC) $(OPTS) $? -g -o ntripserver $(LIBS)
    1717
    1818clean:
  • trunk/ntripserver/ntripserver.c

    r1113 r1570  
    11/*
    2  * $Id: ntripserver.c,v 1.44 2008/06/06 07:34:09 stuerze Exp $
     2 * $Id: ntripserver.c,v 1.45 2008/09/11 09:18:23 stuerze Exp $
    33 *
    44 * Copyright (c) 2003...2007
     
    3737
    3838/* CVS revision and version */
    39 static char revisionstr[] = "$Revision: 1.44 $";
    40 static char datestr[]     = "$Date: 2008/06/06 07:34:09 $";
     39static char revisionstr[] = "$Revision: 1.45 $";
     40static char datestr[]     = "$Date: 2008/09/11 09:18:23 $";
    4141
    4242#include <ctype.h>
     
    5656  #include <winsock2.h>
    5757  #include <io.h>
    58   #include <sys/stat.h> 
    59   #include <windows.h> 
     58  #include <sys/stat.h>
     59  #include <windows.h>
    6060  typedef SOCKET sockettype;
    6161  typedef u_long in_addr_t;
     
    9090CASTER = 6, LAST };
    9191
    92 enum OUTMODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, END };
     92enum OUTMODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, UDP = 4, END };
    9393
    9494#define AGENTSTRING     "NTRIP NtripServerPOSIX"
     
    132132static const char * mountpoint = NULL;
    133133static int udp_cseq            = 1;
     134static int udp_tim, udp_seq, udp_init;
    134135
    135136/* Forward references */
     
    150151#else
    151152static HANDLE openserial(const char * tty, int baud);
    152 #endif 
     153#endif
    153154
    154155
     
    256257  /* setup signal handler for CTRL+C */
    257258  setup_signal_handler(SIGINT, handle_sigint);
    258 #ifndef WINDOWSVERSION 
     259#ifndef WINDOWSVERSION
    259260  /* setup signal handler for boken pipe */
    260261  setup_signal_handler(SIGPIPE, handle_sigpipe);
     
    271272  }
    272273#endif
    273  
     274
    274275  /* get and check program arguments */
    275276  if(argc <= 1)
     
    397398      else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
    398399        outputmode = RTSP;
     400      else if(!strcmp(optarg,"u") || !strcmp(optarg,"udp"))
     401        outputmode = UDP;
    399402      else outputmode = atoi(optarg);
    400403      if((outputmode == 0) || (outputmode >= END))
     
    535538    case INFILE:
    536539      {
    537         if((gps_file = open(filepath, O_RDONLY)) < 0)
    538         {
     540        if((gps_file = open(filepath, O_RDONLY)) < 0)
     541        {
    539542          perror("ERROR: opening input file");
    540543          exit(1);
    541         }
     544        }
    542545#ifndef WINDOWSVERSION
    543         /* set blocking inputmode in case it was not set
     546        /* set blocking inputmode in case it was not set
    544547          (seems to be sometimes for fifo's) */
    545         fcntl(gps_file, F_SETFL, 0);
    546 #endif
    547         printf("file input: file = %s\n", filepath);
     548        fcntl(gps_file, F_SETFL, 0);
     549#endif
     550        printf("file input: file = %s\n", filepath);
    548551      }
    549552      break;
     
    551554      {
    552555#ifndef WINDOWSVERSION
    553         gps_serial = openserial(ttyport, 1, ttybaud);
     556        gps_serial = openserial(ttyport, 1, ttybaud);
    554557#else
    555         gps_serial = openserial(ttyport, ttybaud);
    556 #endif
    557         if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
    558         printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
    559 
    560         if(initfile)
    561         {
     558        gps_serial = openserial(ttyport, ttybaud);
     559#endif
     560        if(gps_serial == INVALID_HANDLE_VALUE) exit(1);
     561        printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
     562
     563        if(initfile)
     564        {
    562565          char buffer[1024];
    563566          FILE *fh;
     
    571574              if((write(gps_serial, buffer, i)) != i)
    572575              {
    573                 perror("WARNING: sending init file");
    574                 input_init = 0;
    575                 break;
     576                perror("WARNING: sending init file");
     577                input_init = 0;
     578                break;
    576579              }
    577580#else
    578               DWORD nWrite = -1; 
     581              DWORD nWrite = -1;
    579582              if(!WriteFile(gps_serial, buffer, sizeof(buffer), &nWrite, NULL))
    580583              {
    581584                fprintf(stderr,"ERROR: sending init file \n");
    582                 input_init = 0;
     585                input_init = 0;
    583586                break;
    584587              }
     
    602605            break;
    603606          }
    604         }
     607        }
    605608      }
    606609      break;
    607610    case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
    608611      {
    609         if(inputmode == SISNET)
    610         {
     612        if(inputmode == SISNET)
     613        {
    611614          if(!inhost) inhost = SISNET_SERVER;
    612615          if(!inport) inport = SISNET_PORT;
    613         }
    614         else if(inputmode == CASTER)
    615         {
     616        }
     617        else if(inputmode == CASTER)
     618        {
    616619          if(!inport) inport = NTRIP_PORT;
    617620          if(!inhost) inhost = NTRIP_CASTER;
    618         }
    619         else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
    620         {
     621        }
     622        else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
     623        {
    621624          if(!inport) inport = SERV_TCP_PORT;
    622625          if(!inhost) inhost = SERV_HOST_ADDR;
    623         }
    624 
    625         if(!(he = gethostbyname(inhost)))
    626         {
     626        }
     627
     628        if(!(he = gethostbyname(inhost)))
     629        {
    627630          fprintf(stderr, "ERROR: Input host <%s> unknown\n", inhost);
    628631          usage(-2, argv[0]);
    629         }
    630 
    631         if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
    632         ? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
    633         {
     632        }
     633
     634        if((gps_socket = socket(AF_INET, inputmode == UDPSOCKET
     635        ? SOCK_DGRAM : SOCK_STREAM, 0)) == INVALID_SOCKET)
     636        {
    634637          fprintf(stderr,
    635638          "ERROR: can't create socket for incoming data stream\n");
    636639          exit(1);
    637         }
    638 
    639         memset((char *) &caster, 0x00, sizeof(caster));
    640         if(!bindmode)
     640        }
     641
     642        memset((char *) &caster, 0x00, sizeof(caster));
     643        if(!bindmode)
    641644          memcpy(&caster.sin_addr, he->h_addr, (size_t)he->h_length);
    642         caster.sin_family = AF_INET;
    643         caster.sin_port = htons(inport);
    644 
    645         fprintf(stderr, "%s input: host = %s, port = %d, %s%s%s%s%s\n",
    646         inputmode == CASTER ? "caster" : inputmode == SISNET ? "sisnet" :
    647         inputmode == TCPSOCKET ? "tcp socket" : "udp socket",
    648         bindmode ? "127.0.0.1" : inet_ntoa(caster.sin_addr),
    649         inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
    650         initfile ? ", initfile = " : "", initfile ? initfile : "",
    651         bindmode ? "binding mode" : "");
    652 
    653         if(bindmode)
    654         {
     645        caster.sin_family = AF_INET;
     646        caster.sin_port = htons(inport);
     647
     648        fprintf(stderr, "%s input: host = %s, port = %d, %s%s%s%s%s\n",
     649        inputmode == CASTER ? "caster" : inputmode == SISNET ? "sisnet" :
     650        inputmode == TCPSOCKET ? "tcp socket" : "udp socket",
     651        bindmode ? "127.0.0.1" : inet_ntoa(caster.sin_addr),
     652        inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
     653        initfile ? ", initfile = " : "", initfile ? initfile : "",
     654        bindmode ? "binding mode" : "");
     655
     656        if(bindmode)
     657        {
    655658          if(bind(gps_socket, (struct sockaddr *) &caster, sizeof(caster)) < 0)
    656659          {
     
    660663            break;
    661664          }
    662         } /* connect to input-caster or proxy server*/
    663         else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
    664         {
     665        } /* connect to input-caster or proxy server*/
     666        else if(connect(gps_socket, (struct sockaddr *)&caster, sizeof(caster)) < 0)
     667        {
    665668          fprintf(stderr, "WARNING: can't connect input to %s at port %d\n",
    666669          inet_ntoa(caster.sin_addr), inport);
    667670          input_init = 0;
    668671          break;
    669         }
    670 
    671         if(stream_name) /* input from Ntrip Version 1.0 caster*/
    672         {
     672        }
     673
     674        if(stream_name) /* input from Ntrip Version 1.0 caster*/
     675        {
    673676          int init = 0;
    674677
     
    702705              reconnect_sec_max =0;
    703706              break;
    704             }             
     707            }
    705708            szSendBuffer[nBufferBytes++] = '\r';
    706709            szSendBuffer[nBufferBytes++] = '\n';
     
    730733          {
    731734            if(strstr(szSendBuffer, "\r\n"))
    732             {             
     735            {
    733736              if(!strstr(szSendBuffer, "ICY 200 OK"))
    734737              {
     
    741744                  fprintf(stderr, "%c", isprint(szSendBuffer[k])
    742745                  ? szSendBuffer[k] : '.');
    743                 }
     746                }
    744747                fprintf(stderr, "\n");
    745748                if(!strstr(szSendBuffer, "SOURCETABLE 200 OK"))
     
    753756            }
    754757          }
    755         } /* end input from Ntrip Version 1.0 caster */
    756 
    757         if(initfile && inputmode != SISNET)
    758         {
     758        } /* end input from Ntrip Version 1.0 caster */
     759
     760        if(initfile && inputmode != SISNET)
     761        {
    759762          char buffer[1024];
    760763          FILE *fh;
     
    767770              if((send(gps_socket, buffer, (size_t)i, 0)) != i)
    768771              {
    769                 perror("WARNING: sending init file");
    770                 input_init = 0;
    771                 break;
     772                perror("WARNING: sending init file");
     773                input_init = 0;
     774                break;
    772775              }
    773776            }
     
    788791            break;
    789792          }
    790         }
     793        }
    791794      }
    792795      if(inputmode == SISNET)
    793796      {
    794         int i, j;
    795         char buffer[1024];
    796 
    797         i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
     797        int i, j;
     798        char buffer[1024];
     799
     800        i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
    798801          : "AUTH,%s,%s", sisnetuser, sisnetpassword);
    799         if((send(gps_socket, buffer, (size_t)i, 0)) != i)
    800         {
     802        if((send(gps_socket, buffer, (size_t)i, 0)) != i)
     803        {
    801804          perror("WARNING: sending authentication for SISNeT data server");
    802805          input_init = 0;
    803806          break;
    804         }
    805         i = sisnet >= 30 ? 7 : 5;
    806         if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
    807         {
     807        }
     808        i = sisnet >= 30 ? 7 : 5;
     809        if((j = recv(gps_socket, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
     810        {
    808811          fprintf(stderr, "WARNING: SISNeT connect failed:");
    809812          for(i = 0; i < j; ++i)
     
    817820          input_init = 0;
    818821          break;
    819         }
    820         if(sisnet >= 31)
    821         {
     822        }
     823        if(sisnet >= 31)
     824        {
    822825          if((send(gps_socket, "START\r\n", 7, 0)) != i)
    823826          {
     
    826829            break;
    827830          }
    828         }
     831        }
    829832      }
    830833      /*** receiver authentication  ***/
     
    832835      || (inputmode == UDPSOCKET)))
    833836      {
    834         if (strlen(recvrid) > (BUFSZ-3))
    835         {
     837        if (strlen(recvrid) > (BUFSZ-3))
     838        {
    836839          fprintf(stderr, "ERROR: Receiver ID too long\n");
    837840          reconnect_sec_max = 0;
    838841          input_init = 0;
    839842          break;
    840         }
    841         else
    842         {
     843        }
     844        else
     845        {
    843846          fprintf(stderr, "Sending user ID for receiver...\n");
    844847          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
     
    851854            break;
    852855          }
    853         }
    854 
    855         if (strlen(recvrpwd) > (BUFSZ-3))
    856         {
    857           fprintf(stderr, "ERROR: Receiver password too long\n"); 
     856        }
     857
     858        if (strlen(recvrpwd) > (BUFSZ-3))
     859        {
     860          fprintf(stderr, "ERROR: Receiver password too long\n");
    858861          reconnect_sec_max = 0;
    859862          input_init = 0;
    860863          break;
    861         }
    862         else
    863         {
     864        }
     865        else
     866        {
    864867          fprintf(stderr, "Sending user password for receiver...\n");
    865868          nBufferBytes = recv(gps_socket, szSendBuffer, BUFSZ, 0);
     
    872875            break;
    873876          }
    874         }
     877        }
    875878      }
    876879      break;
     
    879882      break;
    880883    }
    881    
     884
    882885    /* ----- main part ----- */
    883886    int output_init = 1, fallback = 0;
     
    892895      if(!(he = gethostbyname(outhost)))
    893896      {
    894         fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
    895         outhost);
     897        fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
     898        outhost);
    896899        close_session(casterouthost, mountpoint, session, rtsp_extension, 0);
    897         usage(-2, argv[0]);
     900        usage(-2, argv[0]);
    898901      }
    899902
    900903      /* create socket */
    901       if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    902       {
    903         perror("ERROR: tcp socket");
     904      if((socket_tcp = socket(AF_INET, (outputmode == UDP ? SOCK_DGRAM
     905      : SOCK_STREAM), 0)) == INVALID_SOCKET)
     906      {
     907        perror("ERROR: tcp socket");
    904908        reconnect_sec_max = 0;
    905909        break;
     
    914918      fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s"
    915919      ", mode = %s\n\n", inet_ntoa(caster.sin_addr), outport, mountpoint,
    916       outputmode == NTRIP1 ? "ntrip1" : outputmode == HTTP ? "http" : "rtsp");
     920      outputmode == NTRIP1 ? "ntrip1" : outputmode == HTTP ? "http" :
     921      outputmode == UDP ? "udp" : "rtsp");
    917922
    918923      if(connect(socket_tcp, (struct sockaddr *) &caster, sizeof(caster)) < 0)
    919924      {
    920         fprintf(stderr, "WARNING: can't connect output to %s at port %d\n",
     925        fprintf(stderr, "WARNING: can't connect output to %s at port %d\n",
    921926          inet_ntoa(caster.sin_addr), outport);
    922927        break;
     
    926931      switch(outputmode)
    927932      {
    928         case NTRIP1: /*** OutputMode Ntrip Version 1.0 ***/
     933        case UDP:
     934          {
     935            int session;
     936            char rtpbuf[1526];
     937            int i=12, j;
     938
     939            udp_init = time(0);
     940            srand(udp_init);
     941            session = rand();
     942            udp_tim = rand();
     943            udp_seq = rand();
     944
     945            rtpbuf[0] = (2<<6);
     946            /* padding, extension, csrc are empty */
     947            rtpbuf[1] = 97;
     948            /* marker is empty */
     949            rtpbuf[2] = (udp_seq>>8)&0xFF;
     950            rtpbuf[3] = (udp_seq)&0xFF;
     951            rtpbuf[4] = (udp_tim>>24)&0xFF;
     952            rtpbuf[5] = (udp_tim>>16)&0xFF;
     953            rtpbuf[6] = (udp_tim>>8)&0xFF;
     954            rtpbuf[7] = (udp_tim)&0xFF;
     955            /* sequence and timestamp are empty */
     956            rtpbuf[8] = (session>>24)&0xFF;
     957            rtpbuf[9] = (session>>16)&0xFF;
     958            rtpbuf[10] = (session>>8)&0xFF;
     959            rtpbuf[11] = (session)&0xFF;
     960            ++udp_seq;
     961
     962            j = snprintf(rtpbuf+i, sizeof(rtpbuf)-i-40, /* leave some space for login */
     963            "POST /%s HTTP/1.1\r\n"
     964            "Host: %s\r\n"
     965            "Ntrip-Version: Ntrip/2.0\r\n"
     966            "User-Agent: %s/%s\r\n"
     967            "Authorization: Basic %s%s%s\r\n"
     968            "Connection: close\r\n"
     969            "Transfer-Encoding: chunked\r\n\r\n",
     970            mountpoint, casterouthost, AGENTSTRING,
     971            revisionstr, authorization, ntrip_str ? "\r\nNtrip-STR: " : "",
     972            ntrip_str);
     973            i += j;
     974            if(i > (int)sizeof(rtpbuf)-40 || j < 0) /* second check for old glibc */
     975            {
     976              fprintf(stderr, "Requested data too long\n");
     977              reconnect_sec_max = 0;
     978              output_init = 0;
     979              break;
     980            }
     981            else
     982            {
     983              rtpbuf[i++] = '\r';
     984              rtpbuf[i++] = '\n';
     985              rtpbuf[i++] = '\r';
     986              rtpbuf[i++] = '\n';
     987
     988              if(send(socket_tcp, rtpbuf, i, 0) != i)
     989              {
     990                perror("Could not send UDP packet");
     991                reconnect_sec_max = 0;
     992                output_init = 0;
     993                break;
     994              }
     995              else
     996              {
     997                int stop = 0;
     998                int numbytes;
     999                if((numbytes=recv(socket_tcp, rtpbuf, sizeof(rtpbuf)-1, 0)) > 0)
     1000                {
     1001                  /* we don't expect message longer than 1513, so we cut the last
     1002                    byte for security reasons to prevent buffer overrun */
     1003                  rtpbuf[numbytes] = 0;
     1004                  if(numbytes > 17+12 &&
     1005                  (!strncmp(rtpbuf+12, "HTTP/1.1 200 OK\r\n", 17) ||
     1006                  !strncmp(rtpbuf+12, "HTTP/1.0 200 OK\r\n", 17)))
     1007                  {
     1008                    const char *sessioncheck = "session: ";
     1009                    int l = strlen(sessioncheck)-1;
     1010                    int j=0;
     1011                    for(i = 12; j != l && i < numbytes-l; ++i)
     1012                    {
     1013                      for(j = 0; j < l && tolower(rtpbuf[i+j]) == sessioncheck[j]; ++j)
     1014                        ;
     1015                    }
     1016                    if(i != numbytes-l) /* found a session number */
     1017                    {
     1018                      i+=l;
     1019                      session = 0;
     1020                      while(i < numbytes && rtpbuf[i] >= '0' && rtpbuf[i] <= '9')
     1021                        session = session * 10 + rtpbuf[i++]-'0';
     1022                      if(rtpbuf[i] != '\r')
     1023                      {
     1024                        fprintf(stderr, "Could not extract session number\n");
     1025                        stop = 1;
     1026                      }
     1027                    }
     1028                  }
     1029                  else
     1030                  {
     1031                    int k;
     1032                    fprintf(stderr, "Could not access mountpoint: ");
     1033                    for(k = 12; k < numbytes && rtpbuf[k] != '\n' && rtpbuf[k] != '\r'; ++k)
     1034                    {
     1035                      fprintf(stderr, "%c", isprint(rtpbuf[k]) ? rtpbuf[k] : '.');
     1036                    }
     1037                    fprintf(stderr, "\n");
     1038                    stop = 1;
     1039                  }
     1040                }
     1041                if(!stop)
     1042                {
     1043                  send_receive_loop(socket_tcp, outputmode, NULL, 0, session);
     1044                  input_init = output_init = 0;
     1045                  /* send connection close always to allow nice session closing */
     1046                  udp_tim += (time(0)-udp_init)*1000000/TIME_RESOLUTION;
     1047                  rtpbuf[0] = (2<<6);
     1048                  /* padding, extension, csrc are empty */
     1049                  rtpbuf[1] = 98;
     1050                  /* marker is empty */
     1051                  rtpbuf[2] = (udp_seq>>8)&0xFF;
     1052                  rtpbuf[3] = (udp_seq)&0xFF;
     1053                  rtpbuf[4] = (udp_tim>>24)&0xFF;
     1054                  rtpbuf[5] = (udp_tim>>16)&0xFF;
     1055                  rtpbuf[6] = (udp_tim>>8)&0xFF;
     1056                  rtpbuf[7] = (udp_tim)&0xFF;
     1057                  /* sequence and timestamp are empty */
     1058                  rtpbuf[8] = (session>>24)&0xFF;
     1059                  rtpbuf[9] = (session>>16)&0xFF;
     1060                  rtpbuf[10] = (session>>8)&0xFF;
     1061                  rtpbuf[11] = (session)&0xFF;
     1062
     1063                  send(socket_tcp, rtpbuf, 12, 0); /* cleanup */
     1064                }
     1065                else
     1066                {
     1067                  reconnect_sec_max = 600;
     1068                  output_init = 0;
     1069                }
     1070              }
     1071            }
     1072          }
     1073          break;
     1074        case NTRIP1: /*** OutputMode Ntrip Version 1.0 ***/
    9291075          fallback = 0;
    9301076          nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
     
    9581104            fprintf(stderr, "\n");
    9591105            if((strstr(szSendBuffer,"ERROR - Bad Password"))
    960             || (strstr(szSendBuffer,"400 Bad Request")))           
     1106            || (strstr(szSendBuffer,"400 Bad Request")))
    9611107            reconnect_sec_max = 0;
    9621108            output_init = 0;
     
    9731119          input_init = output_init = 0;
    9741120          break;
    975         case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
     1121        case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
    9761122          nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
    9771123            "POST %s/%s HTTP/1.1\r\n"
     
    10421188          input_init = output_init = 0;
    10431189          break;
    1044         case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
     1190        case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
    10451191          if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)
    10461192          {
     
    11091255              for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
    11101256              {
    1111                 fprintf(stderr, "%c", isprint(*a) ? *a : '.');
     1257                fprintf(stderr, "%c", isprint(*a) ? *a : '.');
    11121258              }
    11131259              fprintf(stderr, "\n");
     
    11151261              if(strncmp(szSendBuffer, "RTSP",4) != 0)
    11161262              {
    1117                 if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
    1118                 {
     1263                if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
     1264                {
    11191265                  fprintf(stderr,
    11201266                  "       RTSP not implemented at Destination caster <%s>%s%s%s\n\n"
     
    11261272                  fallback = 1;
    11271273                  break;
    1128                 }
    1129                 else
    1130                 {
     1274                }
     1275                else
     1276                {
    11311277                  fprintf(stderr,
    11321278                  "       Ntrip-Version 2.0 not implemented at Destination caster"
     
    11421288                  fallback = 1;
    11431289                  break;
    1144                 }
     1290                }
    11451291              }
    11461292              else if((strstr(szSendBuffer, "RTSP/1.0 401 Unauthorized"))
     
    11641310              token = strtok(NULL, dlim))
    11651311              {
    1166                 tok_buf[i] = token; i++;
     1312                tok_buf[i] = token; i++;
    11671313              }
    11681314              session = atoi(tok_buf[6]);
    11691315              server_port = atoi(tok_buf[10]);
    11701316              nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
    1171                 "RECORD rtsp://%s%s/%s RTSP/1.0\r\n"
    1172                 "CSeq: %d\r\n"
    1173                 "Session: %d\r\n"
    1174                 "\r\n",
    1175                 casterouthost, rtsp_extension,  mountpoint,  udp_cseq++,
    1176                 session);
     1317                "RECORD rtsp://%s%s/%s RTSP/1.0\r\n"
     1318                "CSeq: %d\r\n"
     1319                "Session: %d\r\n"
     1320                "\r\n",
     1321                casterouthost, rtsp_extension,  mountpoint,  udp_cseq++,
     1322                session);
    11771323              if((nBufferBytes >= (int)sizeof(szSendBuffer))
    11781324              || (nBufferBytes < 0))
    11791325              {
    1180                     fprintf(stderr, "ERROR: Destination caster request to long\n");
     1326                    fprintf(stderr, "ERROR: Destination caster request to long\n");
    11811327                reconnect_sec_max = 0;
    11821328                output_init = 0;
    1183                 break;
     1329                break;
    11841330              }
    11851331              if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
    11861332              {
    11871333                output_init = 0;
    1188                     break;
     1334                    break;
    11891335              }
    11901336            }
    11911337            else if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n")) && (strstr(szSendBuffer,
    1192             "CSeq: 2\r\n"))) 
     1338            "CSeq: 2\r\n")))
    11931339            {
    11941340              /* fill structure with caster address information for UDP */
     
    11961342              casterRTP.sin_family = AF_INET;
    11971343              casterRTP.sin_port   = htons(((uint16_t)server_port));
    1198               if((he = gethostbyname(outhost))== NULL) 
     1344              if((he = gethostbyname(outhost))== NULL)
    11991345              {
    1200                 fprintf(stderr, "ERROR: Destination caster unknown\n");
     1346                fprintf(stderr, "ERROR: Destination caster unknown\n");
    12011347                reconnect_sec_max = 0;
    12021348                output_init = 0;
     
    12051351              else
    12061352              {
    1207                 memcpy((char *)&casterRTP.sin_addr.s_addr,
    1208                 he->h_addr_list[0], (size_t)he->h_length);
     1353                memcpy((char *)&casterRTP.sin_addr.s_addr,
     1354                he->h_addr_list[0], (size_t)he->h_length);
    12091355              }
    12101356              len = (socklen_t)sizeof(casterRTP);
    1211               send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP, 
     1357              send_receive_loop(socket_udp, outputmode, (struct sockaddr *)&casterRTP,
    12121358              (socklen_t)len, session);
    12131359              break;
     
    12451391  time_t   laststate = time(0);
    12461392
    1247   if(outmode == RTSP)
    1248   {
     1393  if(outmode == UDP)
     1394  {
     1395    rtptime = time(0);
    12491396#ifdef WINDOWSVERSION
    12501397    u_long blockmode = 1;
     
    12581405    }
    12591406  }
     1407  else if(outmode == RTSP)
     1408  {
     1409#ifdef WINDOWSVERSION
     1410    u_long blockmode = 1;
     1411    if(ioctlsocket(socket_tcp, FIONBIO, &blockmode))
     1412#else /* WINDOWSVERSION */
     1413    if(fcntl(socket_tcp, F_SETFL, O_NONBLOCK) < 0)
     1414#endif /* WINDOWSVERSION */
     1415    {
     1416      fprintf(stderr, "Could not set nonblocking mode\n");
     1417      return;
     1418    }
     1419  }
    12601420
    12611421  /* data transmission */
    12621422  fprintf(stderr,"transfering data ...\n");
    12631423  int  send_recv_success = 0;
    1264 #ifdef WINDOWSVERSION 
    1265   time_t nodata_begin = 0, nodata_current = 0; 
     1424#ifdef WINDOWSVERSION
     1425  time_t nodata_begin = 0, nodata_current = 0;
    12661426#endif
    12671427  while(1)
    12681428  {
    1269     if(send_recv_success < 3) send_recv_success++; 
     1429    if(send_recv_success < 3) send_recv_success++;
    12701430    if(!nodata)
    1271     { 
     1431    {
    12721432#ifndef WINDOWSVERSION
    12731433      alarm(ALARMTIME);
     
    12761436#endif
    12771437    }
    1278     else 
    1279     {
    1280       nodata = 0; 
    1281 #ifdef WINDOWSVERSION 
     1438    else
     1439    {
     1440      nodata = 0;
     1441#ifdef WINDOWSVERSION
    12821442      time(&nodata_current);
    1283       if(difftime(nodata_current, nodata_begin) >= ALARMTIME) 
     1443      if(difftime(nodata_current, nodata_begin) >= ALARMTIME)
    12841444      {
    12851445        sigalarm_received = 1;
     
    13161476      else if(inputmode == SERIAL)
    13171477      {
    1318 #ifndef WINDOWSVERSION       
     1478#ifndef WINDOWSVERSION
    13191479        nBufferBytes = read(gps_serial, buffer, sizeof(buffer));
    13201480#else
    1321         DWORD nRead = 0; 
     1481        DWORD nRead = 0;
    13221482        if(!ReadFile(gps_serial, buffer, sizeof(buffer), &nRead, NULL))
    13231483        {
     
    13581518      }
    13591519    }
     1520    if(nBufferBytes < 0)
     1521      return;
    13601522    /**  send data ***/
    13611523    if((nBufferBytes)  && (outmode == NTRIP1)) /*** Ntrip-Version 1.0 ***/
     
    13811543      {
    13821544        nBufferBytes = 0;
     1545      }
     1546    }
     1547    else if((nBufferBytes)  && (outmode == UDP))
     1548    {
     1549      char rtpbuf[1592];
     1550      int i;
     1551      int ct = time(0);
     1552      udp_tim += (ct-udp_init)*1000000/TIME_RESOLUTION;
     1553      udp_init = ct;
     1554      rtpbuf[0] = (2<<6);
     1555      rtpbuf[1] = 96;
     1556      rtpbuf[2] = (udp_seq>>8)&0xFF;
     1557      rtpbuf[3] = (udp_seq)&0xFF;
     1558      rtpbuf[4] = (udp_tim>>24)&0xFF;
     1559      rtpbuf[5] = (udp_tim>>16)&0xFF;
     1560      rtpbuf[6] = (udp_tim>>8)&0xFF;
     1561      rtpbuf[7] = (udp_tim)&0xFF;
     1562      rtpbuf[8] = (rtpssrc>>24)&0xFF;
     1563      rtpbuf[9] = (rtpssrc>>16)&0xFF;
     1564      rtpbuf[10] = (rtpssrc>>8)&0xFF;
     1565      rtpbuf[11] = (rtpssrc)&0xFF;
     1566      ++udp_seq;
     1567      memcpy(rtpbuf+12, buffer, nBufferBytes);
     1568      if((i = send(socket_tcp, rtpbuf, (size_t)nBufferBytes+12, MSG_DONTWAIT))
     1569      != nBufferBytes+12)
     1570      {
     1571        if(errno != EAGAIN)
     1572        {
     1573          perror("WARNING: could not send data to Destination caster");
     1574          return;
     1575        }
     1576      }
     1577      else
     1578        nBufferBytes = 0;
     1579      i = recv(socket_tcp, rtpbuf, sizeof(rtpbuf), 0);
     1580      if(i >= 12 && (unsigned char)rtpbuf[0] == (2 << 6) && rtpbuf[1] >= 96
     1581      && rtpbuf[1] <= 98 && rtpssrc ==
     1582      (((unsigned char)rtpbuf[8]<<24)+((unsigned char)rtpbuf[9]<<16)
     1583      +((unsigned char)rtpbuf[10]<<8)+(unsigned char)rtpbuf[11]))
     1584      {
     1585        rtptime = time(0);
     1586      }
     1587      else if(time(0) > rtptime+60)
     1588      {
     1589        fprintf(stderr, "Timeout\n");
     1590        return;
    13831591      }
    13841592    }
     
    16421850static HANDLE openserial(const char * tty, int baud)
    16431851{
    1644   DCB dcb; 
     1852  DCB dcb;
    16451853  COMMTIMEOUTS cmt;
    16461854  char compath[15] = "";
     
    16481856  snprintf(compath, sizeof(compath), "\\\\.\\%s", tty);
    16491857/*** opening the serial port ***/
    1650   gps_serial = CreateFile(compath, GENERIC_READ | GENERIC_WRITE, 0, 0, 
     1858  gps_serial = CreateFile(compath, GENERIC_READ | GENERIC_WRITE, 0, 0,
    16511859    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    16521860  if(gps_serial == INVALID_HANDLE_VALUE)
     
    17151923    break;
    17161924  }
    1717   dcb.BaudRate = baud; 
     1925  dcb.BaudRate = baud;
    17181926  dcb.ByteSize = 8;
    17191927  dcb.StopBits = ONESTOPBIT;
     
    17311939  {
    17321940    fprintf(stderr, "ERROR: set serial timeouts\n");
    1733     return (INVALID_HANDLE_VALUE); 
     1941    return (INVALID_HANDLE_VALUE);
    17341942  }
    17351943  return (gps_serial);
     
    18262034  fprintf(stderr, "       2 = rtsp: NTRIP Version 2.0 Caster in RTSP/RTP mode\n");
    18272035  fprintf(stderr, "       3 = ntrip1: NTRIP Version 1.0 Caster\n");
     2036  fprintf(stderr, "       4 = udp: NTRIP Version 2.0 Caster in Plain UDP mode\n");
    18282037  fprintf(stderr, "       optional\n\n");
    18292038  fprintf(stderr, "       Defaults to NTRIP1.0, but will change to 2.0 in future versions\n");
     
    19962205 * close session                                                    *
    19972206*********************************************************************/
    1998 static void close_session(const char *caster_addr, const char *mountpoint, 
     2207static void close_session(const char *caster_addr, const char *mountpoint,
    19992208int session, char *rtsp_ext, int fallback)
    20002209{
     
    20042213  if(!fallback)
    20052214  {
    2006     if((gps_socket != INVALID_SOCKET) && 
    2007        ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) || 
     2215    if((gps_socket != INVALID_SOCKET) &&
     2216       ((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET) ||
    20082217       (inputmode == CASTER)    || (inputmode == SISNET)))
    2009     { 
     2218    {
    20102219      if(closesocket(gps_socket) == -1)
    20112220      {
     
    20162225      {
    20172226        gps_socket = -1;
    2018 #ifndef NDEBUG 
     2227#ifndef NDEBUG
    20192228        fprintf(stderr, "close input device: successful\n");
    20202229#endif
     
    20392248      {
    20402249        gps_serial = INVALID_HANDLE_VALUE;
    2041 #ifndef NDEBUG 
     2250#ifndef NDEBUG
    20422251        fprintf(stderr, "close input device: successful\n");
    20432252#endif
     
    20452254    }
    20462255    else if((gps_file != -1) && (inputmode == INFILE))
    2047     { 
     2256    {
    20482257      if(close(gps_file) == -1)
    20492258      {
     
    20542263      {
    20552264        gps_file = -1;
    2056 #ifndef NDEBUG 
     2265#ifndef NDEBUG
    20572266        fprintf(stderr, "close input device: successful\n");
    20582267#endif
Note: See TracChangeset for help on using the changeset viewer.