Skip to content

Commit

Permalink
working on, udp socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
objeck committed Dec 8, 2024
1 parent e9dc62d commit 21b62e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
22 changes: 15 additions & 7 deletions core/vm/arch/win32/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,22 @@ class IPSocket {
****************************/
class UDPSocket {
public:
static bool Bind(int port, struct sockaddr_in* serv_addr) {
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);
static struct sockaddr_in* Bind(int port) {
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock != INVALID_SOCKET) {
struct sockaddr_in* serv_addr = new struct sockaddr_in;
memset(serv_addr, 0, sizeof(struct sockaddr_in));

serv_addr->sin_family = AF_INET;
serv_addr->sin_addr.s_addr = INADDR_ANY;
serv_addr->sin_port = htons(port);

return bind(sock, (SOCKADDR*)serv_addr, sizeof(sockaddr_in)) > -1;
if(bind(sock, (SOCKADDR*)serv_addr, sizeof(sockaddr_in) != SOCKET_ERROR)) {
return serv_addr;
}
}

return false;
return nullptr;
}

static char ReadByte(struct sockaddr_in* addr_in) {
Expand All @@ -518,7 +523,7 @@ class UDPSocket {
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock != INVALID_SOCKET) {
int addr_in_size = sizeof(struct sockaddr_in);
if(recvfrom(sock, &value, 1, 0 /* no flags*/, (SOCKADDR*)&addr_in, &addr_in_size)) {
if(recvfrom(sock, &value, 1, 0, (SOCKADDR*)&addr_in, &addr_in_size)) {
return value;
}
}
Expand All @@ -529,8 +534,11 @@ class UDPSocket {
static int WriteByte(char value, struct sockaddr_in* addr_in) {
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock != INVALID_SOCKET) {
const int addr_in_size = sizeof(addr_in);
return sendto(sock, &value, 1, 0, (SOCKADDR*)&addr_in, addr_in_size);
const int addr_in_size = sizeof(struct sockaddr_in);

const int foo = sendto(sock, &value, 1, 0, (SOCKADDR*)&addr_in, addr_in_size);

return foo;
}
}
};
Expand Down
38 changes: 21 additions & 17 deletions core/vm/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4362,35 +4362,40 @@ bool TrapProcessor::SockUdpCreate(StackProgram* program, size_t* inst, size_t*&
array = (size_t*)array[0];
const std::wstring waddr = (wchar_t*)(array + 3);
const std::string addr = UnicodeToBytes(waddr);


struct sockaddr_in ip_addr;
if(inet_pton(AF_INET, addr.c_str(), &ip_addr.sin_addr) == 1) {
struct sockaddr_in* serv_addr = new struct sockaddr_in;
serv_addr->sin_family = AF_INET;
serv_addr->sin_port = htons(port);
serv_addr->sin_addr = ip_addr.sin_addr;

inst[0] = (size_t)serv_addr;
}
}

return true;
}

bool TrapProcessor::SockUdpBind(StackProgram* program, size_t* inst, size_t*& op_stack, long*& stack_pos, StackFrame* frame) {
if(inst) {
const long port = (long)inst[2];

struct sockaddr_in* serv_addr = new struct sockaddr_in;
memset(serv_addr, 0, sizeof(struct sockaddr_in));

if(UDPSocket::Bind(port, serv_addr)) {
inst[1] = (size_t)serv_addr;
return true;
}

// error clean up
delete serv_addr;
serv_addr = nullptr;
const long port = (long)inst[1];
inst[0] = (size_t)UDPSocket::Bind(port);
}

return true;
}

bool TrapProcessor::SockUdpCloseCreate(StackProgram* program, size_t* inst, size_t*& op_stack, long*& stack_pos, StackFrame* frame) {
return false;
size_t* instance = (size_t*)PopInt(op_stack, stack_pos);
if(instance && instance[0]) {
struct sockaddr_in* sock_addr = (struct sockaddr_in*)inst[0];

delete sock_addr;
sock_addr = nullptr;
}

return true;
}

bool TrapProcessor::SockUdpCloseBind(StackProgram* program, size_t* inst, size_t*& op_stack, long*& stack_pos, StackFrame* frame) {
Expand Down Expand Up @@ -4437,8 +4442,7 @@ bool TrapProcessor::SockUdpOutByte(StackProgram* program, size_t* inst, size_t*&
size_t* instance = (size_t*)PopInt(op_stack, stack_pos);
if(instance && instance[0]) {
struct sockaddr_in* sock_addr = (struct sockaddr_in*)inst[0];
UDPSocket::WriteByte((char)value, sock_addr);
PushInt(1, op_stack, stack_pos);
PushInt(UDPSocket::WriteByte((char)value, sock_addr) > -1, op_stack, stack_pos);
}
else {
PushInt(0, op_stack, stack_pos);
Expand Down
Binary file modified docs/api.zip
Binary file not shown.

0 comments on commit 21b62e2

Please sign in to comment.