Skip to content

Commit ca05a32

Browse files
authored
Cacheable FD/Test Improvements (#2)
* Cache selectable file descriptor * UDP/DNS test improvement
1 parent c6996c2 commit ca05a32

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pcap.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,20 @@ static int php_pcap_stream_cast(php_stream *stream, int castas, void **ret)
247247
case PHP_STREAM_AS_FD_FOR_SELECT:
248248
case PHP_STREAM_AS_FD:
249249
case PHP_STREAM_AS_SOCKETD:
250+
if (sess->fd && ret) {
251+
*(int *) ret = sess->fd;
252+
253+
return SUCCESS;
254+
}
255+
250256
fd = pcap_get_selectable_fd(sess->pcap);
251257

252258
if (fd < 0) {
253259
return FAILURE;
254260
}
255261

262+
sess->fd = fd;
263+
256264
if (ret) {
257265
*(int *) ret = fd;
258266
}
@@ -369,6 +377,7 @@ static php_stream *php_pcap_fopen(php_stream_wrapper *wrapper, const char *path,
369377
sess->timeout = 1000;
370378
sess->filter = NULL;
371379
sess->context = context;
380+
sess->fd = 0;
372381

373382
php_url_free(parsed_url);
374383

php_pcap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct pcap_capture_session {
4040
long timeout;
4141
char *filter;
4242
php_stream_context *context;
43+
int fd;
4344
} pcap_capture_session_t;
4445

4546
#endif /* PHP_PCAP_H */

tests/008.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ $localMac = '';
3838
$remoteMac = '';
3939
$ipv4Request = false;
4040
$ipv4Response = false;
41+
$ipv4Addr = '';
4142
$ipv6Request = false;
4243
$ipv6Response = false;
44+
$ipv6Addr = '';
4345
$replies = 0;
4446

4547
$startedAt = time();
@@ -81,9 +83,11 @@ while (!$ipv4Request || !$ipv4Response || !$ipv6Request || !$ipv6Response) {
8183
} else { // Answer
8284
if ($dns['answers'][0]['type'] === 1) { // A
8385
$ipv4Response = true;
86+
$ipv4Addr = $dns['answers'][0]['address'];
8487
echo "A DNS reply for {$dns['queries'][0]['name']}: {$dns['answers'][0]['address']} TTL={$dns['answers'][0]['ttl']}\n";
8588
} elseif ($dns['answers'][0]['type'] === 28) { // AAAA
8689
$ipv6Response = true;
90+
$ipv6Addr = $dns['answers'][0]['address'];
8791
echo "AAAA DNS reply for {$dns['queries'][0]['name']}: {$dns['answers'][0]['address']} TTL={$dns['answers'][0]['ttl']}\n";
8892
}
8993
}
@@ -95,6 +99,9 @@ while (!$ipv4Request || !$ipv4Response || !$ipv6Request || !$ipv6Response) {
9599
}
96100
}
97101

102+
var_dump($ipv4Addr === filter_var($ipv4Addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
103+
var_dump($ipv6Addr === filter_var($ipv6Addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
104+
98105
var_dump($localMac);
99106
var_dump($remoteMac);
100107

@@ -107,6 +114,8 @@ A DNS query for example.com.
107114
A DNS reply for example.com.: %d.%d.%d.%d TTL=%d
108115
AAAA DNS query for example.com.
109116
AAAA DNS reply for example.com.: %x:%x:%x:%x:%x:%x:%x:%x TTL=%d
117+
bool(true)
118+
bool(true)
110119
string(17) "%x:%x:%x:%x:%x:%x"
111120
string(17) "%x:%x:%x:%x:%x:%x"
112121
done!

0 commit comments

Comments
 (0)