久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2614|回復: 1
打印 上一主題 下一主題
收起左側

Ntrip服務器源碼

[復制鏈接]
跳轉到指定樓層
樓主
ID:61844 發表于 2018-5-3 14:11 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
1、Ntrip服務器源碼

源程序如下:
  1. /*
  2. * NtripServerLinux.c
  3. *
  4. * Copyright (c) 2003...2007
  5. * German Federal Agency for Cartography and Geodesy (BKG)
  6. *
  7. * Developed for Networked Transport of RTCM via Internet Protocol (NTRIP)
  8. * for streaming GNSS data over the Internet.
  9. *
  10. * Designed by Informatik Centrum Dortmund http://www.icd.de
  11. *
  12. * NTRIP is currently an experimental technology.
  13. * The BKG disclaims any liability nor responsibility to any person or
  14. * entity with respect to any loss or damage caused, or alleged to be
  15. * caused, directly or indirectly by the use and application of the NTRIP
  16. * technology.
  17. *
  18. * For latest information and updates, access:
  19. * http://igs.ifag.de/index_ntrip.htm
  20. *
  21. * Georg Weber
  22. * BKG, Frankfurt, Germany, June 2003-06-13
  23. * E-mail: euref-ip@bkg.bund.de
  24. *
  25. * Based on the GNU General Public License published nmead
  26. *
  27. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License
  29. * as published by the Free Software Foundation; either version 2
  30. * of the License, or (at your option) any later version.
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35. * GNU General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  40. * USA.
  41. */

  42. /*
  43. * Changes - Version 0.7
  44. * Sep 22 2003  Steffen Tschirpke <St.Tschirpke@actina.de>
  45. *           - socket support
  46. *           - command line option handling
  47. *           - error handling
  48. *           - help screen
  49. *
  50. * Changes - Version 0.9
  51. * Feb 15 2005  Dirk Stoecker <soft@dstoecker.de>
  52. *           - some minor updates, fixed serial baudrate settings
  53. *
  54. * Changes - Version 0.10
  55. * Apr 05 2005  Dirk Stoecker <soft@dstoecker.de>
  56. *           - some cleanup and miscellaneous fixes
  57. *           - replaced non-working simulate with file input (stdin)
  58. *           - TCP sending now somewhat more stable
  59. *           - cleanup of error handling
  60. *           - Modes may be symbolic and not only numeric
  61. *
  62. * Changes - Version 0.11
  63. * Jun 02 2005  Dirk Stoecker <soft@dstoecker.de>
  64. *           - added SISNeT support
  65. *           - added UDP support
  66. *           - cleanup of host and port handling
  67. *           - added inactivity alarm of 60 seconds
  68. *
  69. * Changes - Version 0.12
  70. * Jun 07 2005  Dirk Stoecker <soft@dstoecker.de>
  71. *           - added UDP bindmode
  72. *
  73. * Changes - Version 0.13
  74. * Apr 25 2006  Andrea Stuerze <andrea.stuerze@bkg.bund.de>
  75. *           - added stream retrieval from caster
  76. *
  77. * Changes - Version 0.14
  78. * May 16 2006  Andrea Stuerze <andrea.stuerze@bkg.bund.de>
  79. *           - bug fixed in base64_encode-function
  80. *
  81. * Changes - Version 0.15
  82. * Jun 02 2006  Georg Weber <georg.weber@bkg.bund.de>
  83. *           - modification for SISNeT 3.1 protocol
  84. *
  85. * Changes - Version 0.16
  86. * Jul 06 2006 Andrea Stuerze <andrea.stuerze@bkg.bund.de>
  87. *           - more flexible caster's response
  88. *
  89. * Changes - Version 0.17
  90. * Jul 27 2006  Dirk Stoecker <soft@dstoecker.de>
  91. *           - fixed some problems with caster download
  92. *           - some minor cosmetic changes
  93. *
  94. * Changes - Version 0.18
  95. * Oct 30 2006 Andrea Stuerze <andrea.stuerze@bkg.bund.de>
  96. *           - added possibility to send receiver ID with password
  97. *             restricted to mode UDPSOCKET and TCPSOCKET.
  98. *
  99. * Changes - Version 0.19
  100. * Nov 30 2006 Andrea Stuerze <andrea.stuerze@bkg.bund.de>
  101. *           - bug fixed in Mode 2
  102. *
  103. * Changes - Version 0.20
  104. * Feb 13 2007  Dirk Stoecker <soft@dstoecker.de>
  105. *           - default port changed from 80 to 2101
  106. *           - fixed illegal memory access
  107. *           - cleanup of no data alarm timer
  108. *           - fixed zero byte handling in buffers
  109. *
  110. */

  111. #include <ctype.h>
  112. #include <errno.h>
  113. #include <fcntl.h>
  114. #include <getopt.h>
  115. #include <netdb.h>
  116. #include <signal.h>
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include <unistd.h>
  121. #include <arpa/inet.h>
  122. #include <netinet/in.h>
  123. #include <sys/socket.h>
  124. #include <sys/termios.h>
  125. #include <sys/types.h>

  126. #ifndef MSG_DONTWAIT
  127. #define MSG_DONTWAIT 0 /* prevent compiler errors */
  128. #endif
  129. #ifndef O_EXLOCK
  130. #define O_EXLOCK 0 /* prevent compiler errors */
  131. #endif

  132. enum MODE { SERIAL = 1, TCPSOCKET = 2, INFILE = 3, SISNET = 4, UDPSOCKET = 5,
  133. CASTER = 6, LAST};

  134. #define VERSION         "NTRIP NtripServerLinux/0.20"
  135. #define BUFSZ           1024

  136. /* default socket source */
  137. #define SERV_HOST_ADDR  "localhost"
  138. #define SERV_TCP_PORT   2101

  139. /* default destination */
  140. #define NTRIP_CASTER    "www.euref-ip.net"
  141. #define NTRIP_PORT      2101

  142. /* default sisnet source */
  143. #define SISNET_SERVER   "131.176.49.142"
  144. #define SISNET_PORT     7777

  145. #define ALARMTIME       60

  146. static int ttybaud             = 19200;
  147. static const char *ttyport     = "/dev/gps";
  148. static const char *filepath    = "/dev/stdin";
  149. static enum MODE mode          = INFILE;
  150. static int sisnet              = 31;
  151. static int gpsfd               = -1;

  152. /* Forward references */
  153. static int openserial(const char * tty, int blocksz, int baud);
  154. static void send_receive_loop(int sock, int fd);
  155. static void usage(int);
  156. static int encode(char *buf, int size, const char *user, const char *pwd);

  157. #ifdef __GNUC__
  158. static __attribute__ ((noreturn)) void sighandler_alarm(
  159. int sig __attribute__((__unused__)))
  160. #else /* __GNUC__ */
  161. static void sighandler_alarm(int sig)
  162. #endif /* __GNUC__ */
  163. {
  164.   fprintf(stderr, "ERROR: more than %d seconds no activity\n", ALARMTIME);
  165.   exit(1);
  166. }

  167. /*
  168. * main
  169. *
  170. * Main entry point for the program.  Processes command-line arguments and
  171. * prepares for action.
  172. *
  173. * Parameters:
  174. *     argc : integer        : Number of command-line arguments.
  175. *     argv : array of char  : Command-line arguments as an array of
  176. *                             zero-terminated pointers to strings.
  177. *
  178. * Return Value:
  179. *     The function does not return a value (although its return type is int).
  180. *
  181. * Remarks:
  182. *
  183. */

  184. int main(int argc, char **argv)
  185. {
  186.   int c;
  187.   int size = 2048;              /* for setting send buffer size */

  188.   const char *inhost = 0;
  189.   const char *outhost = 0;
  190.   unsigned int outport = 0;
  191.   unsigned int inport = 0;
  192.   const char *mountpoint = NULL;
  193.   const char *password = "";
  194.   const char *sisnetpassword = "";
  195.   const char *sisnetuser = "";
  196.   
  197.   const char *stream_name=0;
  198.   const char *stream_user=0;
  199.   const char *stream_password=0;
  200.   
  201.   const char *initfile = NULL;
  202.   
  203.   const char *recvrid=0;
  204.   const char *recvrpwd=0;
  205.   
  206.   int bindmode = 0;
  207.   int sock_id;
  208.   char szSendBuffer[BUFSZ];
  209.   int nBufferBytes;
  210.   struct hostent *he;
  211.   struct sockaddr_in addr;

  212.   signal(SIGALRM,sighandler_alarm);
  213.   alarm(ALARMTIME);
  214.   /* get and check program arguments */
  215.   if(argc <= 1)
  216.   {
  217.     usage(2);
  218.     exit(1);
  219.   }
  220.   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"))
  221.   != EOF)
  222.   {
  223.     switch (c)
  224.     {
  225.     case 'M':
  226.       if(!strcmp(optarg, "serial")) mode = SERIAL;
  227.       else if(!strcmp(optarg, "tcpsocket")) mode = TCPSOCKET;
  228.       else if(!strcmp(optarg, "file")) mode = INFILE;
  229.       else if(!strcmp(optarg, "sisnet")) mode = SISNET;
  230.       else if(!strcmp(optarg, "udpsocket")) mode = UDPSOCKET;
  231.       else if(!strcmp(optarg, "caster")) mode = CASTER;
  232.       else mode = atoi(optarg);
  233.       if((mode == 0) || (mode >= LAST))
  234.       {
  235.         fprintf(stderr, "ERROR: can't convert %s to a valid mode\n", optarg);
  236.         usage(-1);
  237.       }
  238.       break;
  239.     case 'i':                  /* gps serial ttyport */
  240.       ttyport = optarg;
  241.       break;
  242.     case 'B':
  243.       bindmode = 1;
  244.       break;
  245.     case 'V':
  246.       if(!strcmp("3.0", optarg)) sisnet = 30;
  247.       else if(!strcmp("3.1", optarg)) sisnet = 31;
  248.       else if(!strcmp("2.1", optarg)) sisnet = 21;
  249.       else
  250.       {
  251.         fprintf(stderr, "ERROR: unknown SISNeT version %s\n", optarg);
  252.         usage(-2);
  253.       }
  254.       break;
  255.     case 'b':                  /* serial ttyin speed */
  256.       ttybaud = atoi(optarg);
  257.       if(ttybaud <= 1)
  258.       {
  259.         fprintf(stderr, "ERROR: can't convert %s to valid serial speed\n",
  260.           optarg);
  261.         usage(1);
  262.       }
  263.       break;
  264.     case 'a':                  /* http server IP address A.B.C.D */
  265.       outhost = optarg;
  266.       break;
  267.     case 'p':                  /* http server port */
  268.       outport = atoi(optarg);
  269.       if(outport <= 1 || outport > 65535)
  270.       {
  271.         fprintf(stderr,
  272.           "ERROR: can't convert %s to a valid HTTP server port\n", optarg);
  273.         usage(1);
  274.       }
  275.       break;
  276.     case 'm':                  /* http server mountpoint */
  277.       mountpoint = optarg;
  278.       break;
  279.     case 's':                  /* datastream from file */
  280.       filepath = optarg;
  281.       break;
  282.     case 'f':
  283.       initfile = optarg;
  284.       break;
  285.     case 'x':
  286.       recvrid = optarg;  
  287.       break;      
  288.     case 'y':
  289.       recvrpwd = optarg;  
  290.       break;            
  291.     case 'u':
  292.       sisnetuser = optarg;
  293.       break;
  294.     case 'l':
  295.       sisnetpassword = optarg;
  296.       break;
  297.     case 'c':                  /* DestinationCasterPassword */
  298.       password = optarg;
  299.       break;
  300.     case 'H':                  /* SourceCasterHost */
  301.       inhost = optarg;
  302.       break;
  303.     case 'P':                  /* SourceCasterPort */
  304.       inport = atoi(optarg);
  305.       if(inport <= 1 || inport > 65535)
  306.       {
  307.         fprintf(stderr, "ERROR: can't convert %s to a valid port number\n",
  308.           optarg);
  309.         usage(1);
  310.       }
  311.       break;
  312.     case 'D':
  313.      stream_name=optarg;        /* desired stream from SourceCaster */
  314.      break;
  315.     case 'U':
  316.      stream_user=optarg;        /* username for desired stream */
  317.      break;
  318.     case 'W':
  319.      stream_password=optarg;    /* passwd for desired stream */
  320.      break;
  321.     case 'h':                  /* help */
  322.     case '?':
  323.       usage(0);
  324.       break;
  325.     default:
  326.       usage(2);
  327.       break;
  328.     }
  329.   }

  330.   argc -= optind;
  331.   argv += optind;

  332.   if(argc > 0)
  333.   {
  334.     fprintf(stderr, "ERROR: Extra args on command line: ");
  335.     for(; argc > 0; argc--)
  336.     {
  337.       fprintf(stderr, " %s", *argv++);
  338.     }
  339.     fprintf(stderr, "\n");
  340.     usage(1);                   /* never returns */
  341.   }

  342.   if(mountpoint == NULL)
  343.   {
  344.     fprintf(stderr, "ERROR: Missing mountpoint argument\n");
  345.     exit(1);
  346.   }
  347.   if(!password[0])
  348.   {
  349.     fprintf(stderr,
  350.       "WARNING: Missing password argument - are you really sure?\n");
  351.   }

  352.   if(stream_name && stream_user && !stream_password)
  353.   {
  354.     fprintf(stderr, "WARNING: Missing password argument for download"
  355.       " - are you really sure?\n");
  356.   }

  357.   if(!outhost) outhost = NTRIP_CASTER;
  358.   if(!outport) outport = NTRIP_PORT;

  359.   switch(mode)
  360.   {
  361.   case INFILE:
  362.     {
  363.       if((gpsfd = open(filepath, O_RDONLY)) < 0)
  364.       {
  365.         perror("ERROR: opening input file");
  366.         exit(1);
  367.       }
  368.       /* set blocking mode in case it was not set
  369.         (seems to be sometimes for fifo's) */
  370.       fcntl(gpsfd, F_SETFL, 0);
  371.       printf("file input: file = %s\n", filepath);
  372.     }
  373.     break;
  374.   case SERIAL:                 /* open serial port */
  375.     {
  376.       gpsfd = openserial(ttyport, 1, ttybaud);
  377.       if(gpsfd < 0)
  378.       {
  379.         exit(1);
  380.       }
  381.       printf("serial input: device = %s, speed = %d\n", ttyport, ttybaud);
  382.     }
  383.     break;
  384.   case TCPSOCKET: case UDPSOCKET: case SISNET: case CASTER:
  385.     {
  386.       if(mode == SISNET)
  387.       {
  388.         if(!inhost) inhost = SISNET_SERVER;
  389.         if(!inport) inport = SISNET_PORT;
  390.       }
  391.       else if(mode == CASTER)
  392.       {
  393.         if(!inport) inport = NTRIP_PORT;
  394.         if(!inhost) inhost = NTRIP_CASTER;
  395.       }
  396.       else if((mode == TCPSOCKET) || (mode == UDPSOCKET))
  397.       {
  398.         if(!inport) inport = SERV_TCP_PORT;
  399.         if(!inhost) inhost = "127.0.0.1";
  400.       }      

  401.       if(!(he = gethostbyname(inhost)))
  402.       {
  403.         fprintf(stderr, "ERROR: host %s unknown\n", inhost);
  404.         usage(-2);
  405.       }

  406.       if((gpsfd = socket(AF_INET, mode == UDPSOCKET
  407.       ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
  408.       {
  409.         fprintf(stderr, "ERROR: can't create socket\n");
  410.         exit(1);
  411.       }

  412.       memset((char *) &addr, 0x00, sizeof(addr));
  413.       if(!bindmode)
  414.         memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
  415.       addr.sin_family = AF_INET;
  416.       addr.sin_port = htons(inport);

  417.       printf("%s input: host = %s, port = %d, %s%s%s%s%s\n",
  418.       mode == CASTER ? "caster" : mode == SISNET ? "sisnet" :
  419.       mode == TCPSOCKET ? "tcp socket" : "udp socket",
  420.       bindmode ? "127.0.0.1" : inet_ntoa(addr.sin_addr),
  421.       inport, stream_name ? "stream = " : "", stream_name ? stream_name : "",
  422.       initfile ? ", initfile = " : "", initfile ? initfile : "",
  423.       bindmode ? "binding mode" : "");

  424.       if(bindmode)
  425.       {
  426.         if(bind(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  427.         {
  428.           fprintf(stderr, "ERROR: can't bind input to port %d\n", inport);
  429.           exit(1);
  430.         }
  431.       }
  432.       else if(connect(gpsfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  433.       {
  434.         fprintf(stderr, "ERROR: can't connect input to %s at port %d\n",
  435.           inet_ntoa(addr.sin_addr), inport);
  436.         exit(1);
  437.       }
  438.             
  439.       if(stream_name) /* data stream from caster */
  440.       {
  441.         int init = 0;

  442.         /* set socket buffer size */
  443.         setsockopt(gpsfd, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
  444.           sizeof(const char *));
  445.         if(stream_user && stream_password)
  446.         {
  447.           /* leave some space for login */
  448.           nBufferBytes=snprintf(szSendBuffer, sizeof(szSendBuffer)-40,
  449.           "GET /%s HTTP/1.0\r\n"
  450.           "User-Agent: %s\r\n"
  451.           "Authorization: Basic ", stream_name, VERSION);
  452.           /* second check for old glibc */
  453.           if(nBufferBytes > (int)sizeof(szSendBuffer)-40 || nBufferBytes < 0)
  454.           {
  455.             fprintf(stderr, "Requested data too long\n");
  456.             exit(1);
  457.           }
  458.           nBufferBytes += encode(szSendBuffer+nBufferBytes,
  459.             sizeof(szSendBuffer)-nBufferBytes-4, stream_user, stream_password);
  460.           if(nBufferBytes > (int)sizeof(szSendBuffer)-4)
  461.           {
  462.             fprintf(stderr, "Username and/or password too long\n");
  463.             exit(1);
  464.           }
  465.           szSendBuffer[nBufferBytes++] = '\r';
  466.           szSendBuffer[nBufferBytes++] = '\n';
  467.           szSendBuffer[nBufferBytes++] = '\r';
  468.           szSendBuffer[nBufferBytes++] = '\n';
  469.         }
  470.         else
  471.         {
  472.           nBufferBytes = snprintf(szSendBuffer, sizeof(szSendBuffer),
  473.           "GET /%s HTTP/1.0\r\n"
  474.           "User-Agent: %s\r\n"
  475.           "\r\n", stream_name, VERSION);
  476.         }
  477.         if((send(gpsfd, szSendBuffer, (size_t)nBufferBytes, 0))
  478.         != nBufferBytes)
  479.         {
  480.           fprintf(stderr, "ERROR: could not send to caster\n");
  481.           exit(1);
  482.         }
  483.         nBufferBytes = 0;
  484.         /* check caster's response */
  485.         while(!init && nBufferBytes < (int)sizeof(szSendBuffer)
  486.         && (nBufferBytes += recv(gpsfd, szSendBuffer,
  487.         sizeof(szSendBuffer)-nBufferBytes, 0)) > 0)
  488.         {
  489.           if(strstr(szSendBuffer, "\r\n"))
  490.           {
  491.             if(!strncmp(szSendBuffer, "ICY 200 OK\r\n", 10))
  492.               init = 1;
  493.             else
  494.             {
  495.               int k;
  496.               fprintf(stderr, "Could not get the requested data: ");
  497.               for(k = 0; k < nBufferBytes && szSendBuffer[k] != '\n'
  498.               && szSendBuffer[k] != '\r'; ++k)
  499.               {
  500.                 fprintf(stderr, "%c", isprint(szSendBuffer[k])
  501.                 ? szSendBuffer[k] : '.');
  502.               }
  503.               fprintf(stderr, "\n");
  504.               exit(1);
  505.             }
  506.           }
  507.         }
  508.         if(!init)
  509.         {
  510.           fprintf(stderr, "Could not init caster download.");
  511.           exit(1);
  512.         }
  513.       } /* end data stream from caster */

  514.       if(initfile && mode != SISNET)
  515.       {
  516.         char buffer[1024];
  517.         FILE *fh;
  518.         int i;

  519.         if((fh = fopen(initfile, "r")))
  520.         {
  521.           while((i = fread(buffer, 1, sizeof(buffer), fh)) > 0)
  522.           {
  523.             if((send(gpsfd, buffer, (size_t)i, 0)) != i)
  524.             {
  525.               perror("ERROR: sending init file");
  526.               exit(1);
  527.             }
  528.           }
  529.           if(i < 0)
  530.           {
  531.             perror("ERROR: reading init file");
  532.             exit(1);
  533.           }
  534.           fclose(fh);
  535.         }
  536.         else
  537.         {
  538.           fprintf(stderr, "ERROR: can't read init file %s\n", initfile);
  539.           exit(1);
  540.         }
  541.       }
  542.     }
  543.     if(mode == SISNET)
  544.     {
  545.       int i, j;
  546.       char buffer[1024];

  547.       i = snprintf(buffer, sizeof(buffer), sisnet >= 30 ? "AUTH,%s,%s\r\n"
  548.         : "AUTH,%s,%s", sisnetuser, sisnetpassword);
  549.       if((send(gpsfd, buffer, (size_t)i, 0)) != i)
  550.       {
  551.         perror("ERROR: sending authentication");
  552.         exit(1);
  553.       }
  554.       i = sisnet >= 30 ? 7 : 5;
  555.       if((j = recv(gpsfd, buffer, i, 0)) != i && strncmp("*AUTH", buffer, 5))
  556.       {
  557.         fprintf(stderr, "ERROR: SISNeT connect failed:");
  558.         for(i = 0; i < j; ++i)
  559.         {
  560.           if(buffer[i] != '\r' && buffer[i] != '\n')
  561.           {
  562.             fprintf(stderr, "%c", isprint(buffer[i]) ? buffer[i] : '.');
  563.           }
  564.         }
  565.         fprintf(stderr, "\n");
  566.         exit(1);
  567.       }
  568.       if(sisnet >= 31)
  569.       {
  570.         if((send(gpsfd, "START\r\n", 7, 0)) != i)
  571.         {
  572.           perror("ERROR: sending start command");
  573.           exit(1);
  574.         }
  575.       }
  576.     }

  577.     if (recvrid && recvrpwd && ((mode == TCPSOCKET) || (mode == UDPSOCKET)))
  578.     {
  579.       if (strlen(recvrid) > (BUFSZ-3)){
  580.         fprintf(stderr, "Receiver ID too long\n"); exit(0);
  581.       }else{
  582.         fprintf(stderr, "Sending user ID for receiver...\n");
  583.         nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);      
  584.         strcpy(szSendBuffer, recvrid);
  585.         strcat(szSendBuffer,"\r\n");
  586.         send(gpsfd,szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);
  587.       }
  588.       
  589.       if (strlen(recvrpwd) > (BUFSZ-3)){
  590.         fprintf(stderr, "Receiver password too long\n"); exit(0);
  591.       }else{
  592.         fprintf(stderr, "Sending user password for receiver...\n");
  593.         nBufferBytes = read(gpsfd, szSendBuffer, BUFSZ);
  594.         strcpy(szSendBuffer, recvrpwd);
  595.         strcat(szSendBuffer,"\r\n");
  596.         send(gpsfd, szSendBuffer, strlen(szSendBuffer), MSG_DONTWAIT);   
  597.       }
  598.     }      
  599.     break;
  600.   default:
  601.     usage(-1);
  602.     break;
  603.   }

  604.   /* ----- main part ----- */
  605.   for(;;)
  606.   {
  607.     if(!(he = gethostbyname(outhost)))
  608.     {
  609.       fprintf(stderr, "ERROR: host %s unknown\n", outhost);
  610.       usage(-2);
  611.     }

  612.     /* create socket */
  613.     if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  614.     {
  615.       fprintf(stderr, "ERROR: could not create socket\n");
  616.       exit(2);
  617.     }

  618.     memset((char *) &addr, 0x00, sizeof(addr));
  619.     memcpy(&addr.sin_addr, he->h_addr, (size_t)he->h_length);
  620.     addr.sin_family = AF_INET;
  621.     addr.sin_port = htons(outport);

  622.     /* connect to caster */
  623.     fprintf(stderr, "caster output: host = %s, port = %d, mountpoint = %s\n",
  624.       inet_ntoa(addr.sin_addr), outport, mountpoint);
  625.     if(connect(sock_id, (struct sockaddr *) &addr, sizeof(addr)) < 0)
  626.     {
  627.       fprintf(stderr, "ERROR: can't connect output to %s at port %d\n",
  628.         inet_ntoa(addr.sin_addr), outport);
  629.       close(sock_id);
  630.       exit(3);
  631.     }

  632.     /* set socket buffer size */
  633.     setsockopt(sock_id, SOL_SOCKET, SO_SNDBUF, (const char *) &size,
  634.       sizeof(const char *));
  635.     /* send message to caster */
  636.     nBufferBytes = sprintf(szSendBuffer, "SOURCE %s /%s\r\nSource-Agent: "
  637.     VERSION "\r\n\r\n", password, mountpoint);
  638.     if((send(sock_id, szSendBuffer, (size_t)nBufferBytes, 0)) != nBufferBytes)
  639.     {
  640.       fprintf(stderr, "ERROR: could not send to caster\n");
  641.       break;
  642.     }
  643.     /* check caster's response */
  644.     nBufferBytes = recv(sock_id, szSendBuffer, sizeof(szSendBuffer), 0);
  645.     szSendBuffer[nBufferBytes] = '\0';
  646.     if(!strstr(szSendBuffer, "OK"))
  647.     {
  648.       char *a;
  649.       fprintf(stderr, "ERROR: caster's reply is not OK : ");
  650.       for(a = szSendBuffer; *a && *a != '\n' && *a != '\r'; ++a)
  651.       {
  652.         fprintf(stderr, "%.1s", isprint(*a) ? a : ".");
  653.       }
  654.       fprintf(stderr, "\n");
  655.       break;
  656.     }
  657.     printf("connection successfull\n");
  658.     send_receive_loop(sock_id, gpsfd);
  659.   }
  660.   close(sock_id);
  661.   sleep(5);
  662.   return 0;
  663. }

  664. static void send_receive_loop(int sock, int fd)
  665. {
  666.   int nodata = 0;
  667.   char buffer[BUFSZ] = { 0 };
  668.   char sisnetbackbuffer[200];
  669.   int nBufferBytes = 0;
  670.   /* data transmission */
  671.   printf("transfering data ...\n");
  672.   while(1)
  673.   {
  674.     if(!nodata) alarm(ALARMTIME);
  675.     else nodata = 0;

  676.     if(!nBufferBytes)
  677.     {
  678.       if(mode == SISNET && sisnet <= 30)
  679.       {
  680.         int i;
  681.         /* a somewhat higher rate than 1 second to get really each block */
  682.         /* means we need to skip double blocks sometimes */
  683.         struct timeval tv = {0,700000};
  684.         select(0, 0, 0, 0, &tv);
  685.         memcpy(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer));
  686.         i = (sisnet >= 30 ? 5 : 3);
  687.         if((send(gpsfd, "MSG\r\n", i, 0)) != i)
  688.         {
  689.           perror("ERROR: sending data request");
  690.           exit(1);
  691.         }
  692.       }
  693.       /* receiving data */
  694.       nBufferBytes = read(fd, buffer, sizeof(buffer));
  695.       if(!nBufferBytes)
  696.       {
  697.         printf("WARNING: no data received from input\n");
  698.         sleep(3);
  699.         nodata = 1;
  700.         continue;
  701.       }
  702.       else if(nBufferBytes < 0)
  703.       {
  704.         perror("ERROR: reading input failed");
  705.         exit(1);
  706.       }
  707.       /* we can compare the whole buffer, as the additional bytes
  708.          remain unchanged */
  709.       if(mode == SISNET && sisnet <= 30 &&
  710.       !memcmp(sisnetbackbuffer, buffer, sizeof(sisnetbackbuffer)))
  711.       {
  712.         nBufferBytes = 0;
  713.       }
  714.     }
  715.     if(nBufferBytes)
  716.     {
  717.       int i;
  718.       /* send data */
  719.       if((i = send(sock, buffer, (size_t)nBufferBytes, MSG_DONTWAIT))
  720.         != nBufferBytes)
  721.       {
  722.         if(i < 0)
  723.         {
  724.           if(errno != EAGAIN)
  725.           {
  726.             perror("WARNING: could not send data - retry connection");
  727.             close(sock);
  728.             sleep(5);
  729.             return;
  730.           }
  731.         }
  732.         else if(i)
  733.         {
  734.           memmove(buffer, buffer+i, (size_t)(nBufferBytes-i));
  735.           nBufferBytes -= i;
  736.         }
  737.       }
  738.       else
  739.       {
  740.         nBufferBytes = 0;
  741.       }
  742.     }
  743.   }
  744. }

  745. /*
  746. * openserial
  747. *
  748. * Open the serial port with the given device name and configure it for
  749. * reading NMEA data from a GPS receiver.
  750. *
  751. * Parameters:
  752. *     tty     : pointer to    : A zero-terminated string containing the device
  753. *               unsigned char   name of the appropriate serial port.
  754. *     blocksz : integer       : Block size for port I/O
  755. *     baud :    integer       : Baud rate for port I/O
  756. *
  757. * Return Value:
  758. *     The function returns a file descriptor for the opened port if successful.
  759. *     The function returns -1 in the event of an error.
  760. *
  761. * Remarks:
  762. *
  763. */

  764. static int openserial(const char * tty, int blocksz, int baud)
  765. {
  766.   int fd;
  767.   struct termios termios;

  768.   fd = open(tty, O_RDWR | O_NONBLOCK | O_EXLOCK);
  769.   if(fd < 0)
  770.   {
  771.     perror("ERROR: opening serial connection");
  772.     return (-1);
  773.   }
  774.   if(tcgetattr(fd, &termios) < 0)
  775.   {
  776.     perror("ERROR: get serial attributes");
  777.     return (-1);
  778.   }
  779.   termios.c_iflag = 0;
  780.   termios.c_oflag = 0;          /* (ONLRET) */
  781.   termios.c_cflag = CS8 | CLOCAL | CREAD;
  782.   termios.c_lflag = 0;
  783.   {
  784.     int cnt;
  785.     for(cnt = 0; cnt < NCCS; cnt++)
  786.       termios.c_cc[cnt] = -1;
  787.   }
  788.   termios.c_cc[VMIN] = blocksz;
  789.   termios.c_cc[VTIME] = 2;

  790. #if (B4800 != 4800)
  791.   /*
  792.    * Not every system has speed settings equal to absolute speed value.
  793.    */

  794.   switch (baud)
  795.   {
  796.   case 300:
  797.     baud = B300;
  798.     break;
  799.   case 1200:
  800.     baud = B1200;
  801.     break;
  802.   case 2400:
  803.     baud = B2400;
  804.     break;
  805.   case 4800:
  806.     baud = B4800;
  807.     break;
  808.   case 9600:
  809.     baud = B9600;
  810.     break;
  811.   case 19200:
  812.     baud = B19200;
  813.     break;
  814.   case 38400:
  815.     baud = B38400;
  816.     break;
  817. #ifdef B57600
  818.   case 57600:
  819.     baud = B57600;
  820.     break;
  821. #endif
  822. #ifdef B115200
  823.   case 115200:
  824.     baud = B115200;
  825.     break;
  826. #endif
  827. #ifdef B230400
  828.   case 230400:
  829.     baud = B230400;
  830.     break;
  831. #endif
  832.   default:
  833.     fprintf(stderr, "WARNING: Baud settings not useful, using 19200\n");
  834.     baud = B19200;
  835.     break;
  836.   }
  837. #endif

  838.   if(cfsetispeed(&termios, baud) != 0)
  839.   {
  840.     perror("ERROR: setting serial speed with cfsetispeed");
  841.     return (-1);
  842.   }
  843.   if(cfsetospeed(&termios, baud) != 0)
  844.   {
  845.     perror("ERROR: setting serial speed with cfsetospeed");
  846.     return (-1);
  847.   }
  848.   if(tcsetattr(fd, TCSANOW, &termios) < 0)
  849.   {
  850.     perror("ERROR: setting serial attributes");
  851.     return (-1);
  852.   }
  853.   if(fcntl(fd, F_SETFL, 0) == -1)
  854.   {
  855.     perror("WARNING: setting blocking mode failed");
  856.   }
  857.   return (fd);
  858. }

  859. /*
  860. * usage
  861. *
  862. * Send a usage message to standard error and quit the program.
  863. *
  864. * Parameters:
  865. *     None.
  866. *
  867. * Return Value:
  868. *     The function does not return a value.
  869. *
  870. * Remarks:
  871. *
  872. */

  873. static
  874. #ifdef __GNUC__
  875. __attribute__ ((noreturn))
  876. #endif /* __GNUC__ */
  877. void usage(int rc)
  878. {
  879.   fprintf(stderr, "Usage: %s [OPTIONS]\n", VERSION);
  880.   fprintf(stderr, "  Options are: [-]           \n");
  881.   fprintf(stderr, "    -a DestinationCaster name or address (default: %s)\n",
  882.     NTRIP_CASTER);
  883.   fprintf(stderr, "    -p DestinationCaster port (default: %d)\n", NTRIP_PORT);
  884.   fprintf(stderr, "    -m DestinationCaster mountpoint\n");
  885.   fprintf(stderr, "    -c DestinationCaster password\n");
  886.   fprintf(stderr, "    -h|? print this help screen\n");
  887.   fprintf(stderr, "    -M <mode>  sets the input mode\n");
  888.   fprintf(stderr, "               (1=serial, 2=tcpsocket, 3=file, 4=sisnet"
  889.     ", 5=udpsocket, 6=caster)\n");
  890.   fprintf(stderr, "  Mode = file:\n");
  891.   fprintf(stderr, "    -s file, simulate data stream by reading log file\n");
  892.   fprintf(stderr, "       default/current setting is %s\n", filepath);
  893.   fprintf(stderr, "  Mode = serial:\n");
  894.   fprintf(stderr, "    -b baud_rate, sets serial input baud rate\n");
  895.   fprintf(stderr, "       default/current value is %d\n", ttybaud);
  896.   fprintf(stderr, "    -i input_device, sets name of serial input device\n");
  897.   fprintf(stderr, "       default/current value is %s\n", ttyport);
  898.   fprintf(stderr, "       (normally a symbolic link to /dev/tty\?\?)\n");
  899.   fprintf(stderr, "  Mode = tcpsocket or udpsocket:\n");
  900.   fprintf(stderr, "    -P receiver port (default: %d)\n", SERV_TCP_PORT);
  901.   fprintf(stderr, "    -H hostname of TCP server (default: %s)\n",
  902.     SERV_HOST_ADDR);
  903.   fprintf(stderr, "    -f initfile send to server\n");
  904.   fprintf(stderr, "    -x receiver id\n");
  905.   fprintf(stderr, "    -y receiver password\n");  
  906.   fprintf(stderr, "    -B bindmode: bind to incoming UDP stream\n");
  907.   fprintf(stderr, "  Mode = sisnet:\n");
  908.   fprintf(stderr, "    -P receiver port (default: %d)\n", SISNET_PORT);
  909.   fprintf(stderr, "    -H hostname of TCP server (default: %s)\n",
  910.     SISNET_SERVER);
  911.   fprintf(stderr, "    -u username\n");
  912.   fprintf(stderr, "    -l password\n");
  913.   fprintf(stderr, "    -V version [2.1, 3.0 or 3.1] (default: 3.1)\n");
  914.   fprintf(stderr, "  Mode = caster:\n");
  915.   fprintf(stderr, "    -P SourceCaster port (default: %d)\n", NTRIP_PORT);
  916.   fprintf(stderr, "    -H SourceCaster hostname (default: %s)\n",
  917.     NTRIP_CASTER);
  918.   fprintf(stderr, "    -D SourceCaster mountpoint\n");
  919.   fprintf(stderr, "    -U SourceCaster mountpoint username\n");
  920.   fprintf(stderr, "    -W SourceCaster mountpoint password\n");  
  921.   fprintf(stderr, "\n");
  922.   exit(rc);
  923. }

  924. static const char encodingTable [64] = {
  925.   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  926.   'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
  927.   'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
  928.   'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
  929. };

  930. /* does not buffer overrun, but breaks directly after an error */
  931. /* returns the number of required bytes */
  932. static int encode(char *buf, int size, const char *user, const char *pwd)
  933. {
  934.   unsigned char inbuf[3];
  935.   char *out = buf;
  936.   int i, sep = 0, fill = 0, bytes = 0;

  937.   while(*user || *pwd)
  938.   {
  939.     i = 0;
  940.     while(i < 3 && *user) inbuf[i++] = *(user++);
  941.     if(i < 3 && !sep)    {inbuf[i++] = ':'; ++sep; }
  942.     while(i < 3 && *pwd)  inbuf[i++] = *(pwd++);
  943.     while(i < 3)         {inbuf[i++] = 0; ++fill; }
  944.     if(out-buf < size-1)
  945.       *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
  946.     if(out-buf < size-1)
  947.       *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
  948.                | ((inbuf [1] & 0xF0) >> 4)];
  949.     if(out-buf < size-1)
  950.     {
  951.       if(fill == 2)
  952.         *(out++) = '=';
  953.       else
  954.         *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
  955.                  | ((inbuf [2] & 0xC0) >> 6)];
  956.     }
  957.     if(out-buf < size-1)
  958.     {
  959.       if(fill >= 1)
  960.         *(out++) = '=';
  961.       else
  962.         *(out++) = encodingTable[inbuf [2] & 0x3F];
  963.     }
  964.     bytes += 4;
  965.   }
  966.   if(out-buf < size)
  967.     *out = 0;
  968.   return bytes;
  969. }
復制代碼

所有資料51hei提供下載:
NtripServerLinux.rar (8 KB, 下載次數: 10)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:386168 發表于 2019-6-14 15:09 | 只看該作者
下來看看,感謝分享
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品夜色一区二区三区 | 国产欧美精品区一区二区三区 | www.色综合| 亚洲一区二区三区国产 | 久久久久久久av | 日韩黄 | 欧美在线视频网 | 成人精品一区二区三区中文字幕 | 波多野结衣av中文字幕 | 午夜三区 | 日本天天操 | 国产高清精品一区二区三区 | 欧美男人亚洲天堂 | 欧美性久久| 国产免费一区二区 | 精品一区二区三区在线观看国产 | av在线一区二区三区 | 日韩电影免费在线观看中文字幕 | 视频一区 国产精品 | h网站在线观看 | 欧美美女爱爱 | 99精品欧美一区二区三区综合在线 | 婷婷在线视频 | 欧美午夜视频 | 久久久www | 人人看人人射 | 在线免费观看一区二区 | 香蕉二区 | 久久精品久久精品久久精品 | 一区二区三区免费在线观看 | 亚洲一区二区三区在线 | 国产精品久久久久不卡 | 欧美日韩在线免费 | 国产精品久久久久久久久免费樱桃 | 久久精品99国产精品 | 九九在线视频 | 天天曰夜夜 | 91精品久久久久久久久中文字幕 | 亚洲精品国产一区 | 久久精品国产亚洲夜色av网站 | 日韩精品在线一区 |