Programmatically enable / disable the UNIX network interface

There are many very similar questions, but they are all for Windows - I want to know how (if possible), I can make my C program (run as root, on Linux) disable the network interface so that it no longer receives any packets.

If someone tells me what functions I need to do, it will be great, but even better if you can link me to an example script or tutorial somewhere that (example) gratia disables the network interface again.

+4
source share
5 answers

For Linux, everything is easily doable with MNL and knowledge of the RTNL protocol:

http://git.netfilter.org/cgi-bin/gitweb.cgi?p=libmnl.git;a=blob;f=examples/rtnl/rtnl-link-set.c;hb=HEAD

In Unices or other operating systems, only an OS-specific call and / or an archaic ioctl call are available, the last of which cannot correctly express multiple Linux addresses on one interface, so serious programs do not use it on this platform.

+4
source

You do not have a complete example, but you should start the following keywords (at least on Linux, not sure about other versions of Unix):

ioctl, SIOCSIFFLAGS, IFF_UP

The following may be useful for showing you the appropriate APIs: http://www.google.com/codesearch/p?hl=en#2--Ws53NXRc/src/ifdown.c

+2
source

On Linux, you can use ifdown . I do not know how this is carried over with other unixes.

+1
source

On Linux and most of the Unix-based systems I used, ifconfig <interface> up/down used to move the interface up or down. Not sure if there is a C procedure for this without running ifconfig.

+1
source

On Linux, the ip link set down dev ethX does nothing more or less what you want. I would suggest calling this program to do your job if you are on this platform.

If you want to do it yourself, the C api is not an easy thing to do. You can dive into the iproute source to find out what it does.

Check out @ user611775's answer for a great example of how to do this in C.

If you are on a different Unix, the answer will probably be different for each particular taste.

+1
source

Source: https://habr.com/ru/post/1341013/


All Articles