Add IsClientSocket()

This commit is contained in:
René Linder 2023-09-20 17:11:44 +02:00
parent 548c80c974
commit 28ddc320e5

View file

@ -56,6 +56,8 @@ DeclareModule lsocket
; socket.h ; socket.h
#PF_INET = 2 #PF_INET = 2
#AF_INET = #PF_INET #AF_INET = #PF_INET
#SOL_SOCKET = $ffff
#SO_ERROR = $1007
Global LoggerUUID.s = "" Global LoggerUUID.s = ""
Global Log_Level_Info = 0 Global Log_Level_Info = 0
@ -67,6 +69,7 @@ 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 IsClientSocket(ClientSocket.i)
Declare.s GetSocketIP(Socket.i) Declare.s GetSocketIP(Socket.i)
EndDeclareModule EndDeclareModule
@ -74,20 +77,27 @@ 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()
Procedure IsClientSocket(ClientSocket.i)
Protected error.q = 0
Protected retval
retval = getsockopt_(ClientSocket, #SOL_SOCKET, #SO_ERROR, error, 8);
ProcedureReturn retval
EndProcedure
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)
If SocketID = -1 : ProcedureReturn -1 : EndIf If SocketID = -1 : ProcedureReturn -1 : EndIf
lsockets_server(Str(SocketID))\sin_family = Protocol lsockets_server(Str(SocketID))\sin_family = Protocol
lsockets_server(Str(SocketID))\sin_addr\s_addr = Inet_addr_(BindedIP) lsockets_server(Str(SocketID))\sin_addr\s_addr = inet_addr_(BindedIP)
lsockets_server(Str(SocketID))\sin_port = htons_(Port) lsockets_server(Str(SocketID))\sin_port = htons_(Port)
If bind_(SocketID, @lsockets_server(Str(SocketID)), SizeOf(sockaddr_in)) <> 0 If bind_(SocketID, @lsockets_server(Str(SocketID)), SizeOf(sockaddr_in)) <> 0
Close_(SocketID) close_(SocketID)
DeleteMapElement(lsockets_server(), Str(SocketID)) DeleteMapElement(lsockets_server(), Str(SocketID))
ProcedureReturn -2 ProcedureReturn -2
Else Else
If listen_(SocketID, Queuelengt) <> 0 If listen_(SocketID, Queuelengt) <> 0
Close_(SocketID) close_(SocketID)
DeleteMapElement(lsockets_server(), Str(SocketID)) DeleteMapElement(lsockets_server(), Str(SocketID))
ProcedureReturn -3 ProcedureReturn -3
EndIf EndIf
@ -98,9 +108,9 @@ Module lsocket
Procedure.s GetSocketIP(Socket.i) Procedure.s GetSocketIP(Socket.i)
Protected IPAddresse.s Protected IPAddresse.s
If lsockets_client(Str(Socket))\sin_family = 0 If lsockets_client(Str(Socket))\sin_family = 0
IPAddresse = PeekS(Inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii) IPAddresse = PeekS(inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii)
Else Else
IPAddresse = PeekS(Inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii) IPAddresse = PeekS(inet_ntoa_(lsockets_client(Str(Socket))\sin_addr\s_addr), -1, #PB_Ascii)
EndIf EndIf
ldl::Logging("Requested IP Information:" + IPAddresse, LoggerUUID, Log_Level_Info) ldl::Logging("Requested IP Information:" + IPAddresse, LoggerUUID, Log_Level_Info)
ProcedureReturn IPAddresse ProcedureReturn IPAddresse
@ -115,7 +125,7 @@ Module lsocket
If ClientSID < 0 : FreeMemory(memory) : ProcedureReturn ClientSID : EndIf If ClientSID < 0 : FreeMemory(memory) : ProcedureReturn ClientSID : EndIf
MapID = Str(ClientSID) MapID = Str(ClientSID)
CopyMemory(memory, lsockets_client(MapID), ClientLength) CopyMemory(memory, lsockets_client(MapID), ClientLength)
ldl::Logging("Connected IP:" + PeekS(Inet_ntoa_(lsockets_client(MapID)\sin_addr\s_addr), -1, #PB_Ascii), LoggerUUID, Log_Level_Info) ldl::Logging("Connected IP:" + PeekS(inet_ntoa_(lsockets_client(MapID)\sin_addr\s_addr), -1, #PB_Ascii), LoggerUUID, Log_Level_Info)
FreeMemory(memory) FreeMemory(memory)
ProcedureReturn ClientSID ProcedureReturn ClientSID
EndProcedure EndProcedure