*expose DHCP server addr to client script
authorKonrad Rosenbaum <konrad@silmor.de>
Sun, 4 Oct 2009 13:11:34 +0000 (13:11 +0000)
committerKonrad Rosenbaum <konrad@silmor.de>
Sun, 4 Oct 2009 13:11:34 +0000 (13:11 +0000)
*fix crash on missing IAs

git-svn-id: https://silmor.de/svn/misc/tdhcp@136 752dcaaa-03e5-0310-b894-97e397d490cd

client.c

index 4285199..fa73b3c 100644 (file)
--- a/client.c
+++ b/client.c
@@ -59,6 +59,7 @@ struct option longopt[]= {
  "    $DNSDOM - space-separated list of DNS search domain names\n"\
  "    $IPADDR - space-separated list of assigned IP addresses\n"\
  "    $PREFIX - space-separated list of assigned prefixes\n"\
+ "    $DHCPSRV - the address of the DHCPv6 server that responded\n"\
  "\n"\
  "TDHCP client options:\n" \
  "  -h | --help\n" \
@@ -116,7 +117,7 @@ static void printhelp()
 /*maximum amount of any item that we can handle: 16 is sensitive for addresses, prefixes and DNS settings*/
 #define MAXITEMS 16
 
-static struct in6_addr addresses[MAXITEMS], prefixes[MAXITEMS], dnsservers[MAXITEMS];
+static struct in6_addr addresses[MAXITEMS], prefixes[MAXITEMS], dnsservers[MAXITEMS], dhcpserver;
 static char **dnsnames;
 static unsigned char prefixlens[MAXITEMS];
 static struct in6_addr NULLADDR;
@@ -193,6 +194,7 @@ static int handlemessage(struct dhcp_msg*rmsg,struct dhcp_msg*smsg)
        /*find PREFIX info*/
        if(getprefix){
                p=messagefindoption(rmsg,OPT_IAPD);
+               if(p>=0)
                for(i=0;i<rmsg->msg_opt[p].opt_numopts;i++)
                        if(rmsg->msg_opt[p].subopt[i].opt_type==OPT_IAPREFIX){
                                j=addaddr(prefixes,rmsg->msg_opt[p].subopt[i].opt_iaprefix.prefix);
@@ -203,10 +205,13 @@ static int handlemessage(struct dhcp_msg*rmsg,struct dhcp_msg*smsg)
        /*find IANA info*/
        if(getaddress){
                p=messagefindoption(rmsg,OPT_IANA);
+               if(p>=0)
                for(i=0;i<rmsg->msg_opt[p].opt_numopts;i++)
                        if(rmsg->msg_opt[p].subopt[i].opt_type==OPT_IAADDR)
                                addaddr(addresses,rmsg->msg_opt[p].subopt[i].opt_iaaddress.addr);
        }
+       /*copy server address*/
+       memcpy(&dhcpserver,&rmsg->msg_peer.sin6_addr,16);
        /*check for rapid commit or type=REPLY; if so: tell caller it can stop now*/
        if(rmsg->msg_type==MSG_REPLY)return 0;
        if(messagefindoption(rmsg,OPT_RAPIDCOMMIT)>=0)return 0;
@@ -276,6 +281,8 @@ static int execscript()
                strncat(buf,dnsnames[i],sizeof(buf));
        }
        if(buf[0])setenv("DNSDOM",buf,1);
+       /*dhcp server addr*/
+       setenv("DHCPSRV",inet_ntop(AF_INET6,&dhcpserver,tmp,sizeof(tmp)),1);
        /*call*/
        return system(script)!=0;
 }