Index: /trunk/ntripserver/NtripLinuxServer.c
===================================================================
--- /trunk/ntripserver/NtripLinuxServer.c	(revision 466)
+++ /trunk/ntripserver/NtripLinuxServer.c	(revision 467)
@@ -1,4 +1,4 @@
 /*
- * $Id: NtripLinuxClient.c,v 1.27 2007/05/16 14:16:21 stoecker Exp $
+ * $Id: NtripLinuxServer.c,v 1.28 2007/06/06 09:44:27 stoecker Exp $
  *
  * Copyright (c) 2003...2007
@@ -11,16 +11,13 @@
  *
  * 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 
+ * 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.ifag.de/index_ntrip.htm
+ * http://igs.bkg.bund.de/index_ntrip_down.htm
  *
- * Georg Weber
- * BKG, Frankfurt, Germany, June 2003-06-13
+ * BKG, Frankfurt, Germany, February 2007
  * E-mail: euref-ip@bkg.bund.de
- *
- * Based on the GNU General Public License published nmead
  *
  * This program is free software; you can redistribute it and/or
@@ -36,11 +33,10 @@
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
- * USA.
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 /* CVS revision and version */
-static char revisionstr[] = "$Revision: 1.27 $";
-static char datestr[]     = "$Date: 2007/05/16 14:16:21 $";
+static char revisionstr[] = "$Revision: 1.28 $";
+static char datestr[]     = "$Date: 2007/06/06 09:44:27 $";
 
 #include <ctype.h>
@@ -59,4 +55,5 @@
 #include <sys/termios.h>
 #include <sys/types.h>
+#include <sys/time.h>
 
 #ifndef COMPILEDATE
@@ -72,8 +69,11 @@
 
 enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
-CASTER = 6, LAST};
+CASTER = 6, LAST };
+
+enum OUTMODE { NTRIPV1, HTTP, RTSP, END };
 
 #define AGENTSTRING     "NTRIP NtripServerLinux"
 #define BUFSZ           1024
+#define SZ              64
 
 /* default socket source */
@@ -91,16 +91,30 @@
 #define ALARMTIME       60
 
+#define RTP_VERSION     2
+#define TIME_RESOLUTION 125
+
 static int ttybaud             = 19200;
 static const char *ttyport     = "/dev/gps";
 static const char *filepath    = "/dev/stdin";
-static enum MODE mode          = INFILE;
+static enum MODE inputmode     = INFILE;
 static int sisnet              = 31;
 static int gpsfd               = -1;
+static int sigint_received     = 0;
 
 /* Forward references */
-static int openserial(const char * tty, int blocksz, int baud);
-static void send_receive_loop(int sock, int fd);
-static void usage(int, char*);
-static int encode(char *buf, int size, const char *user, const char *pwd);
+static int  openserial(const char * tty, int blocksz, int baud);
+static void send_receive_loop(int sock, int fd, int outmode,
+  struct sockaddr * pcasterRTP, socklen_t length);
+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, int socket, int input_size);
+static void close_session(int sock_udp, int sock_tcp,
+  const char *caster_addr, const char *mountpoint, int cseq,
+  int session, char *rtsp_ext, int in_fd);
+
+/* Signal Handling */
+static void handle_sigint(int sig);
+static void setup_signal_handler(int sig, void (*handler)(int));
+static int  signal_was_caught(void);
 
 #ifdef __GNUC__
@@ -135,31 +149,66 @@
 int main(int argc, char **argv)
 {
-  int c;
-  int size = 2048;              /* for setting send buffer size */
-
-  const char *inhost = 0;
-  const char *outhost = 0;
-  unsigned int outport = 0;
-  unsigned int inport = 0;
-  const char *mountpoint = NULL;
-  const char *password = "";
-  const char *sisnetpassword = "";
-  const char *sisnetuser = "";
-  
-  const char *stream_name=0;
-  const char *stream_user=0;
-  const char *stream_password=0;
-  
-  const char *initfile = NULL;
-  
-  const char *recvrid=0;
-  const char *recvrpwd=0;
-  
-  int bindmode = 0;
-  int sock_id;
-  char szSendBuffer[BUFSZ];
-  int nBufferBytes;
-  struct hostent *he;
-  struct sockaddr_in addr;
+  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 *       mountpoint = NULL;
+
+  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 ***/
+  const char *       casterouthost = 0;
+  unsigned int       casteroutport = 0;
+  const char *       outhost = 0;
+  unsigned int       outport = 0;
+  char               post_extension[SZ] = "";
+  char               rtsp_extension[SZ] = "";
+
+  const char *       ntrip_str = "";
+
+  const char *       user = "";
+  const char *       password = "";
+
+  int                socket_tcp = 0;
+  int                outputmode = NTRIPV1;
+
+  struct sockaddr_in casterRTP;
+  struct sockaddr_in local;
+  int                socket_udp = 0;
+  int                client_port = 0;
+  int                server_port = 0;
+  int                session = 0;
+  int                cseq = 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];
 
   setbuf(stdout, 0);
@@ -167,4 +216,5 @@
   setbuf(stderr, 0);
 
+  {
   char *a;
   int i = 0;
@@ -182,7 +232,10 @@
   datestr[4] = datestr[7] = '-';
   datestr[10] = 0;
-
+  }
+
+  /* setup signal handler for timeout */
   signal(SIGALRM,sighandler_alarm);
   alarm(ALARMTIME);
+
   /* get and check program arguments */
   if(argc <= 1)
@@ -191,106 +244,123 @@
     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:B"))
-  != EOF)
+  while((c = getopt(argc, argv,
+  "M:i:h:b:p:s:a:m:c:H:P:f:x:y:l:u:V:D:U:W:B:O:E:F:N:n:")) != EOF)
   {
     switch (c)
     {
-    case 'M':
-      if(!strcmp(optarg, "serial")) mode = SERIAL;
-      else if(!strcmp(optarg, "tcpsocket")) mode = TCPSOCKET;
-      else if(!strcmp(optarg, "file")) mode = INFILE;
-      else if(!strcmp(optarg, "sisnet")) mode = SISNET;
-      else if(!strcmp(optarg, "udpsocket")) mode = UDPSOCKET;
-      else if(!strcmp(optarg, "caster")) mode = CASTER;
-      else mode = atoi(optarg);
-      if((mode == 0) || (mode >= LAST))
-      {
-        fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
+    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':                  /* gps serial ttyport */
+    case 'i': /* serial input device */
       ttyport = optarg;
       break;
-    case 'B':
+    case 'B': /* bind to incoming UDP stream */
       bindmode = 1;
       break;
-    case 'V':
-      if(!strcmp("3.0", optarg)) sisnet = 30;
+    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);
+        fprintf(stderr, "ERROR: unknown SISNeT version <%s>\n", optarg);
         usage(-2, argv[0]);
       }
       break;
-    case 'b':                  /* serial ttyin speed */
+    case 'b': /* serial input baud rate */
       ttybaud = atoi(optarg);
       if(ttybaud <= 1)
       {
-        fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
+        fprintf(stderr, "ERROR: can't convert <%s> to valid serial baud rate\n",
           optarg);
         usage(1, argv[0]);
       }
       break;
-    case 'a':                  /* http server IP address A.B.C.D */
-      outhost = optarg;
-      break;
-    case 'p':                  /* http server port */
-      outport = atoi(optarg);
-      if(outport <= 1 || outport > 65535)
+    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);
+          "ERROR: can't convert <%s> to a valid HTTP server port\n", optarg);
         usage(1, argv[0]);
       }
       break;
-    case 'm':                  /* http server mountpoint */
+    case 'm': /* Destination caster mountpoint for stream upload */
       mountpoint = optarg;
       break;
-    case 's':                  /* datastream from file */
+    case 's': /* File name for input data simulation from file */
       filepath = optarg;
       break;
-    case 'f':
+    case 'f': /* name of an initialization file */
       initfile = optarg;
       break;
-    case 'x':
-      recvrid = optarg;  
-      break;      
-    case 'y':
-      recvrpwd = optarg;  
-      break;            
-    case 'u':
+    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':
+    case 'l': /* Sisnet data server password */
       sisnetpassword = optarg;
       break;
-    case 'c':                  /* DestinationCasterPassword */
+    case 'c': /* DestinationCaster password for stream upload to mountpoint */
       password = optarg;
       break;
-    case 'H':                  /* SourceCasterHost */
-      inhost = optarg;
-      break;
-    case 'P':                  /* SourceCasterPort */
-      inport = atoi(optarg);
-      if(inport <= 1 || inport > 65535)
-      {
-        fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
+    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':
-     stream_name=optarg;        /* desired stream from SourceCaster */
-     break; 
-    case 'U':
-     stream_user=optarg;        /* username for desired stream */
+    case 'D': /* Source caster mountpoint for stream input */
+     stream_name = optarg;
      break;
-    case 'W':
-     stream_password=optarg;    /* passwd for desired stream */
+    case 'U': /* Source caster user ID for input stream access */
+     stream_user = optarg;
      break;
-    case 'h':                  /* help */
+    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 'O': /* OutputMode - default: Ntrip-Version 2.0 TCP/IP */
+      if     (!strcmp(optarg,"f")) outputmode = NTRIPV1;
+      else if(!strcmp(optarg,"r")) outputmode = RTSP;
+      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]);
@@ -305,4 +375,5 @@
   argv += optind;
 
+  /*** argument analysis ***/
   if(argc > 0)
   {
@@ -316,25 +387,82 @@
   }
 
-  if(mountpoint == NULL)
-  {
-    fprintf(stderr, "ERROR: Missing mountpoint argument\n");
+  if(*ntrip_str && (outputmode == NTRIPV1))
+  {
+     fprintf(stderr, "WARNING: OutputMode is Ntrip version 1.0"
+     " - Ntrip-STR will not be considered\n");
+  }
+
+  if(!mountpoint)
+  {
+    fprintf(stderr, "ERROR: Missing mountpoint argument for stream upload\n");
     exit(1);
   }
+
   if(!password[0])
   {
-    fprintf(stderr,
-      "WARNING: Missing password argument - are you really sure?\n");
+    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 download"
+    fprintf(stderr, "WARNING: Missing password argument for stream download"
       " - are you really sure?\n");
   }
 
-  if(!outhost) outhost = NTRIP_CASTER;
-  if(!outport) outport = NTRIP_PORT;
-
-  switch(mode)
+  /*** 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;
+  }
+
+  /*** InputMode handling ***/
+  switch(inputmode)
   {
   case INFILE:
@@ -345,5 +473,5 @@
         exit(1);
       }
-      /* set blocking mode in case it was not set
+      /* set blocking inputmode in case it was not set
         (seems to be sometimes for fifo's) */
       fcntl(gpsfd, F_SETFL, 0);
@@ -351,5 +479,5 @@
     }
     break;
-  case SERIAL:                 /* open serial port */
+  case SERIAL: /* open serial port */
     {
       gpsfd = openserial(ttyport, 1, ttybaud);
@@ -363,43 +491,44 @@
   case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
     {
-      if(mode == SISNET)
+      if(inputmode == SISNET)
       {
         if(!inhost) inhost = SISNET_SERVER;
         if(!inport) inport = SISNET_PORT;
       }
-      else if(mode == CASTER)
+      else if(inputmode == CASTER)
       {
         if(!inport) inport = NTRIP_PORT;
         if(!inhost) inhost = NTRIP_CASTER;
       }
-      else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
+      else if((inputmode == TCPSOCKET) || (inputmode == UDPSOCKET))
       {
         if(!inport) inport = SERV_TCP_PORT;
         if(!inhost) inhost = "127.0.0.1";
-      }      
+      }
 
       if(!(he = gethostbyname(inhost)))
       {
-        fprintf(stderr, "ERROR: host %s unknown\n", inhost);
+        fprintf(stderr, "ERROR: Input host <%s> unknown\n", inhost);
         usage(-2, argv[0]);
       }
 
-      if((gpsfd = socket(AF_INET, mode == UDPSOCKET
+      if((gpsfd = socket(AF_INET, inputmode == UDPSOCKET
       ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
       {
-        fprintf(stderr, "ERROR: can't create socket\n");
+        fprintf(stderr,
+        "ERROR: can't create socket for incoming data stream\n");
         exit(1);
       }
 
-      memset((char *) &addr, 0x00, sizeof(addr));
+      memset((char *) &caster, 0x00, sizeof(caster));
       if(!bindmode)
-        memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
-      addr.sin_family = AF_INET;
-      addr.sin_port = htons(inport);
-
-      printf("%s input: host = %s, port = %d, %s%s%s%s%s\n",
-      mode == CASTER ? "caster" : mode == SISNET ? "sisnet" :
-      mode == TCPSOCKET ? "tcp socket" : "udp socket",
-      bindmode ? "127.0.0.1" : inet_ntoa(addr.sin_addr),
+        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 : "",
@@ -408,18 +537,18 @@
       if(bindmode)
       {
-        if(bind(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+        if(bind(gpsfd, (struct sockaddr *) &caster, sizeof(caster)) < 0)
         {
           fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
           exit(1);
         }
-      }
-      else if(connect(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+      } /* connect to input-caster or proxy server*/
+      else if(connect(gpsfd, (struct sockaddr *)&caster, sizeof(caster)) < 0)
       {
         fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
-          inet_ntoa(addr.sin_addr), inport);
+        inet_ntoa(caster.sin_addr), inport);
         exit(1);
       }
-            
-      if(stream_name) /* data stream from caster */
+
+      if(stream_name) /* input from Ntrip Version 1.0 caster*/
       {
         int init = 0;
@@ -432,11 +561,12 @@
           /* leave some space for login */
           nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
-          "GET /%s HTTP/1.0\r\n"
+          "GET %s/%s HTTP/1.0\r\n"
           "User-Agent: %s/%s\r\n"
-          "Authorization: Basic ", stream_name, AGENTSTRING, revisionstr);
+          "Authorization: Basic ", get_extension, stream_name,
+          AGENTSTRING, revisionstr);
           /* second check for old glibc */
           if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
           {
-            fprintf(stderr, "Requested data too long\n");
+            fprintf(stderr, "ERROR: Source caster request too long\n");
             exit(1);
           }
@@ -445,5 +575,6 @@
           if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
           {
-            fprintf(stderr, "Username and/or password too long\n");
+            fprintf(stderr,
+            "ERROR: Source caster user ID and/or password too long\n");
             exit(1);
           }
@@ -456,16 +587,16 @@
         {
           nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
-          "GET /%s HTTP/1.0\r\n"
+          "GET %s/%s HTTP/1.0\r\n"
           "User-Agent: %s/%s\r\n"
-          "\r\n", stream_name, AGENTSTRING, revisionstr);
+          "\r\n", get_extension, stream_name, AGENTSTRING, revisionstr);
         }
         if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
         != nBufferBytes)
         {
-          fprintf(stderr, "ERROR: could not send to caster\n");
+          fprintf(stderr, "ERROR: could not send Source caster request\n");
           exit(1);
         }
         nBufferBytes = 0;
-        /* check caster's response */ 
+        /* check Source caster's response */
         while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
         && (nBufferBytes += recv(gpsfd, szSendBuffer,
@@ -479,5 +610,6 @@
             {
               int k;
-              fprintf(stderr, "Could not get the requested data: ");
+              fprintf(stderr,
+              "ERROR: could not get requested data from Source caster: ");
               for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
               && szSendBuffer[k] != '\r'; ++k)
@@ -493,10 +625,10 @@
         if(!init)
         {
-          fprintf(stderr, "Could not init caster download.");
+          fprintf(stderr, "ERROR: could not init Source caster download\n");
           exit(1);
         }
-      } /* end data stream from caster */
-
-      if(initfile && mode != SISNET)
+      } /* end input from Ntrip Version 1.0 caster */
+
+      if(initfile && inputmode != SISNET)
       {
         char buffer[1024];
@@ -523,10 +655,10 @@
         else
         {
-          fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
+          fprintf(stderr, "ERROR: can't read init file <%s>\n", initfile);
           exit(1);
         }
       }
     }
-    if(mode == SISNET)
+    if(inputmode == SISNET)
     {
       int i, j;
@@ -537,5 +669,5 @@
       if((send(gpsfd, buffer, (size_t)i, 0)) != i)
       {
-        perror("ERROR: sending authentication");
+        perror("ERROR: sending authentication for SISNeT data server");
         exit(1);
       }
@@ -558,32 +690,41 @@
         if((send(gpsfd, "START\r\n", 7, 0)) != i)
         {
-          perror("ERROR: sending start command");
+          perror("ERROR: sending Sisnet start command");
           exit(1);
         }
       }
     }
-
-    if (recvrid && recvrpwd && ((mode == TCPSOCKET) || (mode == UDPSOCKET)))
-    {
-      if (strlen(recvrid) > (BUFSZ-3)){
-        fprintf(stderr, "Receiver ID too long\n"); exit(0);
-      }else{
+    /*** receiver authentication  ***/
+    if (recvrid && recvrpwd && ((inputmode == TCPSOCKET)
+    || (inputmode == UDPSOCKET)))
+    {
+      if (strlen(recvrid) > (BUFSZ-3))
+      {
+        fprintf(stderr, "ERROR: Receiver ID too long\n");
+        exit(0);
+      }
+      else
+      {
         fprintf(stderr, "Sending user ID for receiver...\n");
-        nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);      
+        nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
         strcpy(szSendBuffer, recvrid);
         strcat(szSendBuffer,"\r\n");
         send(gpsfd,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);
       }
-      
-      if (strlen(recvrpwd) > (BUFSZ-3)){
-        fprintf(stderr, "Receiver password too long\n"); exit(0);
-      }else{
+
+      if (strlen(recvrpwd) > (BUFSZ-3))
+      {
+        fprintf(stderr, "ERROR: Receiver password too long\n");
+        exit(0);
+      }
+      else
+      {
         fprintf(stderr, "Sending user password for receiver...\n");
         nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
         strcpy(szSendBuffer, recvrpwd);
         strcat(szSendBuffer,"\r\n");
-        send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);    
-      }
-    }      
+        send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);
+      }
+    }
     break;
   default:
@@ -593,78 +734,332 @@
 
   /* ----- main part ----- */
-  for(;;)
-  {
+  /* setup signal handler for CTRL+C */
+  setup_signal_handler(SIGINT,  handle_sigint);
+
+  while(outputmode != END)
+  {
+    if(signal_was_caught())
+    {
+      fprintf(stderr, "NtripLinuxServer terminates\n");
+      break;
+    }
+
     if(!(he = gethostbyname(outhost)))
     {
-      fprintf(stderr, "ERROR: host %s unknown\n", outhost);
+      fprintf(stderr, "ERROR: Destination caster or proxy host <%s> unknown\n",
+      outhost);
       usage(-2, argv[0]);
     }
 
     /* create socket */
-    if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-    {
-      fprintf(stderr, "ERROR: could not create socket\n");
+    if((socket_tcp = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+      perror("ERROR: tcp socket");
       exit(2);
     }
 
-    memset((char *) &addr, 0x00, sizeof(addr));
-    memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
-    addr.sin_family = AF_INET;
-    addr.sin_port = htons(outport);
-
-    /* connect to caster */
+    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\n",
-      inet_ntoa(addr.sin_addr), outport, mountpoint);
-    if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+    inet_ntoa(caster.sin_addr), outport, mountpoint);
+
+    if(connect(socket_tcp, (struct sockaddr *) &caster, sizeof(caster)) < 0)
     {
       fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
-        inet_ntoa(addr.sin_addr), outport);
-      close(sock_id);
+        inet_ntoa(caster.sin_addr), outport);
+      if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
       exit(3);
     }
 
-    /* set socket buffer size */
-    setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
-      sizeof(const char *));
-    /* send message to caster */
-    nBufferBytes = sprintf(szSendBuffer, "SOURCE %s /%s\r\nSource-Agent: "
-    "%s/%s\r\n\r\n", password, mountpoint, AGENTSTRING, revisionstr);
-    if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
-    {
-      fprintf(stderr, "ERROR: could not send to caster\n");
-      break;
-    }
-    /* check caster's response */
-    nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
-    szSendBuffer[nBufferBytes] = '\0';
-    if(!strstr(szSendBuffer, "OK"))
-    {
-      char *a;
-      fprintf(stderr, "ERROR: caster's reply is not OK : ");
-      for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
-      {
-        fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
-      }
-      fprintf(stderr, "\n");
-      break;
-    }
-    printf("connection successfull\n");
-    send_receive_loop(sock_id, gpsfd);
-  }
-  close(sock_id);
-  sleep(5);
+    /*** OutputMode handling ***/
+    switch(outputmode){
+      case NTRIPV1: /*** OutputMode Ntrip Version 1.0 ***/
+        nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
+          "SOURCE %s %s/%s\r\n"
+          "Source-Agent: %s\r\n\r\n",
+          password, post_extension, mountpoint, AGENTSTRING);
+        if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
+        {
+          fprintf(stderr, "ERROR: Destination caster request to long\n");
+          outputmode = END;
+          break;
+        }
+        if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
+        {
+          outputmode = END;
+          break;
+        }
+        /* check Destination caster's response */
+        nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
+        szSendBuffer[nBufferBytes] = '\0';
+        if(!strstr(szSendBuffer, "OK"))
+        {
+          char *a;
+          fprintf(stderr,
+          "ERROR: Destination caster's or Proxy's reply is not OK: ");
+          for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
+          {
+            fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
+          }
+          fprintf(stderr, "\n");
+          outputmode = END;
+          break;
+        }
+        else
+        {
+          fprintf(stderr,
+          "Destination caster response:\n%s\nconnection successfull\n",
+          szSendBuffer);
+        }
+        send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
+        break;
+      case HTTP: /*** Ntrip-Version 2.0 HTTP/1.1 ***/
+        nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
+          "POST %s/%s HTTP/1.1\r\n"
+          "Host: %s\r\n"
+          "Ntrip-Version: Ntrip/2.0\r\n"
+          "User-Agent: NTRIP %s\r\n"
+          "Authorization: Basic %s\r\n"
+          "Ntrip-STR: %s\r\n"
+          "Transfer-Encoding: chunked\r\n\r\n",
+          post_extension, mountpoint, casterouthost, AGENTSTRING,
+          authorization, ntrip_str);
+        if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
+        {
+          fprintf(stderr, "ERROR: Destination caster request to long\n");
+          outputmode = END;
+          break;
+        }
+        if (!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
+        {
+          outputmode = END;
+          break;
+        }
+        /* check Destination caster's response */
+        nBufferBytes = recv(socket_tcp, szSendBuffer, sizeof(szSendBuffer), 0);
+        szSendBuffer[nBufferBytes] = '\0';
+        if(!strstr(szSendBuffer, "HTTP/1.1 200 OK"))
+        {
+          char *a;
+          fprintf(stderr,
+          "ERROR: Destination caster's or Proxy's reply is not OK: ");
+          for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
+          {
+            fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
+          }
+          fprintf(stderr, "\n");
+          /* fallback if necessary */
+          if(!strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
+          {
+            fprintf(stderr,
+            " - Ntrip Version 2.0 not implemented at Destination caster"
+            " <%s> or Proxy <%s> or\n"
+            " - HTTP/1.1 not implemented at Proxy or\n"
+            " - RTSP/1.0 not implemented at Destination caster or Proxy\n\n"
+            "caster fallback: Fallback to Ntrip Version 1.0\n\n",
+            casterouthost, proxyhost);
+            outputmode = NTRIPV1;
+            break;
+          }
+          outputmode = END;
+          break;
+        }
+        else
+        {
+          fprintf(stderr, "Destination caster response:\n%s\n"
+          "connection successfull\n",szSendBuffer);
+        }
+        send_receive_loop(socket_tcp, gpsfd, outputmode, NULL, 0);
+        break;
+      case RTSP: /*** Ntrip-Version 2.0 RTSP / RTP ***/
+        if((socket_udp = socket(AF_INET, SOCK_DGRAM,0)) < 0)
+        {
+          perror("ERROR: udp socket");
+          exit(4);
+        }
+        /* fill structure with local address information for UDP */
+        memset(&local, 0, sizeof(local));
+        local.sin_family = AF_INET;
+        local.sin_port = htons(0);
+        local.sin_addr.s_addr = htonl(INADDR_ANY);
+        len = (socklen_t)sizeof(local);
+        /* bind() in order to get a random RTP client_port */
+        if((bind(socket_udp,(struct sockaddr *)&local, len)) < 0)
+        {
+          perror("ERROR: udp bind");
+          if(close(socket_udp)==-1) perror("ERROR: close udp socket");
+          if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
+          exit(4);
+        }
+        if((getsockname(socket_udp, (struct sockaddr*)&local, &len)) != -1)
+        {
+          client_port = (unsigned int)ntohs(local.sin_port);
+        }else{
+          perror("ERROR: getsockname(localhost)");
+          if(close(socket_udp)==-1) perror("ERROR: close udp socket");
+          if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
+          exit(4);
+        }
+        nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
+          "SETUP rtsp://%s%s/%s RTSP/1.0\r\n"
+          "CSeq: 1\r\n"
+          "Ntrip-Version: Ntrip/2.0\r\n"
+          "Ntrip-Component: Ntripserver\r\n"
+          "User-Agent: NTRIP %s \r\n"
+          "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
+          "Authorization: Basic %s\r\n"
+          "Ntrip-STR: %s\r\n\r\n",
+          casterouthost, rtsp_extension, mountpoint, AGENTSTRING, client_port,
+          authorization, ntrip_str);
+        if((nBufferBytes > (int)sizeof(szSendBuffer)) || (nBufferBytes < 0))
+        {
+          fprintf(stderr, "ERROR: Destination caster request to long\n");
+          outputmode = END;
+          break;
+        }
+        if (!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
+        {
+          outputmode = END;
+          break;
+        }
+        while((nBufferBytes = recv(socket_tcp, szSendBuffer,
+        sizeof(szSendBuffer), 0)) > 0)
+        {
+          /* check Destination caster's response */
+          szSendBuffer[nBufferBytes] = '\0';
+          if(!strstr(szSendBuffer, "RTSP/1.0 200 OK"))
+          {
+            char *a;
+            fprintf(stderr,
+            "ERROR: Destination caster's or Proxy's reply is not OK: ");
+            for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
+            {
+              fprintf(stderr, "%c", isprint(*a) ? *a : '.');
+            }
+            fprintf(stderr, "\n");
+            /* fallback if necessary */
+            if(strncmp(szSendBuffer, "RTSP",4) != 0)
+            {
+              if(strstr(szSendBuffer,"Ntrip-Version: Ntrip/2.0\r\n"))
+              {
+                fprintf(stderr,
+                " - RTSP not implemented at Destination caster <%s> or"
+                " Proxy <%s>\n\n"
+                "caster fallback: Fallback to Ntrip Version 2.0 in TCP/IP"
+                " mode\n\n", casterouthost, proxyhost);
+                outputmode = HTTP;
+                break;
+              }
+              else
+              {
+                fprintf(stderr,
+                " - Ntrip-Version 2.0 not implemented at Destination caster"
+                "<%s> or Proxy <%s> or\n"
+                " - HTTP/1.1 not implemented at Proxy or\n"
+                " - RTSP/1.0 not implemented at Destination caster or Proxy\n\n"
+                "caster fallback: Fallback to Ntrip Version 1.0\n\n",
+                casterouthost, proxyhost);
+                outputmode = NTRIPV1;
+                break;
+              }
+            }
+            else
+            {
+              outputmode = END;
+              break;
+            }
+          }
+          else
+          {
+            fprintf(stderr, "Destination caster response:\n%s\n",szSendBuffer);
+          }
+          if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n"))
+          && (strstr(szSendBuffer,"CSeq: 1\r\n")))
+          {
+            for(token = strtok(szSendBuffer, dlim); token != NULL;
+            token = strtok(NULL, dlim))
+            {
+              tok_buf[i] = token; i++;
+            }
+            session = atoi(tok_buf[6]);
+            server_port = atoi(tok_buf[10]);
+            nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
+              "POST rtsp://%s%s/%s RTSP/1.0\r\n"
+              "CSeq: 2\r\n"
+              "Session: %d\r\n"
+              "\r\n",
+              casterouthost, rtsp_extension,  mountpoint,  session);
+            if((nBufferBytes >= (int)sizeof(szSendBuffer))
+            || (nBufferBytes < 0))
+            {
+              fprintf(stderr, "ERROR: Destination caster request to long\n");
+              outputmode = END;
+              break;
+            }
+            if(!send_to_caster(szSendBuffer, socket_tcp, nBufferBytes))
+            {
+               outputmode = END;
+               break;
+            }
+          }else if((strstr(szSendBuffer,"RTSP/1.0 200 OK\r\n")) && (strstr(szSendBuffer,"CSeq: 2\r\n"))) {
+            /* fill structure with caster address information for UDP */
+            memset(&casterRTP, 0, sizeof(casterRTP));
+            casterRTP.sin_family = AF_INET;
+            casterRTP.sin_port   = htons(((uint16_t)server_port));
+            if((he = gethostbyname(outhost))== NULL) {
+              fprintf(stderr, "ERROR: Destination caster unknown\n");
+              if(close(socket_udp)==-1) perror("ERROR: close udp socket");
+              if(close(socket_tcp)==-1) perror("ERROR: close tcp socket");
+              exit(4);
+            }else{
+              memcpy((char *)&casterRTP.sin_addr.s_addr,
+              he->h_addr_list[0], (size_t)he->h_length);
+            }
+            cseq = 2;
+            len = (socklen_t)sizeof(casterRTP);
+            send_receive_loop(socket_udp, gpsfd, outputmode, (struct sockaddr *)&casterRTP, (socklen_t)len);
+            break;
+          }else{break;}
+        }
+        break;
+    }
+  }
+  close_session(socket_udp, socket_tcp, casterouthost, mountpoint, cseq, session, rtsp_extension, gpsfd);
   return 0;
 }
 
