From 33de0a6e1a6902205598c5aeb0264f2b68369dd3 Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Fri, 7 Nov 2014 10:15:59 +0100 Subject: [PATCH] mtapi_network_c: added socket wrapper, windows implementation --- mtapi_network_c/src/embb_mtapi_network.c | 22 ++++++++++++++++++++++ mtapi_network_c/src/embb_mtapi_network.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ mtapi_network_c/src/embb_mtapi_network_socket.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_network_c/src/embb_mtapi_network_socket.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 mtapi_network_c/src/embb_mtapi_network.c create mode 100644 mtapi_network_c/src/embb_mtapi_network.h create mode 100644 mtapi_network_c/src/embb_mtapi_network_socket.c create mode 100644 mtapi_network_c/src/embb_mtapi_network_socket.h diff --git a/mtapi_network_c/src/embb_mtapi_network.c b/mtapi_network_c/src/embb_mtapi_network.c new file mode 100644 index 0000000..be6547b --- /dev/null +++ b/mtapi_network_c/src/embb_mtapi_network.c @@ -0,0 +1,22 @@ +#include + +#include + +int embb_mtapi_network_initialize() { + WORD ver_request; + WSADATA wsa_data; + int err; + + ver_request = MAKEWORD(2, 2); + + err = WSAStartup(ver_request, &wsa_data); + if (err != 0) { + return 0; + } else { + return 1; + } +} + +void embb_mtapi_network_finalize() { + WSACleanup(); +} diff --git a/mtapi_network_c/src/embb_mtapi_network.h b/mtapi_network_c/src/embb_mtapi_network.h new file mode 100644 index 0000000..43b0920 --- /dev/null +++ b/mtapi_network_c/src/embb_mtapi_network.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_C_SRC_EMBB_MTAPI_NETWORK_H_ +#define MTAPI_C_SRC_EMBB_MTAPI_NETWORK_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +int embb_mtapi_network_initialize(); + +void embb_mtapi_network_finalize(); + + +#ifdef __cplusplus +} +#endif + +#endif // MTAPI_C_SRC_EMBB_MTAPI_NETWORK_H_ diff --git a/mtapi_network_c/src/embb_mtapi_network_socket.c b/mtapi_network_c/src/embb_mtapi_network_socket.c new file mode 100644 index 0000000..8bef1a9 --- /dev/null +++ b/mtapi_network_c/src/embb_mtapi_network_socket.c @@ -0,0 +1,123 @@ +#define _WINSOCK_DEPRECATED_NO_WARNINGS + +#include +#include +#include + +int embb_mtapi_network_socket_initialize( + embb_mtapi_network_socket_t * that) { + that->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (INVALID_SOCKET == that->handle) { + return 0; + } else { + return 1; + } +} + +void embb_mtapi_network_socket_finalize( + embb_mtapi_network_socket_t * that) { + if (INVALID_SOCKET != that->handle) { + closesocket(that->handle); + that->handle = INVALID_SOCKET; + } +} + +int embb_mtapi_network_socket_setnonblock( + embb_mtapi_network_socket_t * that) { + u_long mode = 1; + if (SOCKET_ERROR == ioctlsocket(that->handle, FIONBIO, &mode)) { + return 0; + } else { + return 1; + } +} + +int embb_mtapi_network_socket_bind_and_listen( + embb_mtapi_network_socket_t * that, + char const * host, + uint16_t port) { + struct sockaddr_in in_addr; + int reuseaddr_on = 1; + + // addr reuse + if (SOCKET_ERROR == setsockopt(that->handle, SOL_SOCKET, SO_REUSEADDR, + (const char *)&reuseaddr_on, sizeof(reuseaddr_on))) { + return 0; + } + + // bind & listen + memset(&in_addr, 0, sizeof(in_addr)); + in_addr.sin_family = AF_INET; + in_addr.sin_addr.s_addr = (NULL == host) ? + htonl(INADDR_ANY) : inet_addr(host); + in_addr.sin_port = htons(port); + + if (SOCKET_ERROR == bind(that->handle, (struct sockaddr *) &in_addr, + sizeof(in_addr))) { + return 0; + } + + if (SOCKET_ERROR == listen(that->handle, 5)) { + return 0; + } + + // set non-block + return embb_mtapi_network_socket_setnonblock(that); +} + +int embb_mtapi_network_socket_accept( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_socket_t * sock) { + sock->handle = accept(that->handle, NULL, NULL); + if (INVALID_SOCKET == sock->handle) { + return 0; + } else { + return 1; + } +} + +int embb_mtapi_network_socket_connect( + embb_mtapi_network_socket_t * that, + const char * host, + uint16_t port) { + struct sockaddr_in addr; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(host); + addr.sin_port = htons(port); + + if (SOCKET_ERROR == connect(that->handle, (struct sockaddr *)&addr, + sizeof(addr))) { + if (WSAEWOULDBLOCK != WSAGetLastError()) + return 0; + } + + return 1; +} + +int embb_mtapi_network_socket_sendbuffer( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_buffer_t * buffer) { + int result = send(that->handle, buffer->data, buffer->size, 0); + if (result == buffer->size) { + return buffer->size; + } else { + return 0; + } +} + +int embb_mtapi_network_socket_recvbuffer( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_buffer_t * buffer) { + u_long bytes_available = 0; + if (0 != ioctlsocket(that->handle, FIONREAD, &bytes_available)) + return 0; + if (buffer->capacity > (int)bytes_available) + return 0; + int result = recv(that->handle, buffer->data, buffer->capacity, 0); + if (result != buffer->capacity) + return 0; + buffer->size = buffer->capacity; + return buffer->size; +} diff --git a/mtapi_network_c/src/embb_mtapi_network_socket.h b/mtapi_network_c/src/embb_mtapi_network_socket.h new file mode 100644 index 0000000..7277d92 --- /dev/null +++ b/mtapi_network_c/src/embb_mtapi_network_socket.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2014, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_C_SRC_EMBB_MTAPI_NETWORK_SOCKET_H_ +#define MTAPI_C_SRC_EMBB_MTAPI_NETWORK_SOCKET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +struct embb_mtapi_network_socket_struct { + unsigned int handle; +}; + +typedef struct embb_mtapi_network_socket_struct embb_mtapi_network_socket_t; + +int embb_mtapi_network_socket_initialize( + embb_mtapi_network_socket_t * that +); + +void embb_mtapi_network_socket_finalize( + embb_mtapi_network_socket_t * that +); + +int embb_mtapi_network_socket_bind_and_listen( + embb_mtapi_network_socket_t * that, + char const * host, + uint16_t port +); + +int embb_mtapi_network_socket_accept( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_socket_t * sock +); + +int embb_mtapi_network_socket_connect( + embb_mtapi_network_socket_t * that, + const char * host, + uint16_t port +); + +int embb_mtapi_network_socket_sendbuffer( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_buffer_t * buffer +); + +int embb_mtapi_network_socket_recvbuffer( + embb_mtapi_network_socket_t * that, + embb_mtapi_network_buffer_t * buffer +); + +#ifdef __cplusplus +} +#endif + +#endif // MTAPI_C_SRC_EMBB_MTAPI_NETWORK_SOCKET_H_ -- libgit2 0.26.0