-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_inet.php
617 lines (471 loc) · 13.8 KB
/
ngx_inet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* Created by PhpStorm.
* User: zenus
* Date: 15-11-21
* Time: 下午10:56
*/
define('INADDR_ANY',0x00000000);
/* Address to send to all hosts. */
define('INADDR_BROADCAST',0xffffffff);
/* Address indicating an error return. */
define('INADDR_NONE',0xffffffff);
class ngx_url_t {
/** ngx_str_t **/ private $url;
/** ngx_str_t **/ private $host;
/** ngx_str_t **/ private $port_text;
/** ngx_str_t **/ private $uri;
/** in_port_t **/ private $port;
/** in_port_t **/ private $default_port;
/** int **/ private $family;
/** unsigned **/ private $listen;
/** unsigned **/ private $uri_part;
/** unsigned **/ private $no_resolve;
/** unsigned **/ private $one_addr; /* compatibility */
/** unsigned **/ private $no_port;
/** unsigned **/ private $wildcard;
/** socklen_t **/ private $socklen;
/** u_char **/ private $sockaddr;
/** ngx_addr_t **/ private $addrs;
/** ngx_uint_t **/ private $naddrs;
/** char **/ private $err;
public function __set($property, $value){
$this->$property = $value ;
}
public function __get($property){
return $this->$property;
}
}
function ngx_parse_url( ngx_url_t $u)
{
$p = $u->url;
if (ngx_strncasecmp($p, "unix:", 5) == 0) {
return ngx_parse_unix_domain_url($u);
}
//todo we temporarily don't realize this
// if ($p[0] == '[') {
// return ngx_parse_inet6_url($u);
//}
return ngx_parse_inet_url($u);
}
function ngx_parse_inet_url( ngx_url_t $u)
{
$sin = $u->sockaddr;
$sin->sin_family = AF_INET;
$u->family = AF_INET;
$host = $u->url;
$last = strlen($host);
$port = ngx_strlchr($host, $last, ':');
$uri = ngx_strlchr($host, $last, '/');
$args = ngx_strlchr($host, $last, '?');
if ($args) {
if ($uri == NULL || $args < $uri) {
$uri = $args;
}
}
if ($uri) {
if ($u->listen || !$u->uri_part) {
$u->err = "invalid host";
return NGX_ERROR;
}
$u->uri = substr($host,$uri);
$last = $uri;
if ($uri < $port) {
$port = NULL;
}
}
if ($port) {
$len = $last - ($port+1);
$port_data = substr($host,$port);
$n = ngx_atoi($port_data);
if ($n < 1 || $n > 65535) {
$u->err = "invalid port";
return NGX_ERROR;
}
$u->port = $n;
$sin->sin_port = htons($n);
$u->port_text = $port_data;
$last = $port ;
} else {
if ($uri == NULL) {
if ($u->listen) {
/* test value as port only */
$n = ngx_atoi(substr($host,0,$last));
if ($n != NGX_ERROR) {
if ($n < 1 || $n > 65535) {
$u->err = "invalid port";
return NGX_ERROR;
}
$u->port = $n;
$sin->sin_port = htons($n);
$u->port_text = $host;
$u->wildcard = 1;
return NGX_OK;
}
}
}
$u->no_port = 1;
$u->port = $u->default_port;
$sin->sin_port = htons($u->default_port);
}
$len = $last;
if ($len == 0) {
$u->err = "no host";
return NGX_ERROR;
}
$u->host =substr($host,0,$last);
if ($u->listen && $len == 1 && $host == '*') {
$sin->sin_addr = INADDR_ANY;
$u->wildcard = 1;
return NGX_OK;
}
$sin->sin_addr = ngx_inet_addr($u->host);
if ($sin->sin_addr != INADDR_NONE) {
if ($sin->sin_addr == INADDR_ANY) {
$u->wildcard = 1;
}
$u->naddrs = 1;
$addr = new ngx_addr_t();
$sin = new sockaddr_in();
$sin = $u->sockaddr;
$addr->sockaddr = $sin;
$addr->name = ngx_sprintf('', "%V:%d", array($u->host, $u->port));
$u->addrs[] = $addr;
return NGX_OK;
}
if ($u->no_resolve) {
return NGX_OK;
}
if (ngx_inet_resolve_host($u) != NGX_OK) {
return NGX_ERROR;
}
$u->family = $u->addrs[0]->sockaddr->sa_family;
$u->sockaddr = $u->addrs[0]->sockaddr;
switch ($u->family) {
default: /* AF_INET */
$sin = $u->sockaddr;
if ($sin->sin_addr == INADDR_ANY) {
$u->wildcard = 1;
}
break;
}
return NGX_OK;
}
function ngx_parse_unix_domain_url( ngx_url_t $u)
{
$len = strlen($u->url);
$path = $u->url;
$path = substr($path,5);
$len -= 5;
if ($u->uri_part) {
$last = strlen($path);
$uri = ngx_strlchr($path, $last,':');
if ($uri) {
$len = $uri+1;
$u->uri->len = $last - ($uri+1);
$u->uri = substr($path,$uri);
}
}
if ($len == 0) {
$u->err = "no path in the unix domain socket";
return NGX_ERROR;
}
$u->host = $path;
//$u->socklen = sizeof(struct sockaddr_un);
// $saun = $u->sockaddr;
$saun = new sockaddr_un();
$saun->sun_family = AF_UNIX;
$saun->sun_path = $path;
$addr = new ngx_addr_t();
$addr->sockaddr = $saun;
$addr->name = $u->url;
$u->addrs[0] = $addr;
// u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t));
// if (u->addrs == NULL) {
// return NGX_ERROR;
//}
// saun = ngx_pcalloc(pool, sizeof(struct sockaddr_un));
// if (saun == NULL) {
// return NGX_ERROR;
// }
$u->family = AF_UNIX;
$u->naddrs = 1;
// saun->sun_family = AF_UNIX;
// (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
// $u->addrs[0].sockaddr = (struct sockaddr *) saun;
// $u->addrs[0].socklen = sizeof(struct sockaddr_un);
// $u->addrs[0].name.len = len + 4;
// $u->addrs[0].name.data = u->url.data;
return NGX_OK;
}
function htons($n){
return pack('n', $n);
}
function ntohs($n){
return unpack('v', $n);
}
//class sockaddr_un
// {
// private $sun_family;
// private $sun_path;
//
// public function __set($property, $value){
// $this->$property = $value;
// }
// public function __get($property){
// return $this->$property;
// }
// }
class ngx_addr_t {
/***struct sockaddr**/ private $sockaddr;
// /**socklen_t**/ private $socklen;
/**ngx_str_t **/ private $name;
/**todo self create**/
/**ngx_str_t **/ private $family;
/**ngx_str_t **/ private $port;
public function __set($property, $value){
$this->$property = $value;
}
public function __get($property){
return $this->$property;
}
}
function ngx_inet_addr($text)
{
return inet_pton($text);
}
//class sockaddr_in
// {
// // __SOCKADDR_COMMON (sin_);
// /** in_port_t **/ private $sin_port; /* Port number. */
// /** struct in_addr **/ private $sin_addr; /* Internet address. */
// /** sin_family* */ private $sin_family;
//
// /* Pad to size of `struct sockaddr'. */
//// unsigned char sin_zero[sizeof (struct sockaddr) -
//// __SOCKADDR_COMMON_SIZE -
//// sizeof (in_port_t) -
//// sizeof (struct in_addr)];
// public function __set($property,$value){
// $this->$property = $value;
// }
//
// public function __get($property){
// return $this->$property;
// }
// }
function ngx_inet_resolve_host( ngx_url_t $u)
{
$port = htons($u->port);
$in_addr = ngx_inet_addr($u->host);
if ($in_addr == INADDR_NONE) {
$host = $u->host;
$h = gethostbynamel($host);
// for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
//
// /* MP: ngx_shared_palloc() */
//
// u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_addr_t));
// if (u->addrs == NULL) {
// return NGX_ERROR;
// }
$addr = new ngx_addr_t();
$i = count($h);
$u->naddrs = $i;
for ($i = 0; $i < $u->naddrs; $i++) {
// sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
// if (sin == NULL) {
// return NGX_ERROR;
// }
$sin = new sockaddr_in();
$sin->sin_family = AF_INET;
$sin->sin_port = $port;
$sin->sin_addr = $h[$i];
$addr->sockaddr = $sin;
// $u->addrs[i].sockaddr = (struct sockaddr *) sin;
//
// len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1;
//
// p = ngx_pnalloc(pool, len);
// if (p == NULL) {
// return NGX_ERROR;
// }
$p = '';
//todo why the port is NO 1
$len = ngx_sock_ntop($sin,
$p, 1);
$addr->name = $p;
//u->addrs[i].name.len = len;
$u->addrs[$i] = $addr;
}
} else {
/* MP: ngx_shared_palloc() */
$addr = new ngx_addr_t();
// u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t));
// if (u->addrs == NULL) {
// return NGX_ERROR;
// }
$sin = new sockaddr_in();
// sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
// if (sin == NULL) {
// return NGX_ERROR;
// }
$u->naddrs = 1;
$sin->sin_family = AF_INET;
$sin->sin_port = $port;
$sin->sin_addr = $in_addr;
$addr->sockaddr = $sin;
$addr->name = ngx_sprintf('', "%V:%d", array($u->host, ntohs($port)));
}
return NGX_OK;
}
function ngx_sock_ntop($sa, $text, $port)
{
// u_char *p;
// struct sockaddr_in *sin;
//#if (NGX_HAVE_INET6)
// size_t n;
// struct sockaddr_in6 *sin6;
//#endif
//#if (NGX_HAVE_UNIX_DOMAIN)
// struct sockaddr_un *saun;
//#endif
switch ($sa->sa_family) {
case AF_INET:
$sin = $sa;
$p = $sin->sin_addr;
if ($port) {
$p = ngx_snprintf($text,"%ud.%ud.%ud.%ud:%d",
array($p[0], $p[1], $p[2], $p[3], ntohs($sin->sin_port)));
} else {
$p = ngx_snprintf($text, "%ud.%ud.%ud.%ud",
array($p[0], $p[1], $p[2], $p[3]));
}
return $p;
case AF_UNIX:
$saun = $sa;
/* on Linux sockaddr might not include sun_path at all */
// if ($socklen <= offsetof(struct sockaddr_un, sun_path)) {
// $p = ngx_snprintf($text, "unix:%Z");
//
// } else {
$p = ngx_snprintf($text, "unix:%s%Z", $saun->sun_path);
// }
/* we do not include trailing zero in address length */
return $p;
default:
return 0;
}
}
function ngx_cmp_sockaddr( sockaddr $sa1, sockaddr $sa2, $cmp_port)
{
// struct sockaddr_in *sin1, *sin2;
//#if (NGX_HAVE_INET6)
// struct sockaddr_in6 *sin61, *sin62;
//#endif
//#if (NGX_HAVE_UNIX_DOMAIN)
// struct sockaddr_un *saun1, *saun2;
//#endif
if ($sa1->sa_family != $sa2->sa_family) {
return NGX_DECLINED;
}
switch ($sa1->sa_family) {
case AF_INET6:
$sin61 = /**(struct sockaddr_in6 *)**/ $sa1;
$sin62 = /**(struct sockaddr_in6 *)**/ $sa2;
if ($cmp_port && $sin61->sin6_port != $sin62->sin6_port) {
return NGX_DECLINED;
}
if (ngx_strcmp($sin61->sin6_addr, $sin62->sin6_addr) != 0) {
return NGX_DECLINED;
}
break;
case AF_UNIX:
/* TODO length */
$saun1 = /*(struct sockaddr_un *)*/ $sa1;
$saun2 = /*(struct sockaddr_un *)*/ $sa2;
if (ngx_strcmp($saun1->sun_path, $saun2->sun_path) != 0)
{
return NGX_DECLINED;
}
break;
default: /* AF_INET */
$sin1 = /*(struct sockaddr_in *)*/ $sa1;
$sin2 = /*(struct sockaddr_in *)*/ $sa2;
if ($cmp_port && $sin1->sin_port != $sin2->sin_port) {
return NGX_DECLINED;
}
if ($sin1->sin_addr != $sin2->sin_addr) {
return NGX_DECLINED;
}
break;
}
return NGX_OK;
}
class sockaddr
{
//__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
//char sa_data[14]; /* Address data. */
private $sa_family;
private $sa_data;
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
};
/* Ditto, for IPv6. */
class sockaddr_in6 extends sockaddr
{
private $sin6_family;
private $sin6_port;
private $sin6_addr;
// __SOCKADDR_COMMON (sin6_);
// in_port_t sin6_port; /* Transport layer port # */
// uint32_t sin6_flowinfo; /* IPv6 flow information */
// struct in6_addr sin6_addr; /* IPv6 address */
// uint32_t sin6_scope_id; /* IPv6 scope-id */
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
};
/* Structure describing the address of an AF_LOCAL (aka AF_UNIX) socket. */
class sockaddr_un extends sockaddr
{
private $sa_family = AF_UNIX;
private $sun_family;
private $sun_path;
// __SOCKADDR_COMMON (sun_);
// char sun_path[108]; /* Path name. */
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
};
/* Structure describing an Internet socket address. */
class sockaddr_in extends sockaddr
{
private $sin_family;
private $sin_addr;
private $sin_port;
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
// __SOCKADDR_COMMON (sin_);
// in_port_t sin_port; /* Port number. */
// struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
// unsigned char sin_zero[sizeof (struct sockaddr) -
// __SOCKADDR_COMMON_SIZE -
// sizeof (in_port_t) -
// sizeof (struct in_addr)];
};