ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
(Generate patch)

Comparing BDB/BDB.xs (file contents):
Revision 1.67 by root, Fri Jan 9 22:32:12 2009 UTC vs.
Revision 1.68 by root, Tue Jul 14 19:29:23 2009 UTC

5#include <errno.h> 5#include <errno.h>
6 6
7#include "EXTERN.h" 7#include "EXTERN.h"
8#include "perl.h" 8#include "perl.h"
9#include "XSUB.h" 9#include "XSUB.h"
10
11#include "schmorp.h"
10 12
11// perl stupidly defines these as macros, breaking 13// perl stupidly defines these as macros, breaking
12// lots and lots of code. 14// lots and lots of code.
13#undef open 15#undef open
14#undef close 16#undef close
456 free (req->buf3); 458 free (req->buf3);
457 459
458 Safefree (req); 460 Safefree (req);
459} 461}
460 462
461#ifdef USE_SOCKETS_AS_HANDLES
462# define TO_SOCKET(x) (win32_get_osfhandle (x))
463#else
464# define EV_SELECT_IS_WINSOCKET 1
465# define TO_SOCKET(x) (x)
466#endif
467
468#ifdef _WIN32
469/* taken verbatim from libev's ev_win32.c */
470/* oh, the humanity! */
471static int
472ev_pipe (int filedes [2])
473{
474 struct sockaddr_in addr = { 0 };
475 int addr_size = sizeof (addr);
476 struct sockaddr_in adr2;
477 int adr2_size;
478 SOCKET listener;
479 SOCKET sock [2] = { -1, -1 };
480
481 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
482 return -1;
483
484 addr.sin_family = AF_INET;
485 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
486 addr.sin_port = 0;
487
488 if (bind (listener, (struct sockaddr *)&addr, addr_size))
489 goto fail;
490
491 if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
492 goto fail;
493
494 if (listen (listener, 1))
495 goto fail;
496
497 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
498 goto fail;
499
500 if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
501 goto fail;
502
503 if ((sock [1] = accept (listener, 0, 0)) < 0)
504 goto fail;
505
506 /* windows vista returns fantasy port numbers for getpeername.
507 * example for two interconnected tcp sockets:
508 *
509 * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364
510 * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363
511 * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363
512 * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365
513 *
514 * wow! tridirectional sockets!
515 *
516 * this way of checking ports seems to work:
517 */
518 if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))
519 goto fail;
520
521 if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))
522 goto fail;
523
524 errno = WSAEINVAL;
525 if (addr_size != adr2_size
526 || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */
527 || addr.sin_port != adr2.sin_port)
528 goto fail;
529
530 closesocket (listener);
531
532#if EV_SELECT_IS_WINSOCKET
533 filedes [0] = _open_osfhandle (sock [0], 0);
534 filedes [1] = _open_osfhandle (sock [1], 0);
535#else
536 /* when select isn't winsocket, we also expect socket, connect, accept etc.
537 * to work on fds */
538 filedes [0] = sock [0];
539 filedes [1] = sock [1];
540#endif
541
542 return 0;
543
544fail:
545 closesocket (listener);
546
547 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
548 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
549
550 return -1;
551}
552
553#define pipe(filedes) ev_pipe(filedes)
554#endif
555
556static void 463static void
557create_respipe (void) 464create_respipe (void)
558{ 465{
559#ifdef _WIN32 466#ifdef _WIN32
560 int arg; /* argg */ 467 int arg; /* argg */
561#endif 468#endif
562 int old_readfd = respipe [0]; 469 int old_readfd = respipe [0];
563 470
564 if (respipe [1] >= 0) 471 if (respipe [1] >= 0)
565 respipe_close (TO_SOCKET (respipe [1])); 472 respipe_close (S_TO_SOCKET (respipe [1]));
566 473
567 if (pipe (respipe)) 474 if (s_pipe (respipe))
568 croak ("unable to initialize result pipe"); 475 croak ("unable to initialize result pipe");
569 476
570 if (old_readfd >= 0) 477 if (old_readfd >= 0)
571 { 478 {
572 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) 479 if (dup2 (S_TO_SOCKET (respipe [0]), S_TO_SOCKET (old_readfd)) < 0)
573 croak ("unable to initialize result pipe(2)"); 480 croak ("unable to initialize result pipe(2)");
574 481
575 respipe_close (respipe [0]); 482 respipe_close (respipe [0]);
576 respipe [0] = old_readfd; 483 respipe [0] = old_readfd;
577 } 484 }
578 485
579#ifdef _WIN32 486#ifdef _WIN32
580 arg = 1; 487 arg = 1;
581 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) 488 if (ioctlsocket (S_TO_SOCKET (respipe [0]), FIONBIO, &arg)
582 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) 489 || ioctlsocket (S_TO_SOCKET (respipe [1]), FIONBIO, &arg))
583#else 490#else
584 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) 491 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
585 || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 492 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
586#endif 493#endif
587 croak ("unable to initialize result pipe(3)"); 494 croak ("unable to initialize result pipe(3)");
588 495
589 respipe_osf [0] = TO_SOCKET (respipe [0]); 496 respipe_osf [0] = S_TO_SOCKET (respipe [0]);
590 respipe_osf [1] = TO_SOCKET (respipe [1]); 497 respipe_osf [1] = S_TO_SOCKET (respipe [1]);
591} 498}
592 499
593static void bdb_request (bdb_req req); 500static void bdb_request (bdb_req req);
594X_THREAD_PROC (bdb_proc); 501X_THREAD_PROC (bdb_proc);
595 502

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines