					Implementation of dns caching in proxyserver
					--------------------------------------------

																		Author   : R.Jeyasudha
																		Date 	   : 08-October-1999
																		Revision : 1
			
																		Author   : R.Jeyasudha
																		Date 	   : 22-October-1999
																		Revision : 2


Scheme as of now, today
-----------------------
What is DNS caching ?
1. All proxy clients can have ProxyServer ( secured lan ip address ) as their
DNS server in their TCP/IP stack. No need to configure a valid DNS server 
address. 

2. ProxyServer internally maintains a DNS cache - DNS_REPLY_CACHE ( an array 
of 32 entries max ) . Each entry will have a DNS query as well as the 
response pkt for that query. Along with that, min_ttl_value as well as the 
length of dns response pkt will also be stored. This response pkt will be, 
as it is got from the actual DNS server. We won't modify any of the bytes in 
that pkt, except for scanning those pkts, to find out the min_ttl_value among 
all the rrs in that pkt. "min_ttl_value" will be the least ttl_value of all 
the rrs in that response pkt. This "min_ttl_value" will be decremented in a
timer loop. Once we found that, min_ttl_value of an entry is zero, it is now
time, for the entry to be deleted from the cache. It is no longer valid, to
be stored. The length of the response pkt, stored, will be used while sending
the response pkt to client's, upon their DNS query & a matching found. Length
size of memory has to be malloced, to frame the response pkt from proxy to
the client.

3. So when a client, requests for DNS query, proxy will first search in
its dns reply cache, for that DNS query. If matching entry is found in cache, 
(query name matching, question count in query name should be 1) proxy frames 
DNS reply packet & sends it back to the client. If no matching entry is found, 
proxy will try to contact either its configured DNS server address or recent 
ISP negotiated DNS server address & will proxy the client's DNS query. When 
the response comes from those actual DNS servers, proxy caches those responses 
against DNS query name as the key & then sends the response back to client, as 
if it were resolved by Proxy. The whole concept of contacting DNS server & 
getting the reply back, what DNS server contacted etc., are transparent to the 
clients.

Issues in DNS caching 
---------------------
Which DNS server address should be contacted ? Order of preference ?
At present, we maintain the order like :
1. If ISP negotiated primary DNS server addr present, contact that DNS server.
This negotiated value is the recent one. In the sense, say, we have 3 wan
ports. Wan 3 has negotiated recently, compared to the other 2 wan ports. In 
this case, ISP negotiated primary DNS server addr will be the one, 
negotiated by Wan 3 & ISP. And not the ones negotiated by the other 2 links.

Note :
-----
ISP negotiated DNS server addr, Primary as well as Secondary, by that, we 
mean, actually, non-zero DNS server addresses.
	
Actual Strategy of DNS options negotiation with ISP:
----------------------------------------------------
If we have configured DNS primary server address alone in IP section, & not
secondary DNS server addr, we will negotiate with ISP for secondary DNS 
server address. Similarly, if we have configured secondary server address 
alone, we will negotiate with ISP for Primary DNS server addr. If we have
configured both primary as well as secondary DNS server addr, we won't 
negotiate with ISP, for DNS servers. If we have not configured both primary
as well as secondary, we will negotiate for both the DNS server addresses
with ISP.

The simple concept is that, for whichever DNS server addr we have not 
configured, ( primary or secondary or both ) we will negotiate accordingly 
with ISP & will update our resp DNS server addr.

DNS server addr updation in DHCP entries as well as in DNS caching
------------------------------------------------------------------
Whenever we negotiate DNS options with ISP,(for whatever wan port it may be)
& get a non-zero DNS server addr, we will update 
recent_ppp_negotiated_dns_primary_server_address as well as
recent_ppp_negotiated_dns_secondary_server_address variables accordingly, 
for all the ports. This is for DNS caching.

Initially, those 2 variables will be initialized to 0x00000000L. Whenever, 
any of the wan port is coming up & negotiates DNS options with ISP, these
vars are updated for all the ports. Or rather overwritten every time, to 
reflect the negotiated DNS server addresses of the recently up wan port.

The order of preference as of now :	( giving preference to negotiated values
rather than configured ).

	if (negotiated_primary_DNS_server)
		get(negotiated_primary_DNS_server);
	else
		if (negotiated_secondary_DNS_server)
			get(negotiated_secondary_DNS_server);
		else
			if (configured_primary_DNS_server)
				get(configured_primary_DNS_server);
			else
				if (configured_secondary_DNS_server)
					get(configured_secondary_DNS_server);

The proposed order of preference will be : ( preference to be given to primary
first irrespective of configured or negotiated, than secondary. Also, 
configured has to be given priority than negotiated ).

	if (configured_primary_DNS_server)
		get(configured_primary_DNS_server);
	else
		if (negotiated_primary_DNS_server)
			get(negotiated_primary_DNS_server);
		else
			if (configured_secondary_DNS_server)
				get(configured_secondary_DNS_server);
			else
				if (negotiated_secondary_DNS_server)
					get(negotiated_secondary_DNS_server);
		
The present code has to be changed according to this scheme, once got approval
from Kevin. Change is needed only in 
	* dnsresp.c file -> in get_dns_server_address(port_number) function.

/* sudha Revision 2... */

22-Oct-1999
-----------
Changed in code according to the proposed order of preference & after getting
approved by Kevin.

/* ...sudha Revision 2 */

Regarding DNS server updation in DHCP entries, right now, the scheme is :
Irrespective of whether any DNS server addr is configured or not & for all 
the DHCP entries, we will update DNS server :
*  when any primary DNS addr is configured. If not primary, if some secondary
	DNS addr is configured.
* 	whenever we negotiate any one of primary or secondary DNS server
	address with ISP, we try to overwrite or update, this negotiated primary 
	DNS server addr as the DNS server address for all the entries in DHCP.

Some errors are there in this scheme.
* If only secondary has been negotiated, even in this case, we will be 
  updating with primary negotiated value, which may be zero value. So in
  this case, the actual configured value will also be gone, for DHCP entries.

/* sudha Revision 2... */

22-Oct-1999
-----------
* How to prevent this error ? 
	We can just check for non-zero DNS server addr, before updating in DHCP.
This checking can be done even before calling the function to update DNS
server in DHCP entries.

This is done, right now!!!

/* ...sudha Revision 2 */

* Also, while updating in DHCP 
		file -> dhcpsuti.c
		function -> update_dns_server_address_in_dhcp_address_list(server_address)
	we are not checking for non-zero address of server address.
* DNS caching concept is not made use of for DHCP clients. That is, in case
	if we find that, get_dns_server_address(port_number) function ( key function
	which determines, finally, which DNS server addr is used for DNS caching )
	returns a non-zero DNS server address, we can update the DNS addr of all
	the DHCP entries with our Proxy's lan addr. If the function returns a zero
	IP address, then we can update the negotiated DNS server addr after checking
	properly for non-zero values. In this way, we can achieve DNS caching for 
	DHCP clients also. We should however implement it. 

But right now, haven't changed in code, for that.	
