From: beware Date: Mon, 19 Jan 2009 11:48:03 +0000 (+0000) Subject: add support for raw sockets X-Git-Url: http://www.lcore.org/git/lcore.git/commitdiff_plain/1958bd56792f6815dbf06aad80d4144a13439be7?ds=inline add support for raw sockets git-svn-id: file:///svnroot/lcore/trunk@21 b1de8a11-f9be-4011-bde0-cc7ace90066a --- diff --git a/lsocket.pas b/lsocket.pas index 2eb4dbd..a9adbd3 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -99,7 +99,7 @@ type localaddr:string; localport:string; proto:string; - udp:boolean; + udp,dgram:boolean; listenqueue:integer; {$ifdef secondlistener} secondlistener:tlsocket; @@ -158,16 +158,30 @@ end; procedure tlsocket.realconnect; var - a:integer; - + a,b:integer; begin // writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port); makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr); inc(currentip); if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false; - udp := uppercase(proto) = 'UDP'; - if udp then a := SOCK_DGRAM else a := SOCK_STREAM; - a := Socket(inaddr.inaddr.family,a,0); + + udp := false; + if (uppercase(proto) = 'UDP') then begin + b := IPPROTO_UDP; + a := SOCK_DGRAM; + udp := true; + dgram := true; + end else if (uppercase(proto) = 'TCP') then begin + b := IPPROTO_TCP; + a := SOCK_STREAM; + dgram := false; + end else begin + b := strtointdef(proto,IPPROTO_ICMP); + a := SOCK_RAW; + dgram := true; + end; + + a := Socket(inaddr.inaddr.family,a,b); //writeln(ord(inaddr.inaddr.family)); if a = -1 then begin lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; @@ -176,7 +190,7 @@ begin try dup(a); bindsocket; - if udp then begin + if dgram then begin {$ifndef win32} SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); {$endif} @@ -241,7 +255,7 @@ end; procedure tlsocket.sendstr(const str : string); begin - if udp then begin + if dgram then begin send(@str[1],length(str)) end else begin inherited sendstr(str); @@ -250,7 +264,7 @@ end; function tlsocket.send(data:pointer;len:integer):integer; begin - if udp then begin + if dgram then begin // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes'); result := sendto(inaddr,getaddrsize,data,len); @@ -264,7 +278,7 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin - if udp then begin + if dgram then begin {$ifdef secondlistener} if lastsessionfromsecond then begin result := secondlistener.receive(buf,bufsize); @@ -293,7 +307,6 @@ begin localaddr := '0.0.0.0'; end; //gethostbyname(localaddr,host); - inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp); If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin @@ -540,7 +553,7 @@ begin eventcore.rmasterclr(fdhandlein); if assigned(onsessionAvailable) then onsessionAvailable(self,0); end; - if udp and readtrigger then begin + if dgram and readtrigger then begin if assigned(ondataAvailable) then ondataAvailable(self,0); {!!!test} exit;