minor fix to return E_USAGE on -V instead of exit(0);
[oss-qm-packages.git] / lib / sockets.c
blob737a8f626f17f67199f8a9a44c25cfdf9aacc432
1 /* sockets.c. Rewriten by Andi Kleen. Subject to the GPL. */
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. */
9 #include <sys/socket.h>
10 #include <stdio.h>
11 #include <unistd.h>
13 #include "config.h"
14 #include "sockets.h"
15 #include "intl.h"
16 #include "util.h"
17 #include "net-support.h"
19 int skfd = -1; /* generic raw socket desc. */
21 int sockets_open(int family)
23 struct aftype **aft;
24 int sfd = -1;
25 static int force = -1;
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;
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;
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;
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;
62 if (sfd < 0)
63 fprintf(stderr, _("No usable address families found.\n"));
64 return sfd;