/[lcore]/trunk/lsocket.pas
ViewVC logotype

Annotation of /trunk/lsocket.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 94 - (hide annotations)
Mon Feb 21 21:40:05 2011 UTC (9 years ago) by beware
File size: 25783 byte(s)
eliminated a lot of hints and warnings
1 plugwash 1 {lsocket.pas}
2    
3     {socket code by plugwash}
4    
5 beware 17 { Copyright (C) 2005 Bas Steendijk and Peter Green
6     For conditions of distribution and use, see copyright notice in zlib_license.txt
7     which is included in the package
8     ----------------------------------------------------------------------------- }
9 plugwash 1 {
10     changes by plugwash (20030728)
11     * created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it
12     * changed tlasio to tlasio
13     * split fdhandle into fdhandlein and fdhandleout
14     * i now use fdsrmaster and fdswmaster instead of rebuilding the lists every loop
15     * split lsocket.pas into lsocket.pas and lcore.pas
16    
17    
18     changes by beware (20030903)
19     * added getxaddr, getxport (local addr, port, as string)
20     * added getpeername, remote addr+port as binary
21     * added htons and htonl functions (endian swap, same interface as windows API)
22    
23     beware (20030905)
24     * if connect failed (conn refused) set state to connected and call internalclose, to get closed handler (instead of fdclose)
25     * (lcore) if closing the fd's in internalcose, set fd's to -1 because closing an fd makes it invalid
26    
27     beware (20030927)
28     * fixed: on connect failed, tried to close fdhandle's which were already set to -1, added check
29    
30     beware (20031017)
31     * added getpeeraddr, getpeerport, remote addr+port as string
32     }
33    
34    
35     unit lsocket;
36 beware 17 {$ifdef fpc}
37     {$mode delphi}
38     {$endif}
39    
40     {$include lcoreconfig.inc}
41    
42 plugwash 1 interface
43     uses
44     sysutils,
45 beware 17 {$ifdef win32}
46     windows,winsock,
47 plugwash 1 {$else}
48 beware 17
49     {$ifdef VER1_0}
50     linux,
51     {$else}
52     baseunix,unix,unixutil,
53     {$endif}
54     sockets,
55 plugwash 1 {$endif}
56 beware 17 classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;
57    
58     {$ifdef ipv6}
59     const
60     v4listendefault:boolean=false;
61     {$endif}
62    
63    
64 plugwash 1 type
65     sunB = packed record
66     s_b1, s_b2, s_b3, s_b4: byte;
67     end;
68    
69     SunW = packed record
70     s_w1, s_w2: word;
71     end;
72    
73     TInAddr = packed record
74     case integer of
75     0: (S_un_b: SunB);
76     1: (S_un_w: SunW);
77     2: (S_addr: cardinal);
78     end;
79    
80     type
81     TLsocket = class(tlasio)
82     public
83     //a: string;
84    
85     inAddr : TInetSockAddrV;
86 beware 17
87     biniplist:tbiniplist;
88     trymoreips:boolean;
89     currentip:integer;
90     connecttimeout:tltimer;
91    
92 plugwash 1 { inAddrSize:integer;}
93    
94     //host : THostentry ;
95    
96     //mainthread : boolean ; //for debuggin only
97 zipplet 79 addr:thostname;
98     port:ansistring;
99     localaddr:thostname;
100     localport:ansistring;
101     proto:ansistring;
102 beware 21 udp,dgram:boolean;
103 plugwash 1 listenqueue:integer;
104 beware 86
105     onconnecttryip:procedure(sender:tobject; const ip:tbinip) of object;
106    
107 beware 17 {$ifdef secondlistener}
108     secondlistener:tlsocket;
109     lastsessionfromsecond:boolean;
110     procedure secondaccepthandler(sender:tobject;error:word);
111     procedure internalclose(error:word);override;
112     {$endif}
113 plugwash 1 function getaddrsize:integer;
114     procedure connect; virtual;
115 beware 17 procedure realconnect;
116 plugwash 1 procedure bindsocket;
117     procedure listen;
118     function accept : longint;
119 beware 17 function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual;
120     function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual;
121    
122 plugwash 1 procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;
123     function send(data:pointer;len:integer):integer;override;
124 zipplet 79 procedure sendstr(const str : tbufferstring);override;
125 plugwash 1 function Receive(Buf:Pointer;BufSize:integer):integer; override;
126     function getpeername(var addr:tsockaddrin;addrlen:integer):integer; virtual;
127     procedure getXaddrbin(var binip:tbinip); virtual;
128     procedure getpeeraddrbin(var binip:tbinip); virtual;
129 zipplet 79 function getXaddr:thostname; virtual;
130     function getpeeraddr:thostname; virtual;
131     function getXport:ansistring; virtual;
132     function getpeerport:ansistring; virtual;
133 plugwash 1 constructor Create(AOwner: TComponent); override;
134 plugwash 32
135     //this one has to be kept public for now because lcorewsaasyncselect calls it
136     procedure connectionfailedhandler(error:word);
137 beware 94
138     {public in tlasio, and can't be private in both places, so should be public here.
139     fixes delphi warning --beware}
140     {$ifdef win32}
141     procedure myfdclose(fd : integer); override;
142     function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
143     function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
144     {$endif}
145    
146 plugwash 32 private
147 plugwash 76 {$ifdef ipv6}
148     isv6socket : boolean; //identifies if the socket is v6, set by bindsocket
149     {$endif}
150 plugwash 32 procedure taskcallconnectionfailedhandler(wparam,lparam : longint);
151    
152     procedure connecttimeouthandler(sender:tobject);
153     procedure connectsuccesshandler;
154 plugwash 1 end;
155     tsocket=longint; // for compatibility with twsocket
156    
157     twsocket=tlsocket; {easy}
158    
159 beware 2
160 plugwash 1 const
161     TCP_NODELAY=1;
162     IPPROTO_TCP=6;
163    
164     implementation
165     {$include unixstuff.inc}
166    
167    
168     function tlsocket.getaddrsize:integer;
169     begin
170 beware 17 result := inaddrsize(inaddr);
171 plugwash 1 end;
172    
173 plugwash 60 //I used to use the system versions of these from within lsocket (which has
174     //functions whose name clashes with them) by using sockets.* and but I can't do
175     //that anymore since in some cases connect is now provided by unixstuff.inc
176     //hence these wrapper functions --plugwash
177     {$ifndef win32}
178     function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
179     begin
180     result := connect(sock,addr,addrlen);
181     end;
182     function system_SendTo(Sock: LongInt; const Buf;BufLen: LongInt;Flags: LongInt;var Addr;AddrLen: LongInt):LongInt;
183     begin
184     result := sendto(sock,buf,buflen,flags,addr,addrlen);
185     end;
186     function system_getpeername(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
187     begin
188     result := getpeername(sock,addr,addrlen);
189     end;
190     function system_listen(Sock: LongInt; MaxConnect: LongInt):Boolean;
191     begin
192     result := listen(sock,maxconnect);
193     end;
194     function system_Accept(Sock: LongInt;var Addr;var Addrlen: LongInt):LongInt;
195     begin
196     result := accept(sock,addr,addrlen);
197     end;
198     {$endif}
199 plugwash 1
200 beware 17 procedure tlsocket.realconnect;
201 plugwash 1 var
202 beware 21 a,b:integer;
203 beware 86 iptemp:tbinip;
204 beware 16 begin
205 beware 86 iptemp := biniplist_get(biniplist,currentip);
206 plugwash 32 //writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);
207 beware 86 if assigned(onconnecttryip) then onconnecttryip(self,iptemp);
208     makeinaddrv(iptemp,port,inaddr);
209 beware 17 inc(currentip);
210     if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;
211 beware 21
212     udp := false;
213     if (uppercase(proto) = 'UDP') then begin
214     b := IPPROTO_UDP;
215     a := SOCK_DGRAM;
216     udp := true;
217     dgram := true;
218 plugwash 32 end else if (uppercase(proto) = 'TCP') or (uppercase(proto) = '') then begin
219 beware 21 b := IPPROTO_TCP;
220     a := SOCK_STREAM;
221     dgram := false;
222 plugwash 32 end else if (uppercase(proto) = 'ICMP') or (strtointdef(proto,256) < 256) then begin
223 beware 21 b := strtointdef(proto,IPPROTO_ICMP);
224     a := SOCK_RAW;
225     dgram := true;
226 plugwash 32 end else begin
227     raise ESocketException.create('unrecognised protocol');
228 beware 21 end;
229    
230     a := Socket(inaddr.inaddr.family,a,b);
231 beware 17 //writeln(ord(inaddr.inaddr.family));
232 plugwash 1 if a = -1 then begin
233 plugwash 32 //unable to create socket, fire an error event (better to use an error event
234     //to avoid poor interaction with multilistener stuff.
235     //a socket value of -2 is a special value to say there is no socket but
236     //we want internalclose to act as if there was
237     fdhandlein := -2;
238     fdhandleout := -2;
239     tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);
240     exit;
241 plugwash 1 end;
242     try
243     dup(a);
244     bindsocket;
245 beware 21 if dgram then begin
246 beware 17 {$ifndef win32}
247     SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
248 zipplet 25 {$else}
249     SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
250 beware 17 {$endif}
251 plugwash 1 state := wsconnected;
252     if assigned(onsessionconnected) then onsessionconnected(self,0);
253 beware 17
254     eventcore.rmasterset(fdhandlein,false);
255     eventcore.wmasterclr(fdhandleout);
256 plugwash 1 end else begin
257     state :=wsconnecting;
258 beware 17 {$ifdef win32}
259     //writeln(inaddr.inaddr.port);
260     winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
261     {$else}
262 plugwash 60 system_Connect(fdhandlein,inADDR,getaddrsize);
263 beware 17 {$endif}
264     eventcore.rmasterset(fdhandlein,false);
265     eventcore.wmasterset(fdhandleout);
266     if trymoreips then connecttimeout.enabled := true;
267 plugwash 1 end;
268     //sendq := '';
269     except
270     on e: exception do begin
271     fdcleanup;
272     raise; //reraise the exception
273     end;
274     end;
275 beware 17
276 plugwash 1 end;
277    
278 beware 17 procedure tlsocket.connecttimeouthandler(sender:tobject);
279     begin
280     connecttimeout.enabled := false;
281     destroying := true; //hack to not cause handler to trigger
282     internalclose(0);
283     destroying := false;
284     realconnect;
285     end;
286    
287 plugwash 60
288    
289    
290 beware 17 procedure tlsocket.connect;
291     begin
292     if state <> wsclosed then close;
293     //prevtime := 0;
294     if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);
295     if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);
296    
297     //makeinaddrv(addr,port,inaddr);
298    
299     currentip := 0;
300     if not assigned(connecttimeout) then begin
301     connecttimeout := tltimer.create(self);
302     connecttimeout.ontimer := connecttimeouthandler;
303 beware 87 connecttimeout.interval := 5000;
304 beware 17 connecttimeout.enabled := false;
305     end;
306     realconnect;
307     end;
308    
309 zipplet 79 procedure tlsocket.sendstr(const str : tbufferstring);
310 plugwash 1 begin
311 beware 21 if dgram then begin
312 plugwash 1 send(@str[1],length(str))
313     end else begin
314     inherited sendstr(str);
315     end;
316     end;
317    
318     function tlsocket.send(data:pointer;len:integer):integer;
319     begin
320 beware 21 if dgram then begin
321 beware 17 // writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');
322     result := sendto(inaddr,getaddrsize,data,len);
323    
324     // writeln('send result ',result);
325     // writeln('errno',errno);
326 plugwash 1 end else begin
327     result := inherited send(data,len);
328     end;
329     end;
330    
331    
332     function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;
333     begin
334 beware 21 if dgram then begin
335 beware 17 {$ifdef secondlistener}
336     if lastsessionfromsecond then begin
337     result := secondlistener.receive(buf,bufsize);
338     lastsessionfromsecond := false;
339     end else
340     {$endif}
341     result := myfdread(self.fdhandlein,buf^,bufsize);
342 plugwash 1 end else begin
343     result := inherited receive(buf,bufsize);
344     end;
345     end;
346    
347     procedure tlsocket.bindsocket;
348     var
349     inAddrtemp:TInetSockAddrV;
350 beware 17 inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
351 plugwash 1 inaddrtempsize:integer;
352     begin
353     try
354     if (localaddr <> '') or (localport <> '') then begin
355     if localaddr = '' then begin
356     {$ifdef ipv6}
357     if inaddr.inaddr.family = AF_INET6 then localaddr := '::' else
358     {$endif}
359     localaddr := '0.0.0.0';
360     end;
361     //gethostbyname(localaddr,host);
362 beware 17 inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);
363 plugwash 76 {$ifdef ipv6}
364     isv6socket := (inaddrtemp.inaddr.family = AF_INET6);
365     {$endif}
366 beware 17 If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin
367 plugwash 1 state := wsclosed;
368 beware 17 lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};
369 beware 20 raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));
370 plugwash 1 end;
371     state := wsbound;
372     end;
373     except
374     on e: exception do begin
375     fdcleanup;
376     raise; //reraise the exception
377     end;
378     end;
379     end;
380    
381     procedure tlsocket.listen;
382     var
383 beware 94 {$ifndef win32}
384 plugwash 73 yes,no:longint;
385 beware 94 {$endif}
386 plugwash 1 socktype:integer;
387     biniptemp:tbinip;
388 zipplet 79 origaddr:thostname;
389 plugwash 1 begin
390     if state <> wsclosed then close;
391     udp := uppercase(proto) = 'UDP';
392 beware 24 if udp then begin
393     socktype := SOCK_DGRAM;
394     dgram := true;
395     end else socktype := SOCK_STREAM;
396 plugwash 1 origaddr := addr;
397    
398     if addr = '' then begin
399     {$ifdef ipv6}
400 plugwash 75 //writeln('ipv6 is defined');
401 plugwash 1 if not v4listendefault then begin
402 plugwash 75 //writeln('setting addr to ::');
403 plugwash 1 addr := '::';
404     end else
405     {$endif}
406     addr := '0.0.0.0';
407     end;
408 beware 17 if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);
409 plugwash 1 addr := ipbintostr(biniptemp);
410 plugwash 75 //writeln('after ipbintostr call addr =',addr);
411     //writeln('biniptemp.family =',biniptemp.family);
412     //writeln('AF_INET6=',AF_INET6);
413     //writeln('PF_INET6=',PF_INET6);
414     //writeln('AF_INET=',AF_INET);
415     //writeln('PF_INET=',PF_INET);
416    
417 plugwash 1 fdhandlein := socket(biniptemp.family,socktype,0);
418     {$ifdef ipv6}
419     if (addr = '::') and (origaddr = '') and (fdhandlein < 0) then begin
420 zipplet 79 {writeln('failed to create an IPV6 socket with error ',socketerror,'. trying to create an IPV4 one instead');}
421 plugwash 1 addr := '0.0.0.0';
422 plugwash 75 fdhandlein := socket(PF_INET,socktype,0);
423 plugwash 1 end;
424     {$endif}
425 beware 17
426 plugwash 32 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});
427 beware 17 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
428     //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
429 plugwash 1 state := wsclosed; // then set this back as it was an undesired side effect of dup
430    
431     try
432 beware 94 {$ifndef win32}
433     yes := $01010101; {Copied this from existing code. Value is empiric,
434 plugwash 1 but works. (yes=true<>0) }
435 beware 94 no := 0;
436    
437 beware 17 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
438 plugwash 75 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
439 beware 17 end;
440 plugwash 75 //writeln('addr=',addr);
441     //writeln('setting IPV6_V6ONLY option to 0');
442     //allow v4 connections on v6 sockets
443     if biniptemp.family = af_inet6 then begin
444     if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
445     writeln(IPPROTO_IPV6);
446     writeln(IPV6_V6ONLY);
447     raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
448    
449     end;
450 plugwash 73 end;
451 beware 17 {$endif}
452 plugwash 1 localaddr := addr;
453     localport := port;
454     bindsocket;
455    
456     if not udp then begin
457     {!!! allow custom queue length? default 5}
458     if listenqueue = 0 then listenqueue := 5;
459 plugwash 60 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
460 zipplet 25 esocketexception.create('unable to listen');
461 plugwash 1 state := wsListening;
462     end else begin
463 beware 17 {$ifndef win32}
464     SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
465 zipplet 25 {$else}
466     SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
467 beware 17 {$endif}
468 plugwash 1 state := wsconnected;
469     end;
470 beware 17
471     {$ifdef secondlistener}
472     //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
473     if addr = '::' then begin
474     secondlistener := tlsocket.create(nil);
475     secondlistener.proto := proto;
476     secondlistener.addr := '0.0.0.0';
477     secondlistener.port := port;
478     if udp then begin
479     secondlistener.ondataavailable := secondaccepthandler;
480     end else begin
481     secondlistener.onsessionAvailable := secondaccepthandler;
482     end;
483     try
484     secondlistener.listen;
485     except
486     secondlistener.destroy;
487     secondlistener := nil;
488     end;
489     end;
490     {$endif}
491 plugwash 1 finally
492     if state = wsclosed then begin
493     if fdhandlein >= 0 then begin
494     {one *can* get here without fd -beware}
495 beware 17 eventcore.rmasterclr(fdhandlein);
496     myfdclose(fdhandlein); // we musnt leak file discriptors
497     eventcore.setfdreverse(fdhandlein,nil);
498 plugwash 1 fdhandlein := -1;
499     end;
500     end else begin
501 beware 17 eventcore.rmasterset(fdhandlein,not udp);
502 plugwash 1 end;
503 beware 17 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
504 plugwash 1 end;
505 beware 17 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
506 plugwash 1 end;
507    
508 beware 17 {$ifdef secondlistener}
509     procedure tlsocket.internalclose(error:word);
510     begin
511     if assigned(secondlistener) then begin
512     secondlistener.destroy;
513     secondlistener := nil;
514     end;
515     inherited internalclose(error);
516     end;
517    
518     procedure tlsocket.secondaccepthandler;
519     begin
520     lastsessionfromsecond := true;
521     if udp then begin
522     ondataavailable(self,error);
523     end else begin
524     if assigned(onsessionavailable) then onsessionavailable(self,error);
525     end;
526     end;
527     {$endif}
528    
529 plugwash 1 function tlsocket.accept : longint;
530     var
531     FromAddrSize : LongInt; // i don't realy know what to do with these at this
532 beware 17 FromAddr : TInetSockAddrV; // at this point time will tell :)
533     a:integer;
534 plugwash 1 begin
535 beware 17 {$ifdef secondlistener}
536     if (lastsessionfromsecond) then begin
537     lastsessionfromsecond := false;
538     result := secondlistener.accept;
539     exit;
540     end;
541     {$endif}
542    
543 plugwash 1 FromAddrSize := Sizeof(FromAddr);
544 beware 17 {$ifdef win32}
545     result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
546     {$else}
547 plugwash 60 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
548 beware 17 {$endif}
549     //now we have accepted one request start monitoring for more again
550     eventcore.rmasterset(fdhandlein,true);
551    
552     if result = -1 then begin
553     raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
554     end;
555 plugwash 1 if result > absoloutemaxs then begin
556 beware 17 myfdclose(result);
557     a := result;
558 beware 94 { result := -1;}
559 beware 17 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
560 plugwash 1 end;
561     end;
562    
563 plugwash 60
564 beware 17 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
565     var
566 plugwash 75 {$ifdef ipv6}
567     realdest : tinetsockaddrv;
568     biniptemp : tbinip;
569     {$endif}
570     destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
571    
572 plugwash 1 begin
573 beware 17 {$ifdef secondlistener}
574 plugwash 75 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
575     result := secondlistener.sendto(dest,destlen,data,len);
576     exit;
577     end;
578 beware 17 {$endif}
579 plugwash 75 {$ifdef ipv6}
580     if isv6socket then begin
581     biniptemp := inaddrvtobinip(dest);
582     converttov6(biniptemp);
583     destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
584     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
585     end else begin
586     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
587     end;
588     {$else}
589     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
590     {$endif}
591    
592     result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
593 plugwash 1 end;
594    
595 plugwash 60
596 beware 17 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
597     var
598 beware 20 tempsrc:TInetSockAddrV;
599     tempsrclen:integer;
600     srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
601     biniptemp:tbinip;
602 plugwash 1 begin
603 beware 17 {$ifdef secondlistener}
604     if assigned(secondlistener) then if lastsessionfromsecond then begin
605     lastsessionfromsecond := false;
606     result := secondlistener.receivefrom(data,len,src,srclen);
607     exit;
608     end;
609     {$endif}
610 beware 20 tempsrclen := sizeof(tempsrc);
611 plugwash 60 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
612 beware 20
613     {$ifdef ipv6}
614     biniptemp := inaddrvtobinip(tempsrc);
615     if needconverttov4(biniptemp) then begin
616     converttov4(biniptemp);
617     tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
618     end;
619     {$endif}
620    
621     move(tempsrc,src,srclen);
622     srclen := tempsrclen;
623 plugwash 1 end;
624    
625 plugwash 32 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
626     begin
627     connectionfailedhandler(wparam);
628     end;
629    
630 beware 17 procedure tlsocket.connectionfailedhandler(error:word);
631     begin
632     if trymoreips then begin
633     // writeln('failed with error ',error);
634     connecttimeout.enabled := false;
635     destroying := true;
636     state := wsconnected;
637     self.internalclose(0);
638     destroying := false;
639     realconnect;
640     end else begin
641     state := wsconnected;
642     if assigned(onsessionconnected) then onsessionconnected(self,error);
643     self.internalclose(0);
644     recvq.del(maxlongint);
645     end;
646     end;
647    
648     procedure tlsocket.connectsuccesshandler;
649     begin
650     trymoreips := false;
651     connecttimeout.enabled := false;
652     if assigned(onsessionconnected) then onsessionconnected(self,0);
653     end;
654    
655    
656 plugwash 1 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
657     var
658     tempbuf:array[0..receivebufsize-1] of byte;
659     begin
660 beware 17 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
661 plugwash 1 if (state =wslistening) and readtrigger then begin
662     { debugout('listening socket triggered on read');}
663 beware 17 eventcore.rmasterclr(fdhandlein);
664 plugwash 1 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
665     end;
666 beware 21 if dgram and readtrigger then begin
667 plugwash 1 if assigned(ondataAvailable) then ondataAvailable(self,0);
668     {!!!test}
669     exit;
670     end;
671     if (state =wsconnecting) and writetrigger then begin
672     // code for dealing with the reults of a non-blocking connect is
673     // rather complex
674     // if just write is triggered it means connect suceeded
675 beware 17 // if both read and write are triggered it can mean 2 things
676 plugwash 1 // 1: connect ok and data availible
677     // 2: connect fail
678     // to find out which you must read from the socket and look for errors
679     // there if we read successfully we drop through into the code for fireing
680     // the read event
681     if not readtrigger then begin
682     state := wsconnected;
683 beware 17 connectsuccesshandler;
684 plugwash 1 end else begin
685 beware 17 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
686 plugwash 1 if numread <> -1 then begin
687     state := wsconnected;
688 beware 17 connectsuccesshandler;
689 plugwash 1 //connectread := true;
690     recvq.add(@tempbuf,numread);
691     end else begin
692 beware 17 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
693     exit;
694 plugwash 1 end;
695     // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
696     // so we drop down into the processing for data availible
697     end;
698     if fdhandlein >= 0 then begin
699     if state = wsconnected then begin
700 beware 17 eventcore.rmasterset(fdhandlein,false);
701 plugwash 1 end else begin
702 beware 17 eventcore.rmasterclr(fdhandlein);
703 plugwash 1 end;
704     end;
705     if fdhandleout >= 0 then begin
706     if sendq.size = 0 then begin
707     //don't clear the bit in fdswmaster if data is in the sendq
708 beware 17 eventcore.wmasterclr(fdhandleout);
709 plugwash 1 end;
710     end;
711    
712     end;
713     inherited handlefdtrigger(readtrigger,writetrigger);
714     end;
715    
716     constructor tlsocket.Create(AOwner: TComponent);
717     begin
718     inherited create(aowner);
719     closehandles := true;
720 beware 17 trymoreips := true;
721 plugwash 1 end;
722    
723    
724 plugwash 60
725 plugwash 1 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
726 beware 17 var
727     addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
728 plugwash 1 begin
729 plugwash 60 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
730 plugwash 1 end;
731    
732     procedure tlsocket.getxaddrbin(var binip:tbinip);
733     var
734     addr:tinetsockaddrv;
735     i:integer;
736     begin
737     i := sizeof(addr);
738     fillchar(addr,sizeof(addr),0);
739    
740 beware 17 {$ifdef win32}
741     winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
742     {$else}
743 plugwash 73 getsocketname(self.fdhandlein,addr,i);
744 beware 17 {$endif}
745 beware 20 binip := inaddrvtobinip(addr);
746 plugwash 1 converttov4(binip);
747     end;
748    
749     procedure tlsocket.getpeeraddrbin(var binip:tbinip);
750     var
751     addr:tinetsockaddrv;
752     i:integer;
753     begin
754     i := sizeof(addr);
755     fillchar(addr,sizeof(addr),0);
756 beware 17 {$ifdef win32}
757     winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
758     {$else}
759 plugwash 60 system_getpeername(self.fdhandlein,addr,i);
760 beware 17 {$endif}
761 plugwash 1
762 beware 20 binip := inaddrvtobinip(addr);
763 plugwash 1 converttov4(binip);
764     end;
765    
766 zipplet 79 function tlsocket.getXaddr:thostname;
767 plugwash 1 var
768     biniptemp:tbinip;
769     begin
770     getxaddrbin(biniptemp);
771     result := ipbintostr(biniptemp);
772     if result = '' then result := 'error';
773     end;
774    
775 zipplet 79 function tlsocket.getpeeraddr:thostname;
776 plugwash 1 var
777     biniptemp:tbinip;
778     begin
779     getpeeraddrbin(biniptemp);
780     result := ipbintostr(biniptemp);
781     if result = '' then result := 'error';
782     end;
783    
784 zipplet 79 function tlsocket.getXport:ansistring;
785 plugwash 1 var
786 beware 17 addr:tinetsockaddrv;
787 plugwash 1 i:integer;
788     begin
789     i := sizeof(addr);
790 beware 17 {$ifdef win32}
791     winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
792    
793     {$else}
794 plugwash 73 getsocketname(self.fdhandlein,addr,i);
795 beware 17
796     {$endif}
797     result := inttostr(htons(addr.InAddr.port));
798 plugwash 1 end;
799    
800 zipplet 79 function tlsocket.getpeerport:ansistring;
801 plugwash 1 var
802 beware 17 addr:tinetsockaddrv;
803 plugwash 1 i:integer;
804     begin
805     i := sizeof(addr);
806 beware 17 {$ifdef win32}
807     winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
808    
809     {$else}
810 plugwash 60 system_getpeername(self.fdhandlein,addr,i);
811 beware 17
812     {$endif}
813     result := inttostr(htons(addr.InAddr.port));
814 plugwash 1 end;
815    
816 beware 17 {$ifdef win32}
817     procedure tlsocket.myfdclose(fd : integer);
818     begin
819     closesocket(fd);
820     end;
821     function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
822     begin
823     result := winsock.send(fd,(@buf)^,size,0);
824     end;
825     function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
826     begin
827     result := winsock.recv(fd,buf,size,0);
828     end;
829     {$endif}
830 beware 18
831 plugwash 1 end.
832 beware 17

Properties

Name Value
svn:eol-style CRLF
svn:executable

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.22