From c739c8ce49e9a285de6cf2c5df73c974dd8bab2e Mon Sep 17 00:00:00 2001 From: Christoph Lipka Date: Mon, 6 Jun 2016 11:57:52 +0900 Subject: dlt-client: Use correct port on connect Signed-off-by: Christoph Lipka --- src/gateway/dlt_gateway.c | 2 -- src/lib/dlt_client.c | 54 +++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/gateway/dlt_gateway.c b/src/gateway/dlt_gateway.c index c88d35e..edae0c7 100644 --- a/src/gateway/dlt_gateway.c +++ b/src/gateway/dlt_gateway.c @@ -427,8 +427,6 @@ int dlt_gateway_store_connection(DltGateway *gateway, /* setup DltClient Structure */ gateway->connections[i].client.servIP = strdup(gateway->connections[i].ip_address); - // TODO: modify DltClient structure to make port configurable - // g->connections[i].client.servPort = g->connections[i].port; if (ret != 0) { diff --git a/src/lib/dlt_client.c b/src/lib/dlt_client.c index 71a0069..f3fbbf0 100644 --- a/src/lib/dlt_client.c +++ b/src/lib/dlt_client.c @@ -92,6 +92,7 @@ #include /* for malloc(), free() */ #include /* for strlen(), memcmp(), memmove() */ #include +#include #include "dlt_types.h" #include "dlt_client.h" @@ -107,11 +108,9 @@ void dlt_client_register_message_callback(int (*registerd_callback) (DltMessage DltReturnValue dlt_client_init_port(DltClient *client, int port, int verbose) { - if (verbose) + if (verbose && port != DLT_DAEMON_TCP_PORT) { - snprintf(str, DLT_CLIENT_TEXTBUFSIZE, - "Init dlt client struct with port %d\n", port); - dlt_log(LOG_INFO, str); + dlt_vlog(LOG_INFO, "Init dlt client struct with port %d\n", port); } if (client == NULL) @@ -133,14 +132,37 @@ DltReturnValue dlt_client_init_port(DltClient *client, int port, int verbose) DltReturnValue dlt_client_init(DltClient *client, int verbose) { + char *env_daemon_port; + int tmp_port; + /* the port may be specified by an environment variable, defaults to DLT_DAEMON_TCP_PORT */ + unsigned short servPort = DLT_DAEMON_TCP_PORT; + + /* the port may be specified by an environment variable */ + env_daemon_port = getenv(DLT_CLIENT_ENV_DAEMON_TCP_PORT); + if (env_daemon_port != NULL) + { + tmp_port = atoi(env_daemon_port); + if ((tmp_port < IPPORT_RESERVED) || (tmp_port > USHRT_MAX)) + { + dlt_vlog(LOG_ERR, + "Specified port is out of possible range: %d.\n", + tmp_port); + return DLT_RETURN_ERROR; + } + else + { + servPort = tmp_port; + } + } + if (verbose) { - snprintf(str, DLT_CLIENT_TEXTBUFSIZE, - "Init dlt client struct with default port.\n"); - dlt_log(LOG_INFO, str); + dlt_vlog(LOG_INFO, + "Init dlt client struct with default port: %hu.\n", + servPort); } - return dlt_client_init_port(client, DLT_DAEMON_TCP_PORT, verbose); + return dlt_client_init_port(client, servPort, verbose); } DltReturnValue dlt_client_connect(DltClient *client, int verbose) @@ -149,9 +171,6 @@ DltReturnValue dlt_client_connect(DltClient *client, int verbose) struct addrinfo hints, *servinfo, *p; struct sockaddr_un addr; int rv; - char *env_daemon_port; - /* the port may be specified by an environment variable, defaults to DLT_DAEMON_TCP_PORT */ - unsigned short servPort = DLT_DAEMON_TCP_PORT; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; @@ -160,21 +179,10 @@ DltReturnValue dlt_client_connect(DltClient *client, int verbose) return DLT_RETURN_ERROR; } - /* the port may be specified by an environment variable */ - env_daemon_port = getenv(DLT_CLIENT_ENV_DAEMON_TCP_PORT); - if (env_daemon_port != NULL) - { - servPort = atoi(env_daemon_port); - } - if (servPort == 0) - { - servPort = DLT_DAEMON_TCP_PORT; - } - switch (client->mode) { case DLT_CLIENT_MODE_TCP: - snprintf(portnumbuffer, 32, "%d", servPort); + snprintf(portnumbuffer, 32, "%d", client->port); if ((rv = getaddrinfo(client->servIP, portnumbuffer, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return DLT_RETURN_ERROR; -- cgit v1.2.1