## coova-chilli_1.0.12-wpad.patch 
##
## Author: Michael Mende <kontakt@failover.de> http://www.failover.de
## Copyright: (C) 2008 Michael Mende
## License: same as coovachilli v1.0.12
##

--- coova-chilli-1.0.12.orig/src/dhcp.h
+++ coova-chilli-1.0.12/src/dhcp.h
@@ -40,6 +40,9 @@
 #define DHCP_OPTION_CLIENT_IDENTIFIER 61
 #define DHCP_OPTION_CLIENT_FQDN    81
 
+/* coova-chilli_1.0.12-wpad.patch */
+#define DHCP_OPTION_WPADURL        252
+
 /* !!highly experimental!! */
 #define DHCP_OPTION_CALLED_STATION_ID  197
 #define DHCP_OPTION_CAPTIVE_PORTAL_ACL 198
@@ -92,6 +95,9 @@
 
 #define DHCP_DNAT_MAX        64
 
+/* coova-chilli_1.0.12-wpad.patch */
+#define DHCP_WPADURL_LEN     64
+
 struct dhcp_conn_t {
   int inuse;                    /* Free = 0; Inuse = 1 */
   time_t lasttime;      /* Last time we heard anything from client */
@@ -108,6 +114,7 @@
   struct in_addr dns1;         /* Client DNS address */
   struct in_addr dns2;         /* Client DNS address */
   char domain[DHCP_DOMAIN_LEN];/* Domain name to use for DNS lookups */
+  char wpadurl[DHCP_WPADURL_LEN];/* coova-chilli_1.0.12-wpad.patch */
   int authstate;               /* 0: Unauthenticated, 1: Authenticated */
   uint8_t unauth_cp;           /* Unauthenticated codepoint */
   uint8_t auth_cp;             /* Authenticated codepoint */
@@ -212,7 +219,8 @@
 int dhcp_set_addrs(struct dhcp_conn_t *conn, 
 	       struct in_addr *hisip, struct in_addr *hismask,
 	       struct in_addr *ourip, struct in_addr *ourmask,
-	       struct in_addr *dns1, struct in_addr *dns2, char *domain);
+	       struct in_addr *dns1, struct in_addr *dns2, char *domain, 
+	       char *wpadurl);/* coova-chilli_1.0.12-wpad.patch */
 
 
 /* Called whenever a packet arrives */
--- coova-chilli-1.0.12.orig/src/options.c
+++ coova-chilli-1.0.12/src/options.c
@@ -778,6 +778,10 @@
   if (options.domain) free(options.domain);
   options.domain = STRDUP(args_info.domain_arg);
 
+  /* coova-chilli_1.0.12-wpad.patch */
+  if (options.wpadurl) free(options.wpadurl);
+  options.wpadurl = STRDUP(args_info.wpadurl_arg);
+
   if (options.ipup) free(options.ipup);
   options.ipup = STRDUP(args_info.ipup_arg);
 
--- coova-chilli-1.0.12.orig/src/cmdline.c
+++ coova-chilli-1.0.12/src/cmdline.c
@@ -130,6 +130,7 @@
   "      --dnsparanoia             Inspect DNS packets and drop responses with any \n                                  non- A, CNAME, SOA, or MX records (to prevent \n                                  dns tunnels)  (default=off)",
   "      --usetap                  Use a TAP instead of TUN (linux only)  \n                                  (default=off)",
   "      --routeif=STRING          Interface to use as default route; turns on \n                                  'manual' routing",
+  "      --wpadurl=STRING          URL of wpad.dat",
     0
 };
 
@@ -201,6 +202,7 @@
   args_info->dns1_given = 0 ;
   args_info->dns2_given = 0 ;
   args_info->domain_given = 0 ;
+  args_info->wpadurl_given = 0 ;/* coova-chilli_1.0.12-wpad.patch */
   args_info->ipup_given = 0 ;
   args_info->ipdown_given = 0 ;
   args_info->conup_given = 0 ;
@@ -319,6 +321,9 @@
   args_info->dns2_arg = NULL;
   args_info->dns2_orig = NULL;
   args_info->domain_arg = gengetopt_strdup ("coova.org");
+  /*args_info->wpadurl_arg = gengetopt_strdup ("http://192.168.252.1:80/wpad.dat");*//* coova-chilli_1.0.12-wpad.patch */
+  args_info->wpadurl_arg = NULL;/* coova-chilli_1.0.12-wpad.patch */
+  args_info->wpadurl_orig = NULL;/* coova-chilli_1.0.12-wpad.patch */
   args_info->domain_orig = NULL;
   args_info->ipup_arg = NULL;
   args_info->ipup_orig = NULL;
