1 #include <php.h>
2 #include <php_network.h>
3 #include "php_sockets.h"
4
5 #ifdef PHP_WIN32
6 #include "windows_common.h"
7 #else
8 #include <netdb.h>
9 #include <arpa/inet.h>
10 #endif
11
12 extern int php_string_to_if_index(const char *val, unsigned *out);
13
14 #if HAVE_IPV6
15 /* Sets addr by hostname, or by ip in string form (AF_INET6) */
php_set_inet6_addr(struct sockaddr_in6 * sin6,char * string,php_socket * php_sock)16 int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock) /* {{{ */
17 {
18 struct in6_addr tmp;
19 #if HAVE_GETADDRINFO
20 struct addrinfo hints;
21 struct addrinfo *addrinfo = NULL;
22 #endif
23 char *scope = strchr(string, '%');
24
25 if (inet_pton(AF_INET6, string, &tmp)) {
26 memcpy(&(sin6->sin6_addr.s6_addr), &(tmp.s6_addr), sizeof(struct in6_addr));
27 } else {
28 #if HAVE_GETADDRINFO
29
30 memset(&hints, 0, sizeof(struct addrinfo));
31 hints.ai_family = AF_INET6;
32 #if HAVE_AI_V4MAPPED
33 hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
34 #else
35 hints.ai_flags = AI_ADDRCONFIG;
36 #endif
37 getaddrinfo(string, NULL, &hints, &addrinfo);
38 if (!addrinfo) {
39 #ifdef PHP_WIN32
40 PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
41 #else
42 PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
43 #endif
44 return 0;
45 }
46 if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
47 php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
48 freeaddrinfo(addrinfo);
49 return 0;
50 }
51
52 memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
53 freeaddrinfo(addrinfo);
54
55 #else
56 /* No IPv6 specific hostname resolution is available on this system? */
57 php_error_docref(NULL, E_WARNING, "Host lookup failed: getaddrinfo() not available on this system");
58 return 0;
59 #endif
60
61 }
62
63 if (scope) {
64 zend_long lval = 0;
65 double dval = 0;
66 unsigned scope_id = 0;
67
68 scope++;
69
70 if (IS_LONG == is_numeric_string(scope, strlen(scope), &lval, &dval, 0)) {
71 if (lval > 0 && (zend_ulong)lval <= UINT_MAX) {
72 scope_id = lval;
73 }
74 } else {
75 php_string_to_if_index(scope, &scope_id);
76 }
77
78 sin6->sin6_scope_id = scope_id;
79 }
80
81 return 1;
82 }
83 /* }}} */
84 #endif
85
86 /* Sets addr by hostname, or by ip in string form (AF_INET) */
php_set_inet_addr(struct sockaddr_in * sin,char * string,php_socket * php_sock)87 int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock) /* {{{ */
88 {
89 struct in_addr tmp;
90 struct hostent *host_entry;
91
92 #ifdef HAVE_INET_PTON
93 if (inet_pton(AF_INET, string, &tmp)) {
94 #else
95 if (inet_aton(string, &tmp)) {
96 #endif
97 sin->sin_addr.s_addr = tmp.s_addr;
98 } else {
99 if (strlen(string) > MAXFQDNLEN || ! (host_entry = php_network_gethostbyname(string))) {
100 /* Note: < -10000 indicates a host lookup error */
101 #ifdef PHP_WIN32
102 PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
103 #else
104 PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
105 #endif
106 return 0;
107 }
108 if (host_entry->h_addrtype != AF_INET) {
109 php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
110 return 0;
111 }
112 memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
113 }
114
115 return 1;
116 }
117 /* }}} */
118
119 /* Sets addr by hostname or by ip in string form (AF_INET or AF_INET6,
120 * depending on the socket) */
121 int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock) /* {{{ */
122 {
123 if (php_sock->type == AF_INET) {
124 struct sockaddr_in t = {0};
125 if (php_set_inet_addr(&t, string, php_sock)) {
126 memcpy(ss, &t, sizeof t);
127 ss->ss_family = AF_INET;
128 *ss_len = sizeof(t);
129 return 1;
130 }
131 }
132 #if HAVE_IPV6
133 else if (php_sock->type == AF_INET6) {
134 struct sockaddr_in6 t = {0};
135 if (php_set_inet6_addr(&t, string, php_sock)) {
136 memcpy(ss, &t, sizeof t);
137 ss->ss_family = AF_INET6;
138 *ss_len = sizeof(t);
139 return 1;
140 }
141 }
142 #endif
143 else {
144 php_error_docref(NULL, E_WARNING,
145 "IP address used in the context of an unexpected type of socket");
146 }
147 return 0;
148 }
149