diff -r 98034084033f -r e5fbb8f9edbe src/network.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/network.c Wed Feb 05 14:07:43 2014 +0100 @@ -0,0 +1,115 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Mike Becker. 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. + * + */ + +#include +#include +#include "network.h" + +int server_bind(Server *server) { + server->fd = socket(server->info->ai_family, + server->info->ai_socktype, server->info->ai_protocol); + if (server->fd > -1) { + int true = 1; + setsockopt(server->fd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)); + if (bind(server->fd, + server->info->ai_addr, server->info->ai_addrlen)) { + server->fd = -1; + return 1; + } else { + return 0; + } + } else { + return 1; + } +} + +int net_create(Server *server, char* port) { + return net_find(server, "localhost", port) || server_bind(server); +} + +int getaddrinfo_intrnl(char *host, char *port, struct addrinfo **info) { + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + return getaddrinfo(host, port, &hints, info); +} + +int net_find(Server *server, char *host, char* port) { + memset(server, 0, sizeof(Server)); + server->fd = -1; + + return getaddrinfo_intrnl(host, port, &(server->info)); +} + +int net_listen(Server *server) { + listen(server->fd, 1); + Client* client = calloc(1, sizeof(Client)); + client->fd = -1; + server->client = client; + + client->fd = accept(server->fd, + &(client->address), &(client->address_len)); + + return client->fd == -1; +} + +int net_connect(Server *server) { + struct addrinfo *info; + if (getaddrinfo_intrnl("localhost", NULL, &info)) { + return 1; + } + + Client* client = calloc(1, sizeof(Client)); + client->fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol); + server->client = client; + + freeaddrinfo(info); + if (client->fd == -1) { + return 1; + } + + return connect(client->fd, server->info->ai_addr, server->info->ai_addrlen); +} + +int net_destroy(Server *server) { + if (server->info) { + freeaddrinfo(server->info); + } + if (server->client) { + shutdown(server->client->fd, SHUT_RDWR); + free(server->client); + } + if (server->fd > -1) { + return shutdown(server->fd, SHUT_RDWR); + } + + return EXIT_SUCCESS; +}