gpt4 book ai didi

检查这是否为空

转载 作者:行者123 更新时间:2023-11-30 19:39:54 25 4
gpt4 key购买 nike

我使用这行代码作为打印 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com