Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Structure of dhclient.leases(5) lease { ... } entry
- #
- f_struct_define DHCP_LEASE \
- interface \
- fixed_address \
- filename \
- server_name \
- script \
- medium \
- host_name \
- subnet_mask \
- routers \
- domain_name_servers \
- domain_name \
- broadcast_address \
- dhcp_lease_time \
- dhcp_message_type \
- dhcp_server_identifier \
- dhcp_renewal_time \
- dhcp_rebinding_time \
- renew \
- rebind \
- expire
- # int dhcpParseLeases(char *file, char *hostname, char *domain,
- # char *nameserver, char *ipaddr, char *gateway,
- # char *netmask)
- f_dhcp_parse_leases() # $leasefile struct_name
- {
- local leasefile="$1" struct_name="$2"
- [ "$struct_name" ] || return $FAILURE
- if [ ! -e "$leasefile" ]; then
- f_dprintf "%s: No such file or directory\n" "$leasefile"
- return $FAILURE
- fi
- f_struct $struct_name && $struct_name.free
- f_struct_new DHCP_LEASE $struct_name
- eval "$( awk -v struct=$struct_name '
- BEGIN {
- lease_found = 0
- keyword_list = " \
- interface \
- fixed-address \
- filename \
- server-name \
- script \
- medium \
- "
- split(keyword_list, keywords, FS)
- time_list = "renew rebind expire"
- split(time_list, times, FS)
- option_list = " \
- host-name \
- subnet-mask \
- routers \
- domain-name-servers \
- domain-name \
- broadcast-address \
- dhcp-lease-time \
- dhcp-message-type \
- dhcp-server-identifier \
- dhcp-renewal-time \
- dhcp-rebinding-time \
- "
- split(option_list, options, FS)
- }
- function set_value(prop,value)
- {
- lease_found = 1
- gsub(/[^[:alnum:]_]/, "_", prop)
- sub(/;$/, "", value)
- sub(/^"/, "", value)
- sub(/"$/, "", value)
- sub(/,.*/, "", value)
- printf "%s[%s] = \"%s\"\n", struct, prop, value
- }
- /^lease {$/, /^}$/ \
- {
- if ( $0 ~ /^lease {$/ ) next
- if ( $0 ~ /^}$/ ) exit
- for (k in keywords)
- {
- keyword = keywords[k]
- if ( $1 == keyword )
- {
- set_value(keyword, $2)
- next
- }
- }
- for (t in times)
- {
- time = times[t]
- if ( $1 == time )
- {
- set_value(time, $2 " " $3 " " $4)
- next
- }
- }
- if ( $1 != "option" ) next
- for (o in options)
- {
- option = options[o]
- if ( $2 == option )
- {
- set_value(option, $3)
- next
- }
- }
- }
- EXIT {
- if ( ! lease_found )
- {
- print struct ".free"
- print "return $FAILURE"
- }
- }
- ' "$leasefile" )"
- }
- # static void dhcpGetInfo(Device *devp)
- f_dhcp_get_info() # $interface
- {
- local interface="$1"
- local leasefile="/var/db/dhclient.leases.$interface"
- # If it fails, do it the old-fashioned way
- local _ipaddr _netmask _gateway _hostname="" _nameserver=""
- if f_dhcp_parse_leases "$leasefile" lease; then
- setvar $VAR_IPADDR $( lease[fixed_address] )
- setvar $VAR_NETMASK $( lease[subnet_mask] )
- local _routers="$( lease[routers] )"
- setvar $VAR_GATEWAY ${_routers%%,*}
- local _nameservers="$( lease[domain_name_servers] )"
- setvar $VAR_NAMESERVERS ${_nameservers%%,*}
- f_quietly lease[host_name] &&
- setvar $VAR_HOSTNAME $( lease[host_name] )
- lease.free
- else
- # Bah, now we have to get the information from ifconfig
- f_dprintf "DHCP configured interface returns %s\n" \
- "$( ifconfig "$interface" )"
- setvar $VAR_IPADDR $( f_ifconfig_inet "$interface" )
- setvar $VAR_NETMASK $( f_ifconfig_netmask "$interface" )
- setvar $VAR_GATEWAY $( f_route_get_default )
- fi
- # If we didn't get a name server value, hunt for it in resolv.conf
- if [ -r "$RESOLV_CONF" -a ! "$( f_getvar $VAR_NAMESERVER )" ]; then
- local _nameservers="$( f_resolv_conf_nameservers )"
- _nameserver="${_nameservers%%[$IFS]*}"
- setvar $VAR_NAMESERVER "$_nameserver"
- fi
- return $SUCCESS
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement