Debian Codegraph @

vendor/clue/socket-raw/src/Factory.php

raw
1 <?php
2
3 namespace Socket\Raw;
4
5 use \InvalidArgumentException;
6
7 class Factory
8 {
9 /**
10 * create client socket connected to given target address
11 *
12 * @param string $address target address to connect to
13 * @param null|float $timeout connection timeout (in seconds), default null = no limit
14 * @return \Socket\Raw\Socket
15 * @throws InvalidArgumentException if given address is invalid
16 * @throws Exception on error
17 * @uses self::createFromString()
18 * @uses Socket::connect()
19 * @uses Socket::connectTimeout()
20 */
21 public function createClient($address, $timeout = null)
22 {
23 $socket = $this->createFromString($address, $scheme);
24
25 try {
26 if ($timeout === null) {
27 $socket->connect($address);
28 } else {
29 // connectTimeout enables non-blocking mode, so turn blocking on again
30 $socket->connectTimeout($address, $timeout);
31 $socket->setBlocking(true);
32 }
33 } catch (Exception $e) {
34 $socket->close();
35 throw $e;
36 }
37
38 return $socket;
39 }
40
41 /**
42 * create server socket bound to given address (and start listening for streaming clients to connect to this stream socket)
43 *
44 * @param string $address address to bind socket to
45 * @return \Socket\Raw\Socket
46 * @throws Exception on error
47 * @uses self::createFromString()
48 * @uses Socket::bind()
49 * @uses Socket::listen() only for stream sockets (TCP/UNIX)
50 */
51 public function createServer($address)
52 {
53 $socket = $this->createFromString($address, $scheme);
54
55 try {
56 $socket->bind($address);
57
58 if ($socket->getType() === SOCK_STREAM) {
59 $socket->listen();
60 }
61 } catch (Exception $e) {
62 $socket->close();
63 throw $e;
64 }
65
66 return $socket;
67 }
68
69 /**
70 * create TCP/IPv4 stream socket
71 *
72 * @return \Socket\Raw\Socket
73 * @throws Exception on error
74 * @uses self::create()
75 */
76 public function createTcp4()
77 {
78 return $this->create(AF_INET, SOCK_STREAM, SOL_TCP);
79 }
80
81 /**
82 * create TCP/IPv6 stream socket
83 *
84 * @return \Socket\Raw\Socket
85 * @throws Exception on error
86 * @uses self::create()
87 */
88 public function createTcp6()
89 {
90 return $this->create(AF_INET6, SOCK_STREAM, SOL_TCP);
91 }
92
93 /**
94 * create UDP/IPv4 datagram socket
95 *
96 * @return \Socket\Raw\Socket
97 * @throws Exception on error
98 * @uses self::create()
99 */
100 public function createUdp4()
101 {
102 return $this->create(AF_INET, SOCK_DGRAM, SOL_UDP);
103 }
104
105 /**
106 * create UDP/IPv6 datagram socket
107 *
108 * @return \Socket\Raw\Socket
109 * @throws Exception on error
110 * @uses self::create()
111 */
112 public function createUdp6()
113 {
114 return $this->create(AF_INET6, SOCK_DGRAM, SOL_UDP);
115 }
116
117 /**
118 * create local UNIX stream socket
119 *
120 * @return \Socket\Raw\Socket
121 * @throws Exception on error
122 * @uses self::create()
123 */
124 public function createUnix()
125 {
126 return $this->create(AF_UNIX, SOCK_STREAM, 0);
127 }
128
129 /**
130 * create local UNIX datagram socket (UDG)
131 *
132 * @return \Socket\Raw\Socket
133 * @throws Exception on error
134 * @uses self::create()
135 */
136 public function createUdg()
137 {
138 return $this->create(AF_UNIX, SOCK_DGRAM, 0);
139 }
140
141 /**
142 * create raw ICMP/IPv4 datagram socket (requires root!)
143 *
144 * @return \Socket\Raw\Socket
145 * @throws Exception on error
146 * @uses self::create()
147 */
148 public function createIcmp4()
149 {
150 return $this->create(AF_INET, SOCK_RAW, getprotobyname('icmp'));
151 }
152
153 /**
154 * create raw ICMPv6 (IPv6) datagram socket (requires root!)
155 *
156 * @return \Socket\Raw\Socket
157 * @throws Exception on error
158 * @uses self::create()
159 */
160 public function createIcmp6()
161 {
162 return $this->create(AF_INET6, SOCK_RAW, 58 /*getprotobyname('icmp')*/);
163 }
164
165 /**
166 * create low level socket with given arguments
167 *
168 * @param int $domain
169 * @param int $type
170 * @param int $protocol
171 * @return \Socket\Raw\Socket
172 * @throws Exception if creating socket fails
173 * @throws \Error PHP 8 only: throws \Error when arguments are invalid
174 * @uses socket_create()
175 */
176 public function create($domain, $type, $protocol)
177 {
178 $sock = @socket_create($domain, $type, $protocol);
179 if ($sock === false) {
180 throw Exception::createFromGlobalSocketOperation('Unable to create socket');
181 }
182 return new Socket($sock);
183 }
184
185 /**
186 * create a pair of indistinguishable sockets (commonly used in IPC)
187 *
188 * @param int $domain
189 * @param int $type
190 * @param int $protocol
191 * @return \Socket\Raw\Socket[]
192 * @throws Exception if creating pair of sockets fails
193 * @throws \Error PHP 8 only: throws \Error when arguments are invalid
194 * @uses socket_create_pair()
195 */
196 public function createPair($domain, $type, $protocol)
197 {
198 $ret = @socket_create_pair($domain, $type, $protocol, $pair);
199 if ($ret === false) {
200 throw Exception::createFromGlobalSocketOperation('Unable to create pair of sockets');
201 }
202 return array(new Socket($pair[0]), new Socket($pair[1]));
203 }
204
205 /**
206 * create TCP/IPv4 stream socket and listen for new connections
207 *
208 * @param int $port
209 * @param int $backlog
210 * @return \Socket\Raw\Socket
211 * @throws Exception if creating listening socket fails
212 * @throws \Error PHP 8 only: throws \Error when arguments are invalid
213 * @uses socket_create_listen()
214 * @see self::createServer() as an alternative to bind to specific IP, IPv6, UDP, UNIX, UGP
215 */
216 public function createListen($port, $backlog = 128)
217 {
218 $sock = @socket_create_listen($port, $backlog);
219 if ($sock === false) {
220 throw Exception::createFromGlobalSocketOperation('Unable to create listening socket');
221 }
222 return new Socket($sock);
223 }
224
225 /**
226 * create socket for given address
227 *
228 * @param string $address (passed by reference in order to remove scheme, if present)
229 * @param string|null $scheme default scheme to use, defaults to TCP (passed by reference in order to update with actual scheme used)
230 * @return \Socket\Raw\Socket
231 * @throws InvalidArgumentException if given address is invalid
232 * @throws Exception in case creating socket failed
233 * @uses self::createTcp4() etc.
234 */
235 public function createFromString(&$address, &$scheme)
236 {
237 if ($scheme === null) {
238 $scheme = 'tcp';
239 }
240
241 $hasScheme = false;
242
243 $pos = strpos($address, '://');
244 if ($pos !== false) {
245 $scheme = substr($address, 0, $pos);
246 $address = substr($address, $pos + 3);
247 $hasScheme = true;
248 }
249
250 if (strpos($address, ':') !== strrpos($address, ':') && in_array($scheme, array('tcp', 'udp', 'icmp'))) {
251 // TCP/UDP/ICMP address with several colons => must be IPv6
252 $scheme .= '6';
253 }
254
255 if ($scheme === 'tcp') {
256 $socket = $this->createTcp4();
257 } elseif ($scheme === 'udp') {
258 $socket = $this->createUdp4();
259 } elseif ($scheme === 'tcp6') {
260 $socket = $this->createTcp6();
261 } elseif ($scheme === 'udp6') {
262 $socket = $this->createUdp6();
263 } elseif ($scheme === 'unix') {
264 $socket = $this->createUnix();
265 } elseif ($scheme === 'udg') {
266 $socket = $this->createUdg();
267 } elseif ($scheme === 'icmp') {
268 $socket = $this->createIcmp4();
269 } elseif ($scheme === 'icmp6') {
270 $socket = $this->createIcmp6();
271 if ($hasScheme) {
272 // scheme was stripped from address, resulting IPv6 must not
273 // have a port (due to ICMP) and thus must not be enclosed in
274 // square brackets
275 $address = trim($address, '[]');
276 }
277 } else {
278 throw new InvalidArgumentException('Invalid address scheme given');
279 }
280 return $socket;
281 }
282 }