检查这是否为空

标签 c struct arguments

我使用这行代码作为打印 Linux 上接口(interface)的 IP 地址的代码段的最后一部分。

printf ("%s\n", inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));

这工作得很好,但如果我不输入接口(interface)名称作为参数,就会出现段错误。

有没有办法检查此代码是否:

inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr)

是否已提供参数?谢谢。

这是上下文的完整源代码。

/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/********************************************************************
* Description:
* Author: John Cartwright <>
* Created at: Wed Jan 13 11:44:29 AEDT 2016
* Computer: ubuntu
* System: Linux 4.3.0-5-generic on x86_64
*
* Copyright (c) 2016 John Cartwright,,,  All rights reserved.
*
********************************************************************/

#include <linux/kernel.h>       /* for struct sysinfo */
#include <sys/sysinfo.h>        /* For the sysinfo struct. */
#include <stdio.h>
#include <string.h>     /* for strncpy */
#include <unistd.h>     /* for close */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdlib.h> /* For the system() function. */
#include <arpa/inet.h>

#include "info.h"

#define BUF 0x05

int main (int argc, char **argv)
{
    int fd;
    struct ifreq ifr;
    char *myarg1 = argv[2];
    char hostname[128];
    char *iface = myarg1;
    unsigned char *mac;
    char command[50];

    if (argv[1] == NULL)
    {
        printf ("********************************************\n");
        printf ("*          Simple system information.      *\n");
        printf ("*                                          *\n");
        printf ("* IP info: --ip <IFACE>                    *\n");
        printf ("* Print ARP table: --arp                   *\n");
        printf ("* Print sys info: --system                 *\n");
        printf ("********************************************\n");
        printf ("\nThe PID of this program was: %i\n", getpid ());
    }

    /* Getting information about the routing table. */

    if (argc > 1 && strncmp (argv[1], "--arp", BUF) == 0)
    {
        printf ("This is the routing table.\n");
        char *arg[] = { "cat", "/proc/net/arp", NULL };
        execvp (arg[0], arg);
    }

    if (argc > 1 && strncmp (argv[1], "--system", BUF) == 0)
    {
        information();
    }


    if (argc > 1 && strncmp (argv[1], "--hostname", BUF) == 0)
    {
        gethostname(hostname, sizeof hostname);
        printf("Hostname: %s\n", hostname);
    }

    if (argc > 1 && strncmp (argv[1], "--ip", BUF) == 0)
    {
        // This code from: http://www.binarytides.com/c-program-to-get-mac-address-from-interface-name-on-linux/
        FILE *f;
        char line[100] , *p , *c;
        char myip;

        f = fopen("/proc/net/route" , "r");

        while(fgets(line , 100 , f))
        {
            p = strtok(line , " \t");
            c = strtok(NULL , " \t");

            if(p!=NULL && c!=NULL)
            {
                if(strcmp(c , "00000000") == 0)
                {
                    printf("Default interface is : %s \n" , p);
                    break;
                }
            }
        }

        fd = socket(AF_INET, SOCK_DGRAM, 0);

        ifr.ifr_addr.sa_family = AF_INET;
        strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);

        ioctl(fd, SIOCGIFHWADDR, &ifr);

        close(fd);

        mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;

        //display mac address
        printf("Mac : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], \
        mac[3], mac[4], mac[5]);

        fd = socket (AF_INET, SOCK_DGRAM, 0);
        /* I want to get an IPv4 IP address */
        ifr.ifr_addr.sa_family = AF_INET;

        /* I want IP address attached to the specified interface. */
        strncpy (ifr.ifr_name, myarg1, IFNAMSIZ - 1);

        ioctl (fd, SIOCGIFADDR, &ifr);

        close (fd);

        // End binarytides code.

        /* display result */
        printf ("IP information.\n");
        printf("Gateway IP:\n");

        strcpy( command, "/sbin/ip route | awk '/default/ { print $3 }'" );
        system(command);

        printf("IP address:\n");
        printf ("%s\n", inet_ntoa (((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));
    }

    return 0;
}

最佳答案

我怀疑问题在于 sin_add 为 NULL,但被取消引用,这就是导致段错误的原因。

按照 M.M 的建议,从这里开始向后推算。

关于检查这是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001940/

相关文章:

c - 访问结构中的字符指针字段

c++ - 我如何修改此函数,以便如果函数的参数是特定结构,它会返回具有更大整数的结构?

c# - 如何计算结构实例的大小?

c - 作为 const 或操作数的正确参数

C Lang - 如何检测文件是否已用作输入参数?

debugging - 如何在带有Golang的VSCode中以 Debug模式传递命令行参数

c - 根据初始格式解析字符串

转换c指针类型

c++ - 生成直到给定数字 N 的步进数字

c - C 中的 strftime 警告错误