[Util] parse port number
authorJaeyun <jy1210.jung@samsung.com>
Thu, 11 Aug 2022 07:13:48 +0000 (16:13 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Fri, 12 Aug 2022 01:31:10 +0000 (10:31 +0900)
Add util function to parse string and get port number.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
src/libnnstreamer-edge/nnstreamer-edge-common.c
src/libnnstreamer-edge/nnstreamer-edge-common.h
src/libnnstreamer-edge/nnstreamer-edge-internal.c

index 9880f2976d286792655727cb028234f4784eecec..394640203e2d148c729c0a8fc06136f733f396cf 100644 (file)
@@ -71,6 +71,27 @@ nns_edge_parse_host_string (const char *host_str, char **host, int *port)
   }
 }
 
+/**
+ * @brief Parse string and get port number. Return negative value when failed to get port number.
+ */
+int
+nns_edge_parse_port_number (const char *port_str)
+{
+  int port;
+
+  if (!port_str)
+    return -1;
+
+  port = (int) strtoll (port_str, NULL, 10);
+
+  if (port <= 0 || port > 65535) {
+    nns_edge_loge ("Invalid port number %d.", port);
+    port = -1;
+  }
+
+  return port;
+}
+
 /**
  * @brief Free allocated memory.
  */
index 4d9aa3e26a7382a88c191a670560eb967256a139..32c1c70d4afea727195d40a2e0de9c7cc5692b1f 100644 (file)
@@ -178,6 +178,11 @@ char *nns_edge_get_host_string (const char *host, const int port);
  */
 void nns_edge_parse_host_string (const char *host_str, char **host, int *port);
 
+/**
+ * @brief Parse string and get port number. Return negative value when failed to get port number.
+ */
+int nns_edge_parse_port_number (const char *port_str);
+
 /**
  * @brief Free allocated memory.
  */
index 628233f66fbfb5ee43c90c8afc19d21f4041e8fe..08322b2fc791f48c24bd59c8f749ee7d7c785b0c 100644 (file)
@@ -1520,10 +1520,9 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value)
     SAFE_FREE (eh->host);
     eh->host = nns_edge_strdup (value);
   } else if (0 == strcasecmp (key, "PORT")) {
-    int port = (int) strtoll (value, NULL, 10);
+    int port = nns_edge_parse_port_number (value);
 
-    if (port <= 0 || port > 65535) {
-      nns_edge_loge ("Invalid port number %d.", port);
+    if (port < 0) {
       ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
     } else {
       eh->port = port;
@@ -1533,10 +1532,9 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value)
     SAFE_FREE (eh->dest_host);
     eh->dest_host = nns_edge_strdup (value);
   } else if (0 == strcasecmp (key, "DEST_PORT")) {
-    int port = (int) strtoll (value, NULL, 10);
+    int port = nns_edge_parse_port_number (value);
 
-    if (port <= 0 || port > 65535) {
-      nns_edge_loge ("Invalid port number %d.", port);
+    if (port < 0) {
       ret = NNS_EDGE_ERROR_INVALID_PARAMETER;
     } else {
       eh->dest_port = port;