Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for getsockname(2)

Support getsockname(2) for netlink. When a netlink socket is opened,
it uses getsockname to find it's address.

The inet module has sockname/1 which would be the usual way of get the
address. In this case, the inet module rejects the netlink socket.
  • Loading branch information...
commit 561469d061b64f30b6b2e96e968216d30f8007a0 1 parent 9b0336d
@msantos authored
Showing with 37 additions and 0 deletions.
  1. +33 −0 c_src/procket.c
  2. +4 −0 src/procket.erl
View
33 c_src/procket.c
@@ -538,6 +538,38 @@ nif_setsockopt(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
+// int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
+ static ERL_NIF_TERM
+nif_getsockname(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ int s = -1;
+ ErlNifBinary addr = {0};
+ socklen_t addrlen = 0;
+
+
+ if (!enif_get_int(env, argv[0], &s))
+ return enif_make_badarg(env);
+
+ if (!enif_inspect_binary(env, argv[1], &addr))
+ return enif_make_badarg(env);
+
+ /* Make the binary mutable */
+ if (!enif_realloc_binary(&addr, addr.size))
+ return error_tuple(env, ENOMEM);
+
+ addrlen = addr.size;
+
+ if (getsockname(s, (struct sockaddr *)addr.data, (socklen_t *)&addrlen) < 0)
+ return error_tuple(env, errno);
+
+ PROCKET_REALLOC(addr, addrlen);
+
+ return enif_make_tuple2(env,
+ atom_ok,
+ enif_make_binary(env, &addr));
+}
+
+
/* Allocate structures for ioctl
*
* Some ioctl request structures have a field pointing
@@ -714,6 +746,7 @@ static ErlNifFunc nif_funcs[] = {
{"bind", 2, nif_bind},
{"connect", 2, nif_connect},
{"listen", 2, nif_listen},
+ {"getsockname", 2, nif_getsockname},
{"ioctl", 3, nif_ioctl},
{"socket_nif", 3, nif_socket},
{"recvfrom", 4, nif_recvfrom},
View
4 src/procket.erl
@@ -46,6 +46,7 @@
bind/2,
ioctl/3,
setsockopt/4,
+ getsockname/2,
alloc/1,
buf/1,
@@ -142,6 +143,9 @@ writev(_,_) ->
setsockopt(_,_,_,_) ->
erlang:error(not_implemented).
+getsockname(_,_) ->
+ erlang:error(not_implemented).
+
errno_id(_) ->
erlang:error(not_implemented).
Please sign in to comment.
Something went wrong with that request. Please try again.