将 IPv4 地址从 ifaddrs 结构分配给sockaddr_in结构

Assigning IPv4 Address from ifaddrs Structure to sockaddr_in Structure

提问人:Dark Sorrow 提问时间:4/11/2023 最后编辑:dbushDark Sorrow 更新时间:4/11/2023 访问量:93

问:

我正在开发一个 UDP 服务器应用程序。
我希望将我的服务器套接字绑定到特定接口(从配置文件中读取)。在这种情况下.
我用来读取有关当前网络接口的所有信息。
然后,我遍历返回的链表,并根据我想要的接口检查每个接口的名称。
找到匹配项后,我计划从中提取 IPv4 地址并将其分配给对象。
目前我不确定如何传递,或者这是我遵循的正确/推荐的方法。
eth0getifaddrsgetifaddrsstruct ifaddrsstruct sockaddr_instruct ifaddrs->ifa_addr->sa_datastruct sockaddr_in.sin_addr.s_addr

法典:

bool start_server(void)
{
    struct sockaddr_in  server_address;
    struct ifaddrs      *ifaddr;
    int                 return_value;
    try
    {
        this->sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (this->sock_fd < 0) 
        {
            //TODO : Log Error
            return false;
        }

        // Initialize and configure struct sockaddr_in server_address
        std::memset(&server_address, sizeof(sockaddr_in), 0); // Initialize struct sockaddr_in server_address to 0
        server_address.sin_family = AF_INET;
        server_address.sin_port = htons(6666); //We are using the port 6666

        //Get IP address from the interface (eth0 currently) -> Interface name to be read from the configuration file
        return_value = getifaddrs(&ifaddr);
        if (return_value < 0) //On error, -1 is returned
        {
            //TODO : Log Error
            return false;
        }
        for (struct ifaddrs *ifa; ifa != nullptr; ifa = ifaddr->ifa_next)//ifa stand for interface address
        {
            if (std::strcmp(ifa->ifa_name, "eth0") == 0) //We check the interface name in the linked Strings are exact match
            {
                if (ifa->ifa_addr == nullptr)
                {
                    //TODO : Log Error - Selected interface doesn't have an assoiciated IP address
                    break; 
                }
                else
                {
                    server_address.sin_addr.s_addr = inet_addr(ifa->ifa_addr->sa_data); //I am unsure will this work or if this is the right approach
                    break;
                }

            }
        }
        freeifaddrs(ifaddr); //Free up memeory allocated by getifaddrs. Prevents Memory Leaks
}
C++ C 套接字 vxworks

评论

0赞 Gaurav Pathak 4/11/2023
这看起来不像 C
0赞 ryyker 4/11/2023
C并且是不同的语言。标签需要编辑。C++
1赞 stark 4/11/2023
您忘记在循环中初始化 IFA
1赞 dbush 4/11/2023
我认为 C 标签应该保留,因为问题是关于 C API 的。
1赞 Remy Lebeau 4/12/2023
该 API 可能基于 C,但所讨论的代码显然是 C++(没有或 C)。this->std::

答:

3赞 dbush 4/11/2023 #1

该成员是指向 .首先,您要通过检查是否设置为 来确保它适用于 IPv4 地址,如果是,则可以将其复制到 .ifa_addrstruct sockaddrsa_familyAF_INETmemcpystruct sockadr_in

    for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifaddr->ifa_next)
    {
        if (std::strcmp(ifa->ifa_name, "eth0") == 0)
        {
            if (ifa->ifa_addr == nullptr)
            {
                //TODO : Log Error - 
                //Selected interface doesn't have an assoiciated IP address
                break; 
            }
            else if (ifa->ifa_addr->sa_family == AF_INET)
            {
                memcpy(&server_address.sin_addr, ifa->ifa_addr, 
                       sizeof(server_address.sin_addr));
                break;
            }

        }
    }