来自recvfrom的错误

时间:2018-01-09 21:44:57

标签: c

我尝试创建一个将icmp数据包发送到另一台计算机的函数,当另一台计算机发回一些东西时,该函数返回1,否则为0.但是recvfrom函数返回错误:" errno:资源暂时不可用"。我在Mac OS X上工作,所以我不包含linux内核的头文件。任何人都可以帮助我,因为我被卡住了。

#include "info.h"


char *getip()
{
    char buffer[256];
    struct hostent *h;

    gethostname(buffer, 256);
    h = gethostbyname(buffer);

    return inet_ntoa(*(struct in_addr *)h->h_addr);

}


int host_alive(char *dst_addr, char *src_addr)
{
    struct ip *ippacket;
    struct ip *ip_reply;
    struct icmp *icmppacket;
    struct sockaddr_in connection;
    struct timeval tv;
    char *packet;
    char *buffer;
    int optval;
    int addrlen;
    int size;
    int sock = 0;

    packet = malloc(sizeof(struct ip) + sizeof(struct icmp));
    buffer = malloc(sizeof(struct ip) + sizeof(struct icmp));

    check(getuid() == 0, "Root priviliges are needed. Try: sudo ./bin/main");

    ippacket = (struct ip *) packet;
    icmppacket = (struct icmp *) (packet + sizeof(struct ip));

    ippacket->ip_hl = 5;
    ippacket->ip_v = 4;
    ippacket->ip_tos = 0;
    ippacket->ip_len = sizeof(struct ip) + sizeof(struct icmp);
    ippacket->ip_id = htons(random());
    ippacket->ip_ttl = 255;
    ippacket->ip_p = IPPROTO_ICMP;
    inet_aton(src_addr, &ippacket->ip_src);
    inet_aton(dst_addr, &ippacket->ip_dst);

    tv.tv_sec = 5;
    tv.tv_usec = 0;

    check((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1,\
          "Failed to create socket");

    check(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int)) != -1,\
          "Failed to set the option to the socket.");
    check(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(struct timeval)) != -1,\
          "Failed to set the option to the socket.");

    icmppacket->icmp_type = ICMP_ECHO;
    icmppacket->icmp_code = 0;
    icmppacket->icmp_id = 0;
    icmppacket->icmp_seq = 0;
    icmppacket->icmp_cksum = in_cksum((unsigned short *)icmppacket, sizeof(struct icmp));

    ippacket->ip_sum = in_cksum((unsigned short *)ippacket, sizeof(struct ip));

    connection.sin_family = AF_INET;
    connection.sin_addr.s_addr = inet_addr(dst_addr);

    sendto(sock, packet, ippacket->ip_len, 0, (struct sockaddr *)&connection,\
          sizeof(struct sockaddr));

    addrlen = sizeof(connection);
    check((size = recvfrom(sock, buffer, sizeof(struct ip) + sizeof(struct icmp), 0,\
      (struct sockaddr *)&connection, (socklen_t *)&addrlen)) != -1,\
      "Failed to receive a message.");

    printf("Received %d byte reply from %s:\n", size , dst_addr);
    ip_reply = (struct ip*) buffer;
    printf("ID: %d\n", ntohs(ip_reply->ip_id));
    printf("TTL: %d\n", ip_reply->ip_ttl);

    close(sock);

    free(packet);
    free(buffer);

    return 1;

  error:
    if (sock)
        close(sock);
    free(packet);
    free(buffer);
    return 0;
}


unsigned short in_cksum(unsigned short *addr, int len)
{
    int sum = 0;
    u_short answer = 0;
    u_short *w = addr;
    int nleft = len;

    while (nleft > 1) {
        sum += *w++;
        nleft -= 2;
    }

    if (nleft == 1) {
        *(u_char *) (&answer) = *(u_char *) w;
        sum += answer;
    }
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    answer = ~sum;
    return (answer);
}

1 个答案:

答案 0 :(得分:3)

根据recvfrom的文档,如果您使用非阻止通话,则会出现这种情况:

如果套接字上没有可用消息,则接收调用等待消息到达,除非套接字是非阻塞的(参见fcntl(2)),在这种情况下返回值-1和外部变量      能够将错误设置为EAGAIN。接收呼叫通常会返回任何可用的数据,直到请求的数量,而不是等待收到所请求的全部金额;这种行为受到了影响      SO_RCVLOWAT中描述的套接字级选项SO_RCVTIMEOgetsockopt(2)

如果您对哪些值映射到哪些错误感到好奇,可以在errno中查找/usr/include/sys/errno.h个值。

如果您希望阻止此功能,您可能需要设置MSG_WAITALL标志,其中"请求操作阻止,直到满足完整请求为止#34;

通常在低级UNIX套接字代码中,您在该套接字上执行select等待读取信号,然后调用recvfrom以接收数据当且仅当该信号触发。您也可以进行非阻塞接收,并在EAGAIN上等待一段时间后再尝试,但效率较低。