summaryrefslogtreecommitdiff
blob: bf3286b650d31e9e415b7ece0eff5a1a085f5517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>

/*
 * open a socket, get the process group information of the socket, and use the
 * socket to get the network interface configuration list
 */
main(int argc, char *argv[])
{
  int sock;
  int ioctl_result;

  /* get a socket */
  sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0)
    {
      perror("Cannot create socket");
      exit(1);
    }

  /* use ioctl() to get the process group information */
  {
    int get_process_group;

    ioctl_result = ioctl(sock, SIOCGPGRP, (char *) &get_process_group);

    if (ioctl_result < 0)
      {
        int my_errno = errno;

        fprintf(stderr, "errno %d ", my_errno);
        perror("ioctl(get process group)");
      }
  }

  /* use ioctl() to get the interface configuration list */
  {
    static struct ifconf ifc;	/* init to 0 */

    ioctl_result = ioctl(sock, SIOCGIFCONF, (char *) &ifc);

    if (ioctl_result < 0)
      {
        int my_errno = errno;

        fprintf(stderr, "errno %d ", my_errno);
        perror("ioctl(get interface configuration list)");
      }
  }
}