From b1004f5617d29fb45be3b562daf676719dafeae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Mon, 24 Jul 2017 16:25:15 -0500 Subject: [PATCH] Implemented error handling for `bind()' --- src/connection.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/connection.c b/src/connection.c index 247d428..7fafeb8 100644 --- a/src/connection.c +++ b/src/connection.c @@ -44,8 +44,8 @@ int NeoComm_bind(const unsigned short port) { else { char error_msg[128]; - snprintf(error_msg, 128, "Failed to initiate socket with errno `%d'", - errno); + snprintf(error_msg, 128, + "Failed to initiate socket with errno `%d'", errno); NeoComm_error(error_msg); } return 0; @@ -56,7 +56,27 @@ int NeoComm_bind(const unsigned short port) { serv_addr.sin_addr.s_addr = INADDR_ANY; if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) { - // TODO: Handle all these errors and shit, I'm too lazy right now + if(errno == EADDRINUSE) + NeoComm_error("Address in use"); + else if(errno == EADDRNOTAVAIL) + NeoComm_error("Address unavailable on system"); + else if(errno == EAFNOSUPPORT) + NeoComm_error("Unsupported address family for socket"); + else if(errno == EALREADY) + NeoComm_error("Assignment request already exists for socket"); + else if(errno == EINVAL) + NeoComm_error("Socket already bound"); + else if(errno == ENOBUFS) + NeoComm_error("Insufficient resources"); + else if(errno == EACCES) + NeoComm_error("Insufficient priviliges"); + else + { + char error_msg[128]; + snprintf(error_msg, 128, + "Failed to bind to socket with errno `%d'", errno); + NeoComm_error(error_msg); + } return 0; }