2017-07-23 23:19:20 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net>
|
|
|
|
* Author: Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "neocomm/connection.h"
|
|
|
|
|
|
|
|
#include "internal_error.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int NeoComm_bind(const unsigned short port) {
|
|
|
|
int sockfd;//, newsockfd;
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(sockfd < 0)
|
|
|
|
{
|
2017-07-24 16:14:47 -05:00
|
|
|
if(errno == EACCES)
|
2017-07-23 23:19:20 -05:00
|
|
|
NeoComm_error("Insufficient privileges");
|
2017-07-24 16:14:47 -05:00
|
|
|
else if(errno == ENOBUFS)
|
2017-07-23 23:19:20 -05:00
|
|
|
NeoComm_error("Insufficient resources available");
|
2017-07-24 16:14:47 -05:00
|
|
|
else if(errno == ENOMEM)
|
2017-07-23 23:19:20 -05:00
|
|
|
NeoComm_error("Insufficient memory to open socket");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char error_msg[128];
|
2017-07-24 16:25:15 -05:00
|
|
|
snprintf(error_msg, 128,
|
|
|
|
"Failed to initiate socket with errno `%d'", errno);
|
2017-07-23 23:19:20 -05:00
|
|
|
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)
|
|
|
|
{
|
2017-07-24 16:25:15 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-07-23 23:19:20 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(sockfd, 5);
|
|
|
|
return 1;
|
|
|
|
}
|