\r
{socket code by plugwash}\r
\r
-{ Copyright (C) 2005 Bas Steendijk and Peter Green\r
- For conditions of distribution and use, see copyright notice in zlib_license.txt\r
- which is included in the package\r
- ----------------------------------------------------------------------------- }\r
+{use this code for whatever you like in programs under whater licence you like\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
+}\r
+\r
{\r
changes by plugwash (20030728)\r
* created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it\r
\r
\r
unit lsocket;\r
-{$ifdef fpc}\r
- {$mode delphi}\r
-{$endif}\r
+{$mode delphi}\r
interface\r
uses\r
sysutils,\r
- {$ifdef win32}\r
- windows,winsock,\r
+ {$ifdef VER1_0}\r
+ linux,\r
{$else}\r
-\r
- {$ifdef VER1_0}\r
- linux,\r
- {$else}\r
- baseunix,unix,unixutil,\r
- {$endif}\r
- sockets,\r
+ baseunix,unix,\r
{$endif}\r
- classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
+ sockets,classes,pgdebugout,pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
type\r
sunB = packed record\r
s_b1, s_b2, s_b3, s_b4: byte;\r
1: (InAddr6:TInetSockAddr6);\r
{$endif}\r
end;\r
- Pinetsockaddrv = ^Tinetsockaddrv;\r
-\r
\r
type\r
tsockaddrin=TInetSockAddr;\r
function getXport:string; virtual;\r
function getpeerport:string; virtual;\r
constructor Create(AOwner: TComponent); override;\r
- {$ifdef win32}\r
- procedure myfdclose(fd : integer); override;\r
- function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;\r
- function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;\r
- {$endif}\r
end;\r
tsocket=longint; // for compatibility with twsocket\r
\r
implementation\r
{$include unixstuff.inc}\r
\r
-function longip(s:string):longint;{$ifdef fpc}inline;{$endif}\r
+function longip(s:string):longint;inline;\r
var\r
l:longint;\r
a,b:integer;\r
\r
-function convertbyte(const s:string):integer;{$ifdef fpc}inline;{$endif}\r
+function convertbyte(const s:string):integer;inline;\r
begin\r
result := strtointdef(s,-1);\r
if result < 0 then exit;\r
\r
function htons(w:word):word;\r
begin\r
- {$ifndef ENDIAN_BIG}\r
+ {$ifdef ENDIAN_LITTLE}\r
result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);\r
{$else}\r
result := w;\r
\r
function htonl(i:integer):integer;\r
begin\r
- {$ifndef ENDIAN_BIG}\r
+ {$ifdef ENDIAN_LITTLE}\r
result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);\r
{$else}\r
result := i;\r
result := 0;\r
biniptemp := forwardlookup(addr,10);\r
fillchar(inaddr,sizeof(inaddr),0);\r
- //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));\r
if biniptemp.family = AF_INET then begin\r
inAddr.InAddr.family:=AF_INET;\r
inAddr.InAddr.port:=htons(strtointdef(port,0));\r
if udp then a := SOCK_DGRAM else a := SOCK_STREAM;\r
a := Socket(inaddr.inaddr.family,a,0);\r
\r
- //writeln(ord(inaddr.inaddr.family));\r
if a = -1 then begin\r
- lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
+ lasterror := socketerror;\r
raise esocketexception.create('unable to create socket');\r
end;\r
try\r
dup(a);\r
bindsocket;\r
if udp then begin\r
- {$ifndef win32}\r
- SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
- {$endif}\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
end else begin\r
state :=wsconnecting;\r
- {$ifdef win32}\r
- //writeln(inaddr.inaddr.port);\r
- winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);\r
- {$else}\r
- sockets.Connect(fdhandlein,inADDR,getaddrsize);\r
- {$endif}\r
+ sockets.Connect(fdhandlein,inADDR,getaddrsize);\r
end;\r
- eventcore.rmasterset(fdhandlein,false);\r
+ rmasterset(fdhandlein);\r
if udp then begin\r
- eventcore.wmasterclr(fdhandleout);\r
+ wmasterclr(fdhandleout);\r
end else begin\r
- eventcore.wmasterset(fdhandleout);\r
+ wmasterset(fdhandleout);\r
end;\r
//sendq := '';\r
except\r
function tlsocket.send(data:pointer;len:integer):integer;\r
begin\r
if udp then begin\r
- //writeln('sending to '+inttohex(inaddr.inaddr.addr,8));\r
result := sendto(inaddr.inaddr,getaddrsize,data,len)\r
-;\r
- //writeln('send result',result);\r
- //writeln('errno',errno);\r
end else begin\r
result := inherited send(data,len);\r
end;\r
function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;\r
begin\r
if udp then begin\r
- result := myfdread(self.fdhandlein,buf^,bufsize);\r
+ result := fdread(self.fdhandlein,buf^,bufsize);\r
end else begin\r
result := inherited receive(buf,bufsize);\r
end;\r
var\r
a:integer;\r
inAddrtemp:TInetSockAddrV;\r
- inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;\r
inaddrtempsize:integer;\r
begin\r
try\r
\r
inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp);\r
\r
- If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
+ If Not Bind(fdhandlein,inaddrtemp,inaddrtempsize) Then begin\r
state := wsclosed;\r
- lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
+ lasterror := socketerror;\r
raise ESocketException.create('unable to bind, error '+inttostr(lasterror));\r
end;\r
state := wsbound;\r
end;\r
{$endif}\r
if fdhandlein = -1 then raise ESocketException.create('unable to create socket');\r
- dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
- //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup\r
+ dup(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
+ fdreverse[fdhandlein] := self;\r
state := wsclosed; // then set this back as it was an undesired side effect of dup\r
\r
try\r
yes := $01010101; {Copied this from existing code. Value is empiric,\r
but works. (yes=true<>0) }\r
- {$ifndef win32}\r
- if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin\r
- raise ESocketException.create('unable to set socket options');\r
- end;\r
- {$endif}\r
+ if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin\r
+ raise ESocketException.create('unable to set socket options');\r
+ end;\r
+\r
localaddr := addr;\r
localport := port;\r
bindsocket;\r
if not udp then begin\r
{!!! allow custom queue length? default 5}\r
if listenqueue = 0 then listenqueue := 5;\r
- If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen');\r
+ If Not sockets.Listen(fdhandlein,listenqueue) Then raise esocketexception.create('unable to listen');\r
state := wsListening;\r
end else begin\r
- {$ifndef win32}\r
- SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
- {$endif}\r
state := wsconnected;\r
end;\r
finally\r
if state = wsclosed then begin\r
if fdhandlein >= 0 then begin\r
{one *can* get here without fd -beware}\r
- eventcore.rmasterclr(fdhandlein);\r
- myfdclose(fdhandlein); // we musnt leak file discriptors\r
- eventcore.setfdreverse(fdhandlein,nil);\r
+ rmasterclr(fdhandlein);\r
+ fdclose(fdhandlein); // we musnt leak file discriptors\r
+ fdreverse[fdhandlein] := nil;\r
fdhandlein := -1;\r
end;\r
end else begin\r
- eventcore.rmasterset(fdhandlein,true);\r
+ rmasterset(fdhandlein);\r
end;\r
- if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);\r
+ if fdhandleout >= 0 then wmasterclr(fdhandleout);\r
end;\r
- //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); \r
end;\r
\r
function tlsocket.accept : longint;\r
var\r
FromAddrSize : LongInt; // i don't realy know what to do with these at this\r
- FromAddr : TInetSockAddrV; // at this point time will tell :)\r
+ FromAddr : TInetSockAddr; // at this point time will tell :)\r
begin\r
-\r
+ rmasterset(fdhandlein);\r
FromAddrSize := Sizeof(FromAddr);\r
- {$ifdef win32}\r
- result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);\r
- {$else}\r
- result := sockets.accept(fdhandlein,fromaddr,fromaddrsize);\r
- {$endif}\r
- //now we have accepted one request start monitoring for more again\r
- eventcore.rmasterset(fdhandlein,true);\r
-\r
- if result = -1 then raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');\r
+ result := sockets.accept(fdhandlein,fromaddr,fromaddrsize);\r
+ if result = -1 then raise esocketexception.create('error '+inttostr(socketerror)+' while accepting');\r
if result > absoloutemaxs then begin\r
- myfdclose(result);\r
+ fdclose(result);\r
result := -1;\r
raise esocketexception.create('file discriptor out of range');\r
end;\r
end;\r
\r
-function tlsocket.sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer;\r
-var\r
- destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute dest;\r
+function tlsocket.sendto;\r
begin\r
- result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen);\r
+ result := sockets.sendto(self.fdhandleout,data^,len,0,dest,destlen);\r
end;\r
\r
-function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer;\r
-var\r
- srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute src;\r
+function tlsocket.receivefrom;\r
begin\r
- result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);\r
+ result := sockets.recvfrom(self.fdhandlein,data^,len,0,src,srclen);\r
end;\r
\r
procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);\r
var\r
tempbuf:array[0..receivebufsize-1] of byte;\r
begin\r
- //writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger);\r
if (state =wslistening) and readtrigger then begin\r
{ debugout('listening socket triggered on read');}\r
- eventcore.rmasterclr(fdhandlein);\r
+ rmasterclr(fdhandlein);\r
if assigned(onsessionAvailable) then onsessionAvailable(self,0);\r
end;\r
if udp and readtrigger then begin\r
// code for dealing with the reults of a non-blocking connect is\r
// rather complex\r
// if just write is triggered it means connect suceeded\r
- // if both read and write are triggered it can mean 2 things\r
+ // if both read and write are suceededed it can mean 2 things\r
// 1: connect ok and data availible\r
// 2: connect fail\r
// to find out which you must read from the socket and look for errors\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
end else begin\r
- numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));\r
+ numread := fdread(fdhandlein,tempbuf,sizeof(tempbuf));\r
if numread <> -1 then begin\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
recvq.add(@tempbuf,numread);\r
end else begin\r
state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,{$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});\r
+ if assigned(onsessionconnected) then onsessionconnected(self,linuxerror);\r
{ debugout('connect fail');}\r
self.internalclose(0);\r
recvq.del(maxlongint);\r
end;\r
if fdhandlein >= 0 then begin\r
if state = wsconnected then begin\r
- eventcore.rmasterset(fdhandlein,false);\r
+ rmasterset(fdhandlein);\r
end else begin\r
- eventcore.rmasterclr(fdhandlein);\r
+ rmasterclr(fdhandlein);\r
end;\r
end;\r
if fdhandleout >= 0 then begin\r
if sendq.size = 0 then begin\r
//don't clear the bit in fdswmaster if data is in the sendq\r
- eventcore.wmasterclr(fdhandleout);\r
+ wmasterclr(fdhandleout);\r
end;\r
end;\r
\r
\r
\r
function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;\r
-var\r
- addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;\r
begin\r
- result := {$ifdef win32}winsock{$else}sockets{$endif}.getpeername(self.fdhandlein,addrx,addrlen);\r
+ result := sockets.getpeername(self.fdhandlein,addr,addrlen);\r
end;\r
\r
procedure tlsocket.getxaddrbin(var binip:tbinip);\r
begin\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
+ sockets.getsocketname(self.fdhandlein,addr,i);\r
\r
- {$ifdef win32}\r
- winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);\r
- {$else}\r
- sockets.getsocketname(self.fdhandlein,addr,i);\r
- {$endif}\r
binip.family := addr.inaddr.family;\r
{$ifdef ipv6}\r
if addr.inaddr6.sin6_family = AF_INET6 then begin\r
begin\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
- {$ifdef win32}\r
- winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);\r
- {$else}\r
- sockets.getpeername(self.fdhandlein,addr,i);\r
- {$endif}\r
+ sockets.getpeername(self.fdhandlein,addr,i);\r
\r
binip.family := addr.inaddr.family;\r
{$ifdef ipv6}\r
\r
function tlsocket.getXport:string;\r
var\r
- addr:tinetsockaddrv;\r
+ addr:tinetsockaddr;\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- {$ifdef win32}\r
- winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);\r
-\r
- {$else}\r
- sockets.getsocketname(self.fdhandlein,addr,i);\r
-\r
- {$endif}\r
- result := inttostr(htons(addr.InAddr.port));\r
+ sockets.getsocketname(self.fdhandlein,addr,i);\r
+ i := htons(addr.port);\r
+ result := inttostr(i);\r
end;\r
\r
function tlsocket.getpeerport:string;\r
var\r
- addr:tinetsockaddrv;\r
+ addr:tinetsockaddr;\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- {$ifdef win32}\r
- winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);\r
-\r
- {$else}\r
- sockets.getpeername(self.fdhandlein,addr,i);\r
-\r
- {$endif}\r
- result := inttostr(htons(addr.InAddr.port));\r
+ sockets.getpeername(self.fdhandlein,addr,i);\r
+ i := htons(addr.port);\r
+ result := inttostr(i);\r
end;\r
\r
-{$ifdef win32}\r
- procedure tlsocket.myfdclose(fd : integer);\r
- begin\r
- closesocket(fd);\r
- end;\r
- function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;\r
- begin\r
- result := winsock.send(fd,(@buf)^,size,0);\r
- end;\r
- function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;\r
- begin\r
- result := winsock.recv(fd,buf,size,0);\r
- end;\r
-{$endif}\r
-
end.\r
-\r