简体   繁体   English

我不知道如何使用他的指南中的 Beej 程序

[英]I don't know how to use Beej's programm from his Guide

1I'm reading Beej's programming network guide. 1我正在阅读 Beej 的编程网络指南。 I read his code and did an important change to check ipv6 but it's not working right.我阅读了他的代码并进行了重要更改以检查 ipv6,但它无法正常工作。 Can't get an ip Address.无法获得 ip 地址。 How to use this on a Linux system?如何在 Linux 系统上使用它? https://i.imgur.com/USVzBw1.png https://i.imgur.com/USVzBw1.png

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];

    if (argc != 2) {
        fprintf(stderr,"usage: showip hostname\n");
        return 1;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;

    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
        return 2;
    }

    printf("IP addresses for %s:\n\n", argv[1]);

    for(p = res;p != NULL; p = p->ai_next) {
        void *addr;
        char *ipver;

        // get the pointer to the address itself,
        // different fields in IPv4 and IPv6:
        if (p->ai_family == AF_INET) { // IPv4
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        } else if (p->ai_family==AF_INET6) { // IPv6
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }

        // convert the IP to a string and print it:
        inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
        printf("  %s: %s\n", ipver, ipstr);
    }

    freeaddrinfo(res); // free the linked list

    return 0;
}```

This looks suspicious:这看起来很可疑:

You check to make sure argc is 2 to validate you have at least one argument.您检查以确保argc为 2 以验证您至少有一个参数。 Makes sense说得通

if (argc != 2) {

But then later:但后来:

if ((status = getaddrinfo(argv[2], NULL, &hints, &res)) != 0) {

You reference argv[2] , which is undefined when the only valid indicies in argv are 0 and 1 .您引用argv[2] ,当argv中唯一有效的索引是01时,它是未定义的。

Pretty sure you meant to use argv[1] as follows:很确定您打算按如下方式使用argv[1]

if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {

Also, this is a bug:此外,这是一个错误:

    // convert the IP to a string and print it:
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf("  %s: %s\n", ipver, ipstr);

Nothing wrong with those statements, but when p->ai_family is neither AF_INET nor AF_INET6 , then those statements get executed anyway.这些语句没有错,但是当p->ai_family既不是AF_INET也不是AF_INET6时,无论如何都会执行这些语句。 addr and ipver are undefined. addripver未定义。 Hence, undefined behavior.因此,未定义的行为。 And yes, on Linux, there's plenty of address types that are not IP based.是的,在 Linux 上,有很多不基于 IP 的地址类型。

You are welcome to reference my ResolveHostname function on Github .欢迎您在Github 上引用我的 ResolveHostname function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道如何在 c++ 中使用 memcmp - I don't know how to use memcmp in c++ 我不知道它如何工作表[+1]? - I don't know how it work table[+1]? 我不想使用std :: copy ...这是正确的for循环替代方案吗? - i don't want to use std::copy…his this the right for-loop alternative? 我不知道如何使用代码块 win32 gui - I don't know how to use codeblocks win32 gui 当我不知道文件名(c++)时如何使用“fread”? - how to use "fread" when I don't know the file name(c++)? 我从源代码安装了 GCC 5.2,但我不知道如何在 Ubuntu 15.04 上卸载它 - I installed GCC 5.2 from source and I don't know how to uninstall it on Ubuntu 15.04 不知道如何正确使用IShellWindows :: Item - don't know how to use IShellWindows::Item correctly 如果你不知道要读取的字符数,如何使用fgets? - How to use fgets if you don't know the number of characters to be read? 我不知道如何获取从文件中读入的矩阵的const值 - I don't know how to get a const value for my matrix that is read in from a file 这个指针和 memory 代码打印是什么? 我不知道它是在打印垃圾还是如何打印我需要的值 - What is this pointer and memory code printing? I don't know if it's printing garbage or how to print the value that I need
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM