首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自recvfrom的错误

来自recvfrom的错误
EN

Stack Overflow用户
提问于 2018-01-09 21:44:56
回答 1查看 924关注 0票数 0

我试图创建一个函数,将icmp数据包发送到另一台计算机,当另一台计算机发回某物时,该函数返回1,否则返回0。但是recvfrom函数返回一个错误:"errno:资源暂时不可用“。我在Mac上工作,所以我不包括来自linux内核的头。有人能帮我吗因为我被困住了。

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-09 21:54:39

根据recvfrom的文档,如果您使用的是非阻塞调用,则需要这样做:

如果套接字上没有可用的消息,接收调用将等待消息到达,除非套接字是非阻塞的(请参阅fcntl(2)),在这种情况下,值-1被返回,外部vari可执行的errno设置为EAGAIN。接收呼叫通常返回任何可用的数据,直到请求的数量为止,而不是等待收到所请求的全部金额;此行为受到getsockopt(2)中描述的套接字级选项SO_RCVLOWATSO_RCVTIMEO的影响。

如果您想知道哪些值映射到哪些错误,则可以在errno中查找/usr/include/sys/errno.h值。

如果要阻止此函数,则可能需要设置MSG_WAITALL标志,该标志“请求操作块,直到满足完整请求”。

通常,在低级UNIX代码中,您会在该套接字上执行一个select以等待读取信号,然后调用recvfrom来接收该信号触发的情况下的数据。您也可以进行非阻塞接收,并且在EAGAIN上只需等待一段短时间才能再次尝试,尽管这样做的效率较低。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48177104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档