-static void send_receive_loop(int sock, int fd)
+static void send_receive_loop(int sock, int fd, int outmode, struct sockaddr* pcasterRTP,
+socklen_t length)
 {
   int nodata = 0;
   char buffer[BUFSZ] = { 0 };
   char sisnetbackbuffer[200];
+  char szSendBuffer[BUFSZ] = "";
   int nBufferBytes = 0;
+
+   /* RTSP / RTP Mode */
+  int    isfirstpacket = 1;
+  struct timeval now;
+  struct timeval last = {0,0};
+  long int sendtimediff;
+  int rtpseq = 0;
+  int rtpssrc = 0;
+  int rtptime = 0;
+
   /* data transmission */
-  printf("transfering data ...\n");
+  fprintf(stderr,"transfering data ...\n");
+
   while(1)
   {
+    /* signal handling*/
+    if(signal_was_caught())
+    {
+      fprintf(stderr, "NtripLinuxServer exits send-receive mode\n");
+      break;
+    }
     if(!nodata) alarm(ALARMTIME);
     else nodata = 0;
@@ -672,5 +1067,5 @@
     if(!nBufferBytes)
     {
-      if(mode == SISNET && sisnet <= 30)
+      if(inputmode == SISNET && sisnet <= 30)
       {
         int i;
@@ -683,9 +1078,9 @@
         if((send(gpsfd, "MSG\r\n", i, 0)) != i)
         {
-          perror("ERROR: sending data request");
+          perror("ERROR: sending SISNeT data request");
           exit(1);
         }
       }
-      /* receiving data */
+      /*** receiving data ****/
       nBufferBytes = read(fd, buffer, sizeof(buffer));
       if(!nBufferBytes)
@@ -703,5 +1098,5 @@
       /* we can compare the whole buffer, as the additional bytes
          remain unchanged */
-      if(mode == SISNET && sisnet <= 30 &&
+      if(inputmode == SISNET && sisnet <= 30 &&
       !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
       {
@@ -709,10 +1104,10 @@
       }
     }
-    if(nBufferBytes)
+    /**  send data ***/
+    if((nBufferBytes)  && (outmode == NTRIPV1)) /*** Ntrip-Version 1.0 ***/
     {
       int i;
-      /* send data */
       if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
-        != nBufferBytes)
+      != nBufferBytes)
       {
         if(i < 0)
@@ -720,5 +1115,6 @@
           if(errno != EAGAIN)
           {
-            perror("WARNING: could not send data - retry connection");
+            perror("WARNING: could not send data to Destination caster"
+            " - retry connection");
             close(sock);
             sleep(5);
@@ -731,14 +1127,119 @@
           nBufferBytes -= i;
         }
+      }else
+      {
+        nBufferBytes = 0;
+      }
+    }
+    /*** Ntrip-Version 2.0 HTTP/1.1 ***/
+    else if((nBufferBytes)  && (outmode == HTTP))
+    {
+      int i, nChunkBytes, j = 1;
+      nChunkBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),"%x\r\n",
+      nBufferBytes);
+      send(sock, szSendBuffer, nChunkBytes, MSG_DONTWAIT);
+      if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
+      != nBufferBytes)
+      {
+        if(i < 0)
+        {
+          if(errno != EAGAIN)
+          {
+            perror("WARNING: could not send data to Destination caster"
+            " - retry connection");
+            close(sock);
+            sleep(5);
+            return;
+          }
+        }
+        else if(i)
+        {
+          while(j>0)
+          {
+            j = send(sock, buffer, (size_t)BUFSZ, MSG_DONTWAIT);
+          }
+        }
       }
       else
       {
+        send(sock, "\r\n", strlen("\r\n"), MSG_DONTWAIT);
         nBufferBytes = 0;
       }
     }
