Bugfix ltls and lsocket

main
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
Structure in_addr
s_addr.w
s_addr.l
EndStructure
Structure sockaddr_in
sin_family.w
sin_port.w
sin_addr.in_addr
sin_zero.s{8}
sin_zero.a[8]
EndStructure
; Socket types:
@ -51,12 +51,13 @@ DeclareModule lsocket
Declare ReadSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Read
Declare WriteSocket(ClientSocket.i, *Buffer, Length.i) ;Return Effective Writen
Declare CloseSocket(Socket.i)
Declare.s GetSocketIP(Socket.i)
EndDeclareModule
Module lsocket
Global NewMap lsockets_server.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
Protected SocketID.i
SocketID = socket_(Protocol, Type, 0)
@ -78,16 +79,28 @@ Module lsocket
ProcedureReturn SocketID
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
Protected ClientSID.i, MapID.s, ClientLength
Protected Client.sockaddr_in
ClientSID = accept_(ServerSocket, @Client, @ClientLength)
If ClientSID < 0 : ProcedureReturn ClientSID : EndIf
Protected memory
memory = AllocateMemory(SizeOf(sockaddr_in))
ClientLength = SizeOf(sockaddr_in)
ClientSID = accept_(ServerSocket, memory, @ClientLength)
If ClientSID < 0 : FreeMemory(memory) : ProcedureReturn ClientSID : EndIf
MapID = Str(ClientSID)
lsockets_client(MapID)\sin_addr\s_addr = Client\sin_addr\s_addr
lsockets_client(MapID)\sin_family = Client\sin_family
lsockets_client(MapID)\sin_port = Client\sin_port
lsockets_client(MapID)\sin_zero = Client\sin_zero
CopyMemory(memory, lsockets_client(MapID), ClientLength)
Debug "Connected IP:" + PeekS(Inet_ntoa_(lsockets_client(MapID)\sin_addr\s_addr), -1, #PB_Ascii)
FreeMemory(memory)
ProcedureReturn ClientSID
EndProcedure

View File

@ -32,10 +32,18 @@ DeclareModule ltls
Declare CloseTLS(TLSID.i = 0)
Declare.s ErrorTLSSrv(TLSID.i = 0)
Declare.s ErrorTLSCli(Connection)
Declare GetSocket(Connection.i)
EndDeclareModule
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_config_new()
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
tlssID = tls_accept_socket(tls_ctx, @cctx, sockID)
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
EndProcedure
@ -194,6 +205,11 @@ Module ltls
Procedure CloseTLSSocket(Connection.i)
tls_close(Connection)
DeleteMapElement(socket_sort(), Str(Connection))
EndProcedure
Procedure GetSocket(Connection.i)
ProcedureReturn socket_sort(Str(Connection))\client_id
EndProcedure
Procedure.s ErrorTLSSrv(TLSID.i = 0)