Back to index

php5  5.3.10
sockets.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org>                    |
00016    |          Sterling Hughes  <sterling@php.net>                         |
00017    |                                                                      |
00018    | WinSock: Daniel Beulshausen <daniel@php4win.de>                      |
00019    +----------------------------------------------------------------------+
00020  */
00021 
00022 /* $Id: sockets.c 321634 2012-01-01 13:15:04Z felipe $ */
00023 
00024 /* Code originally from ext/sockets */
00025 
00026 #include <stdio.h>
00027 #include <fcntl.h>
00028 
00029 #include "php.h"
00030 
00031 PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
00032 {
00033        struct sockaddr_in address;
00034        SOCKET redirect;
00035        int size = sizeof(address);
00036 
00037        if(domain != AF_INET) {
00038               WSASetLastError(WSAENOPROTOOPT);
00039               return -1;
00040        }
00041 
00042 
00043        sock[0]                            = socket(domain, type, protocol);
00044        address.sin_addr.s_addr            = INADDR_ANY;
00045        address.sin_family          = AF_INET;
00046        address.sin_port            = 0;
00047 
00048        bind(sock[0], (struct sockaddr*)&address, sizeof(address));
00049 
00050        if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
00051        }
00052 
00053        listen(sock[0], 2);
00054        sock[1] = socket(domain, type, protocol); 
00055        address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
00056 
00057        connect(sock[1], (struct sockaddr*)&address, sizeof(address));
00058        redirect = accept(sock[0],(struct sockaddr*)&address, &size);
00059 
00060        closesocket(sock[0]);
00061        sock[0] = redirect;
00062 
00063        if(sock[0] == INVALID_SOCKET ) {
00064               closesocket(sock[0]);
00065               closesocket(sock[1]);
00066               WSASetLastError(WSAECONNABORTED);
00067               return -1;
00068        }
00069        
00070        return 0;
00071 }