Handle misaligned ifreq on macos

This commit is contained in:
Nikita Popov 2019-06-20 16:13:16 +02:00
parent d80f0ff6c0
commit dd6d471834

View File

@ -788,28 +788,27 @@ int php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *i
for (p = if_conf.ifc_buf;
p < if_conf.ifc_buf + if_conf.ifc_len;
p += entry_len) {
struct ifreq *cur_req;
/* let's hope the pointer is aligned */
cur_req = (struct ifreq*) p;
/* p may be misaligned on macos. */
struct ifreq cur_req;
memcpy(&cur_req, p, sizeof(struct ifreq));
#ifdef HAVE_SOCKADDR_SA_LEN
entry_len = cur_req->ifr_addr.sa_len + sizeof(cur_req->ifr_name);
entry_len = cur_req.ifr_addr.sa_len + sizeof(cur_req.ifr_name);
#else
/* if there's no sa_len, assume the ifr_addr field is a sockaddr */
entry_len = sizeof(struct sockaddr) + sizeof(cur_req->ifr_name);
entry_len = sizeof(struct sockaddr) + sizeof(cur_req.ifr_name);
#endif
entry_len = MAX(entry_len, sizeof(*cur_req));
entry_len = MAX(entry_len, sizeof(cur_req));
if ((((struct sockaddr*)&cur_req->ifr_addr)->sa_family == AF_INET) &&
(((struct sockaddr_in*)&cur_req->ifr_addr)->sin_addr.s_addr ==
if ((((struct sockaddr*)&cur_req.ifr_addr)->sa_family == AF_INET) &&
(((struct sockaddr_in*)&cur_req.ifr_addr)->sin_addr.s_addr ==
addr->s_addr)) {
#if defined(SIOCGIFINDEX)
if (ioctl(php_sock->bsd_socket, SIOCGIFINDEX, (char*)cur_req)
if (ioctl(php_sock->bsd_socket, SIOCGIFINDEX, (char*)&cur_req)
== -1) {
#elif defined(HAVE_IF_NAMETOINDEX)
unsigned index_tmp;
if ((index_tmp = if_nametoindex(cur_req->ifr_name)) == 0) {
if ((index_tmp = if_nametoindex(cur_req.ifr_name)) == 0) {
#else
#error Neither SIOCGIFINDEX nor if_nametoindex are available
#endif
@ -819,7 +818,7 @@ int php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *i
goto err;
} else {
#if defined(SIOCGIFINDEX)
*if_index = cur_req->ifr_ifindex;
*if_index = cur_req.ifr_ifindex;
#else
*if_index = index_tmp;
#endif