@@ -727,6 +732,7 @@
   free_string_field (&(args_info->dns2_arg));
   free_string_field (&(args_info->dns2_orig));
   free_string_field (&(args_info->domain_arg));
+  free_string_field (&(args_info->wpadurl_arg));/* coova-chilli_1.0.12-wpad.patch */
   free_string_field (&(args_info->domain_orig));
   free_string_field (&(args_info->ipup_arg));
   free_string_field (&(args_info->ipup_orig));
@@ -907,6 +913,8 @@
     write_into_file(outfile, "dns2", args_info->dns2_orig, 0);
   if (args_info->domain_given)
     write_into_file(outfile, "domain", args_info->domain_orig, 0);
+  if (args_info->wpadurl_given)
+    write_into_file(outfile, "wpadurl", args_info->wpadurl_orig, 0);/* coova-chilli_1.0.12-wpad.patch */
   if (args_info->ipup_given)
     write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
   if (args_info->ipdown_given)
@@ -1649,6 +1657,7 @@
         { "dns1",	1, NULL, 0 },
         { "dns2",	1, NULL, 0 },
         { "domain",	1, NULL, 0 },
+        { "wpadurl",	1, NULL, 0 },
         { "ipup",	1, NULL, 0 },
         { "ipdown",	1, NULL, 0 },
         { "conup",	1, NULL, 0 },
@@ -1995,6 +2004,20 @@
               goto failure;
           
           }
+          /* coova-chilli_1.0.12-wpad.patch  */
+          else if (strcmp (long_options[option_index].name, "wpadurl") == 0)
+          {
+          
+          
+            if (update_arg( (void *)&(args_info->wpadurl_arg), 
+                 &(args_info->wpadurl_orig), &(args_info->wpadurl_given),
+                &(local_args_info.wpadurl_given), optarg, 0, 0, ARG_STRING,
+                check_ambiguity, override, 0, 0,
+                "wpadurl", '-',
+                additional_error))
+              goto failure;
+          
+          }
           /* Script to run after link-up.  */
           else if (strcmp (long_options[option_index].name, "ipup") == 0)
           {
--- coova-chilli-1.0.12.orig/src/chilli.c
+++ coova-chilli-1.0.12/src/chilli.c
@@ -1063,7 +1063,8 @@
 		   &appconn->hisip, &appconn->mask,
 		   &appconn->ourip, &appconn->mask,
 		   &appconn->dns1, &appconn->dns2,
-		   options.domain);
+		   options.domain,
+		   options.wpadurl);/* coova-chilli_1.0.12-wpad.patch */
     
     /* This is the one and only place eapol authentication is accepted */
 
@@ -1085,7 +1086,8 @@
 		   &appconn->hisip, &appconn->mask,
 		   &appconn->ourip, &appconn->mask,
 		   &appconn->dns1, &appconn->dns2,
-		   options.domain);
+		   options.domain,
+		   options.wpadurl);/* coova-chilli_1.0.12-wpad.patch */
 
     /* This is the one and only place UAM authentication is accepted */
     dhcpconn->authstate = DHCP_AUTH_PASS;
@@ -1102,7 +1104,8 @@
 		   &appconn->hisip, &appconn->mask, 
 		   &appconn->ourip, &appconn->mask, 
 		   &appconn->dns1, &appconn->dns2,
-		   options.domain);
+		   options.domain,
+		   options.wpadurl);/* coova-chilli_1.0.12-wpad.patch */
     
     /* This is the one and only place WPA authentication is accepted */
     if (appconn->s_params.flags & REQUIRE_UAM_AUTH) {
@@ -1128,7 +1131,8 @@
 		   &appconn->hisip, &appconn->mask, 
 		   &appconn->ourip, &appconn->mask, 
 		   &appconn->dns1, &appconn->dns2,
-		   options.domain);
+		   options.domain,
+		   options.wpadurl);/* coova-chilli_1.0.12-wpad.patch */
     
     dhcpconn->authstate = DHCP_AUTH_PASS;
     break;
@@ -2671,7 +2675,8 @@
 		 &ipm->addr, &options.mask, 
 		 &appconn->ourip, &appconn->mask,
 		 &options.dns1, &options.dns2, 
-		 options.domain);
+		 options.domain,
+		 options.wpadurl);/* coova-chilli_1.0.12-wpad.patch */
 
   /* if not already authenticated, ensure DNAT authstate */
   if (!appconn->s_state.authenticated)
--- coova-chilli-1.0.12.orig/src/cmdline.h
+++ coova-chilli-1.0.12/src/cmdline.h
@@ -80,6 +80,9 @@
   char * dns2_orig;	/**< @brief Secondary DNS server IP address original value given at command line.  */
   const char *dns2_help; /**< @brief Secondary DNS server IP address help description.  */
   char * domain_arg;	/**< @brief Domain to use for DNS lookups (default='coova.org').  */
