source: rtems-libbsd/dhcpcd/if-pref.c @ b8fdbe2

55-freebsd-126-freebsd-12
Last change on this file since b8fdbe2 was f2ed769, checked in by Sebastian Huber <sebastian.huber@…>, on 01/30/14 at 12:29:46

DHCPCD(8): Import

Import DHCPCD(8) from:

http://roy.marples.name/projects/dhcpcd/

The upstream sources can be obtained via:

fossil clone http://roy.marples.name/projects/dhcpcd

The imported version is 2014-01-29 19:46:44 [6b209507bb].

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29
30#include "config.h"
31#include "dhcp.h"
32#include "if-pref.h"
33#include "net.h"
34
35/* Interface comparer for working out ordering. */
36static int
37ifcmp(const struct interface *si, const struct interface *ti)
38{
39        int sill, till;
40        const struct dhcp_state *sis, *tis;
41
42        sis = D_CSTATE(si);
43        tis = D_CSTATE(ti);
44        if (sis && !tis)
45                return -1;
46        if (!sis && tis)
47                return 1;
48        if (!sis && !tis)
49                return 0;
50        /* If one has a lease and the other not, it takes precedence. */
51        if (sis->new && !tis->new)
52                return -1;
53        if (!sis->new && tis->new)
54                return 1;
55        /* If we are either, they neither have a lease, or they both have.
56         * We need to check for IPv4LL and make it non-preferred. */
57        if (sis->new && tis->new) {
58                sill = (sis->new->cookie == htonl(MAGIC_COOKIE));
59                till = (tis->new->cookie == htonl(MAGIC_COOKIE));
60                if (!sill && till)
61                        return 1;
62                if (sill && !till)
63                        return -1;
64        }
65        /* Then carrier status. */
66        if (si->carrier > ti->carrier)
67                return -1;
68        if (si->carrier < ti->carrier)
69                return 1;
70        /* Finally, metric */
71        if (si->metric < ti->metric)
72                return -1;
73        if (si->metric > ti->metric)
74                return 1;
75        return 0;
76}
77
78/* Sort the interfaces into a preferred order - best first, worst last. */
79void
80sort_interfaces(void)
81{
82        struct if_head sorted;
83        struct interface *ifp, *ift;
84
85        if (ifaces == NULL ||
86            (ifp = TAILQ_FIRST(ifaces)) == NULL ||
87            TAILQ_NEXT(ifp, next) == NULL)
88                return;
89
90        TAILQ_INIT(&sorted);
91        TAILQ_REMOVE(ifaces, ifp, next);
92        TAILQ_INSERT_HEAD(&sorted, ifp, next);
93        while ((ifp = TAILQ_FIRST(ifaces))) {
94                TAILQ_REMOVE(ifaces, ifp, next);
95                TAILQ_FOREACH(ift, &sorted, next) {
96                        if (ifcmp(ifp, ift) == -1) {
97                                TAILQ_INSERT_BEFORE(ift, ifp, next);
98                                break;
99                        }
100                }
101                if (ift == NULL)
102                        TAILQ_INSERT_TAIL(&sorted, ifp, next);
103        }
104        TAILQ_CONCAT(ifaces, &sorted, next);
105}
Note: See TracBrowser for help on using the repository browser.