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

Annotation of /trunk/lsocket.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations)
Tue Oct 11 08:08:48 2011 UTC (8 years, 5 months ago) by beware
File size: 25820 byte(s)
fixed listen breaking on kernel compiled without ipv6
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 beware 103 biniptemp := ipstrtobinf(addr);
423 plugwash 75 fdhandlein := socket(PF_INET,socktype,0);
424 plugwash 1 end;
425     {$endif}
426 beware 17
427 plugwash 32 if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});
428 beware 17 dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
429     //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
430 plugwash 1 state := wsclosed; // then set this back as it was an undesired side effect of dup
431    
432     try
433 beware 94 {$ifndef win32}
434     yes := $01010101; {Copied this from existing code. Value is empiric,
435 plugwash 1 but works. (yes=true<>0) }
436 beware 94 no := 0;
437    
438 beware 17 if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin
439 plugwash 75 raise ESocketException.create('unable to set SO_REUSEADDR socket option');
440 beware 17 end;
441 plugwash 75 //writeln('addr=',addr);
442     //writeln('setting IPV6_V6ONLY option to 0');
443     //allow v4 connections on v6 sockets
444     if biniptemp.family = af_inet6 then begin
445     if SetSocketOptions(fdhandlein, IPPROTO_IPV6,IPV6_V6ONLY,no,sizeof(no))=-1 then begin
446     writeln(IPPROTO_IPV6);
447     writeln(IPV6_V6ONLY);
448     raise ESocketException.create('unable to set IPV6_V6ONLY socket option error='+inttostr(socketerror));
449    
450     end;
451 plugwash 73 end;
452 beware 17 {$endif}
453 plugwash 1 localaddr := addr;
454     localport := port;
455     bindsocket;
456    
457     if not udp then begin
458     {!!! allow custom queue length? default 5}
459     if listenqueue = 0 then listenqueue := 5;
460 plugwash 60 If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise
461 zipplet 25 esocketexception.create('unable to listen');
462 plugwash 1 state := wsListening;
463     end else begin
464 beware 17 {$ifndef win32}
465     SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
466 zipplet 25 {$else}
467     SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
468 beware 17 {$endif}
469 plugwash 1 state := wsconnected;
470     end;
471 beware 17
472     {$ifdef secondlistener}
473     //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that
474     if addr = '::' then begin
475     secondlistener := tlsocket.create(nil);
476     secondlistener.proto := proto;
477     secondlistener.addr := '0.0.0.0';
478     secondlistener.port := port;
479     if udp then begin
480     secondlistener.ondataavailable := secondaccepthandler;
481     end else begin
482     secondlistener.onsessionAvailable := secondaccepthandler;
483     end;
484     try
485     secondlistener.listen;
486     except
487     secondlistener.destroy;
488     secondlistener := nil;
489     end;
490     end;
491     {$endif}
492 plugwash 1 finally
493     if state = wsclosed then begin
494     if fdhandlein >= 0 then begin
495     {one *can* get here without fd -beware}
496 beware 17 eventcore.rmasterclr(fdhandlein);
497     myfdclose(fdhandlein); // we musnt leak file discriptors
498     eventcore.setfdreverse(fdhandlein,nil);
499 plugwash 1 fdhandlein := -1;
500     end;
501     end else begin
502 beware 17 eventcore.rmasterset(fdhandlein,not udp);
503 plugwash 1 end;
504 beware 17 if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);
505 plugwash 1 end;
506 beware 17 //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);
507 plugwash 1 end;
508    
509 beware 17 {$ifdef secondlistener}
510     procedure tlsocket.internalclose(error:word);
511     begin
512     if assigned(secondlistener) then begin
513     secondlistener.destroy;
514     secondlistener := nil;
515     end;
516     inherited internalclose(error);
517     end;
518    
519     procedure tlsocket.secondaccepthandler;
520     begin
521     lastsessionfromsecond := true;
522     if udp then begin
523     ondataavailable(self,error);
524     end else begin
525     if assigned(onsessionavailable) then onsessionavailable(self,error);
526     end;
527     end;
528     {$endif}
529    
530 plugwash 1 function tlsocket.accept : longint;
531     var
532     FromAddrSize : LongInt; // i don't realy know what to do with these at this
533 beware 17 FromAddr : TInetSockAddrV; // at this point time will tell :)
534     a:integer;
535 plugwash 1 begin
536 beware 17 {$ifdef secondlistener}
537     if (lastsessionfromsecond) then begin
538     lastsessionfromsecond := false;
539     result := secondlistener.accept;
540     exit;
541     end;
542     {$endif}
543    
544 plugwash 1 FromAddrSize := Sizeof(FromAddr);
545 beware 17 {$ifdef win32}
546     result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
547     {$else}
548 plugwash 60 result := system_accept(fdhandlein,fromaddr,fromaddrsize);
549 beware 17 {$endif}
550     //now we have accepted one request start monitoring for more again
551     eventcore.rmasterset(fdhandlein,true);
552    
553     if result = -1 then begin
554     raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');
555     end;
556 plugwash 1 if result > absoloutemaxs then begin
557 beware 17 myfdclose(result);
558     a := result;
559 beware 94 { result := -1;}
560 beware 17 raise esocketexception.create('file discriptor out of range: '+inttostr(a));
561 plugwash 1 end;
562     end;
563    
564 plugwash 60
565 beware 17 function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;
566     var
567 plugwash 75 {$ifdef ipv6}
568     realdest : tinetsockaddrv;
569     biniptemp : tbinip;
570     {$endif}
571     destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
572    
573 plugwash 1 begin
574 beware 17 {$ifdef secondlistener}
575 plugwash 75 if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin
576     result := secondlistener.sendto(dest,destlen,data,len);
577     exit;
578     end;
579 beware 17 {$endif}
580 plugwash 75 {$ifdef ipv6}
581     if isv6socket then begin
582     biniptemp := inaddrvtobinip(dest);
583     converttov6(biniptemp);
584     destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
585     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
586     end else begin
587     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
588     end;
589     {$else}
590     destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
591     {$endif}
592    
593     result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);
594 plugwash 1 end;
595    
596 plugwash 60
597 beware 17 function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;
598     var
599 beware 20 tempsrc:TInetSockAddrV;
600     tempsrclen:integer;
601     srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
602     biniptemp:tbinip;
603 plugwash 1 begin
604 beware 17 {$ifdef secondlistener}
605     if assigned(secondlistener) then if lastsessionfromsecond then begin
606     lastsessionfromsecond := false;
607     result := secondlistener.receivefrom(data,len,src,srclen);
608     exit;
609     end;
610     {$endif}
611 beware 20 tempsrclen := sizeof(tempsrc);
612 plugwash 60 result := recvfrom(self.fdhandlein,data^,len,0,srcx,tempsrclen);
613 beware 20
614     {$ifdef ipv6}
615     biniptemp := inaddrvtobinip(tempsrc);
616     if needconverttov4(biniptemp) then begin
617     converttov4(biniptemp);
618     tempsrclen := makeinaddrv(biniptemp,inttostr(ntohs(tempsrc.InAddr.port)),tempsrc);
619     end;
620     {$endif}
621    
622     move(tempsrc,src,srclen);
623     srclen := tempsrclen;
624 plugwash 1 end;
625    
626 plugwash 32 procedure tlsocket.taskcallconnectionfailedhandler(wparam,lparam : longint);
627     begin
628     connectionfailedhandler(wparam);
629     end;
630    
631 beware 17 procedure tlsocket.connectionfailedhandler(error:word);
632     begin
633     if trymoreips then begin
634     // writeln('failed with error ',error);
635     connecttimeout.enabled := false;
636     destroying := true;
637     state := wsconnected;
638     self.internalclose(0);
639     destroying := false;
640     realconnect;
641     end else begin
642     state := wsconnected;
643     if assigned(onsessionconnected) then onsessionconnected(self,error);
644     self.internalclose(0);
645     recvq.del(maxlongint);
646     end;
647     end;
648    
649     procedure tlsocket.connectsuccesshandler;
650     begin
651     trymoreips := false;
652     connecttimeout.enabled := false;
653     if assigned(onsessionconnected) then onsessionconnected(self,0);
654     end;
655    
656    
657 plugwash 1 procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);
658     var
659     tempbuf:array[0..receivebufsize-1] of byte;
660     begin
661 beware 17 // writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));
662 plugwash 1 if (state =wslistening) and readtrigger then begin
663     { debugout('listening socket triggered on read');}
664 beware 17 eventcore.rmasterclr(fdhandlein);
665 plugwash 1 if assigned(onsessionAvailable) then onsessionAvailable(self,0);
666     end;
667 beware 21 if dgram and readtrigger then begin
668 plugwash 1 if assigned(ondataAvailable) then ondataAvailable(self,0);
669     {!!!test}
670     exit;
671     end;
672     if (state =wsconnecting) and writetrigger then begin
673     // code for dealing with the reults of a non-blocking connect is
674     // rather complex
675     // if just write is triggered it means connect suceeded
676 beware 17 // if both read and write are triggered it can mean 2 things
677 plugwash 1 // 1: connect ok and data availible
678     // 2: connect fail
679     // to find out which you must read from the socket and look for errors
680     // there if we read successfully we drop through into the code for fireing
681     // the read event
682     if not readtrigger then begin
683     state := wsconnected;
684 beware 17 connectsuccesshandler;
685 plugwash 1 end else begin
686 beware 17 numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));
687 plugwash 1 if numread <> -1 then begin
688     state := wsconnected;
689 beware 17 connectsuccesshandler;
690 plugwash 1 //connectread := true;
691     recvq.add(@tempbuf,numread);
692     end else begin
693 beware 17 connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});
694     exit;
695 plugwash 1 end;
696     // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
697     // so we drop down into the processing for data availible
698     end;
699     if fdhandlein >= 0 then begin
700     if state = wsconnected then begin
701 beware 17 eventcore.rmasterset(fdhandlein,false);
702 plugwash 1 end else begin
703 beware 17 eventcore.rmasterclr(fdhandlein);
704 plugwash 1 end;
705     end;
706     if fdhandleout >= 0 then begin
707     if sendq.size = 0 then begin
708     //don't clear the bit in fdswmaster if data is in the sendq
709 beware 17 eventcore.wmasterclr(fdhandleout);
710 plugwash 1 end;
711     end;
712    
713     end;
714     inherited handlefdtrigger(readtrigger,writetrigger);
715     end;
716    
717     constructor tlsocket.Create(AOwner: TComponent);
718     begin
719     inherited create(aowner);
720     closehandles := true;
721 beware 17 trymoreips := true;
722 plugwash 1 end;
723    
724    
725 plugwash 60
726 plugwash 1 function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
727 beware 17 var
728     addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
729 plugwash 1 begin
730 plugwash 60 result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);
731 plugwash 1 end;
732    
733     procedure tlsocket.getxaddrbin(var binip:tbinip);
734     var
735     addr:tinetsockaddrv;
736     i:integer;
737     begin
738     i := sizeof(addr);
739     fillchar(addr,sizeof(addr),0);
740    
741 beware 17 {$ifdef win32}
742     winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
743     {$else}
744 plugwash 73 getsocketname(self.fdhandlein,addr,i);
745 beware 17 {$endif}
746 beware 20 binip := inaddrvtobinip(addr);
747 plugwash 1 converttov4(binip);
748     end;
749    
750     procedure tlsocket.getpeeraddrbin(var binip:tbinip);
751     var
752     addr:tinetsockaddrv;
753     i:integer;
754     begin
755     i := sizeof(addr);
756     fillchar(addr,sizeof(addr),0);
757 beware 17 {$ifdef win32}
758     winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
759     {$else}
760 plugwash 60 system_getpeername(self.fdhandlein,addr,i);
761 beware 17 {$endif}
762 plugwash 1
763 beware 20 binip := inaddrvtobinip(addr);
764 plugwash 1 converttov4(binip);
765     end;
766    
767 zipplet 79 function tlsocket.getXaddr:thostname;
768 plugwash 1 var
769     biniptemp:tbinip;
770     begin
771     getxaddrbin(biniptemp);
772     result := ipbintostr(biniptemp);
773     if result = '' then result := 'error';
774     end;
775    
776 zipplet 79 function tlsocket.getpeeraddr:thostname;
777 plugwash 1 var
778     biniptemp:tbinip;
779     begin
780     getpeeraddrbin(biniptemp);
781     result := ipbintostr(biniptemp);
782     if result = '' then result := 'error';
783     end;
784    
785 zipplet 79 function tlsocket.getXport:ansistring;
786 plugwash 1 var
787 beware 17 addr:tinetsockaddrv;
788 plugwash 1 i:integer;
789     begin
790     i := sizeof(addr);
791 beware 17 {$ifdef win32}
792     winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
793    
794     {$else}
795 plugwash 73 getsocketname(self.fdhandlein,addr,i);
796 beware 17
797     {$endif}
798     result := inttostr(htons(addr.InAddr.port));
799 plugwash 1 end;
800    
801 zipplet 79 function tlsocket.getpeerport:ansistring;
802 plugwash 1 var
803 beware 17 addr:tinetsockaddrv;
804 plugwash 1 i:integer;
805     begin
806     i := sizeof(addr);
807 beware 17 {$ifdef win32}
808     winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
809    
810     {$else}
811 plugwash 60 system_getpeername(self.fdhandlein,addr,i);
812 beware 17
813     {$endif}
814     result := inttostr(htons(addr.InAddr.port));
815 plugwash 1 end;
816    
817 beware 17 {$ifdef win32}
818     procedure tlsocket.myfdclose(fd : integer);
819     begin
820     closesocket(fd);
821     end;
822     function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;
823     begin
824     result := winsock.send(fd,(@buf)^,size,0);
825     end;
826     function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;
827     begin
828     result := winsock.recv(fd,buf,size,0);
829     end;
830     {$endif}
831 beware 18
832 plugwash 1 end.
833 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