-  }
+    /*** Ntrip-Version 2.0 RTSP(TCP) / RTP(UDP) ***/
+    else if((nBufferBytes)  && (outmode == RTSP))
+    {
+      char rtpbuffer[BUFSZ+12];
+      int i, j;
+      /* Signal Handling */
+      if(signal_was_caught())
+      {
+        fprintf(stderr, "NtripLinuxServer exits send-receive mode \n");
+        break;
+      }
+      gettimeofday(&now, NULL);
+      /* RTP data packet generation*/
+      if(isfirstpacket){
+        rtpseq = rand();
+        rtptime = rand();
+        rtpssrc = rand();
+        last = now;
+        isfirstpacket = 0;
+      }
+      else
+      {
+        ++rtpseq;
+        sendtimediff = (((now.tv_sec - last.tv_sec)*1000000)
+        + (now.tv_usec - last.tv_usec));
+        rtptime += sendtimediff/TIME_RESOLUTION;
+      }
+      rtpbuffer[0] = (RTP_VERSION<<6);
+      /* padding, extension, csrc are empty */
+      rtpbuffer[1] = 96;
+      /* marker is empty */
+      rtpbuffer[2] = rtpseq>>8;
+      rtpbuffer[3] = rtpseq;
+      rtpbuffer[4] = rtptime>>24;
+      rtpbuffer[5] = rtptime>>16;
+      rtpbuffer[6] = rtptime>>8;
+      rtpbuffer[7] = rtptime;
+      rtpbuffer[8] = rtpssrc>>24;
+      rtpbuffer[9] = rtpssrc>>16;
+      rtpbuffer[10] = rtpssrc>>8;
+      rtpbuffer[11] = rtpssrc;
+      for(j=0; j<nBufferBytes; j++) {rtpbuffer[12+j] = buffer[j];}
+      last.tv_sec  = now.tv_sec;
+      last.tv_usec = now.tv_usec;
+
+      if ((i = sendto(sock, rtpbuffer, 12 + nBufferBytes, 0, pcasterRTP,
+      length)) != (nBufferBytes + 12))
+      {
+        if(i < 0)
+        {
+          if(errno != EAGAIN)
+          {
+            perror("WARNING: could not send data to Destination caster - retry connection");
+            close(sock);
+            sleep(5);
+            return;
+          }
+        }
+        else if(i)
+        {
+          memmove(buffer, buffer+(i-12), (size_t)(nBufferBytes-(i-12)));
+          nBufferBytes -= i-12;
+        }
+      }
+      else
+      {
+        nBufferBytes = 0;
+      }
+    }
+  }
+  return;
 }
 
