summaryrefslogtreecommitdiffhomepage
path: root/src/socket/ntapi_sc_getpeername_v1.c
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-03-22 15:43:24 -0400
committermidipix <writeonce@midipix.org>2016-03-22 15:43:24 -0400
commit5d86252d01b43b40c9c34d5c12b731e85a8ffc1c (patch)
treeba7653f5cfd6ca2c6af23277951b447488de589c /src/socket/ntapi_sc_getpeername_v1.c
parentf5a78b094189d5e1e79ef7800f9e37a9c1540329 (diff)
downloadntapi-5d86252d01b43b40c9c34d5c12b731e85a8ffc1c.tar.bz2
ntapi-5d86252d01b43b40c9c34d5c12b731e85a8ffc1c.tar.xz
socket interfaces: sc_getpeername: initial integration.
Integration of this function into the library has been delayed since the AFD ioctl operation, while succeeding, seems to only memset the caller's address buffer, and accordingly to never copy the remote socket address to it. Callers of sc_getpeername() should therefore first check the return value for success -- which may be used as indication that the socket is connected -- and then test the returned address buffer for validity.
Diffstat (limited to 'src/socket/ntapi_sc_getpeername_v1.c')
-rw-r--r--src/socket/ntapi_sc_getpeername_v1.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/socket/ntapi_sc_getpeername_v1.c b/src/socket/ntapi_sc_getpeername_v1.c
new file mode 100644
index 0000000..f38e11a
--- /dev/null
+++ b/src/socket/ntapi_sc_getpeername_v1.c
@@ -0,0 +1,80 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013--2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_server_socket_name_info {
+ uint32_t unknown;
+ uint32_t type;
+ uint32_t service_flags;
+ char sa_data[14];
+} nt_afd_server_socket_name_info;
+
+
+struct __addr_memcpy {
+ uint16_t d0;
+ uint16_t d1;
+ uint16_t d2;
+ uint16_t d3;
+ uint16_t d4;
+ uint16_t d5;
+ uint16_t d6;
+ uint16_t d7;
+};
+
+
+int32_t __cdecl __ntapi_sc_getpeername_v1(
+ __in nt_socket * hssocket,
+ __in nt_sockaddr * addr,
+ __in uint16_t * addrlen,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_server_socket_name_info sock_name_info;
+
+ struct __addr_memcpy * asrc;
+ struct __addr_memcpy * adst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_GET_PEER_NAME,
+ 0,
+ 0,
+ &sock_name_info,
+ sizeof(sock_name_info));
+
+ __ntapi->sc_wait(hssocket,iosb,0);
+
+ if (!hssocket->iostatus) {
+ addr->sa_addr_in4.sa_family = hssocket->domain;
+
+ asrc = (struct __addr_memcpy *)&(sock_name_info.sa_data);
+ adst = (struct __addr_memcpy *)addr;
+
+ adst->d1 = asrc->d0;
+ adst->d2 = asrc->d1;
+ adst->d3 = asrc->d2;
+ adst->d4 = asrc->d3;
+ adst->d5 = asrc->d4;
+ adst->d6 = asrc->d5;
+ adst->d7 = asrc->d6;
+
+ *addrlen = (uint16_t)iosb->info;
+ };
+
+ return hssocket->iostatus;
+}