1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/network.c Wed Feb 05 14:07:43 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2014 Mike Becker. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + * 1.31 + */ 1.32 + 1.33 +#include <stdlib.h> 1.34 +#include <string.h> 1.35 +#include "network.h" 1.36 + 1.37 +int server_bind(Server *server) { 1.38 + server->fd = socket(server->info->ai_family, 1.39 + server->info->ai_socktype, server->info->ai_protocol); 1.40 + if (server->fd > -1) { 1.41 + int true = 1; 1.42 + setsockopt(server->fd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)); 1.43 + if (bind(server->fd, 1.44 + server->info->ai_addr, server->info->ai_addrlen)) { 1.45 + server->fd = -1; 1.46 + return 1; 1.47 + } else { 1.48 + return 0; 1.49 + } 1.50 + } else { 1.51 + return 1; 1.52 + } 1.53 +} 1.54 + 1.55 +int net_create(Server *server, char* port) { 1.56 + return net_find(server, "localhost", port) || server_bind(server); 1.57 +} 1.58 + 1.59 +int getaddrinfo_intrnl(char *host, char *port, struct addrinfo **info) { 1.60 + struct addrinfo hints; 1.61 + memset(&hints, 0, sizeof(hints)); 1.62 + hints.ai_socktype = SOCK_STREAM; 1.63 + hints.ai_protocol = IPPROTO_TCP; 1.64 + 1.65 + return getaddrinfo(host, port, &hints, info); 1.66 +} 1.67 + 1.68 +int net_find(Server *server, char *host, char* port) { 1.69 + memset(server, 0, sizeof(Server)); 1.70 + server->fd = -1; 1.71 + 1.72 + return getaddrinfo_intrnl(host, port, &(server->info)); 1.73 +} 1.74 + 1.75 +int net_listen(Server *server) { 1.76 + listen(server->fd, 1); 1.77 + Client* client = calloc(1, sizeof(Client)); 1.78 + client->fd = -1; 1.79 + server->client = client; 1.80 + 1.81 + client->fd = accept(server->fd, 1.82 + &(client->address), &(client->address_len)); 1.83 + 1.84 + return client->fd == -1; 1.85 +} 1.86 + 1.87 +int net_connect(Server *server) { 1.88 + struct addrinfo *info; 1.89 + if (getaddrinfo_intrnl("localhost", NULL, &info)) { 1.90 + return 1; 1.91 + } 1.92 + 1.93 + Client* client = calloc(1, sizeof(Client)); 1.94 + client->fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol); 1.95 + server->client = client; 1.96 + 1.97 + freeaddrinfo(info); 1.98 + if (client->fd == -1) { 1.99 + return 1; 1.100 + } 1.101 + 1.102 + return connect(client->fd, server->info->ai_addr, server->info->ai_addrlen); 1.103 +} 1.104 + 1.105 +int net_destroy(Server *server) { 1.106 + if (server->info) { 1.107 + freeaddrinfo(server->info); 1.108 + } 1.109 + if (server->client) { 1.110 + shutdown(server->client->fd, SHUT_RDWR); 1.111 + free(server->client); 1.112 + } 1.113 + if (server->fd > -1) { 1.114 + return shutdown(server->fd, SHUT_RDWR); 1.115 + } 1.116 + 1.117 + return EXIT_SUCCESS; 1.118 +}