diff options
author | Michael Scott <michael.scott@linaro.org> | 2017-01-23 13:42:06 -0800 |
---|---|---|
committer | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2017-02-03 15:59:15 +0200 |
commit | dbe46353689d3469dce0ebd60602233701639b33 (patch) | |
tree | 505a788debe0d57df27acd3592a84d382e77a1a5 /samples | |
parent | 01e3c3380f035abb3b98c1f34190aa07eb8ab3ca (diff) |
samples: net: irc_bot: add helper function in_addr_set()
The aim of this helper function is to remove duplicate code for setting
the parameters of in_addr structures.
Change-Id: Id882a5947c47a9b6f92924ce8fb04023540fbb8d
Signed-off-by: Michael Scott <michael.scott@linaro.org>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/net/irc_bot/src/irc-bot.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/samples/net/irc_bot/src/irc-bot.c b/samples/net/irc_bot/src/irc-bot.c index 7d01b1c5d..7392ebf94 100644 --- a/samples/net/irc_bot/src/irc-bot.c +++ b/samples/net/irc_bot/src/irc-bot.c @@ -266,6 +266,43 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf, } } +static int in_addr_set(sa_family_t family, + const char *ip_addr, + int port, + struct sockaddr *_sockaddr) +{ + int rc = 0; + + _sockaddr->family = family; + + if (ip_addr) { + if (family == AF_INET6) { + rc = net_addr_pton(family, + ip_addr, + &net_sin6(_sockaddr)->sin6_addr); + } else { + rc = net_addr_pton(family, + ip_addr, + &net_sin(_sockaddr)->sin_addr); + } + + if (rc < 0) { + NET_ERR("Invalid IP address: %s", ip_addr); + return -EINVAL; + } + } + + if (port >= 0) { + if (family == AF_INET6) { + net_sin6(_sockaddr)->sin6_port = htons(port); + } else { + net_sin(_sockaddr)->sin_port = htons(port); + } + } + + return rc; +} + static void on_context_connect(struct net_context *ctx, void *data) { |