classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
\r
{$ifdef ipv6}\r
-{$define secondlistener}\r
const\r
v4listendefault:boolean=false;\r
{$endif}\r
localaddr:string;\r
localport:string;\r
proto:string;\r
- udp:boolean;\r
+ udp,dgram:boolean;\r
listenqueue:integer;\r
{$ifdef secondlistener}\r
secondlistener:tlsocket;\r
twsocket=tlsocket; {easy}\r
\r
\r
-{!!!function longipdns(s:string):longint;}\r
-\r
const\r
TCP_NODELAY=1;\r
IPPROTO_TCP=6;\r
\r
procedure tlsocket.realconnect;\r
var\r
- a:integer;\r
-\r
+ a,b:integer;\r
begin\r
// writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);\r
makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);\r
inc(currentip);\r
if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;\r
- udp := uppercase(proto) = 'UDP';\r
- if udp then a := SOCK_DGRAM else a := SOCK_STREAM;\r
- a := Socket(inaddr.inaddr.family,a,0);\r
+\r
+ udp := false;\r
+ if (uppercase(proto) = 'UDP') then begin\r
+ b := IPPROTO_UDP;\r
+ a := SOCK_DGRAM;\r
+ udp := true;\r
+ dgram := true;\r
+ end else if (uppercase(proto) = 'TCP') then begin\r
+ b := IPPROTO_TCP;\r
+ a := SOCK_STREAM;\r
+ dgram := false;\r
+ end else begin\r
+ b := strtointdef(proto,IPPROTO_ICMP);\r
+ a := SOCK_RAW;\r
+ dgram := true;\r
+ end;\r
+\r
+ a := Socket(inaddr.inaddr.family,a,b);\r
//writeln(ord(inaddr.inaddr.family));\r
if a = -1 then begin\r
lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
try\r
dup(a);\r
bindsocket;\r
- if udp then begin\r
+ if dgram then begin\r
{$ifndef win32}\r
SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
+ {$else}\r
+ SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$endif}\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
\r
procedure tlsocket.sendstr(const str : string);\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
send(@str[1],length(str))\r
end else begin\r
inherited sendstr(str);\r
\r
function tlsocket.send(data:pointer;len:integer):integer;\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
// writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');\r
result := sendto(inaddr,getaddrsize,data,len);\r
\r
\r
function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;\r
begin\r
- if udp then begin\r
+ if dgram then begin\r
{$ifdef secondlistener}\r
if lastsessionfromsecond then begin\r
result := secondlistener.receive(buf,bufsize);\r
localaddr := '0.0.0.0';\r
end;\r
//gethostbyname(localaddr,host);\r
-\r
inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);\r
\r
If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
state := wsclosed;\r
lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
- raise ESocketException.create('unable to bind, error '+inttostr(lasterror));\r
+ raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));\r
end;\r
state := wsbound;\r
end;\r
begin\r
if state <> wsclosed then close;\r
udp := uppercase(proto) = 'UDP';\r
- if udp then socktype := SOCK_DGRAM else socktype := SOCK_STREAM;\r
+ if udp then begin\r
+ socktype := SOCK_DGRAM;\r
+ dgram := true;\r
+ end else socktype := SOCK_STREAM;\r
origaddr := addr;\r
\r
if addr = '' then begin\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 {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise \r
+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
+ {$else}\r
+ SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$endif}\r
state := wsconnected;\r
end;\r
\r
function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;\r
var\r
- srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src;\r
+ tempsrc:TInetSockAddrV;\r
+ tempsrclen:integer;\r
+ srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;\r
+ biniptemp:tbinip;\r
begin\r
{$ifdef secondlistener}\r
if assigned(secondlistener) then if lastsessionfromsecond then begin\r
exit;\r
end;\r
{$endif}\r
- result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);\r
+ tempsrclen := sizeof(tempsrc);\r
+ result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);\r
+\r
+ {$ifdef ipv6}\r
+ biniptemp := inaddrvtobinip(tempsrc);\r
+ if needconverttov4(biniptemp) then begin\r
+ converttov4(biniptemp);\r
+ tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);\r
+ end;\r
+ {$endif}\r
+\r
+ move(tempsrc,src,srclen);\r
+ srclen := tempsrclen;\r
end;\r
\r
procedure tlsocket.connectionfailedhandler(error:word);\r
eventcore.rmasterclr(fdhandlein);\r
if assigned(onsessionAvailable) then onsessionAvailable(self,0);\r
end;\r
- if udp and readtrigger then begin\r
+ if dgram and readtrigger then begin\r
if assigned(ondataAvailable) then ondataAvailable(self,0);\r
{!!!test}\r
exit;\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
- binip.ip6 := addr.inaddr6.sin6_addr;\r
- end else\r
- {$endif}\r
- begin\r
- binip.ip := addr.inaddr.addr;\r
- end;\r
+ binip := inaddrvtobinip(addr);\r
converttov4(binip);\r
end;\r
\r
sockets.getpeername(self.fdhandlein,addr,i);\r
{$endif}\r
\r
- binip.family := addr.inaddr.family;\r
- {$ifdef ipv6}\r
- if addr.inaddr6.sin6_family = AF_INET6 then begin\r
- binip.ip6 := addr.inaddr6.sin6_addr;\r
- end else\r
- {$endif}\r
- begin\r
- binip.ip := addr.inaddr.addr;\r
- end;\r
+ binip := inaddrvtobinip(addr);\r
converttov4(binip);\r
end;\r
\r
result := winsock.recv(fd,buf,size,0);\r
end;\r
{$endif}\r
-
+\r
end.\r
\r