3 {socket code by plugwash}
\r
5 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
6 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
7 which is included in the package
\r
8 ----------------------------------------------------------------------------- }
\r
10 changes by plugwash (20030728)
\r
11 * created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it
\r
12 * changed tlasio to tlasio
\r
13 * split fdhandle into fdhandlein and fdhandleout
\r
14 * i now use fdsrmaster and fdswmaster instead of rebuilding the lists every loop
\r
15 * split lsocket.pas into lsocket.pas and lcore.pas
\r
18 changes by beware (20030903)
\r
19 * added getxaddr, getxport (local addr, port, as string)
\r
20 * added getpeername, remote addr+port as binary
\r
21 * added htons and htonl functions (endian swap, same interface as windows API)
\r
24 * if connect failed (conn refused) set state to connected and call internalclose, to get closed handler (instead of fdclose)
\r
25 * (lcore) if closing the fd's in internalcose, set fd's to -1 because closing an fd makes it invalid
\r
28 * fixed: on connect failed, tried to close fdhandle's which were already set to -1, added check
\r
31 * added getpeeraddr, getpeerport, remote addr+port as string
\r
53 classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;
\r
55 sunB = packed record
\r
56 s_b1, s_b2, s_b3, s_b4: byte;
\r
59 SunW = packed record
\r
63 TInAddr = packed record
\r
67 2: (S_addr: cardinal);
\r
76 TInetSockAddr6 = packed Record
\r
77 sin6_family : sa_family_t;
\r
78 sin6_port : cuint16;
\r
79 sin6_flowinfo : cuint32;
\r
80 sin6_addr : Tin6_addr;
\r
81 sin6_scope_id : cuint32;
\r
85 TinetSockAddrv = packed record
\r
87 0: (InAddr:TInetSockAddr);
\r
89 1: (InAddr6:TInetSockAddr6);
\r
92 Pinetsockaddrv = ^Tinetsockaddrv;
\r
96 tsockaddrin=TInetSockAddr;
\r
99 TLsocket = class(tlasio)
\r
103 inAddr : TInetSockAddrV;
\r
104 { inAddrSize:integer;}
\r
106 //host : THostentry ;
\r
108 //mainthread : boolean ; //for debuggin only
\r
115 listenqueue:integer;
\r
116 function getaddrsize:integer;
\r
117 procedure connect; virtual;
\r
118 procedure bindsocket;
\r
120 function accept : longint;
\r
121 function sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; virtual;
\r
122 function receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer; virtual;
\r
123 //procedure internalclose(error:word);override;
\r
124 procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;
\r
125 function send(data:pointer;len:integer):integer;override;
\r
126 procedure sendstr(const str : string);override;
\r
127 function Receive(Buf:Pointer;BufSize:integer):integer; override;
\r
128 function getpeername(var addr:tsockaddrin;addrlen:integer):integer; virtual;
\r
129 procedure getXaddrbin(var binip:tbinip); virtual;
\r
130 procedure getpeeraddrbin(var binip:tbinip); virtual;
\r
131 function getXaddr:string; virtual;
\r
132 function getpeeraddr:string; virtual;
\r
133 function getXport:string; virtual;
\r
134 function getpeerport:string; virtual;
\r
135 constructor Create(AOwner: TComponent); override;
\r
137 procedure myfdclose(fd : integer); override;
\r
138 function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
\r
139 function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
\r
142 tsocket=longint; // for compatibility with twsocket
\r
144 twsocket=tlsocket; {easy}
\r
146 function htons(w:word):word;
\r
147 function htonl(i:integer):integer;
\r
148 {!!!function longipdns(s:string):longint;}
\r
152 v4listendefault:boolean=false;
\r
161 {$include unixstuff.inc}
\r
163 function longip(s:string):longint;{$ifdef fpc}inline;{$endif}
\r
168 function convertbyte(const s:string):integer;{$ifdef fpc}inline;{$endif}
\r
170 result := strtointdef(s,-1);
\r
171 if result < 0 then exit;
\r
172 if result > 255 then exit;
\r
175 if (result <> 0) and (s[1] = '0') then begin
\r
181 if not (s[1] in ['0'..'9']) then begin
\r
190 if a = 0 then exit;
\r
191 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
193 s := copy(s,a+1,256);
\r
195 if a = 0 then exit;
\r
196 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
197 l := l or b shl 16;
\r
198 s := copy(s,a+1,256);
\r
200 if a = 0 then exit;
\r
201 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
203 s := copy(s,a+1,256);
\r
204 b := convertbyte(copy(s,1,256));if (b < 0) then exit;
\r
210 function longipdns(s:string):longint;
\r
214 if s = '0.0.0.0' then begin
\r
217 result := longip(s);
\r
218 if result = 0 then begin
\r
219 if gethostbyname(s,host) then begin;
\r
220 result := htonl(Longint(Host.Addr));
\r
222 //writeln(inttohex(longint(host.addr),8))
\r
224 if result = 0 then begin
\r
225 if resolvehostbyname(s,host) then begin;
\r
226 result := htonl(Longint(Host.Addr));
\r
228 //writeln(inttohex(longint(host.addr),8))
\r
235 function htons(w:word):word;
\r
237 {$ifndef ENDIAN_BIG}
\r
238 result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
\r
244 function htonl(i:integer):integer;
\r
246 {$ifndef ENDIAN_BIG}
\r
247 result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
\r
253 function tlsocket.getaddrsize:integer;
\r
256 if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else
\r
258 result := sizeof(tinetsockaddr);
\r
261 function makeinaddrv(addr,port:string;var inaddr:tinetsockaddrv):integer;
\r
266 biniptemp := forwardlookup(addr,10);
\r
267 fillchar(inaddr,sizeof(inaddr),0);
\r
268 //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));
\r
269 if biniptemp.family = AF_INET then begin
\r
270 inAddr.InAddr.family:=AF_INET;
\r
271 inAddr.InAddr.port:=htons(strtointdef(port,0));
\r
272 inAddr.InAddr.addr:=biniptemp.ip;
\r
273 result := sizeof(tinetsockaddr);
\r
276 if biniptemp.family = AF_INET6 then begin
\r
277 inAddr.InAddr6.sin6_family:=AF_INET6;
\r
278 inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));
\r
279 inAddr.InAddr6.sin6_addr:=biniptemp.ip6;
\r
280 result := sizeof(tinetsockaddr6);
\r
283 raise esocketexception.create('unable to resolve address: '+addr);
\r
286 procedure tlsocket.connect;
\r
290 if state <> wsclosed then close;
\r
292 makeinaddrv(addr,port,inaddr);
\r
294 udp := uppercase(proto) = 'UDP';
\r
295 if udp then a := SOCK_DGRAM else a := SOCK_STREAM;
\r
296 a := Socket(inaddr.inaddr.family,a,0);
\r
298 //writeln(ord(inaddr.inaddr.family));
\r
299 if a = -1 then begin
\r
300 lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};
\r
301 raise esocketexception.create('unable to create socket');
\r
308 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
310 state := wsconnected;
\r
311 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
313 state :=wsconnecting;
\r
315 //writeln(inaddr.inaddr.port);
\r
316 winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
\r
318 sockets.Connect(fdhandlein,inADDR,getaddrsize);
\r
321 eventcore.rmasterset(fdhandlein,false);
\r
323 eventcore.wmasterclr(fdhandleout);
\r
325 eventcore.wmasterset(fdhandleout);
\r
329 on e: exception do begin
\r
331 raise; //reraise the exception
\r
336 procedure tlsocket.sendstr(const str : string);
\r
339 send(@str[1],length(str))
\r
341 inherited sendstr(str);
\r
345 function tlsocket.send(data:pointer;len:integer):integer;
\r
348 //writeln('sending to '+inttohex(inaddr.inaddr.addr,8));
\r
349 result := sendto(inaddr.inaddr,getaddrsize,data,len)
\r
351 //writeln('send result',result);
\r
352 //writeln('errno',errno);
\r
354 result := inherited send(data,len);
\r
359 function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
\r
362 result := myfdread(self.fdhandlein,buf^,bufsize);
\r
364 result := inherited receive(buf,bufsize);
\r
368 procedure tlsocket.bindsocket;
\r
371 inAddrtemp:TInetSockAddrV;
\r
372 inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
\r
373 inaddrtempsize:integer;
\r
376 if (localaddr <> '') or (localport <> '') then begin
\r
377 if localaddr = '' then begin
\r
379 if inaddr.inaddr.family = AF_INET6 then localaddr := '::' else
\r
381 localaddr := '0.0.0.0';
\r
383 //gethostbyname(localaddr,host);
\r
385 inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp);
\r
387 If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin
\r
389 lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};
\r
390 raise ESocketException.create('unable to bind, error '+inttostr(lasterror));
\r
395 on e: exception do begin
\r
397 raise; //reraise the exception
\r
402 procedure tlsocket.listen;
\r
409 if state <> wsclosed then close;
\r
410 udp := uppercase(proto) = 'UDP';
\r
411 if udp then socktype := SOCK_DGRAM else socktype := SOCK_STREAM;
\r
414 if addr = '' then begin
\r
416 if not v4listendefault then begin
\r
422 biniptemp := forwardlookup(addr,10);
\r
423 addr := ipbintostr(biniptemp);
\r
424 fdhandlein := socket(biniptemp.family,socktype,0);
\r
426 if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
\r
428 fdhandlein := socket(AF_INET,socktype,0);
\r
431 if fdhandlein = -1 then raise ESocketException.create('unable to create socket');
\r
432 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
\r
433 //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
\r
434 state := wsclosed; // then set this back as it was an undesired side effect of dup
\r
437 yes := $01010101; {Copied this from existing code. Value is empiric,
\r
438 but works. (yes=true<>0) }
\r
440 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
\r
441 raise ESocketException.create('unable to set socket options');
\r
448 if not udp then begin
\r
449 {!!! allow custom queue length? default 5}
\r
450 if listenqueue = 0 then listenqueue := 5;
\r
451 If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen');
\r
452 state := wsListening;
\r
455 SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
\r
457 state := wsconnected;
\r
460 if state = wsclosed then begin
\r
461 if fdhandlein >= 0 then begin
\r
462 {one *can* get here without fd -beware}
\r
463 eventcore.rmasterclr(fdhandlein);
\r
464 myfdclose(fdhandlein); // we musnt leak file discriptors
\r
465 eventcore.setfdreverse(fdhandlein,nil);
\r
469 eventcore.rmasterset(fdhandlein,true);
\r
471 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
\r
473 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
\r
476 function tlsocket.accept : longint;
\r
478 FromAddrSize : LongInt; // i don't realy know what to do with these at this
\r
479 FromAddr : TInetSockAddrV; // at this point time will tell :)
\r
482 FromAddrSize := Sizeof(FromAddr);
\r
484 result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
\r
486 result := sockets.accept(fdhandlein,fromaddr,fromaddrsize);
\r
488 //now we have accepted one request start monitoring for more again
\r
489 eventcore.rmasterset(fdhandlein,true);
\r
491 if result = -1 then raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
\r
492 if result > absoloutemaxs then begin
\r
495 raise esocketexception.create('file discriptor out of range');
\r
499 function tlsocket.sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer;
\r
501 destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute dest;
\r
503 result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen);
\r
506 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer;
\r
508 srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute src;
\r
510 result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);
\r
513 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
\r
515 tempbuf:array[0..receivebufsize-1] of byte;
\r
517 //writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger);
\r
518 if (state =wslistening) and readtrigger then begin
\r
519 { debugout('listening socket triggered on read');}
\r
520 eventcore.rmasterclr(fdhandlein);
\r
521 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
\r
523 if udp and readtrigger then begin
\r
524 if assigned(ondataAvailable) then ondataAvailable(self,0);
\r
528 if (state =wsconnecting) and writetrigger then begin
\r
529 // code for dealing with the reults of a non-blocking connect is
\r
531 // if just write is triggered it means connect suceeded
\r
532 // if both read and write are triggered it can mean 2 things
\r
533 // 1: connect ok and data availible
\r
535 // to find out which you must read from the socket and look for errors
\r
536 // there if we read successfully we drop through into the code for fireing
\r
538 if not readtrigger then begin
\r
539 state := wsconnected;
\r
540 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
542 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
\r
543 if numread <> -1 then begin
\r
544 state := wsconnected;
\r
545 if assigned(onsessionconnected) then onsessionconnected(self,0);
\r
546 //connectread := true;
\r
547 recvq.add(@tempbuf,numread);
\r
549 state := wsconnected;
\r
550 if assigned(onsessionconnected) then onsessionconnected(self,{$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
\r
551 { debugout('connect fail');}
\r
552 self.internalclose(0);
\r
553 recvq.del(maxlongint);
\r
555 // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
\r
556 // so we drop down into the processing for data availible
\r
558 if fdhandlein >= 0 then begin
\r
559 if state = wsconnected then begin
\r
560 eventcore.rmasterset(fdhandlein,false);
\r
562 eventcore.rmasterclr(fdhandlein);
\r
565 if fdhandleout >= 0 then begin
\r
566 if sendq.size = 0 then begin
\r
567 //don't clear the bit in fdswmaster if data is in the sendq
\r
568 eventcore.wmasterclr(fdhandleout);
\r
573 inherited handlefdtrigger(readtrigger,writetrigger);
\r
576 constructor tlsocket.Create(AOwner: TComponent);
\r
578 inherited create(aowner);
\r
579 closehandles := true;
\r
583 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
\r
585 addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
\r
587 result := {$ifdef win32}winsock{$else}sockets{$endif}.getpeername(self.fdhandlein,addrx,addrlen);
\r
590 procedure tlsocket.getxaddrbin(var binip:tbinip);
\r
592 addr:tinetsockaddrv;
\r
596 fillchar(addr,sizeof(addr),0);
\r
599 winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
\r
601 sockets.getsocketname(self.fdhandlein,addr,i);
\r
603 binip.family := addr.inaddr.family;
\r
605 if addr.inaddr6.sin6_family = AF_INET6 then begin
\r
606 binip.ip6 := addr.inaddr6.sin6_addr;
\r
610 binip.ip := addr.inaddr.addr;
\r
612 converttov4(binip);
\r
615 procedure tlsocket.getpeeraddrbin(var binip:tbinip);
\r
617 addr:tinetsockaddrv;
\r
621 fillchar(addr,sizeof(addr),0);
\r
623 winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
\r
625 sockets.getpeername(self.fdhandlein,addr,i);
\r
628 binip.family := addr.inaddr.family;
\r
630 if addr.inaddr6.sin6_family = AF_INET6 then begin
\r
631 binip.ip6 := addr.inaddr6.sin6_addr;
\r
635 binip.ip := addr.inaddr.addr;
\r
637 converttov4(binip);
\r
640 function tlsocket.getXaddr:string;
\r
644 getxaddrbin(biniptemp);
\r
645 result := ipbintostr(biniptemp);
\r
646 if result = '' then result := 'error';
\r
649 function tlsocket.getpeeraddr:string;
\r
653 getpeeraddrbin(biniptemp);
\r
654 result := ipbintostr(biniptemp);
\r
655 if result = '' then result := 'error';
\r
658 function tlsocket.getXport:string;
\r
660 addr:{$ifdef win32}winsock.tsockaddr{$else}tinetsockaddr{$endif};
\r
665 winsock.getsockname(self.fdhandlein,addr,i);
\r
666 i := htons(addr.sin_port);
\r
668 sockets.getsocketname(self.fdhandlein,addr,i);
\r
669 i := htons(addr.port);
\r
671 result := inttostr(i);
\r
674 function tlsocket.getpeerport:string;
\r
676 addr:{$ifdef win32}winsock.tsockaddr{$else}tinetsockaddr{$endif};
\r
681 winsock.getpeername(self.fdhandlein,addr,i);
\r
682 i := htons(addr.sin_port);
\r
684 sockets.getpeername(self.fdhandlein,addr,i);
\r
685 i := htons(addr.port);
\r
687 result := inttostr(i);
\r
691 procedure tlsocket.myfdclose(fd : integer);
\r
695 function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
\r
697 result := winsock.send(fd,(@buf)^,size,0);
\r
699 function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
\r
701 result := winsock.recv(fd,buf,size,0);
\r