forked from socoding/node-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
117 lines (112 loc) · 2.58 KB
/
utils.cpp
File metadata and controls
117 lines (112 loc) · 2.58 KB
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
#include "common.h"
#include "utils.h"
char* filter_arg(char **param, int32_t *len) {
char *begptr = *param;
if (begptr == NULL) return NULL;
while (*begptr != '\0' && (*begptr == ' ' || *begptr == '\t')) {
++begptr;
}
char *endptr = begptr;
while (*endptr != '\0' && (*endptr != ' ' && *endptr != '\t')) {
++endptr;
}
if (begptr != endptr) {
if (*endptr != '\0') { /* don't reach the end */
*param = endptr + 1;
*endptr = '\0';
} else { /* reach the end */
*param = NULL;
}
if (len) {
*len = endptr - begptr;
}
return begptr;
} else { /* reach the end */
*param = NULL;
return NULL;
}
}
extern void* nl_memdup(const void* src, uint32_t len)
{
if (src) {
if (len != 0) {
void* temp = nl_malloc(len);
if (temp) {
return memcpy(temp, src, len);
}
} else {
return nl_malloc(1);
}
}
return NULL;
}
extern bool socket_host(uv_os_sock_t sock, bool local, char* host, uint32_t host_len, bool* ipv6, uint16_t* port)
{
union sock_name_u {
sockaddr_in sock4;
sockaddr_in6 sock6;
} sock_name;
int sock_len = sizeof(sock_name);
#ifdef _WIN32
int result = local ? getsockname(sock, (sockaddr*)&sock_name, &sock_len) : getpeername(sock, (sockaddr*)&sock_name, &sock_len);
#else
int result = local ? getsockname(sock, (sockaddr*)&sock_name, (socklen_t*)&sock_len) : getpeername(sock, (sockaddr*)&sock_name, (socklen_t*)&sock_len);
#endif
if (result != 0)
return false;
uint16_t family = ((sockaddr*)&sock_name)->sa_family;
if (family == AF_INET) {
if (host != NULL) {
uv_ip4_name(&sock_name.sock4, host, host_len);
}
if (ipv6 != NULL) {
*ipv6 = false;
}
if (port != NULL) {
*port = ntohs(sock_name.sock4.sin_port);
}
return true;
}
if (family == AF_INET6) {
if (host != NULL) {
uv_ip6_name(&sock_name.sock6, host, host_len);
}
if (ipv6 != NULL) {
*ipv6 = true;
}
if (port != NULL) {
*port = ntohs(sock_name.sock6.sin6_port);
}
return true;
}
return false;
}
extern bool sockaddr_host(struct sockaddr* addr, char* host, uint32_t host_len, bool* ipv6, uint16_t* port)
{
uint16_t family = addr->sa_family;
if (family == AF_INET) {
if (host != NULL) {
uv_ip4_name((sockaddr_in*)addr, host, host_len);
}
if (ipv6 != NULL) {
*ipv6 = false;
}
if (port != NULL) {
*port = ntohs(((sockaddr_in*)addr)->sin_port);
}
return true;
}
if (family == AF_INET6) {
if (host != NULL) {
uv_ip6_name((sockaddr_in6*)addr, host, host_len);
}
if (ipv6 != NULL) {
*ipv6 = true;
}
if (port != NULL) {
*port = ntohs(((sockaddr_in6*)addr)->sin6_port);
}
return true;
}
return false;
}