Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)

background

In Raspberry Pi + C language, I sometimes want to communicate with two Ethernet interfaces, and it is a memo of what I learned at that time. Describes how to create an Ethernet socket and how to specify an Ethernet socket. We have confirmed the operation in the following environment. [Hardware] Raspberry Pi2 Model B [OS]Raspbian GNU/Linux8.0(jessie)

Ethernet socket creation

When using the Ethernet interface on Linux for socket communication, you can prepare for socket communication by writing the following code.

eth_com.c


     deststr = IP_ADDRESS; //IP address of the connection destination#define IP_ADRESS ("192.168.0.12")
     /*Generate socket*/
     if((*sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
     {
          printf("fail to create socket\n");
          return -1;
     }

     /*Creating an address structure for the destination server*/
     memset(&server, 0, sizeof(server));
     server.sin_family = PF_INET;
     server.sin_addr.s_addr = inet_addr(deststr);
     server.sin_port = htons(PORT);

     /*Connection process*/
     if(connect (*sock, (struct sockaddr *)&server, sizeof(server)) < 0)
     {
          printf("fail to connect\n");
          return -1;
     }

However, with this code, if there are multiple Ethernet interfaces, one Ethernet interface will be used automatically. ** For example, if there are two Ethernet interfaces, "eth0" and "eth1", it will be automatically used. Will create a socket using the "eth0" interface. **

I want to create a sokect for "eth1"

Here, if you want sockcet communication using the "eth1" interface, specify the interface with the setsockopt function </ span> .setsockopt () creates a socket Call between `` socket () ``` and the connection process connect () `.

eth_com.c


    deststr = IP_ADDRESS; //Set IP address
     /*Generate socket*/
     if((*sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
     {
          printf("fail to create socket\n");
          return -1;
     }

     /** For usb-ehternet converter **/
     char *opt;
     opt = "eth1";
     setsockopt(*sock, SOL_SOCKET, SO_BINDTODEVICE, opt, 4);

     /*Creating an address structure for the destination server*/
     memset(&server, 0, sizeof(server));
     server.sin_family = PF_INET;
     server.sin_addr.s_addr = inet_addr(deststr);
     server.sin_port = htons(PORT);

     /*Connection process*/
     if(connect (*sock, (struct sockaddr *)&server, sizeof(server)) < 0)
     {
          printf("fail to connect\n");
          return -1;
     }

Reference: http://stackoverflow.com/questions/3998569/how-to-bind-raw-socket-to-specific-interface

You have to run it on the root

However, ** I could not communicate with the "eth1" interface even with the above code. ** If you read the web page mentioned as a reference carefully,

SO_BINDTODEVICE only works if you run as root, right? (on Linux at least) –  sep332 Nov 27 '12 at 21:29

It is commented. In other words, it seems that you have to execute with root privileges. </ span> By running it with sudo, I can now communicate safely with the "eth1" interface.

Recommended Posts

Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Create a web surveillance camera with Raspberry Pi and OpenCV
Using a webcam with Raspberry Pi
Build a Tensorflow environment with Raspberry Pi [2020]
Make a wash-drying timer with a Raspberry Pi
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant
Quickly create an environment where you can play with Raspberry Pi 4 (target 1 hour)
Create a visitor notification system using Raspberry Pi
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Build an Arch Linux environment on Raspberry Pi
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
Create a random number with an arbitrary probability density
Make an umbrella reminder with Raspberry Pi Zero W
A memorandum when making a surveillance camera with Raspberry Pi
[Linux] Create a self-signed certificate with Docker and apache
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!
Create a partition and then install the Raspberry Pi OS
I made a resource monitor for Raspberry Pi with a spreadsheet
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Create a Todo app with Django ① Build an environment with Docker
I made a surveillance camera with my first Raspberry PI.
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
Create an environment with virtualenv
Create an API with Django
Create a homepage with django
Create a heatmap with pyqtgraph
Create a directory with python
Control the motor with a motor driver using python on Raspberry Pi 3!
[C language] [Linux] Try to create a simple Linux command * Just add! !!
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
How to create a heatmap with an arbitrary domain in Python
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]