telemetry: mark init function as internal-only
[dpdk.git] / lib / librte_telemetry / telemetry_legacy.c
index 8e24eb4..edd76ca 100644 (file)
@@ -2,10 +2,12 @@
  * Copyright(c) 2020 Intel Corporation
  */
 
+#ifndef RTE_EXEC_ENV_WINDOWS
 #include <unistd.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <pthread.h>
+#endif /* !RTE_EXEC_ENV_WINDOWS */
 
 /* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
 #undef RTE_USE_LIBBSD
@@ -77,28 +79,47 @@ static int
 register_client(const char *cmd __rte_unused, const char *params,
                char *buffer __rte_unused, int buf_len __rte_unused)
 {
+#ifndef RTE_EXEC_ENV_WINDOWS
        pthread_t th;
        char data[BUF_SIZE];
        int fd;
        struct sockaddr_un addrs;
+#endif /* !RTE_EXEC_ENV_WINDOWS */
 
+       if (!strchr(params, ':')) {
+               fprintf(stderr, "Invalid data\n");
+               return -1;
+       }
+#ifndef RTE_EXEC_ENV_WINDOWS
        strlcpy(data, strchr(params, ':'), sizeof(data));
        memcpy(data, &data[strlen(":\"")], strlen(data));
+       if (!strchr(data, '\"')) {
+               fprintf(stderr, "Invalid client data\n");
+               return -1;
+       }
        *strchr(data, '\"') = 0;
 
        fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+       if (fd < 0) {
+               perror("Failed to open socket");
+               return -1;
+       }
        addrs.sun_family = AF_UNIX;
        strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
 
        if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
                perror("\nClient connection error\n");
+               close(fd);
                return -1;
        }
        pthread_create(&th, NULL, &legacy_client_handler,
                        (void *)(uintptr_t)fd);
+#endif /* !RTE_EXEC_ENV_WINDOWS */
        return 0;
 }
 
+#ifndef RTE_EXEC_ENV_WINDOWS
+
 static int
 send_error_response(int s, int err)
 {
@@ -178,6 +199,8 @@ parse_client_request(char *buffer, int buf_len, int s)
                if (!strchr(data_ptr, '{'))
                        data_sep = data_ptr[strlen(callbacks[i].data)];
                else {
+                       if (!strchr(data_ptr, '}'))
+                               return -EINVAL;
                        char *data_end = strchr(data_ptr, '}');
                        data = data_ptr + strlen(DATA_REQ_LABEL);
                        data_sep = data_end[1];
@@ -202,7 +225,7 @@ legacy_client_handler(void *sock_id)
        int ret;
        char buffer_recv[BUF_SIZE];
        /* receive data is not null terminated */
-       int bytes = read(s, buffer_recv, sizeof(buffer_recv));
+       int bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
 
        while (bytes > 0) {
                buffer_recv[bytes] = 0;
@@ -219,8 +242,10 @@ legacy_client_handler(void *sock_id)
                        if (ret < 0)
                                printf("\nCould not send error response\n");
                }
-               bytes = read(s, buffer_recv, sizeof(buffer_recv));
+               bytes = read(s, buffer_recv, sizeof(buffer_recv) - 1);
        }
        close(s);
        return NULL;
 }
+
+#endif /* !RTE_EXEC_ENV_WINDOWS */