How do I output my host’s IP addresses from a C pr

2019-01-12 05:50发布

I need to display all the IP addresses from my local computer, using the C language. How can this be done?

标签: c linux sockets ip
8条回答
女痞
2楼-- · 2019-01-12 05:58

Not a complete solution yet, but take a look in /proc/net!

  • dev lists the available interface devices by name,
  • route lists some routes, as does ipv6_route,
  • arp lists devices in the actual routing table (does not include localhost).

Not as high-tech as the other solution, but it can be done with simple file reading. Linux specific, though.

查看更多
爷、活的狠高调
3楼-- · 2019-01-12 06:06

Another way to do it in C. I do have to say though.... there are so many ways to do it from the shell, what's the point?

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>

#include <errno.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>


void show_address_info( struct ifaddrs *ifa ){
  struct sockaddr_in *s4;
  struct sockaddr_in6 *s6;
  /* ipv6 addresses have to fit in this buffer */
  char buf[64];

  if (AF_INET == ifa->ifa_addr->sa_family){
    s4 = (struct sockaddr_in *)(ifa->ifa_addr);
    if (NULL == inet_ntop(ifa->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf))){
      printf("%s: inet_ntop failed!\n", ifa->ifa_name);
    } else {
      printf("IPv4 addr %s: %s\n", ifa->ifa_name, buf);
    }
  }
  else if (AF_INET6 == ifa->ifa_addr->sa_family) {
    s6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
    if (NULL == inet_ntop(ifa->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf))) {
      printf("%s: inet_ntop failed!\n", ifa->ifa_name);
    } else {
      printf("IPv6 addr %s: %s\n", ifa->ifa_name, buf);
      }
  }

}


int main(int argc, char **argv){
  struct ifaddrs *myaddrs, *ifa;
  int status;

  status = getifaddrs(&myaddrs);
  if (status != 0){
    perror("getifaddrs failed!");
    exit(1);
  }

  for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next){
    if (NULL == ifa->ifa_addr){
      continue;
    }
    if ((ifa->ifa_flags & IFF_UP) == 0) {
      continue;
    }
    show_address_info(ifa);
  }
  freeifaddrs(myaddrs);
  return 0;
}
查看更多
我只想做你的唯一
4楼-- · 2019-01-12 06:13

You need POSIX function getaddrinfo() - it returns linked list of all IP addresses.

See man getaddrinfo for details and examples.

查看更多
戒情不戒烟
5楼-- · 2019-01-12 06:14
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

/*
 * Who sez?
 * http://blog.stackoverflow.com/2010/01/stack-overflow-where-we-hate-fun/
 */
int main(int argc, char *argv[])
{
  int status;
  const char * const cmd =  /* die from END is too chatty */
    "/sbin/ifconfig -a | \
     perl -lne \
       'print $1 if /inet6? addr:\\s*(\\S+)/; \
        END { $. > 0 or \
                warn(\"no output from ifconfig\\n\"), \
                exit 1; }'";

  status = system(cmd);
  if (status < 0) {
    perror("system");
    return 1;
  }
  else if (status != 0) {
    const char *extra;
    status = WEXITSTATUS(status);
    extra = status == 127 ? " (is /bin/sh ok?)" : "";
    fprintf(stderr, "%s: command failed with status %d%s\n",
            argv[0], status, extra);
  }

  return 0;
}
查看更多
相关推荐>>
6楼-- · 2019-01-12 06:17
$ sudo ifconfig | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1
213.xx.xxx.xx
192.168.xx.x
127.0.0.1

And you can put that into popen():

/* not tested */
ph = popen("sudo ifconfig | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1", "r");
while (fgets(buf, sizeof buf, ph)) {
    /* ip address, in nul-terminated string format, is in `buf` */
}
pclose(ph);
查看更多
The star\"
7楼-- · 2019-01-12 06:21

How about you simply cheat and look at the source of /sbin/ifconfig/ ? Nothing wrong with standing on the shoulders of other giants...

查看更多
登录 后发表回答