+  char * wpadurl_arg;   /* coova-chilli_1.0.12-wpad.patch */
+  char * wpadurl_orig;  /* coova-chilli_1.0.12-wpad.patch */
+  char * wpadurl_help;  /* coova-chilli_1.0.12-wpad.patch */
   char * domain_orig;	/**< @brief Domain to use for DNS lookups original value given at command line.  */
   const char *domain_help; /**< @brief Domain to use for DNS lookups help description.  */
   char * ipup_arg;	/**< @brief Script to run after link-up.  */
@@ -337,6 +340,7 @@
   unsigned int dns1_given ;	/**< @brief Whether dns1 was given.  */
   unsigned int dns2_given ;	/**< @brief Whether dns2 was given.  */
   unsigned int domain_given ;	/**< @brief Whether domain was given.  */
+  unsigned int wpadurl_given ; /* coova-chilli_1.0.12-wpad.patch */
   unsigned int ipup_given ;	/**< @brief Whether ipup was given.  */
   unsigned int ipdown_given ;	/**< @brief Whether ipdown was given.  */
   unsigned int conup_given ;	/**< @brief Whether conup was given.  */
--- coova-chilli-1.0.12.orig/src/dhcp.c
+++ coova-chilli-1.0.12/src/dhcp.c
@@ -1485,6 +1485,14 @@
   memcpy(&packet.dhcp.options[pos], &conn->ourip.s_addr, 4);
   pos += 4;
 
+  /* coova-chilli_1.0.12-wpad.patch */
+  if (strlen(conn->wpadurl)) {
+    packet.dhcp.options[pos++] = DHCP_OPTION_WPADURL;
+    packet.dhcp.options[pos++] = strlen(conn->wpadurl);
+    memcpy(&packet.dhcp.options[pos], &conn->wpadurl, strlen(conn->wpadurl));
+    pos += strlen(conn->wpadurl);
+  }
+
   packet.dhcp.options[pos++] = DHCP_OPTION_END;
 
   /* UDP header */
@@ -1580,6 +1588,14 @@
   memcpy(&packet.dhcp.options[pos], &conn->ourip.s_addr, 4);
   pos += 4;
 
+  /* coova-chilli_1.0.12-wpad.patch */
+  if (strlen(conn->wpadurl)) {
+    packet.dhcp.options[pos++] = DHCP_OPTION_WPADURL;
+    packet.dhcp.options[pos++] = strlen(conn->wpadurl);
+    memcpy(&packet.dhcp.options[pos], &conn->wpadurl, strlen(conn->wpadurl));
+    pos += strlen(conn->wpadurl);
+  }
+
   packet.dhcp.options[pos++] = DHCP_OPTION_END;
 
   /* UDP header */
@@ -1780,10 +1796,11 @@
  * dhcp_set_addrs()
  * Set various IP addresses of a connection.
  **/
+/* coova-chilli_1.0.12-wpad.patch */
 int dhcp_set_addrs(struct dhcp_conn_t *conn, struct in_addr *hisip,
 		   struct in_addr *hismask, struct in_addr *ourip,
 		   struct in_addr *ourmask, struct in_addr *dns1,
-		   struct in_addr *dns2, char *domain) {
+		   struct in_addr *dns2, char *domain, char *wpadurl) {
 
   conn->hisip.s_addr = hisip->s_addr;
   conn->hismask.s_addr = hismask->s_addr;
@@ -1799,6 +1816,15 @@
     conn->domain[0] = 0;
   }
 
+  /* coova-chilli_1.0.12-wpad.patch */
+  if (wpadurl) {
+    strncpy(conn->wpadurl, wpadurl, DHCP_WPADURL_LEN);
+    conn->wpadurl[DHCP_WPADURL_LEN-1] = 0;
+  }
+  else {
+    conn->wpadurl[0] = 0;
+  }
+
   if (options.uamanyip && 
       (hisip->s_addr & ourmask->s_addr) != (ourip->s_addr & ourmask->s_addr)) {
     /**
--- coova-chilli-1.0.12.orig/src/options.h
+++ coova-chilli-1.0.12/src/options.h
@@ -44,6 +44,7 @@
   struct in_addr dns1;           /* Primary DNS server IP address */
   struct in_addr dns2;           /* Secondary DNS server IP address */
   char * domain;                 /* Domain to use for DNS lookups */
+  char * wpadurl;                /* coova-chilli_1.0.12-wpad.patch */
   char * ipup;                   /* Script to run after link-up */
   char * ipdown;                 /* Script to run after link-down */
   char * conup;                  /* Script to run after session/connection-up */

