[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 3/4] libc: POSIX: getsockname()



+ libc: POSIX: getsockname()

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/modules/include/sys/socket.h |    5 +++++
 src/modules/lib/posix/socket.c   |   29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/modules/include/sys/socket.h b/src/modules/include/sys/socket.h
index bc75e0b..bf5ca98 100644
--- a/src/modules/include/sys/socket.h
+++ b/src/modules/include/sys/socket.h
@@ -94,6 +94,11 @@ int socket(int domain, int type, int protocol);
 int connect(int socket, const struct sockaddr* address, socklen_t address_len);
 
 /**
+ * Gibt die lokale Adresse des Sockets zurueck
+ */
+int getsockname(int sock, struct sockaddr* address, socklen_t* address_len);
+
+/**
  * Liest eine Anzahl Bytes aus einem Socket
  */
 ssize_t recv(int socket, const void *buffer, size_t length, int flags);
diff --git a/src/modules/lib/posix/socket.c b/src/modules/lib/posix/socket.c
index fa505b0..2e7ab77 100644
--- a/src/modules/lib/posix/socket.c
+++ b/src/modules/lib/posix/socket.c
@@ -44,6 +44,7 @@ struct socket {
 
     sa_family_t                     sa_family;
     struct lostio_internal_file     conn;
+    size_t                          local_address_len;
     struct sockaddr*                local_address;
     FILE*                           listen_file;
 };
@@ -191,6 +192,7 @@ int bind(int sock, const struct sockaddr* address, socklen_t address_len)
 
     // FIXME Das wird beim Schliessen geleakt
     socket->local_address = malloc(address_len);
+    socket->local_address_len = address_len;
     memcpy(socket->local_address, address, address_len);
 
     return 0;
@@ -281,6 +283,33 @@ int accept(int sock, struct sockaddr* address, socklen_t* address_len)
 }
 
 /**
+ * Gibt die lokale Adresse des Sockets zurueck
+ */
+int getsockname(int sock, struct sockaddr* address, socklen_t* address_len)
+{
+    struct socket* socket = get_socket(sock);
+    size_t len;
+
+    if (socket == NULL) {
+        errno = EBADF;
+        return -1;
+    }
+
+    if (socket->local_address == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    len = *address_len < socket->local_address_len
+         ? *address_len : socket->local_address_len;
+    memcpy(address, socket->local_address, len);
+
+    *address_len = len;
+
+    return 0;
+}
+
+/**
  * Liest eine Anzahl Bytes aus einem Socket
  *
  * TODO Mit den Flags was sinnvolles machen
-- 
1.6.0.2