Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os_net fixes #200

Merged
merged 14 commits into from
May 2, 2014
8 changes: 4 additions & 4 deletions src/os_net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ NAME=os_net

include ../Config.Make

HEAD = os_net.h
SRCS = os_net.c
OBJS = os_net.o

net:
$(CC) $(CFLAGS) -c $(SRCS)
ar cru os_net.a ${OBJS}
ranlib os_net.a
os_net.a: ${HEAD} ${SRCS}
${CC} ${CFLAGS} -c ${SRCS}
${BUILDLIB} os_net.a ${OBJS}

clean:
${CLEAN}
80 changes: 61 additions & 19 deletions src/os_net/os_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int OS_Bindport(unsigned int _port, unsigned int _proto, char *_ip, int ipv6)
if(setsockopt(ossock, SOL_SOCKET, SO_REUSEADDR,
(char *)&flag, sizeof(flag)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}
Expand All @@ -99,6 +100,7 @@ int OS_Bindport(unsigned int _port, unsigned int _proto, char *_ip, int ipv6)

if(bind(ossock, (struct sockaddr *) &server6, sizeof(server6)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
#endif
Expand All @@ -118,6 +120,7 @@ int OS_Bindport(unsigned int _port, unsigned int _proto, char *_ip, int ipv6)

if(bind(ossock, (struct sockaddr *) &server, sizeof(server)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}
Expand All @@ -128,6 +131,7 @@ int OS_Bindport(unsigned int _port, unsigned int _proto, char *_ip, int ipv6)
{
if(listen(ossock, 32) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}
Expand Down Expand Up @@ -176,24 +180,35 @@ int OS_BindUnixDomain(char * path, int mode, int max_msg_size)

if(bind(ossock, (struct sockaddr *)&n_us, SUN_LEN(&n_us)) < 0)
{
close(ossock);
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}

/* Changing permissions */
chmod(path,mode);
if(chmod(path,mode) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}


/* Getting current maximum size */
if(getsockopt(ossock, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}


/* Setting socket opt */
if(len < max_msg_size)
{
len = max_msg_size;
setsockopt(ossock, SOL_SOCKET, SO_RCVBUF, &len, optlen);
if(setsockopt(ossock, SOL_SOCKET, SO_RCVBUF, &len, optlen) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}

return(ossock);
Expand All @@ -215,7 +230,7 @@ int OS_ConnectUnixDomain(char * path, int max_msg_size)
n_us.sun_family = AF_UNIX;

/* Setting up path */
strncpy(n_us.sun_path,path,sizeof(n_us.sun_path)-1);
strncpy(n_us.sun_path,path,sizeof(n_us.sun_path)-1);

if((ossock = socket(PF_UNIX, SOCK_DGRAM,0)) < 0)
return(OS_SOCKTERR);
Expand All @@ -225,23 +240,33 @@ int OS_ConnectUnixDomain(char * path, int max_msg_size)
* We can use "send" after that
*/
if(connect(ossock,(struct sockaddr *)&n_us,SUN_LEN(&n_us)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}


/* Getting current maximum size */
if(getsockopt(ossock, SOL_SOCKET, SO_SNDBUF, &len, &optlen) == -1)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}


/* Setting maximum message size */
if(len < max_msg_size)
{
len = max_msg_size;
setsockopt(ossock, SOL_SOCKET, SO_SNDBUF, &len, optlen);
if(setsockopt(ossock, SOL_SOCKET, SO_SNDBUF, &len, optlen) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}


/* Returning the socket */
/* Returning the socket */
return(ossock);
}

Expand Down Expand Up @@ -300,7 +325,10 @@ int OS_Connect(unsigned int _port, unsigned int protocol, char *_ip, int ipv6)


if((_ip == NULL)||(_ip[0] == '\0'))
{
OS_CloseSocket(ossock);
return(OS_INVALID);
}


if(ipv6 == 1)
Expand All @@ -312,7 +340,10 @@ int OS_Connect(unsigned int _port, unsigned int protocol, char *_ip, int ipv6)
inet_pton(AF_INET6, _ip, &server6.sin6_addr.s6_addr);

if(connect(ossock,(struct sockaddr *)&server6, sizeof(server6)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
#endif
}
else
Expand All @@ -324,7 +355,10 @@ int OS_Connect(unsigned int _port, unsigned int protocol, char *_ip, int ipv6)


if(connect(ossock,(struct sockaddr *)&server, sizeof(server)) < 0)
{
OS_CloseSocket(ossock);
return(OS_SOCKTERR);
}
}


Expand Down Expand Up @@ -411,7 +445,7 @@ int OS_AcceptTCP(int socket, char *srcip, int addrsize)

if((clientsocket = accept(socket, (struct sockaddr *) &_nc,
&_ncl)) < 0)
return(-1);
return(-1);

strncpy(srcip, inet_ntoa(_nc.sin_addr),addrsize -1);
srcip[addrsize -1]='\0';
Expand All @@ -427,14 +461,15 @@ char *OS_RecvTCP(int socket, int sizet)
{
char *ret;

int retsize=0;

ret = (char *) calloc((sizet), sizeof(char));
if(ret == NULL)
return(NULL);

if((retsize = recv(socket, ret, sizet-1,0)) <= 0)
if(recv(socket, ret, sizet-1,0) <= 0)
{
free(ret);
return(NULL);
}

return(ret);
}
Expand All @@ -445,17 +480,12 @@ char *OS_RecvTCP(int socket, int sizet)
*/
int OS_RecvTCPBuffer(int socket, char *buffer, int sizet)
{
int retsize = 0;
int retsize;

while(!retsize)
if((retsize = recv(socket, buffer, sizet -1, 0)) > 0)
{
retsize = recv(socket, buffer, sizet -1, 0);
if(retsize > 0)
{
buffer[retsize] = '\0';
return(0);
}
return(-1);
buffer[retsize] = '\0';
return(0);
}
return(-1);
}
Expand All @@ -475,7 +505,10 @@ char *OS_RecvUDP(int socket, int sizet)
return(NULL);

if((recv(socket,ret,sizet-1,0))<0)
{
free(ret);
return(NULL);
}

return(ret);
}
Expand Down Expand Up @@ -568,4 +601,13 @@ char *OS_GetHost(char *host, int attempts)
return(NULL);
}

int OS_CloseSocket(int socket)
{
#ifdef WIN32
return (closesocket(socket));
#else
return (close(socket));
#endif /* WIN32 */
}

/* EOF */
7 changes: 7 additions & 0 deletions src/os_net/os_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ int OS_SendUDPbySize(int socket, int size, char *msg);
*/
char *OS_GetHost(char *host, int attempts);

/**
* Close a network socket.
* @param socket the socket to close
* @return 0 on success, else -1 or SOCKET_ERROR
*/
int OS_CloseSocket(int socket);

#endif

/* EOF */