man/pt_BR/ifconfig.8: remove untranslated paragraph
[platform/upstream/net-tools.git] / lib / sockets.c
1 /* sockets.c. Rewriten by Andi Kleen. Subject to the GPL. */
2
3 /* philb 14/11/98: we now stash the socket file descriptor inside
4    the `aftype' structure rather than keeping it in a pile of separate
5    variables.  This is necessary so that "ifconfig eth0 broadcast ..."
6    issues ioctls to the right socket for the address family in use;
7    picking one at random doesn't always work.  */
8
9 #include <sys/socket.h>
10 #include <stdio.h>
11 #include <unistd.h>
12
13 #include "config.h"
14 #include "sockets.h"
15 #include "intl.h"
16 #include "util.h"
17 #include "net-support.h"
18
19 int skfd = -1;                  /* generic raw socket desc.     */
20
21 int sockets_open(int family)
22 {
23     struct aftype **aft;
24     int sfd = -1;
25     static int force = -1;
26
27     if (force < 0) {
28         force = 0;
29         if (kernel_version() < KRELEASE(2, 1, 0))
30             force = 1;
31         if (access("/proc/net", R_OK))
32             force = 1;
33     }
34     for (aft = aftypes; *aft; aft++) {
35         struct aftype *af = *aft;
36         int type = SOCK_DGRAM;
37         if (af->af == AF_UNSPEC)
38             continue;
39         if (family && family != af->af)
40             continue;
41         if (af->fd != -1) {
42             sfd = af->fd;
43             continue;
44         }
45         /* Check some /proc file first to not stress kmod */
46         if (!family && !force && af->flag_file) {
47             if (access(af->flag_file, R_OK))
48                 continue;
49         }
50 #if HAVE_AFNETROM
51         if (af->af == AF_NETROM)
52             type = SOCK_SEQPACKET;
53 #endif
54 #if HAVE_AFX25
55        if (af->af == AF_X25)
56            type = SOCK_SEQPACKET;
57 #endif
58         af->fd = socket(af->af, type, 0);
59         if (af->fd >= 0)
60             sfd = af->fd;
61     }
62     if (sfd < 0)
63         fprintf(stderr, _("No usable address families found.\n"));
64     return sfd;
65 }