Added some debugging printf to troubleshoot issue for BSD

This commit is contained in:
Fabrizio Bertocci 2021-05-13 08:55:10 -04:00
parent b08a23283d
commit f6388b2bf6

View file

@ -16,24 +16,77 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/weaken.h"
#include "libc/sysv/errfuns.h"
#include "libc/calls/internal.h"
#include "libc/sock/sock.h"
#include "libc/sock/internal.h"
#include "libc/sysv/consts/sio.h"
#include "libc/stdio/stdio.h"
#define PRINTF weaken(printf)
/* SIOCGIFCONF:
* Takes an struct ifconf object of a given size
* Modifies the following:
* - ifc_len: set it to the number of valid ifreq structures representing
* the interfaces
* - ifc_ifcu.ifcu_req: sets the name of the interface for each interface
* The ifc_len is an input/output parameter: set it to the total size of
* the ifcu_buf (ifcu_req) buffer on input.
*/
int ioctl_siocgifconf_nt(int, struct ifconf *) hidden;
static int ioctl_siocgifconf_sysv(int fd, struct ifconf *ifc) {
/* Same as the default for now... */
return sys_ioctl(fd, SIOCGIFCONF, ifc);
/*
int rc;
if (IsBsd()) {
if (!weaken(malloc)) {
return enomem();
} else {
/* On BSD the size of the struct ifreq is smaller (16 bytes
* instead of 24 bytes), so buffers need to be adjusted accordingly
*
* TODO: Since BSD requires a SMALLER buffer we don't need to
* malloc a temp buffer, insted reuse the same buffer and
* safely move overlapping ifrn_name chunks
*/
int i;
struct ifconf ifc_bsd;
size_t num_ifreq = ifc->ifc_len / sizeof(struct ifreq);
if ((rc = sys_ioctl(fd, SIOCGIFCONF, &ifc)) != -1) {
return rc;
PRINTF("Mac version!\n");
ifc_bsd.ifc_len = (num_ifreq * sizeof(struct ifreq_bsd)); /* Adjust max buffer */
ifc_bsd.ifc_buf = weaken(malloc)(ifc_bsd.ifc_len);
PRINTF("numInterf Linux=%lu\n", num_ifreq);
PRINTF("BSD size=%lu\n", ifc_bsd.ifc_len);
PRINTF("Linux size=%lu\n", ifc->ifc_len);
if (!ifc_bsd.ifc_buf) {
PRINTF("Malloc failed\n");
return enomem();
}
PRINTF("Calling ioctl()\n");
i = sys_ioctl(fd, SIOCGIFCONF, &ifc_bsd);
PRINTF("rc=%d\n", i);
if (i < 0) {
weaken(free)(ifc_bsd.ifc_buf);
return -1;
}
/* Number of interfaces returned */
num_ifreq = ifc_bsd.ifc_len / sizeof(struct ifreq_bsd);
for (i = 1; i < num_ifreq; ++i) {
/* The first interface always match the same position */
memcpy(ifc->ifc_req[i].ifr_name, ifc_bsd.ifc_req[i].ifr_name, IFNAMSIZ);
}
ifc->ifc_len = num_ifreq * sizeof(struct ifreq);
weaken(free)(ifc_bsd.ifc_buf);
return 0;
}
} else {
// 100% compatible with Linux
return sys_ioctl(fd, SIOCGIFCONF, ifc);
}
return rc;
*/
}
/**