Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Oic - Allows more than one server's instace #1391

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/comms/sol-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ sol_coap_server_new_full(enum sol_socket_type type, uint16_t port)
struct sol_coap_server *server;
struct sol_socket *s;
uint16_t i;
int on = 1;

SOL_LOG_INTERNAL_INIT_ONCE;

Expand All @@ -1486,6 +1487,13 @@ sol_coap_server_new_full(enum sol_socket_type type, uint16_t port)
return NULL;
}

if (sol_socket_setsockopt(s, SOL_SOCKET_LEVEL_SOCKET,
SOL_SOCKET_OPTION_REUSEADDR, &on, sizeof(on)) < 0) {
SOL_WRN("Could not set socket's option: %s", sol_util_strerrora(errno));
sol_socket_del(s);
return NULL;
}

if (sol_socket_bind(s, &servaddr) < 0) {
SOL_WRN("Could not bind socket (%d): %s", errno, sol_util_strerrora(errno));
sol_socket_del(s);
Expand Down
69 changes: 68 additions & 1 deletion src/lib/comms/sol-socket-impl-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,71 @@ sol_socket_linux_bind(struct sol_socket *socket, const struct sol_network_link_a
return 0;
}

static int
sol_socket_option_to_linux(enum sol_socket_option option)
{
switch (option) {
case SOL_SOCKET_OPTION_REUSEADDR:
return SO_REUSEADDR;
case SOL_SOCKET_OPTION_REUSEPORT:
return SO_REUSEPORT;
default:
SOL_WRN("Invalid option %d", option);
break;
}

return -1;
}

static int
sol_socket_level_to_linux(enum sol_socket_level level)
{
switch (level) {
case SOL_SOCKET_LEVEL_SOCKET:
return SOL_SOCKET;
case SOL_SOCKET_LEVEL_IP:
return IPPROTO_IP;
case SOL_SOCKET_LEVEL_IPV6:
return IPPROTO_IPV6;
default:
SOL_WRN("Invalid level %d", level);
break;
}

return -1;
}

static int
sol_socket_linux_setsockopt(struct sol_socket *socket, enum sol_socket_level level,
enum sol_socket_option optname, const void *optval, size_t optlen)
{
int l, option;
struct sol_socket_linux *s = (struct sol_socket_linux *)socket;

l = sol_socket_level_to_linux(level);
option = sol_socket_option_to_linux(optname);

return setsockopt(s->fd, l, option, optval, optlen);
}

static int
sol_socket_linux_getsockopt(struct sol_socket *socket, enum sol_socket_level level,
enum sol_socket_option optname, void *optval, size_t *optlen)
{
int ret, l, option;
socklen_t len;
struct sol_socket_linux *s = (struct sol_socket_linux *)socket;

option = sol_socket_option_to_linux(optname);
l = sol_socket_level_to_linux(level);

ret = getsockopt(s->fd, l, option, optval, &len);
SOL_INT_CHECK(ret, < 0, ret);

*optlen = len;
return 0;
}

const struct sol_socket_impl *
sol_socket_linux_get_impl(void)
{
Expand All @@ -458,7 +523,9 @@ sol_socket_linux_get_impl(void)
.set_on_write = sol_socket_linux_set_on_write,
.set_on_read = sol_socket_linux_set_on_read,
.del = sol_socket_linux_del,
.new = sol_socket_linux_new
.new = sol_socket_linux_new,
.setsockopt = sol_socket_linux_setsockopt,
.getsockopt = sol_socket_linux_getsockopt
};

return &impl;
Expand Down
20 changes: 19 additions & 1 deletion src/lib/comms/sol-socket-impl-riot.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,22 @@ sol_socket_riot_bind(struct sol_socket *s, const struct sol_network_link_addr *a
return 0;
}

static int
sol_socket_riot_setsockopt(struct sol_socket *socket, enum sol_socket_level level,
enum sol_socket_option optname, const void *optval, size_t optlen)
{
SOL_WRN("Not implemented");
return 0;
}

static int
sol_socket_riot_getsockopt(struct sol_socket *socket, enum sol_socket_level level,
enum sol_socket_option optname, void *optval, size_t *optlen)
{
SOL_WRN("Not implemented");
return 0;
}

const struct sol_socket_impl *
sol_socket_riot_get_impl(void)
{
Expand All @@ -371,7 +387,9 @@ sol_socket_riot_get_impl(void)
.set_on_write = sol_socket_riot_set_on_write,
.set_on_read = sol_socket_riot_set_on_read,
.del = sol_socket_riot_del,
.new = sol_socket_riot_new
.new = sol_socket_riot_new,
.setsockopt = sol_socket_riot_setsockopt,
.getsockopt = sol_socket_riot_getsockopt
};

return &impl;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/comms/sol-socket-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ struct sol_socket_impl {
int (*join_group)(struct sol_socket *s, int ifindex, const struct sol_network_link_addr *group);

int (*bind)(struct sol_socket *s, const struct sol_network_link_addr *addr);

int (*setsockopt)(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname, const void *optval, size_t optlen);

int (*getsockopt)(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname, void *optval, size_t *optlen);
};

#ifdef SOL_PLATFORM_LINUX
Expand Down
18 changes: 18 additions & 0 deletions src/lib/comms/sol-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,21 @@ sol_socket_bind(struct sol_socket *s, const struct sol_network_link_addr *addr)

return s->impl->bind(s, addr);
}

SOL_API int
sol_socket_setsockopt(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname, const void *optval, size_t optlen)
{
SOL_NULL_CHECK(s, -EINVAL);
SOL_NULL_CHECK(s->impl->setsockopt, -ENOSYS);

return s->impl->setsockopt(s, level, optname, optval, optlen);
}

SOL_API int
sol_socket_getsockopt(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname, void *optval, size_t *optlen)
{
SOL_NULL_CHECK(s, -EINVAL);
SOL_NULL_CHECK(s->impl->getsockopt, -ENOSYS);

return s->impl->getsockopt(s, level, optname, optval, optlen);
}
17 changes: 17 additions & 0 deletions src/lib/comms/sol-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ enum sol_socket_type {
#endif
};

enum sol_socket_option {
SOL_SOCKET_OPTION_REUSEADDR,
SOL_SOCKET_OPTION_REUSEPORT
};

enum sol_socket_level {
SOL_SOCKET_LEVEL_SOCKET,
SOL_SOCKET_LEVEL_IP,
SOL_SOCKET_LEVEL_IPV6,
};

struct sol_socket *sol_socket_new(int domain, enum sol_socket_type type, int protocol);
void sol_socket_del(struct sol_socket *s);

Expand All @@ -61,3 +72,9 @@ int sol_socket_sendmsg(struct sol_socket *s, const void *buf, size_t len,
int sol_socket_join_group(struct sol_socket *s, int ifindex, const struct sol_network_link_addr *group);

int sol_socket_bind(struct sol_socket *s, const struct sol_network_link_addr *addr);

int sol_socket_setsockopt(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname,
const void *optval, size_t optlen);

int sol_socket_getsockopt(struct sol_socket *s, enum sol_socket_level level, enum sol_socket_option optname,
void *optval, size_t *optlen);