#include #include #include #include #include #include #include #define ERROR(_line...) \ do { \ fprintf(stderr, _line); \ fprintf(stderr, "\n"); \ assert(0); \ } while (0) #define ERROR_CL(_line...) \ do { \ close(sock); \ ERROR(_line); \ } while (0) #define WARN(_line...) \ do { \ printf(_line); \ printf("\n"); \ } while (0) #define MCAST_GROUP "229.17.88.168" #define GROUP_PORT 12345 #define LOCAL_PORT 23456 #define LOCAL_ADDR1 "10.208.14.2" #define LOCAL_ADDR2 "10.208.15.2" #define VLAN1 "999" #define VLAN2 "1001" #define VLAN_IF_STR(_vlan) "eth3." _vlan #define VLAN_IF1 VLAN_IF_STR(VLAN1) #define VLAN_IF2 VLAN_IF_STR(VLAN2) static int init_socket_send(const char *ifname, const char *local_addr) { int sock; struct sockaddr_in addr; struct in_addr ifaddr; if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) ERROR("Can't open socket: %s", strerror(errno)); WARN("if %s, addr %s", ifname, local_addr); if (inet_aton(local_addr, &ifaddr) == 0) ERROR_CL("inet_aton failed: %s", strerror(errno)); if (setsockopt(sock, SOL_IP, IP_MULTICAST_IF, &ifaddr, sizeof(ifaddr)) != 0) ERROR_CL("IP_MULTICAST_IF failed: %s", strerror(errno)); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(LOCAL_PORT); if (inet_pton(AF_INET, local_addr, &addr.sin_addr) != 1) ERROR_CL("Can't convert mcast group address: %s", strerror(errno)); if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) ERROR_CL("bind failed: %s", strerror(errno)); return sock; } int main(void) { int sock1; int sock2; char buf[100]; struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(GROUP_PORT); if (inet_pton(AF_INET, MCAST_GROUP, &addr.sin_addr) != 1) ERROR("Can't convert mcast group address: %s", strerror(errno)); sock1 = init_socket_send(VLAN_IF1, LOCAL_ADDR1); sock2 = init_socket_send(VLAN_IF2, LOCAL_ADDR2); if (sendto(sock1, buf, 99, 0, (struct sockaddr *)&addr, sizeof(addr)) != 99 || sendto(sock2, buf, 98, 0, (struct sockaddr *)&addr, sizeof(addr)) != 98) WARN("send failed: %s", strerror(errno)); sleep(1); if (sendto(sock1, buf, 97, 0, (struct sockaddr *)&addr, sizeof(addr)) != 97 || sendto(sock2, buf, 96, 0, (struct sockaddr *)&addr, sizeof(addr)) != 96) WARN("send failed: %s", strerror(errno)); close(sock1); close(sock2); return EXIT_SUCCESS; }