/* * Send udp packets from two various interfaces, * * This tool used to check correctly work option * setsockopt(SO_BINDTODEVICE) * Use together with b2d_recv.c * Detailed description in file b2d_recv.c * * usage ./b2d_send ip1 ip2 port * example: ./b2d_send 192.168.44.2 192.168.45.2 23777 */ #include #include #include #include #include #include #include #include #include #define RX_BUFF_SIZE 1024 /* create sender socket, if error occured exit(EXIT_FAILURE) */ int create_sender_sock(const char * str_ip, const char * str_port, struct sockaddr_in * sock_addr); int main(int argc, char *argv[]) { struct sockaddr_in sa_sender1, sa_sender2; if (argc != 4) { printf("usage: %s ip1 ip2 port\n", argv[0]); exit(EXIT_FAILURE); } int sfd_sender1 = create_sender_sock(argv[1], argv[3], &sa_sender1); int sfd_sender2 = create_sender_sock(argv[2], argv[3], &sa_sender2); char tx_buff1[] = "Data from first socket"; char tx_buff2[] = "Data from second socket"; ssize_t res = send(sfd_sender1, tx_buff1, strlen(tx_buff1) + 1, 0); if (res < 0) { perror("sender1"); exit(EXIT_FAILURE); } printf("success send %ld bytes to %s:%s \"%s\"\n", res, argv[1], argv[3], tx_buff1); res = send(sfd_sender2, tx_buff2, strlen(tx_buff2) + 1, 0); if (res < 0) { perror("sender2"); exit(EXIT_FAILURE); } printf("success send %ld bytes to %s:%s \"%s\"\n", res, argv[2], argv[3], tx_buff2); exit(EXIT_SUCCESS); }/* main() */ int create_sender_sock(const char * str_ip, const char * str_port, struct sockaddr_in * sock_addr) { int res; int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket"); exit(EXIT_FAILURE); } memset(sock_addr, 0, sizeof(struct sockaddr_in)); sock_addr->sin_family = AF_INET; if (inet_pton(AF_INET, str_ip, &(sock_addr->sin_addr)) != 1) { perror("Bad ip_add"); exit(EXIT_FAILURE); } sock_addr->sin_port = htons((uint16_t)atoi(str_port)); res = connect(sockfd, (struct sockaddr*)sock_addr, sizeof(struct sockaddr_in)); if (res < 0) { perror("connect"); exit(EXIT_FAILURE); } return sockfd; }/* create_sender_socket() */