#include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct sockaddr_in sin; char *buffer; struct ip *ip; size_t length; int fd, i, n; const int on = 1; uint16_t len; if (argc != 5) { fprintf(stderr, "%s", "Usage: test_send remote_addr hdr_len buf_len send_count\n"); return (-1); } if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("socket"); } if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(int)) < 0) { perror("setsockopt"); } memset(&sin, 0, sizeof(struct sockaddr_in)); sin.sin_family = AF_INET; #if defined(__FreeBSD__) || defined(__APPLE__) sin.sin_len = sizeof(struct sockaddr_in); #endif sin.sin_port = htons(0); sin.sin_addr.s_addr = inet_addr(argv[1]); len = (uint16_t)atol(argv[2]); length = (size_t)atol(argv[3]); n = (int)atoi(argv[4]); if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &length, sizeof(length)) < 0) { perror("setsockopt"); } buffer = calloc(1, length); ip = (struct ip *)buffer; ip->ip_v = IPVERSION; ip->ip_hl = (sizeof(struct ip) >> 2); ip->ip_tos = 0; #if defined(__APPLE__) ip->ip_len = len; #else ip->ip_len = htons(len); #endif ip->ip_id = htons(17); ip->ip_off = 0; ip->ip_ttl = 40; ip->ip_p = IPPROTO_SCTP; ip->ip_sum = 0; ip->ip_src.s_addr = INADDR_ANY; ip->ip_dst.s_addr = sin.sin_addr.s_addr; for (i = 0; i < n; i++) { if (sendto(fd, buffer, length, 0, (const struct sockaddr *)&sin, (socklen_t)sizeof(struct sockaddr_in)) != length) { perror("send"); } } if (close(fd) < 0) { perror("close"); } free(buffer); return (0); }