-/*
+
+/********************************************************************
  * openserial
  *
@@ -758,5 +1259,5 @@
  * Remarks:
  *
- */
+ ********************************************************************/
 
 static int openserial(const char * tty, int blocksz, int baud)
@@ -789,7 +1290,5 @@
 
 #if (B4800 != 4800)
-  /*
-   * Not every system has speed settings equal to absolute speed value.
-   */
+/* Not every system has speed settings equal to absolute speed value. */
 
   switch (baud)
@@ -855,24 +1354,23 @@
   if(fcntl(fd, F_SETFL, 0) == -1)
   {
-    perror("WARNING: setting blocking mode failed");
+    perror("WARNING: setting blocking inputmode failed");
   }
   return (fd);
-}
-
-/*
- * usage
- *
- * Send a usage message to standard error and quit the program.
- *
- * Parameters:
- *     None.
- *
- * Return Value:
- *     The function does not return a value.
- *
- * Remarks:
- *
- */
-
+} /* openserial */
+
+/********************************************************************
+* usage
+*
+* Send a usage message to standard error and quit the program.
+*
+* Parameters:
+*     None.
+*
+* Return Value:
+*     The function does not return a value.
+*
+* Remarks:
+*
+*********************************************************************/
 static
 #ifdef __GNUC__
@@ -881,52 +1379,127 @@
 void usage(int rc, char *name)
 {
-  fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]",
+  fprintf(stderr, "Version %s (%s) GPL" COMPILEDATE "\nUsage:\n%s [OPTIONS]\n",
     revisionstr, datestr, name);
-  fprintf(stderr, "  Options are: [-]           \n");
-  fprintf(stderr, "    -a DestinationCaster name or address (default: %s)\n",
-    NTRIP_CASTER);
-  fprintf(stderr, "    -p DestinationCaster port (default: %d)\n", NTRIP_PORT);
-  fprintf(stderr, "    -m DestinationCaster mountpoint\n");
-  fprintf(stderr, "    -c DestinationCaster password\n");
-  fprintf(stderr, "    -h|? print this help screen\n");
-  fprintf(stderr, "    -M <mode>  sets the input mode\n");
-  fprintf(stderr, "               (1=serial, 2=tcpsocket, 3=file, 4=sisnet"
-    ", 5=udpsocket, 6=caster)\n");
-  fprintf(stderr, "  Mode = file:\n");
-  fprintf(stderr, "    -s file, simulate data stream by reading log file\n");
-  fprintf(stderr, "       default/current setting is %s\n", filepath);
-  fprintf(stderr, "  Mode = serial:\n");
-  fprintf(stderr, "    -b baud_rate, sets serial input baud rate\n");
-  fprintf(stderr, "       default/current value is %d\n", ttybaud);
-  fprintf(stderr, "    -i input_device, sets name of serial input device\n");
-  fprintf(stderr, "       default/current value is %s\n", ttyport);
-  fprintf(stderr, "       (normally a symbolic link to /dev/tty\?\?)\n");
-  fprintf(stderr, "  Mode = tcpsocket or udpsocket:\n");
-  fprintf(stderr, "    -P receiver port (default: %d)\n", SERV_TCP_PORT);
-  fprintf(stderr, "    -H hostname of TCP server (default: %s)\n",
-    SERV_HOST_ADDR);
-  fprintf(stderr, "    -f initfile send to server\n");
-  fprintf(stderr, "    -x receiver id\n");
-  fprintf(stderr, "    -y receiver password\n");  
-  fprintf(stderr, "    -B bindmode: bind to incoming UDP stream\n");
-  fprintf(stderr, "  Mode = sisnet:\n");
-  fprintf(stderr, "    -P receiver port (default: %d)\n", SISNET_PORT);
-  fprintf(stderr, "    -H hostname of TCP server (default: %s)\n",
-    SISNET_SERVER);
-  fprintf(stderr, "    -u username\n");
-  fprintf(stderr, "    -l password\n");
-  fprintf(stderr, "    -V version [2.1, 3.0 or 3.1] (default: 3.1)\n");
-  fprintf(stderr, "  Mode = caster:\n");
-  fprintf(stderr, "    -P SourceCaster port (default: %d)\n", NTRIP_PORT);
-  fprintf(stderr, "    -H SourceCaster hostname (default: %s)\n",
-    NTRIP_CASTER);
-  fprintf(stderr, "    -D SourceCaster mountpoint\n");
-  fprintf(stderr, "    -U SourceCaster mountpoint username\n");
-  fprintf(stderr, "    -W SourceCaster mountpoint password\n");  
-  fprintf(stderr, "\n");
+  fprintf(stderr, "PURPOSE\n");
+  fprintf(stderr, "   The purpose of this program is to pick up a GNSS data stream (Input, Source)\n");
+  fprintf(stderr, "   from either\n\n");
+  fprintf(stderr, "     1. a Serial port, or\n");
+  fprintf(stderr, "     2. an IP server, or\n");
+  fprintf(stderr, "     3. a File, or\n");
+  fprintf(stderr, "     4. a SISNeT Data Server, or\n");
+  fprintf(stderr, "     5. a UDP server, or\n");
+  fprintf(stderr, "     6. an NTRIP Version 1.0 Caster\n\n");
+  fprintf(stderr, "   and forward that incoming stream (Output, Destination) to either\n\n");
+  fprintf(stderr, "     - an NTRIP Version 1.0 Caster, or\n");
+  fprintf(stderr, "     - an NTRIP Version 2.0 Caster via TCP/IP or RTSP/RTP.\n\n\n");
+  fprintf(stderr, "OPTIONS\n");
+  fprintf(stderr, "   -h|? print this help screen\n\n");
+  fprintf(stderr, "    -E <ProxyHost>       Proxy server host name or address, required i.e. when\n");
+  fprintf(stderr, "                         running the program in a proxy server protected LAN,\n");
+  fprintf(stderr, "                         optional\n");
+  fprintf(stderr, "    -F <ProxyPort>       Proxy server IP port, required i.e. when running\n");
+  fprintf(stderr, "                         the program in a proxy server protected LAN, optional\n\n");
+  fprintf(stderr, "    -M <InputMode> Sets the input mode (1 = Serial Port, 2 = IP server,\n");
+  fprintf(stderr, "       3 = File, 4 = SISNeT Data Server, 5 = UDP server, 6 = NTRIP Caster),\n");
+  fprintf(stderr, "       mandatory\n\n");
+  fprintf(stderr, "       <InputMode> = 1 (Serial Port):\n");
+  fprintf(stderr, "       -i <Device>       Serial input device, default: /dev/gps, mandatory if\n");
+  fprintf(stderr, "                         <InputMode>=1\n");
+  fprintf(stderr, "       -b <BaudRate>     Serial input baud rate, default: 19200 bps, mandatory\n");
+  fprintf(stderr, "                         if <InputMode>=1\n\n");
+  fprintf(stderr, "       <InputMode> = 2|5 (IP port | UDP port):\n");
+  fprintf(stderr, "       -H <ServerHost>   Input host name or address, default: 127.0.0.1,\n");
+  fprintf(stderr, "                         mandatory if <InputMode> = 2|5\n");
+  fprintf(stderr, "       -P <ServerPort>   Input port, default: 1025, mandatory if <InputMode>= 2|5\n");
+  fprintf(stderr, "       -f <ServerFile>   Name of initialization file to be send to server,\n");
+  fprintf(stderr, "                         optional\n");
+  fprintf(stderr, "       -x <ServerUser>   User ID to access incoming stream, optional\n");
+  fprintf(stderr, "       -y <ServerPass>   Password, to access incoming stream, optional\n");
+  fprintf(stderr, "       -B Bind to incoming UDP stream, optional for <InputMode> = 5\n\n");
+  fprintf(stderr, "       <InputMode> = 3 (File):\n");
+  fprintf(stderr, "       -s <File>         File name to simulate stream by reading data from (log)\n");
+  fprintf(stderr, "                         file, default is /dev/stdin, mandatory for <InputMode> = 3\n\n");
+  fprintf(stderr, "       <InputMode> = 4 (SISNeT Data Server):\n");
+  fprintf(stderr, "       -H <SisnetHost>   SISNeT Data Server name or address,\n");
+  fprintf(stderr, "                         default: 131.176.49.142, mandatory if <InputMode> = 4\n");
+  fprintf(stderr, "       -P <SisnetPort>   SISNeT Data Server port, default: 7777, mandatory if\n");
+  fprintf(stderr, "                         <InputMode> = 4\n");
+  fprintf(stderr, "       -u <SisnetUser>   SISNeT Data Server user ID, mandatory if <InputMode> = 4\n");
+  fprintf(stderr, "       -l <SisnetPass>   SISNeT Data Server password, mandatory if <InputMode> = 4\n");
+  fprintf(stderr, "       -V <SisnetVers>   SISNeT Data Server Version number, options are 2.1, 3.0\n");
+  fprintf(stderr, "                         or 3.1, default: 3.1, mandatory if <InputMode> = 4\n\n");
+  fprintf(stderr, "       <InputMode> = 6 (NTRIP Version 1.0 Caster):\n");
+  fprintf(stderr, "       -H <SourceHost>   Source caster name or address, default: 127.0.0.1,\n");
+  fprintf(stderr, "                         mandatory if <InputMode> = 6\n");
+  fprintf(stderr, "       -P <SourcePort>   Source caster port, default: 2101, mandatory if\n");
+  fprintf(stderr, "                         <InputMode> = 6\n");
+  fprintf(stderr, "       -D <SourceMount>  Source caster mountpoint for stream input, mandatory if\n");
+  fprintf(stderr, "                         <InputMode> = 6\n");
+  fprintf(stderr, "       -U <SourceUser>   Source caster user Id for input stream access, mandatory\n");
+  fprintf(stderr, "                         for protected streams if <InputMode> = 6\n");
+  fprintf(stderr, "       -W <SourcePass>   Source caster password for input stream access, mandatory\n");
+  fprintf(stderr, "                         for protected streams if <InputMode> = 6\n\n");
+  fprintf(stderr, "    -O <OutputMode> Sets the output mode for communatation with the destination\n");
+  fprintf(stderr, "       caster (r = NTRIP Version 2.0 Caster in RTSP/RTP mode, t = Ntrip Version 2.0\n");
+  fprintf(stderr, "       Caster in TCP/IP mode, f = NTRIP Version 1.0 Caster)\n\n");
+  fprintf(stderr, "       Defaults to NTRIP1.0, but wil change to 2.0 in future versions\n");
+  fprintf(stderr, "       Note that the program automatically falls back from mode r to mode t and\n");
+  fprintf(stderr, "       further to mode f if necessary.\n\n");
+  fprintf(stderr, "       -a <DestHost>     Destination caster name or address, default: 127.0.0.1,\n");
+  fprintf(stderr, "                         mandatory\n");
+  fprintf(stderr, "       -p <DestPort>     Destination caster port, default: 2101, mandatory\n");
+  fprintf(stderr, "       -m <DestMount>    Destination caster mountpoint for stream upload,\n");
+  fprintf(stderr, "                         mandatory\n");
+  fprintf(stderr, "       -n <DestUser>     Destination caster user ID for stream upload to\n");
+  fprintf(stderr, "                         mountpoint, only for NTRIP Version 2.0 destination\n");
+  fprintf(stderr, "                         casters, mandatory\n");
+  fprintf(stderr, "       -c <DestPass>     Destination caster password for stream upload to\n");
+  fprintf(stderr, "                         mountpoint, mandatory\n");
+  fprintf(stderr, "       -N <STR-record>   Sourcetable STR-record\n");
+  fprintf(stderr, "                         optional for Ntrip Version 2.0 in RTSP/RTP and TCP/IP mode\n\n");
   exit(rc);
