Reputation: 771
I am writing a program in C that needs to interface with virtual Ethernet devices. Here are some examples of commands that I could write in Shell, that I want to incorporate into my C program:
ip link add A type veth peer name B
ip addr add 172.17.42.1/16 dev A
ip link set A up
ip link set B netns 123
I did some searching but I couldn't find the commands/libraries that I need to perform this sort of operation. Could someone point me in the right direction?
Upvotes: 2
Views: 3202
Reputation: 1
You cannot create any virtual Ethernet device in portable C11 (or portable C99) because the C standard does not have any notion of Ethernet devices.
You need some operating system support for that, and your question becomes operating system specific.
If you are targeting the Linux operating system, you could use strace(1) to understand the system calls executed by your ip
commands.
However, you could simply write some application-specific shell script (running the above commands) and carefully use system(3) or popen(3) to run it (or even use directly fork(2), execve(2), pipe(2), poll(2) etc... read Advanced Linux Programming for more). Be scared of potential code injection, and if you build the command string, check any input data entering it.
As ysdx commented, look inside rtnetlink(7)
Upvotes: 5