LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (https://www.linuxquestions.org/questions/%2Absd-17/)
-   -   freebsd: SIOCSIFADDR returns EINVAL (https://www.linuxquestions.org/questions/%2Absd-17/freebsd-siocsifaddr-returns-einval-4175666955/)

smaclennan 01-02-2020 07:53 PM

freebsd: SIOCSIFADDR returns EINVAL
 
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?

cynwulf 01-03-2020 02:47 AM

Refer to ifconfig(8).

You need a ifconfig_<interface> stanza in rc.conf(8) to bring up the interface on boot.

smaclennan 01-03-2020 07:09 PM

Ok, I solved it. You can't use SIOCSIFADDR, or SIOCIFNETMASK, you must use SIOCAIFADDR (note the A) which takes an ifaliasreq struct. You still use SIOCSIFFLAGS though.


All times are GMT -5. The time now is 12:17 PM.