LWIP application development | broadcast and multicast

Broadcast and multicast

1. Introduction to broadcasting and multicast

1.1 networking mode

There are three communication modes in the network: unicast, broadcast and multicast (multicast)

  • Unicast: the "one-to-one" communication mode between hosts. Switches and routers in the network only forward data without copying. If 10 clients need the same data, the server needs to transfer them one by one and repeat the same work 10 times. Routers and switches in the network choose the transmission path according to their target address and transmit IP unicast data to their designated destination
  • Broadcast: the "one to all" communication mode between hosts. The network unconditionally copies and forwards the signals sent by each host, and all hosts can receive all information (whether you need it or not). In the data network, the broadcast is limited to the LAN of the layer-2 switch, and the broadcast data is prohibited from passing through the router to prevent the broadcast data from affecting the host in a large area (broadcast storm)
  • Multicast: the "one-to-one group" communication mode between hosts, that is, hosts joined to the same group can accept all data in this group, and switches and routers in the network only copy and forward the required data to those who need it. The host can request the router to join or exit a group. The routers and switches in the network selectively copy and transmit data, that is, only the data in the group is transmitted to those hosts who join the group. In this way, data can be transmitted to multiple hosts that need (join the group) at one time, and other communication of other hosts that do not need (not join the group) can be guaranteed not to be affected
1.2 IP address

Broadcast IP address: including the whole network and LAN broadcast addresses

  • Network wide broadcast address
//Network wide broadcast address
 two hundred and fifty-five.two hundred and fifty-five.two hundred and fifty-five.255
  • LAN broadcast address
/******************************/
C Class address:    one hundred and ninety-two.168.1.0
 Subnet mask:    255.255.255.0
 Intra segment broadcast address: 192.168.1.255
/******************************/
B Class address: 	 172.16.0.0
 Subnet mask:    255.255.0.0
 Broadcast address in section: 172.16.255.255
/******************************/
A Class address: 	 10.0.0.0
 Subnet mask:    255.0.0.0
 Broadcast address in the section: 10.255.255.255
/******************************/

Multicast IP address: IP multicast address is used to identify an IP multicast group. IANA allocates class D address space to multicast, ranging from 224.0.0.0 to 239.255.255.255

2. Broadcast and multicast implementation

reference resources Socket API programming optimization In this paper, the code is added on the basis of the engineering source code to realize the functions of broadcasting and multicast

First enable LWIP_IGMP macro switch

//opt. Enable LwIP in H file_ IGMP
#if !defined LWIP_IGMP || defined __DOXYGEN__
#define LWIP_IGMP                       1
#endif
2.1 broadcast realization

Create boradcast in the project C and the corresponding header file, write the broadcast task function and add it to the freertos task

#include "boradcast.h"
#include "socket_tcp_server.h"
#include "socket_wrap.h"
#include "ctype.h"
#include "string.h"
#include "FreeRTOS.h"
#include "task.h"

void vBoradcastTask(void){
  int sfd;
  struct sockaddr_in client_addr;
  socklen_t	client_addr_len;
  int optval = 1;	
  //Create socket 	 udp communication
  sfd = Socket(AF_INET, SOCK_DGRAM, 0);
  setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  client_addr.sin_family = AF_INET;
  client_addr.sin_port = htons(SERVER_PORT);
  client_addr.sin_addr.s_addr = inet_addr("255.255.255.255");
  client_addr_len = sizeof(client_addr);
  while(1){
	//Send broadcast data
	Sendto(sfd, "broadcast data", strlen("broadcast data"), 0, (struct sockaddr *)&client_addr, client_addr_len);
	vTaskDelay(5000);
  }	
}
2.2 multicast implementation

Create a multicast in the project C and the corresponding header file, write the multicast task function and add it to the freertos task

#include "multicast.h"
#include "socket_tcp_server.h"
#include "socket_wrap.h"
#include "ctype.h"
#include "string.h"
#include "FreeRTOS.h"
#include "task.h"

void vMulticastTask(void){
  int sfd;
  struct sockaddr_in client_addr;
  socklen_t	client_addr_len;
  ip_mreq multicast_mreq;
  //Fill in multicast address information
  multicast_mreq.imr_multiaddr.s_addr = inet_addr("224.0.1.1");
  multicast_mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  //socket creation 	 udp communication
  sfd = Socket(AF_INET, SOCK_DGRAM, 0);
  //Set multicast options
  setsockopt(sfd, IPPROTO_IP, IP_MULTICAST_IF, &multicast_mreq, sizeof(multicast_mreq));	
  client_addr.sin_family = AF_INET;
  client_addr.sin_port	= htons(SERVER_PORT);
  client_addr.sin_addr.s_addr = inet_addr("224.0.1.1");	//Multicast ip
  client_addr_len = sizeof(client_addr);
  while(1){
	//Send broadcast data
	Sendto(sfd, "multicast data", strlen("multicast data"), 0, (struct sockaddr *)&client_addr, client_addr_len);
	vTaskDelay(5000);
  }	
}

3. wireshark packet capture verification

wireshark packet capture tool: wireshark download address

Usage process of wireshark: select network card → configure filter

wireshark filter configuration method:

Comparison operator

eq, ==    Equal
ne, !=    Not Equal
gt, >     Greater Than
lt, <     Less Than
ge, >=    Greater than or Equal to
le, <=    Less than or Equal to

Protocol field

#Ethernet filtering
eth.dst eq ff:ff:ff:ff:ff:ff
#IP address filtering
ip.dst eq 192.168.1.10
ip.src == 192.168.1.1
#TCP filtering
tcp.port == 6666
# UDP filtering
udp.port == 6666
# http filtering
http.request.method == "POST"

Bit field operation

# TCP SYN
tcp.flags & 0x02

Logical expression

and, &&   Logical AND
or,  ||   Logical OR
not, !    Logical NOT
# tcp.port == 80 and ip.src == 192.168.2.1

Compile and download the broadcast and multicast codes in the previous chapter to the development board respectively, and then use wireshark to capture packets for verification

  • Select network card

  • Configure filter: UDP Port = = grab the packet of port 6666



Tags: lwip wireshark

Posted by mkoga on Wed, 13 Apr 2022 23:16:55 +0930