151 lines
No EOL
5 KiB
Text
151 lines
No EOL
5 KiB
Text
;************************
|
|
;* Library : lhs_net_socket.pbi
|
|
;*
|
|
;* Socket based Networkcommuncation Module.
|
|
;*
|
|
;* Created and develobed by Linder Hard- und Software
|
|
;*
|
|
;* Current only usable for Linux.
|
|
;*
|
|
;*
|
|
|
|
CompilerIf #PB_Compiler_OS <> #PB_OS_Linux
|
|
CompilerError "Only for Linux"
|
|
CompilerEndIf
|
|
XIncludeFile "../SYS/lhs_sys_debug_wrapper.pbi"
|
|
;***********************
|
|
;* The SIGPIPE Signal must be Handled Inside / Ignored
|
|
#SIGPIPE = 13
|
|
|
|
Procedure SignalIgnore()
|
|
Debug("Signal Ignore")
|
|
EndProcedure
|
|
;***********************
|
|
|
|
signal_(#SIGPIPE, @SignalIgnore());
|
|
|
|
DeclareModule lsocket
|
|
Structure sockaddr
|
|
sa_family.w
|
|
sa_data.s{14}
|
|
EndStructure
|
|
|
|
Structure in_addr
|
|
s_addr.l
|
|
EndStructure
|
|
|
|
Structure sockaddr_in
|
|
sin_family.w
|
|
sin_port.w
|
|
sin_addr.in_addr
|
|
sin_zero.a[8]
|
|
EndStructure
|
|
|
|
; Socket types:
|
|
#SOCK_STREAM = 1 ; stream (connection) socket
|
|
#SOCK_DGRAM = 2 ; datagram (conn.less) socket
|
|
#SOCK_RAW = 3 ; raw socket
|
|
#SOCK_RDM = 4 ; reliably-delivered message
|
|
#SOCK_SEQPACKET = 5 ; sequential packet socket
|
|
#SOCK_PACKET = 10 ; linux specific way of
|
|
; getting packets at the dev
|
|
; level. For writing rarp And
|
|
; other similar things on the
|
|
; user level.
|
|
|
|
; socket.h
|
|
#PF_INET = 2
|
|
#AF_INET = #PF_INET
|
|
#SOL_SOCKET = $ffff
|
|
#SO_ERROR = $1007
|
|
|
|
Global LoggerUUID.s = ""
|
|
Global Log_Level_Info = 0
|
|
Global Log_Level_Debug = 0
|
|
Global Log_Level_Error = 2
|
|
|
|
Declare 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
|
|
Declare WaitSocket(ServerSocket.i) ;Return new SocketID
|
|
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 IsClientSocket(ClientSocket.i)
|
|
Declare.s GetSocketIP(Socket.i)
|
|
EndDeclareModule
|
|
|
|
Module lsocket
|
|
Global NewMap lsockets_server.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
|
|
Protected SocketID.i
|
|
SocketID = socket_(Protocol, Type, 0)
|
|
If SocketID = -1 : ProcedureReturn -1 : EndIf
|
|
lsockets_server(Str(SocketID))\sin_family = Protocol
|
|
lsockets_server(Str(SocketID))\sin_addr\s_addr = inet_addr_(BindedIP)
|
|
lsockets_server(Str(SocketID))\sin_port = htons_(Port)
|
|
If bind_(SocketID, @lsockets_server(Str(SocketID)), SizeOf(sockaddr_in)) <> 0
|
|
close_(SocketID)
|
|
DeleteMapElement(lsockets_server(), Str(SocketID))
|
|
ProcedureReturn -2
|
|
Else
|
|
If listen_(SocketID, Queuelengt) <> 0
|
|
close_(SocketID)
|
|
DeleteMapElement(lsockets_server(), Str(SocketID))
|
|
ProcedureReturn -3
|
|
EndIf
|
|
EndIf
|
|
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
|
|
ldl::Logging("Requested IP Information:" + IPAddresse, LoggerUUID, Log_Level_Info)
|
|
ProcedureReturn IPAddresse
|
|
EndProcedure
|
|
|
|
Procedure WaitSocket(ServerSocket.i) ;Return new ClientSocketID
|
|
Protected ClientSID.i, MapID.s, ClientLength
|
|
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)
|
|
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)
|
|
FreeMemory(memory)
|
|
ProcedureReturn ClientSID
|
|
EndProcedure
|
|
|
|
Procedure ReadSocket(ClientSocket.i, Buffer, Length.i) ;Return Effective Read
|
|
Protected Effective.i
|
|
Effective = read_(ClientSocket, Buffer, Length)
|
|
ProcedureReturn Effective
|
|
EndProcedure
|
|
|
|
Procedure WriteSocket(ClientSocket.i, Buffer, Length.i) ;Return Effective Writen
|
|
Protected Effective.i
|
|
Effective = write_(ClientSocket, Buffer, Length)
|
|
ProcedureReturn Effective
|
|
EndProcedure
|
|
|
|
Procedure CloseSocket(Socket.i)
|
|
Protected Feedback.i
|
|
Feedback = close_(Socket)
|
|
ProcedureReturn Feedback
|
|
EndProcedure
|
|
|
|
EndModule |