xref: /PHP-7.2/ext/sockets/php_sockets.h (revision 60a69dae)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
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 
22 #ifndef PHP_SOCKETS_H
23 #define PHP_SOCKETS_H
24 
25 /* $Id$ */
26 
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #if HAVE_SOCKETS
32 
33 #include <php.h>
34 #ifdef PHP_WIN32
35 # include "windows_common.h"
36 #endif
37 
38 #define PHP_SOCKETS_VERSION PHP_VERSION
39 
40 extern zend_module_entry sockets_module_entry;
41 #define phpext_sockets_ptr &sockets_module_entry
42 
43 #ifdef PHP_WIN32
44 #include <Winsock2.h>
45 #else
46 #if HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 #endif
50 
51 #ifndef PHP_WIN32
52 typedef int PHP_SOCKET;
53 # define PHP_SOCKETS_API PHPAPI
54 #else
55 # define PHP_SOCKETS_API __declspec(dllexport)
56 typedef SOCKET PHP_SOCKET;
57 #endif
58 
59 typedef struct {
60 	PHP_SOCKET	bsd_socket;
61 	int			type;
62 	int			error;
63 	int			blocking;
64 	zval		zstream;
65 } php_socket;
66 
67 #ifdef PHP_WIN32
68 struct	sockaddr_un {
69 	short	sun_family;
70 	char	sun_path[108];
71 };
72 #endif
73 
74 PHP_SOCKETS_API int php_sockets_le_socket(void);
75 PHP_SOCKETS_API php_socket *php_create_socket(void);
76 PHP_SOCKETS_API void php_destroy_socket(zend_resource *rsrc);
77 PHP_SOCKETS_API void php_destroy_sockaddr(zend_resource *rsrc);
78 
79 #define php_sockets_le_socket_name "Socket"
80 #define php_sockets_le_addrinfo_name "AddressInfo"
81 
82 #define PHP_SOCKET_ERROR(socket, msg, errn) \
83 		do { \
84 			int _err = (errn); /* save value to avoid repeated calls to WSAGetLastError() on Windows */ \
85 			(socket)->error = _err; \
86 			SOCKETS_G(last_error) = _err; \
87 			if (_err != EAGAIN && _err != EWOULDBLOCK && _err != EINPROGRESS) { \
88 				php_error_docref(NULL, E_WARNING, "%s [%d]: %s", msg, _err, sockets_strerror(_err)); \
89 			} \
90 		} while (0)
91 
92 ZEND_BEGIN_MODULE_GLOBALS(sockets)
93 	int last_error;
94 	char *strerror_buf;
95 ZEND_END_MODULE_GLOBALS(sockets)
96 
97 ZEND_EXTERN_MODULE_GLOBALS(sockets)
98 #define SOCKETS_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(sockets, v)
99 
100 enum sockopt_return {
101 	SOCKOPT_ERROR,
102 	SOCKOPT_CONTINUE,
103 	SOCKOPT_SUCCESS
104 };
105 
106 char *sockets_strerror(int error);
107 php_socket *socket_import_file_descriptor(PHP_SOCKET sock);
108 
109 #else
110 #define phpext_sockets_ptr NULL
111 #endif
112 
113 #if defined(_AIX) && !defined(HAVE_SA_SS_FAMILY)
114 # define ss_family __ss_family
115 #endif
116 
117 #endif
118 
119 /*
120  * Local variables:
121  * tab-width: 4
122  * c-basic-offset: 4
123  * End:
124  */
125