static int ntrip_stream_req_probe(const struct ntrip_stream_t *stream, struct gpsd_errout_t *errout) { int dsock; ssize_t r; char buf[BUFSIZ]; dsock = netlib_connectsock(AF_UNSPEC, stream->url, stream->port, "tcp"); if (dsock < 0) { gpsd_log(errout, LOG_ERROR, "ntrip stream connect error %d in req probe\n", dsock); return -1; } gpsd_log(errout, LOG_SPIN, "ntrip stream for req probe connected on fd %d\n", dsock); (void)snprintf(buf, sizeof(buf), "GET / HTTP/1.1\r\n" "User-Agent: NTRIP gpsd/%s\r\n" "Host: %s\r\n" "Connection: close\r\n" "\r\n", VERSION, stream->url); r = write(dsock, buf, strlen(buf)); if (r != (ssize_t)strlen(buf)) { gpsd_log(errout, LOG_ERROR, "ntrip stream write error %d on fd %d during probe request %zd\n", errno, dsock, r); (void)close(dsock); return -1; } /* coverity[leaked_handle] This is an intentional allocation */ return dsock; }
static int ntrip_stream_get_req(const struct ntrip_stream_t *stream, const struct gpsd_errout_t *errout) { int dsock; char buf[BUFSIZ]; dsock = netlib_connectsock(AF_UNSPEC, stream->url, stream->port, "tcp"); if (BAD_SOCKET(dsock)) { gpsd_log(errout, LOG_ERROR, "ntrip stream connect error %d\n", dsock); return -1; } gpsd_log(errout, LOG_SPIN, "netlib_connectsock() returns socket on fd %d\n", dsock); (void)snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n" "User-Agent: NTRIP gpsd/%s\r\n" "Host: %s\r\n" "Accept: rtk/rtcm, dgps/rtcm\r\n" "%s" "Connection: close\r\n" "\r\n", stream->mountpoint, VERSION, stream->url, stream->authStr); if (write(dsock, buf, strlen(buf)) != (ssize_t) strlen(buf)) { gpsd_log(errout, LOG_ERROR, "ntrip stream write error %d on fd %d during get request\n", errno, dsock); (void)close(dsock); return -1; } return dsock; }
static int ntrip_stream_probe(const char *caster, const char *port, const char *stream, struct ntrip_stream_t *keep) { int ret; socket_t dsock; char buf[BUFSIZ]; if ((dsock = netlib_connectsock(AF_UNSPEC, caster, port, "tcp")) == -1) { printf("ntrip stream connect error %d\n", dsock); return -1; } (void)snprintf(buf, sizeof(buf), "GET / HTTP/1.1\r\n" "User-Agent: NTRIP gpsd/%s\r\n" "Connection: close\r\n" "\r\n", VERSION); if (write(dsock, buf, strlen(buf)) != (ssize_t) strlen(buf)) { printf("ntrip stream write error %d\n", dsock); return -1; } ret = ntrip_sourcetable_parse(dsock, buf, (ssize_t) sizeof(buf), stream, keep); (void)close(dsock); return ret; }
int gps_sock_open(const char *host, const char *port, struct gps_data_t *gpsdata) { if (!host) host = "localhost"; if (!port) port = DEFAULT_GPSD_PORT; libgps_debug_trace((DEBUG_CALLS, "gps_sock_open(%s, %s)\n", host, port)); #ifndef USE_QT if ((gpsdata->gps_fd = netlib_connectsock(AF_UNSPEC, host, port, "tcp")) < 0) { errno = gpsdata->gps_fd; libgps_debug_trace((DEBUG_CALLS, "netlib_connectsock() returns error %d\n", errno)); return -1; } else libgps_debug_trace((DEBUG_CALLS, "netlib_connectsock() returns socket on fd %d\n", gpsdata->gps_fd)); #else QTcpSocket *sock = new QTcpSocket(); gpsdata->gps_fd = sock; sock->connectToHost(host, QString(port).toInt()); if (!sock->waitForConnected()) qDebug() << "libgps::connect error: " << sock->errorString(); else qDebug() << "libgps::connected!"; #endif /* USE_QT */ /* set up for line-buffered I/O over the daemon socket */ gpsdata->privdata = (void *)malloc(sizeof(struct privdata_t)); if (gpsdata->privdata == NULL) return -1; PRIVATE(gpsdata)->newstyle = false; PRIVATE(gpsdata)->waiting = 0; PRIVATE(gpsdata)->buffer[0] = 0; #ifdef LIBGPS_DEBUG PRIVATE(gpsdata)->waitcount = 0; #endif /* LIBGPS_DEBUG */ return 0; }
int dgpsip_open(struct gps_device_t *device, const char *dgpsserver) /* open a connection to a DGPSIP server */ { char *colon, *dgpsport = "rtcm-sc104"; int opts; device->dgpsip.reported = false; if ((colon = strchr(dgpsserver, ':')) != NULL) { dgpsport = colon + 1; *colon = '\0'; } if (!getservbyname(dgpsport, "tcp")) dgpsport = DEFAULT_RTCM_PORT; device->gpsdata.gps_fd = netlib_connectsock(AF_UNSPEC, dgpsserver, dgpsport, "tcp"); // cppcheck-suppress pointerPositive if (device->gpsdata.gps_fd >= 0) { char hn[256], buf[BUFSIZ]; gpsd_log(&device->context->errout, LOG_PROG, "connection to DGPS server %s established.\n", dgpsserver); (void)gethostname(hn, sizeof(hn)); /* greeting required by some RTCM104 servers; others will ignore it */ (void)snprintf(buf, sizeof(buf), "HELO %s gpsd %s\r\nR\r\n", hn, VERSION); if (write(device->gpsdata.gps_fd, buf, strlen(buf)) != (ssize_t) strlen(buf)) gpsd_log(&device->context->errout, LOG_ERROR, "hello to DGPS server %s failed\n", dgpsserver); } else gpsd_log(&device->context->errout, LOG_ERROR, "can't connect to DGPS server %s, netlib error %d.\n", dgpsserver, device->gpsdata.gps_fd); opts = fcntl(device->gpsdata.gps_fd, F_GETFL); if (opts >= 0) (void)fcntl(device->gpsdata.gps_fd, F_SETFL, opts | O_NONBLOCK); device->servicetype = service_dgpsip; return device->gpsdata.gps_fd; }
int main( int argc, char **argv) { int sock = 0; char buf[4096]; ssize_t wrote = 0; bool timestamp = false; bool new_line = true; long count = -1; int option; unsigned int vflag = 0, l = 0; char *arg = NULL, *colon1, *colon2, *device = NULL; char *port = DEFAULT_GPSD_PORT, *server = "127.0.0.1"; char *serialport = NULL; buf[0] = '\0'; while ((option = getopt(argc, argv, "?hrRwjtvVn:s:")) != -1) { switch (option) { case 'n': count = strtol(optarg, 0, 0); break; case 'r': (void)strlcat(buf, "r=1;", sizeof(buf)); break; case 'R': (void)strlcat(buf, "r=2;", sizeof(buf)); break; case 't': timestamp = true; break; case 'v': vflag++; break; case 'w': (void)strlcat(buf, "w=1;", sizeof(buf)); break; case 'j': (void)strlcat(buf, "j=1;", sizeof(buf)); break; case 'V': (void)fprintf(stderr, "%s: SVN ID: $Id: gpspipe.c 4315 2007-03-24 22:03:17Z ckuethe $ \n", argv[0]); exit(0); case 's': serialport = optarg; break; case '?': case 'h': default: usage(); exit(1); } } if (serialport!=NULL && strstr(buf, "r=1")==NULL) { (void)fprintf(stderr, "gpsipipe: use of '-s' requires '-r'.\n"); exit(1); } if (strstr(buf, "r=")==NULL && strstr(buf, "w=1")==NULL) { (void)fprintf(stderr, "gpspipe: one of '-R', '-r' or '-w' is required.\n"); exit(1); } /* Grok the server, port, and device. */ /*@ -branchstate @*/ if (optind < argc) { arg = strdup(argv[optind]); /*@i@*/colon1 = strchr(arg, ':'); server = arg; if (colon1 != NULL) { if (colon1 == arg) { server = NULL; } else { *colon1 = '\0'; } port = colon1 + 1; colon2 = strchr(port, ':'); if (colon2 != NULL) { if (colon2 == port) { port = NULL; } else { *colon2 = '\0'; } device = colon2 + 1; (void)snprintf(buf, sizeof(buf), "%sF=%s\n", buf, device); } } colon1 = colon2 = NULL; } /*@ +branchstate @*/ /* Open the serial port and set it up. */ if (serialport) open_serial(serialport); /*@ -nullpass @*/ sock = netlib_connectsock( server, port, "tcp"); if (sock < 0) { (void)fprintf(stderr, "gpspipe: could not connect to gpsd %s:%s, %s(%d)\n", server, port, strerror(errno), errno); exit(1); } /*@ +nullpass @*/ /* ship the assembled options */ wrote = write(sock, buf, strlen(buf)); if ((ssize_t)strlen(buf) != wrote) { (void)fprintf(stderr, "gpspipe: write error, %s(%d)\n", strerror(errno), errno); exit(1); } if (isatty(STDERR_FILENO) == 0) vflag = 0; for(;;) { int i = 0; int j = 0; int readbytes = 0; if (vflag) spinner(vflag, l++); readbytes = (int)read(sock, buf, sizeof(buf)); if (readbytes > 0) { for (i = 0 ; i < readbytes ; i++) { char c = buf[i]; if (j < (int)(sizeof(serbuf) - 1)) { serbuf[j++] = buf[i]; } if (new_line && timestamp) { time_t now = time(NULL); new_line = 0; if (fprintf(stdout, "%.24s :", ctime(&now)) <= 0) { (void)fprintf(stderr, "gpspipe: write error, %s(%d)\n", strerror(errno), errno); exit(1); } } if (fputc(c, stdout) == EOF) { fprintf( stderr, "gpspipe: Write Error, %s(%d)\n", strerror(errno), errno); exit(1); } if (c == '\n') { if (serialport != NULL) { if (write(fd_out, serbuf, (size_t)j) == -1) { fprintf(stderr, "gpspipe: Serial port write Error, %s(%d)\n", strerror(errno), errno); exit(1); } j = 0; } new_line = true; /* flush after every good line */ if (fflush(stdout)) { (void)fprintf(stderr, "gpspipe: fflush Error, %s(%d)\n", strerror(errno), errno); exit(1); } if (count > 0) { if (0 >= --count) { /* completed count */ exit(0); } } } } } else if (readbytes < 0) { (void) fprintf(stderr, "gpspipe: read error %s(%d)\n", strerror(errno), errno); exit(1); } } #ifdef __UNUSED__ if (serialport != NULL) { /* Restore the old serial port settings. */ if (tcsetattr(fd, TCSANOW, &oldtio) != 0) { (void)fprintf(stderr, "Error restoring serial port settings\n"); exit(1); } } exit(0); #endif /* __UNUSED__ */ }
/*@ -nullpass @*//* work around a splint bug */ static int ntrip_stream_open(const char *caster, const char *port, /*@null@*/ const char *auth, struct gps_context_t *context, struct ntrip_stream_t *stream) { char buf[BUFSIZ]; char authstr[128]; int opts; if (ntrip_auth_encode(stream, auth, authstr, sizeof(authstr)) < 0) { gpsd_report(LOG_ERROR, "User-ID and password needed for %s:%s/%s\n", caster, port, stream->mountpoint); return -1; } if ((context->dsock = netlib_connectsock(AF_UNSPEC, caster, port, "tcp")) < 0) return -1; (void)snprintf(buf, sizeof(buf), "GET /%s HTTP/1.1\r\n" "User-Agent: NTRIP gpsd/%s\r\n" "Accept: rtk/rtcm, dgps/rtcm\r\n" "%s" "Connection: close\r\n" "\r\n", stream->mountpoint, VERSION, authstr); if (write(context->dsock, buf, strlen(buf)) != (ssize_t) strlen(buf)) { printf("ntrip stream write error on %d\n", context->dsock); return -1; } memset(buf, 0, sizeof(buf)); if (read(context->dsock, buf, sizeof(buf) - 1) == -1) goto close; /* parse 401 Unauthorized */ if (strstr(buf, NTRIP_UNAUTH)) { gpsd_report(LOG_ERROR, "%s not authorized for Ntrip stream %s:%s/%s\n", auth, caster, port, stream->mountpoint); goto close; } /* parse SOURCETABLE */ if (strstr(buf, NTRIP_SOURCETABLE)) { gpsd_report(LOG_ERROR, "Broadcaster doesn't recognize Ntrip stream %s:%s/%s\n", caster, port, stream->mountpoint); goto close; } /* parse ICY 200 OK */ if (!strstr(buf, NTRIP_ICY)) { gpsd_report(LOG_ERROR, "Unknown reply %s from Ntrip service %s:%s/%s\n", buf, caster, port, stream->mountpoint); goto close; } opts = fcntl(context->dsock, F_GETFL); if (opts >= 0) (void)fcntl(context->dsock, F_SETFL, opts | O_NONBLOCK); context->netgnss_service = netgnss_ntrip; #ifndef S_SPLINT_S context->netgnss_privdata = stream; #endif return context->dsock; close: (void)close(context->dsock); return -1; }