Skip to content

Commit

Permalink
Change some socket funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSant0s committed Feb 5, 2024
1 parent 2dd3cff commit 2fae0a6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/ath_env.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef ATH_ENV_H
#define ATH_ENV_H

#include <kernel.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -95,3 +98,6 @@ JSModuleDef *athena_sound_init(JSContext* ctx);
JSModuleDef *athena_camera_init(JSContext* ctx);
#endif



#endif
12 changes: 6 additions & 6 deletions src/ath_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ static JSValue athena_nw_get_config(JSContext *ctx, JSValue this_val, int argc,
if (ps2ip_getconfig("sm0", &ip_info) >= 0)
{
obj = JS_NewObject(ctx);
//JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, inet_ntoa(ip_info.ipaddr)), JS_PROP_C_W_E);
//JS_DefinePropertyValueStr(ctx, obj, "netmask", JS_NewString(ctx, inet_ntoa(ip_info.netmask)), JS_PROP_C_W_E);
//JS_DefinePropertyValueStr(ctx, obj, "gateway", JS_NewString(ctx, inet_ntoa(ip_info.gw)), JS_PROP_C_W_E);
//JS_DefinePropertyValueStr(ctx, obj, "dns", JS_NewString(ctx, inet_ntoa(*dns_getserver(0))), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, ip4addr_ntoa(ip_info.ipaddr)), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "netmask", JS_NewString(ctx, ip4addr_ntoa(ip_info.netmask)), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "gateway", JS_NewString(ctx, ip4addr_ntoa(ip_info.gw)), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "dns", JS_NewString(ctx, ip4addr_ntoa(*libcglue_dns_getserver(0))), JS_PROP_C_W_E);
} else {
obj = JS_ThrowInternalError(ctx, "Unable to read network info.\n");
}
Expand All @@ -86,12 +86,12 @@ static JSValue athena_nw_deinit(JSContext *ctx, JSValue this_val, int argc, JSVa
static JSValue athena_nw_gethostbyname(JSContext *ctx, JSValue this_val, int argc, JSValueConst *argv)
{
const char* host = JS_ToCString(ctx, argv[0]);
struct hostent *host_address = lwip_gethostbyname(host);
struct hostent *host_address = gethostbyname(host);

if (host_address == NULL)
return JS_ThrowInternalError(ctx, "Unable to resolve address.\n");

//return JS_NewString(ctx, inet_ntoa(*(struct in_addr*)host_address->h_addr));
return JS_NewString(ctx, ip4addr_ntoa(*(struct in_addr*)host_address->h_addr));
}

static void athena_nw_dtor(JSRuntime *rt, JSValue val)
Expand Down
19 changes: 9 additions & 10 deletions src/ath_socket.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@


#include "ath_env.h"
#include <netman.h>
#include <ps2ip.h>
#include "include/network.h"
#include <malloc.h>

typedef struct {
Expand Down Expand Up @@ -32,7 +31,7 @@ static JSValue athena_socket_ctor(JSContext *ctx, JSValueConst new_target, int a
JS_ToInt32(ctx, &s->sin_family, argv[0]);
JS_ToInt32(ctx, &protocol, argv[1]);

s->id = lwip_socket(s->sin_family, protocol, 0);
s->id = socket(s->sin_family, protocol, 0);

proto = JS_GetPropertyStr(ctx, new_target, "prototype");
obj = JS_NewObjectProtoClass(ctx, proto, js_socket_class_id);
Expand All @@ -53,11 +52,11 @@ static JSValue athena_socket_connect(JSContext *ctx, JSValue this_val, int argc,
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = s->sin_family;
//addr.sin_addr.s_addr = inet_addr(JS_ToCString(ctx, argv[0]));
addr.sin_addr.s_addr = inet_addr(JS_ToCString(ctx, argv[0]));
JS_ToInt32(ctx, &sin_port, argv[1]);
addr.sin_port = PP_HTONS(sin_port);

int ret = lwip_connect(s->id, (struct sockaddr*)&addr, sizeof(addr));
int ret = connect(s->id, (struct sockaddr*)&addr, sizeof(addr));

return JS_NewInt32(ctx, ret);
}
Expand All @@ -73,11 +72,11 @@ static JSValue athena_socket_bind(JSContext *ctx, JSValue this_val, int argc, JS
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = s->sin_family;
//addr.sin_addr.s_addr = inet_addr(JS_ToCString(ctx, argv[0]));
addr.sin_addr.s_addr = inet_addr(JS_ToCString(ctx, argv[0]));
JS_ToInt32(ctx, &sin_port, argv[1]);
addr.sin_port = PP_HTONS(sin_port);

int ret = lwip_bind(s->id, (struct sockaddr*)&addr, sizeof(addr));
int ret = bind(s->id, (struct sockaddr*)&addr, sizeof(addr));

return JS_NewInt32(ctx, ret);
}
Expand All @@ -90,7 +89,7 @@ static JSValue athena_socket_send(JSContext *ctx, JSValue this_val, int argc, JS
size_t len = 0;
const char* buf = JS_ToCStringLen(ctx, &len, argv[0]);

int ret = lwip_send(s->id, buf, len, MSG_DONTWAIT);
int ret = send(s->id, buf, len, MSG_DONTWAIT);

return JS_NewInt32(ctx, ret);
}
Expand All @@ -100,7 +99,7 @@ static JSValue athena_socket_listen(JSContext *ctx, JSValue this_val, int argc,

JSSocketData* s = JS_GetOpaque2(ctx, this_val, js_socket_class_id);

int ret = lwip_listen(s->id, 0);
int ret = listen(s->id, 0);

return JS_NewInt32(ctx, ret);
}
Expand All @@ -115,7 +114,7 @@ static JSValue athena_socket_recv(JSContext *ctx, JSValue this_val, int argc, JS

void* buf = js_mallocz(ctx, len);

lwip_recv(s->id, buf, len, MSG_PEEK);
recv(s->id, buf, len, MSG_PEEK);
return JS_NewStringLen(ctx, buf, len);
}

Expand Down
7 changes: 5 additions & 2 deletions src/include/network.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "../ath_env.h"
#include <netman.h>
#include <ps2ip.h>
#include <sys/socket.h> /* socket, connect */
#include <arpa/inet.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <loadfile.h>
#include <curl/curl.h>
#include <loadfile.h>
#include <pthread.h>
Expand Down Expand Up @@ -46,4 +49,4 @@ char* jsonToUrl(char *json);
int ethApplyNetIFConfig(int mode);
int ethWaitValidNetIFLinkState(void);
int ethWaitValidDHCPState(void);
int ethApplyIPConfig(int use_dhcp, const struct ip4_addr *ip, const struct ip4_addr *netmask, const struct ip4_addr *gateway, const struct ip4_addr *dns);
int ethApplyIPConfig(int use_dhcp, const struct ip4_addr *ip, const struct ip4_addr *netmask, const struct ip4_addr *gateway, const struct ip4_addr *dns);
3 changes: 2 additions & 1 deletion src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,5 @@ int ethApplyIPConfig(int use_dhcp, const struct ip4_addr *ip, const struct ip4_a
}

return result;
}
}

0 comments on commit 2fae0a6

Please sign in to comment.