You are not logged in.

#1 2010-07-15 02:06:15

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Carrier sense based wired dhcp network.

Thought I'd share this small modification of /etc/rc.d/network I made.

Background:
On my laptop I switch between wired and wireless network often enough that I think havving to manually set up eth0 each time is irritating, on the other hand, so is the 30s boot delay from dhcpcd if booting with the intention of running wireless. So I made the following modifications of the functions ifup and ifdown in /etc/rc.d/network in order to add carrier sense on chosen interfaces using dhcp:

ifup()
{
        if [ "$1" = "" ]; then
                echo "usage: $0 ifup <interface_name>"
                return 1
        fi

        eval ifcfg="\$${1}"

        # Get the name of the interface from the first token in the string

        if [ "${ifcfg%%:*}" = "dhcp" ]; then # << HERE some exctraction code, since the line in /etc/rc.conf is changed
                ifname="$1"
        else
                ifname=${ifcfg%% *}
        fi

        /sbin/ifconfig $ifname up

        wi_up $1 || return 1

        if [ "${ifcfg%%:*}" = "dhcp" ]; then
                # remove the .pid file if it exists
                /bin/rm -f /var/run/dhcpcd-${1}.pid >/dev/null 2>&1
                /bin/rm -f /var/run/dhcpcd-${1}.cache >/dev/null 2>&1

                dhcpopts=${ifcfg#*:}

                sleep ${dhcpopts#*:} 2>/dev/null # << HERE It is possible to specify a delay, so that the device can power on before carrier is checked

                if [ "${dhcpopts%:*}" != "sense" -o  $(cat /sys/class/net/$ifname/carrier) = 1 ]; then # << HERE check for carrier
                        /sbin/dhcpcd $DHCPCD_ARGS ${1}
                else
                        echo No carrier detected on $ifname.
                        return 1
                fi
        else
                /sbin/ifconfig $ifcfg
        fi
        return $?
}

(the change in ifdown is similar to the first marked "dhcp" extraction change above)

I think it works now...

With this extra code I can specify carrier sense in /etc/rc.d as e.g.
eth0="dhcp" -- as before
eth0="dhcp:sense" -- apply carrier sense
eth0="dhcp:sense:delay" -- wait a period of time before checking for carrier.

Answers to strawman reactions:

--Put the -t parameter with a small value in /etc/conf.d/dhcpcd and you won't have to wait those 30s.
Yes, I saw $DHCPCD_ARGS somewhere in the midst of coding, but could to my relief note that it didn't really solve the problem the way I wanted;
Specifying a 3s timeout is enough for the interface to power on and report if a carrier is present (and thus judge if it is worth the trouble of starting dhcpcd) - but if there would be a carrier present, a -t 3 parameter to dhcpcd would make dhcpcd timeout before it has finished to negotiate an ip.
So now it takes 3s to bypass the network if no cable is plugged in, and 3s+ip negotiation time if a cable is plugged in.

--Just get a proper network manager
Eh. It is more interesting this way.


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#2 2010-07-15 02:20:49

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Carrier sense based wired dhcp network.

I'm not sure if I get what you want to do here.

> manually set up eth0 each time
What? Why? You can use both / either interface automatically.

> so is the 30s boot delay from dhcpcd if booting with the intention of running wireless.
DAEMONS=(syslog-ng dbus ... @network ...)

Offline

#3 2010-07-15 02:30:50

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Carrier sense based wired dhcp network.

> What? Why? You can use both / either interface automatically.
I am not sure what you mean, with the standard setup dhcpcd still waits 30 seconds for a bip from the dhcp server even if the cable is unplugged.
This is what I want to avoid, but instead of investing time in reading up on network managers I did it this way.

> DAEMONS=(syslog-ng dbus ... @network ...)
Yes, but because I am an lazyass geek^W^W^W me, I preferred some bash rather than reading up on what daemons needed what daemons to have finished loading (i.e. what can be reliably backgrounded)

Also, my room is full with slightly sub-par, but one hundred percent original wheels.

Last edited by tlvb (2010-07-15 02:32:06)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#4 2010-07-15 02:34:57

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Carrier sense based wired dhcp network.

It's your system, you're free to do as you wish. I simply felt obliged to show another way, so Archers can choose the one they like more.

> still waits 30 seconds for a bip from the dhcp server
IIRC it waits up to 30s - it may be as little as 5.

Offline

#5 2010-07-15 03:09:28

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Carrier sense based wired dhcp network.

If you're going to use Bashisms in the script, you may as well use [[ ]] and && || instead of the POSIX builtins.

However, you can simplify the parameter splitting by using read.

dhcpopts='dhcp:sense:10'
IFS=':' read dhcp sense delay <<< "$dhcpopts"

[[ $dhcp = dhcp ]] && echo 'dhcp'
[[ $sense = sense ]] && echo 'sense'
[[ -n $delay ]] && echo $delay

Note that this still uses Bashisms to avoid a subshell.

Last edited by falconindy (2010-07-15 03:09:51)

Offline

#6 2010-07-15 09:18:48

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Carrier sense based wired dhcp network.

> IIRC it waits up to 30s - it may be as little as 5.
Yes, but if the cable is unplugged it will wait those  30s (or -t time) even though there is no hope of ever getting an ip.
This is what I can avoid, by only running dhcp if there is a carrier present.

falconindy> [...]
I used [ ] etc because that was what the other if statements did, haven't checked if I would gain anything from [[ ]]...
But the option extraction is prettier than mine imo.


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

Board footer

Powered by FluxBB