-}
-
-static const char encodingTable [64] = {
+} /* usage */
+
+
+/********************************************************************/
+/* signal handling                                                  */
+/********************************************************************/
+#ifdef __GNUC__
+static void handle_sigint(int sig __attribute__((__unused__)))
+#else /* __GNUC__ */
+static void handle_sigint(int sig)
+#endif /* __GNUC__ */
+{sigint_received  = 1;}
+
+static void setup_signal_handler(int sig, void (*handler)(int))
+{
+#if _POSIX_VERSION > 198800L
+  struct sigaction action;
+
+  action.sa_handler = handler;
+  sigemptyset(&(action.sa_mask));
+  sigaddset(&(action.sa_mask), sig);
+  action.sa_flags = 0;
+  sigaction(sig, &action, 0);
+#else
+  signal(sig, handler);
+#endif
+  return;
+} /* setupsignal_handler */
+
+static int signal_was_caught(void)
+{
+  fflush(stdout);
+  if(sigint_received)
+    fprintf(stderr, "\nSIGINT received: ");
+
+  return (sigint_received);
+} /* signal_was_caught */
+
+/********************************************************************
+ * base64-encoding                                                  *
+*******************************************************************/
+static const char encodingTable [64] =
+{
   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
   'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
@@ -975,3 +1548,88 @@
     *out = 0;
   return bytes;
-}
+}/* base64 Encoding */
+
+
+/********************************************************************
+ * send message to caster                                           *
+*********************************************************************/
+static int send_to_caster(char *input, int socket, int input_size)
+{
+int send_error = 1;
+
+  if((send(socket, input, (size_t)input_size, 0)) != input_size)
+  {
+    fprintf(stderr, "ERROR: could not send full header to Destination caster\n");
+    send_error = 0;
+  }else{
+    fprintf(stderr, "\nDestination caster request:\n");
+    fprintf(stderr, "%s", input);
+  }
+  return send_error;
+}/* send_to_caster */
+
+
+/********************************************************************
+ * close session                                                    *
+*********************************************************************/
+static void close_session(int sock_udp, int sock_tcp,
+const char *caster_addr, const char *mountpoint, int cseq,
+int session, char *rtsp_ext, int in_fd)
+{
+  int  size_send_buf;
+  char send_buf[BUFSZ];
+
+  if(in_fd)
+  {
+    if(close(in_fd)==-1)
+    {
+      perror("ERROR: close input device ");
+      exit(0);
+    }
+    else
+    {
+      fprintf(stderr, "\nclose input device: successful\n");
+    }
+  }
+
+  if(sock_udp  != 0)
+  {
+    if(cseq == 2)
+    {
+      size_send_buf = snprintf(send_buf, sizeof(send_buf),
+        "TEARDOWN rtsp://%s%s/%s RTSP/1.0\r\n"
+        "CSeq: 2\r\n"
+        "Session: %d\r\n"
+        "\r\n",
+        caster_addr, rtsp_ext, mountpoint, session);
+      if((size_send_buf >= (int)sizeof(send_buf)) || (size_send_buf < 0))
+      {
+        fprintf(stderr, "ERROR: Destination caster request to long\n");
+        exit(0);
+      }
+      send_to_caster(send_buf, sock_tcp, size_send_buf); strcpy(send_buf,"");
+      size_send_buf = recv(sock_tcp, send_buf, sizeof(send_buf), 0);
+      send_buf[size_send_buf] = '\0';
+      fprintf(stderr, "Destination caster response:\n%s", send_buf);
+    }
+    if(close(sock_udp)==-1)
+    {
+      perror("ERROR: close udp socket");
+      exit(0);
+    }
+    else
+    {
+      fprintf(stderr, "close udp socket:   successful\n");
+    }
+  }
+
+  if(close(sock_tcp)==-1)
+  {
+    perror("ERROR: close tcp socket");
+    exit(0);
+  }
+  else
+  {
+    fprintf(stderr, "close tcp socket:   successful\n\n");
+  }
+} /* close_session */
