From 6cc7de63830f507509f30d6034020daf624c23b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Sun, 23 Jul 2017 23:19:20 -0500 Subject: [PATCH] Added connection class with basic binding There's no listening yet, and still a bunch of different errors to handle. --- CMakeLists.txt | 1 + include/neocomm/connection.h | 4 +++ src/connection.c | 67 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/connection.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 77694be..484609e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ include_directories( "include/") set(SRCS + src/connection.c src/error.c src/neocomm.c src/nodes.c) diff --git a/include/neocomm/connection.h b/include/neocomm/connection.h index 52d2811..b0ece99 100644 --- a/include/neocomm/connection.h +++ b/include/neocomm/connection.h @@ -18,3 +18,7 @@ */ #pragma once + +int NeoComm_bind(const unsigned short port); + +void NeoComm_close_connections(); diff --git a/src/connection.c b/src/connection.c new file mode 100644 index 0000000..b876d7e --- /dev/null +++ b/src/connection.c @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#include "neocomm/connection.h" + +#include "internal_error.h" + +#include +#include +#include +#include +#include +#include + +int NeoComm_bind(const unsigned short port) { + int sockfd;//, newsockfd; + struct sockaddr_in serv_addr; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + int errsv = errno; + if(sockfd < 0) + { + if(errsv == EACCES) + NeoComm_error("Insufficient privileges"); + else if(errsv == ENOBUFS) + NeoComm_error("Insufficient resources available"); + else if(errsv == ENOMEM) + NeoComm_error("Insufficient memory to open socket"); + else + { + char error_msg[128]; + snprintf(error_msg, 128, "Failed to initiate socket with errno `%d'", + errsv); + NeoComm_error(error_msg); + } + return 0; + } + bzero((char*) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + serv_addr.sin_addr.s_addr = INADDR_ANY; + if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) + { + /*int errsv = errno;*/ + // TODO: Handle all these errors and shit, I'm too lazy right now + return 0; + } + + listen(sockfd, 5); + return 1; +}