问题描述
我需要在C ++中做很多与网络有关的事情,而我通常会在Linux中用ifconfig
做这些事,但是我想在不解析一组系统调用输出的情况下做这件事.我可以使用哪些C或C ++库来判断网络适配器是打开还是关闭,读取或更改适配器的IP地址和网络掩码以及更改默认的DNS网关?
I need to do a number of network-related things in C++ that I would normally do with ifconfig
in Linux, but I'd like to do it without parsing the output of a group of system calls. Which C or C++ libraries can I use to tell if a network adapter is up or down, read or change an adapter's IP address and netmask, and change the default DNS gateway?
推荐答案
基本上,您需要使用套接字句柄(SIOCGIFADDR,SIOCADDRT)进行一堆ioctl调用.您可以在Linux内核源代码的文档/网络"下找到使用它的示例程序.其他一些链接可能会有所帮助:
Basically you need to make a bunch of ioctl calls using a socket handle (SIOCGIFADDR, SIOCADDRT). You can find sample programs that use it in the Linux kernel source under Documentation/networking. Some other links that might be helpful:
- AIX上的网络接口操作
- XBMC的实现(签出CNetworkInterfaceLinux)
- Network Interface operations on AIX
- XBMC's implementation (check out CNetworkInterfaceLinux)
我还要补充一点,如果您的目标是台式机Linux,则可以考虑使用DBUS API通过NetworkManager查询网络参数.
Let me also add that if your target is desktop linux, then you can consider using the DBUS API to query network parameters using the NetworkManager.
- http://people.redhat.com/dcbw/NetworkManager/NetworkManager DBUS API.txt(抱歉,在编辑中插入链接会出现问题)
- http://people.redhat.com/dcbw/NetworkManager/NetworkManager DBUS API.txt (sorry there's some issue inserting links in edits)
这篇关于如何在C ++中更改IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!