Skip to content

Commit

Permalink
fix handling of errno and timeouts with that
Browse files Browse the repository at this point in the history
  • Loading branch information
dimok789 committed Oct 25, 2016
1 parent f02ced3 commit 4f6a1a8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/dynamic_libs/socket_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ EXPORT_DECL(int, sendto, int s, const void *buffer, int size, int flags, const s
EXPORT_DECL(int, setsockopt, int s, int level, int optname, void *optval, int optlen);
EXPORT_DECL(char *, inet_ntoa, struct in_addr in);
EXPORT_DECL(int, inet_aton, const char *cp, struct in_addr *inp);
EXPORT_DECL(int*, get_h_errno, void);
EXPORT_DECL(const char*, gai_strerror, int);

void InitSocketFunctionPointers(void)
{
Expand Down Expand Up @@ -70,6 +72,8 @@ void InitSocketFunctionPointers(void)
OS_FIND_EXPORT(nsysnet_handle, setsockopt);
OS_FIND_EXPORT(nsysnet_handle, inet_ntoa);
OS_FIND_EXPORT(nsysnet_handle, inet_aton);
OS_FIND_EXPORT(nsysnet_handle, get_h_errno);
OS_FIND_EXPORT(nsysnet_handle, gai_strerror);

unsigned int nn_startupid;
ACInitialize();
Expand Down
14 changes: 14 additions & 0 deletions src/dynamic_libs/socket_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ extern "C" {
#define SO_NONBLOCK 0x1016
#define SO_MYADDR 0x1013

#define ENODATA 1
#define EISCONN 3
#define EWOULDBLOCK 6
#define EALREADY 10
#define EAGAIN EWOULDBLOCK
#define EINVAL 11
#define ENOMEM 18
#define EINPROGRESS 22

#define htonl(x) x
#define htons(x) x
#define ntohl(x) x
Expand Down Expand Up @@ -88,6 +97,11 @@ extern int (*setsockopt)(int s, int level, int optname, void *optval, int optlen
extern char * (*inet_ntoa)(struct in_addr in);
extern int (*inet_aton)(const char *cp, struct in_addr *inp);

extern int *(*get_h_errno)(void);
extern const char *(*gai_strerror)(int errno); // huh, gai o.O

#define geterrno() (*(get_h_errno()))

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 4 additions & 7 deletions src/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ misrepresented as being the original software.
3.This notice may not be removed or altered from any source distribution.
*/
#include <errno.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -32,11 +31,9 @@ misrepresented as being the original software.
#include "dynamic_libs/os_functions.h"
#include "dynamic_libs/socket_functions.h"

#undef EAGAIN
#define EAGAIN 1

//! TODO: fix those function
#define gettime() OSGetTick()
#define errno geterrno()

#include "ftp.h"
#include "virtualpath.h"
Expand Down Expand Up @@ -391,7 +388,7 @@ static s32 prepare_data_connection(client_t *client, void *callback, void *arg,
client->data_callback = callback;
client->data_connection_callback_arg = arg;
client->data_connection_cleanup = cleanup;
client->data_connection_timer = gettime() + SECS_TO_TICKS(30);
client->data_connection_timer = gettime() + SECS_TO_TICKS(10);
}
}
return result;
Expand Down Expand Up @@ -824,8 +821,8 @@ static void process_data_events(client_t *client) {
} else {
result = client->data_callback(client->data_socket, client->data_connection_callback_arg);
}
// stupid mess up in nintys code EAGAIN is sometimes -1 (recv, should be -11) and sometimes -11 (send)
if (result <= 0 && result != -EAGAIN && result != -11) {

if (result <= 0 && result != -EAGAIN) {
cleanup_data_resources(client);
if (result < 0) {
result = write_reply(client, 520, "Closing data connection, error occurred during transfer.");
Expand Down
46 changes: 37 additions & 9 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ misrepresented as being the original software.
3.This notice may not be removed or altered from any source distribution.
*/
#include <errno.h>
#include <gctypes.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -69,32 +68,62 @@ void initialise_network() {

s32 network_socket(u32 domain,u32 type,u32 protocol)
{
return socket(domain, type, protocol);
int sock = socket(domain, type, protocol);
if(sock < 0)
{
return -geterrno();
}
return sock;
}

s32 network_bind(s32 s,struct sockaddr *name,s32 namelen)
{
return bind(s, name, namelen);
int res = bind(s, name, namelen);
if(res < 0)
{
return -geterrno();
}
return res;
}

s32 network_listen(s32 s,u32 backlog)
{
return listen(s, backlog);
int res = listen(s, backlog);
if(res < 0)
{
return -geterrno();
}
return res;
}

s32 network_accept(s32 s,struct sockaddr *addr,s32 *addrlen)
{
return accept(s, addr, addrlen);
int res = accept(s, addr, addrlen);
if(res < 0)
{
return -geterrno();
}
return res;
}

s32 network_connect(s32 s,struct sockaddr *addr, s32 addrlen)
{
return connect(s, addr, addrlen);
int res = connect(s, addr, addrlen);
if(res < 0)
{
return -geterrno();
}
return res;
}

s32 network_read(s32 s,void *mem,s32 len)
{
return recv(s, mem, len, 0);
int res = recv(s, mem, len, 0);
if(res < 0)
{
return -geterrno();
}
return res;
}

u32 network_gethostip()
Expand All @@ -111,7 +140,7 @@ s32 network_write(s32 s, const void *mem,s32 len)
int ret = send(s, mem, len, 0);
if(ret < 0)
{
transfered = ret;
transfered = -geterrno();
break;
}

Expand All @@ -132,7 +161,6 @@ s32 network_close(s32 s)

s32 set_blocking(s32 s, bool blocking) {
s32 block = !blocking;

setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &block, sizeof(block));
return 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "common/common.h"
#include "dynamic_libs/os_functions.h"
#include "dynamic_libs/socket_functions.h"
Expand Down

0 comments on commit 4f6a1a8

Please sign in to comment.