#include #include #include #include #include #include #include #include #include #include #include #include #include #define MAXBUFLEN 65535 uint16_t interval = 2; uint16_t sendsize = 4; uint16_t sendcount = 1; const char *dstport = "7"; /* udp echo */ void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(int argc, char *argv[]) { int sockfd; struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; struct sockaddr_in sa; socklen_t addr_len; int numbytes, rv; int optval = 1; char buf[MAXBUFLEN]; char s[INET6_ADDRSTRLEN]; struct timeval tv; uint32_t ipoptval; if (argc != 2) { fprintf(stderr,"usage: send hostname\n"); exit(1); } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ((rv = getaddrinfo(argv[1], dstport, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and make a socket for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("send: socket"); continue; } break; } if (p == NULL) { fprintf(stderr, "send: failed to create socket\n"); return 2; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); rv = bind(sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr)); rv = connect(sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr)); #define NEW_OPTIONS htonl(IPOPT_EOL | (IPOPT_NOP << 8) | (IPOPT_NOP << 16) \ | (IPOPT_NOP << 24)) ipoptval = NEW_OPTIONS; socklen_t ipoptlen = sizeof(ipoptval); if ((setsockopt(sockfd, IPPROTO_IP, IP_OPTIONS, &optval, ipoptlen) != 0)) { perror("set ip option"); } memset(buf, '4', sendsize); if ((numbytes = sendto(sockfd, buf, sendsize, 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("send: sendto"); exit(1); } printf("send: sent %d bytes to %s\n", numbytes, argv[1]); freeaddrinfo(servinfo); close(sockfd); return 0; }