Bugfix ltls and lsocket

This commit is contained in:
René Linder 2021-05-25 10:02:16 +02:00
parent 07c1b19efe
commit c55ae778fa
2 changed files with 40 additions and 11 deletions

View file

@ -20,14 +20,14 @@ DeclareModule lsocket
EndStructure EndStructure
Structure in_addr Structure in_addr
s_addr.w s_addr.l
EndStructure EndStructure
Structure sockaddr_in Structure sockaddr_in
sin_family.w sin_family.w
sin_port.w sin_port.w
sin_addr.in_addr sin_addr.in_addr
sin_zero.s{8} sin_zero.a[8]
EndStructure EndStructure
; Socket types: ; Socket types:
@ -51,12 +51,13 @@ DeclareModule lsocket
Declare ReadSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Read Declare ReadSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Read
Declare WriteSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Writen Declare WriteSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Writen
Declare CloseSocket(Socket.i) Declare CloseSocket(Socket.i)
Declare.s GetSocketIP(Socket.i)
EndDeclareModule EndDeclareModule
Module lsocket Module lsocket
Global NewMap lsockets_server.sockaddr_in() Global NewMap lsockets_server.sockaddr_in()
Global NewMap lsockets_client.sockaddr_in() Global NewMap lsockets_client.sockaddr_in()
Global NewMap lsockets_scrumble.sockaddr_in()
Procedure CreateSocket(Port.i, Queuelengt.i = 64, Type.i = #SOCK_STREAM, Protocol.i = #AF_INET, BindedIP.s = "127.0.0.1") ; Return Server Socket < 0 Error Procedure CreateSocket(Port.i, Queuelengt.i = 64, Type.i = #SOCK_STREAM, Protocol.i = #AF_INET, BindedIP.s = "127.0.0.1") ; Return Server Socket < 0 Error
Protected SocketID.i Protected SocketID.i
SocketID = socket_(Protocol, Type, 0) SocketID = socket_(Protocol, Type, 0)
@ -78,16 +79,28 @@ Module lsocket
ProcedureReturn SocketID ProcedureReturn SocketID
EndProcedure EndProcedure
Procedure.s GetSocketIP(Socket.i)
Protected IPAddresse.s
If lsockets_client(Str(Socket))\sin_family = 0
IPAddresse = PeekS(Inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii)
Else
IPAddresse = PeekS(Inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii)
EndIf
Debug "Requested IP Information:" + IPAddresse
ProcedureReturn IPAddresse
EndProcedure
Procedure WaitSocket(ServerSocket.i) ;Return new ClientSocketID Procedure WaitSocket(ServerSocket.i) ;Return new ClientSocketID
Protected ClientSID.i, MapID.s, ClientLength Protected ClientSID.i, MapID.s, ClientLength
Protected Client.sockaddr_in Protected memory
ClientSID = accept_(ServerSocket, @Client, @ClientLength) memory = AllocateMemory(SizeOf(sockaddr_in))
If ClientSID < 0 : ProcedureReturn ClientSID : EndIf ClientLength = SizeOf(sockaddr_in)
ClientSID = accept_(ServerSocket, memory, @ClientLength)
If ClientSID < 0 : FreeMemory(memory) : ProcedureReturn ClientSID : EndIf
MapID = Str(ClientSID) MapID = Str(ClientSID)
lsockets_client(MapID)\sin_addr\s_addr = Client\sin_addr\s_addr CopyMemory(memory, lsockets_client(MapID), ClientLength)
lsockets_client(MapID)\sin_family = Client\sin_family Debug "Connected IP:" + PeekS(Inet_ntoa_(lsockets_client(MapID)\sin_addr\s_addr), -1, #PB_Ascii)
lsockets_client(MapID)\sin_port = Client\sin_port FreeMemory(memory)
lsockets_client(MapID)\sin_zero = Client\sin_zero
ProcedureReturn ClientSID ProcedureReturn ClientSID
EndProcedure EndProcedure

View file

@ -32,10 +32,18 @@ DeclareModule ltls
Declare CloseTLS(TLSID.i = 0) Declare CloseTLS(TLSID.i = 0)
Declare.s ErrorTLSSrv(TLSID.i = 0) Declare.s ErrorTLSSrv(TLSID.i = 0)
Declare.s ErrorTLSCli(Connection) Declare.s ErrorTLSCli(Connection)
Declare GetSocket(Connection.i)
EndDeclareModule EndDeclareModule
Module ltls Module ltls
Structure tls_socket
server_id.i
client_id.i
tls_id.i
EndStructure
Global NewMap socket_sort.tls_socket()
PrototypeC.i Pr_tls_init() PrototypeC.i Pr_tls_init()
PrototypeC.i Pr_tls_config_new() PrototypeC.i Pr_tls_config_new()
PrototypeC.i Pr_tls_load_file_pw(FileName.p-utf8, Length, Password.p-utf8) PrototypeC.i Pr_tls_load_file_pw(FileName.p-utf8, Length, Password.p-utf8)
@ -177,6 +185,9 @@ Module ltls
If sockID < 0 : ProcedureReturn -1 : EndIf If sockID < 0 : ProcedureReturn -1 : EndIf
tlssID = tls_accept_socket(tls_ctx, @cctx, sockID) tlssID = tls_accept_socket(tls_ctx, @cctx, sockID)
If tlssID = -1 : ProcedureReturn -2 : EndIf If tlssID = -1 : ProcedureReturn -2 : EndIf
socket_sort(Str(cctx))\client_id = sockID
socket_sort(Str(cctx))\tls_id = cctx
socket_sort(Str(cctx))\server_id = ServerSocket
ProcedureReturn cctx ProcedureReturn cctx
EndProcedure EndProcedure
@ -194,6 +205,11 @@ Module ltls
Procedure CloseTLSSocket(Connection.i) Procedure CloseTLSSocket(Connection.i)
tls_close(Connection) tls_close(Connection)
DeleteMapElement(socket_sort(), Str(Connection))
EndProcedure
Procedure GetSocket(Connection.i)
ProcedureReturn socket_sort(Str(Connection))\client_id
EndProcedure EndProcedure
Procedure.s ErrorTLSSrv(TLSID.i = 0) Procedure.s ErrorTLSSrv(TLSID.i = 0)