Add util function to get available port.
TODO: update testcase (data transfer with fixed port) later.
Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
#include "nnstreamer-edge-common.h"
+/**
+ * @brief Internal util function to get available port number.
+ */
+int
+nns_edge_get_available_port (void)
+{
+ struct sockaddr_in sin;
+ int port = 0, sock;
+ socklen_t len = sizeof (struct sockaddr);
+
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = 0;
+
+ sock = socket (AF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ nns_edge_loge ("Failed to get available port, socket creation failure.");
+ return 0;
+ }
+
+ if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
+ if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
+ port = ntohs (sin.sin_port);
+ nns_edge_logi ("Available port number: %d", port);
+ } else {
+ nns_edge_logw ("Failed to read local socket info.");
+ }
+ }
+ close (sock);
+
+ return port;
+}
+
/**
* @brief Allocate new memory and copy bytes.
* @note Caller should release newly allocated memory using free().
#define __NNSTREAMER_EDGE_COMMON_H__
#include <glib.h> /** @todo remove glib */
+#include <fcntl.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
#include <pthread.h>
+#include <unistd.h>
#include "nnstreamer-edge.h"
#ifdef __cplusplus
#define nns_edge_logd g_debug
#define nns_edge_logf g_error
+/**
+ * @brief Internal util function to get available port number.
+ */
+int nns_edge_get_available_port (void);
+
/**
* @brief Allocate new memory and copy bytes.
* @note Caller should release newly allocated memory using free().
*host = nns_edge_strdup_printf ("%s:%d", ip, port);
}
-/**
- * @brief Get available port number.
- */
-static int
-_get_available_port (void)
-{
- struct sockaddr_in sin;
- int port = 0, sock;
- socklen_t len = sizeof (struct sockaddr);
-
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
- sock = socket (AF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- nns_edge_loge ("Failed to get available port. Socket creation failure.");
- return 0;
- }
- sin.sin_port = port;
- if (bind (sock, (struct sockaddr *) &sin, sizeof (struct sockaddr)) == 0) {
- if (getsockname (sock, (struct sockaddr *) &sin, &len) == 0) {
- port = ntohs (sin.sin_port);
- nns_edge_logi ("Available port number: %d", port);
- } else {
- nns_edge_logw ("Failed to read local socket info.");
- }
- }
- close (sock);
-
- return port;
-}
-
/**
* @brief Internal function to check connection.
*/
eh->is_server = is_server;
if (!is_server && 0 == eh->recv_port) {
- eh->recv_port = _get_available_port ();
+ eh->recv_port = nns_edge_get_available_port ();
if (eh->recv_port <= 0) {
nns_edge_loge ("Failed to start edge. Cannot get available port.");
nns_edge_unlock (eh);
#include "nnstreamer-edge.h"
#include <gio/gio.h>
-#include <netinet/tcp.h>
-#include <netinet/in.h>
/**
* @brief Data structure for edge handle.