Annotation of embedaddon/php/win32/sockets.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:    +----------------------------------------------------------------------+
                      3:    | PHP Version 5                                                        |
                      4:    +----------------------------------------------------------------------+
1.1.1.3 ! misho       5:    | Copyright (c) 1997-2013 The PHP Group                                |
1.1       misho       6:    +----------------------------------------------------------------------+
                      7:    | This source file is subject to version 3.01 of the PHP license,      |
                      8:    | that is bundled with this package in the file LICENSE, and is        |
                      9:    | available through the world-wide-web at the following url:           |
                     10:    | http://www.php.net/license/3_01.txt                                  |
                     11:    | If you did not receive a copy of the PHP license and are unable to   |
                     12:    | obtain it through the world-wide-web, please send a note to          |
                     13:    | license@php.net so we can mail you a copy immediately.               |
                     14:    +----------------------------------------------------------------------+
                     15:    | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org>                    |
                     16:    |          Sterling Hughes  <sterling@php.net>                         |
                     17:    |                                                                      |
                     18:    | WinSock: Daniel Beulshausen <daniel@php4win.de>                      |
                     19:    +----------------------------------------------------------------------+
                     20:  */
                     21: 
1.1.1.2   misho      22: /* $Id$ */
1.1       misho      23: 
                     24: /* Code originally from ext/sockets */
                     25: 
                     26: #include <stdio.h>
                     27: #include <fcntl.h>
                     28: 
                     29: #include "php.h"
                     30: 
                     31: PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
                     32: {
                     33:        struct sockaddr_in address;
                     34:        SOCKET redirect;
                     35:        int size = sizeof(address);
                     36: 
                     37:        if(domain != AF_INET) {
                     38:                WSASetLastError(WSAENOPROTOOPT);
                     39:                return -1;
                     40:        }
                     41: 
1.1.1.3 ! misho      42:        sock[0] = sock[1] = redirect = INVALID_SOCKET;
1.1       misho      43: 
                     44: 
1.1.1.3 ! misho      45:        sock[0] = socket(domain, type, protocol);
        !            46:        if (INVALID_SOCKET == sock[0]) {
        !            47:                goto error;
        !            48:        }
        !            49: 
        !            50:        address.sin_addr.s_addr = INADDR_ANY;
        !            51:        address.sin_family      = AF_INET;
        !            52:        address.sin_port        = 0;
        !            53: 
        !            54:        if (bind(sock[0], (struct sockaddr*)&address, sizeof(address)) != 0) {
        !            55:                goto error;
        !            56:        }
1.1       misho      57: 
                     58:        if(getsockname(sock[0], (struct sockaddr *)&address, &size) != 0) {
1.1.1.3 ! misho      59:                goto error;
        !            60:        }
        !            61: 
        !            62:        if (listen(sock[0], 2) != 0) {
        !            63:                goto error;
        !            64:        }
        !            65: 
        !            66:        sock[1] = socket(domain, type, protocol);
        !            67:        if (INVALID_SOCKET == sock[1]) {
        !            68:                goto error;
1.1       misho      69:        }
                     70: 
                     71:        address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1.1.1.3 ! misho      72:        if(connect(sock[1], (struct sockaddr*)&address, sizeof(address)) != 0) {
        !            73:                goto error;
        !            74:        }
1.1       misho      75: 
                     76:        redirect = accept(sock[0],(struct sockaddr*)&address, &size);
1.1.1.3 ! misho      77:        if (INVALID_SOCKET == redirect) {
        !            78:                goto error;
        !            79:        }
1.1       misho      80: 
                     81:        closesocket(sock[0]);
                     82:        sock[0] = redirect;
                     83: 
                     84:        return 0;
1.1.1.3 ! misho      85: 
        !            86: error:
        !            87:        closesocket(redirect);
        !            88:        closesocket(sock[0]);
        !            89:        closesocket(sock[1]);
        !            90:        WSASetLastError(WSAECONNABORTED);
        !            91:        return -1;
1.1       misho      92: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>