- 论坛徽章:
- 0
|
写了一个测试交换机的性能的工具, 运行时提示: Sendto: Invalid argument
大家帮忙看看。我是菜鸟。
- extern int quit_id, sockfd, ifindex;
- extern char *head[POOL_MAX], destmac[ETH_ALEN];
- extern struct sockaddr_in toaddr;
- int
- send_hdr(char *head[])
- {
- size_t buflen = sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr);
- size_t addrlen = sizeof(struct sockaddr_ll);
- int i, k;
- struct sockaddr_ll toif;
-
- toif.sll_family = PF_PACKET;
- toif.sll_protocol = htons(ETH_P_IP);
- toif.sll_ifindex = ifindex;
- toif.sll_hatype = ARPHRD_ETHER;
- toif.sll_pkttype = PACKET_OTHERHOST;
- toif.sll_halen = ETH_ALEN;
- memset(toif.sll_addr, 0, 8);
- memcpy(toif.sll_addr, destmac, ETH_ALEN);
- while(1) {
- i = get_rand_n(POOL_MAX);
-
- if (sendto(sockfd, head[i], buflen, 0, (struct sockaddr *)&toif, addrlen) < 0) {
- perror("Sendto");
- }
- if (quit_id == 1) {
- goto over;
- }
- }
- over:
- return(0);
- }
- int
- build_hdr(char *head[], struct sockaddr_in *to)
- {
- int i, k;
- struct ethhdr *ethh;
- struct iphdr *iph;
- struct tcphdr *tcph;
- size_t ethhlen, iphlen, tcphlen, buf_size;
- ethhlen = sizeof(struct ethhdr);
- iphlen = sizeof(struct iphdr);
- tcphlen = sizeof(struct tcphdr);
- buf_size = ethhlen + iphlen + tcphlen;
- unsigned char srcmac[ETH_ALEN];
- rand_init();
- for (i = 0; i < POOL_MAX; i ++) {
- head[i] = (char *)malloc(buf_size);
- if (head[i] == NULL) {
- return(1);
- }
- ethh = (struct ethhdr *)head[i];
- iph = (struct iphdr *)(head[i] + ethhlen);
- tcph = (struct tcphdr *)(head[i] + ethhlen + iphlen);
- /* build ethernet header */
- for (k = 0; k < ETH_ALEN; k ++) {
- srcmac[k] = get_rand_uint8();
- }
- memcpy(ethh -> h_dest, destmac, ETH_ALEN);
- memcpy(ethh -> h_source, srcmac, ETH_ALEN);
- ethh -> h_proto = 0x0800;
- /* build ip header */
- iph -> version = 4;
- iph -> ihl = 5; /* min valid ip header length in 32bits word; 5*4 Byte = 20 Byte */
- iph -> tos = 0;
- iph -> tot_len = htons(iphlen + tcphlen); /* ip packet total length */
- iph -> id = get_rand_uint16();
- iph -> frag_off = 0;
- iph -> ttl = get_rand_uint8();
- iph -> protocol = IPPROTO_TCP;
- iph -> check = 0;
- iph -> saddr = get_rand_uint32(); /* spoof source address */
- iph -> daddr = (to -> sin_addr).s_addr;
- /* build tcp header */
- tcph -> source = get_rand_uint16(); /* spoof source port */
- tcph -> dest = to -> sin_port;
- tcph -> seq = htonl(get_rand_uint32() );
- tcph -> ack_seq = 0;
- tcph -> doff = sizeof(struct tcphdr) / 4; /* measured in 32bits word */
- tcph -> res1 = 0;
- tcph -> res2 = 0;
- tcph -> urg = 0;
- tcph -> ack = 0;
- tcph -> psh = 0;
- tcph -> syn = 1;
- tcph -> fin = 0;
- tcph -> rst = 0;
- tcph -> window = htonl(get_rand_uint16() );
- tcph -> urg_ptr = 0;
- tcph -> check = tcp_checksum( iph -> saddr,
- iph -> daddr,
- (unsigned short *)tcph,
- tcphlen);
- }
- }
复制代码
- int
- main(int argc, char **argv)
- {
- ...
- if ((sockfd = socket(PF_INET, SOCK_RAW, htons(ETH_P_ALL))) < 0 ) {
- perror("Socket");
- return(1);
- }
- if (ioctl(sockfd, SIOCGIFINDEX, &ifr ) < 0) {
- perror("Ioctl");
- return(1);
- }
-
- build_hdr(head, &toaddr);
-
- send_hdr(head);
- ...
- }
复制代码 |
|