I am trying to set the IP address of an interface. The interface is virtual (vtnet0) if that matters.
The SIOCGIFADDR ioctl works, but I can't get the set to work. Here is the code fragment:
Code:
static int set_ip(const char *ifname, const char *ip, unsigned mask, int down)
{
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
perror("set_ip socket");
return -1;
}
struct ifreq req;
memset(&req, 0, sizeof(req));
strlcpy(req.ifr_name, ifname, IF_NAMESIZE);
struct sockaddr_in *in = (struct sockaddr_in *)&req.ifr_addr;
#ifndef __linux__
in->sin_len = sizeof(struct sockaddr_in);
#endif
in->sin_family = AF_INET;
if (ip)
in->sin_addr.s_addr = inet_addr(ip);
errno = 0;
if (ioctl(s, SIOCSIFADDR, &req)) {
perror("SIOCSIFADDR");
goto failed;
}
Is this not the correct way to set the IP on FreeBSD?