DNSLookUp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <string.h>
00013 #include <OPENR/OPENRAPI.h>
00014 #include "DNSLookUp.h"
00015 #include <EndpointTypes.h>
00016 #include <DNSEndpointMsg.h>
00017 #include "entry.h"
00018 
00019 DNSLookUp::DNSLookUp()
00020 {
00021 }
00022 
00023 OStatus
00024 DNSLookUp::DoInit(const OSystemEvent& event)
00025 {
00026     return oSUCCESS;
00027 }
00028 
00029 OStatus
00030 DNSLookUp::DoStart(const OSystemEvent& event)
00031 {
00032     ipstackRef = antStackRef("IPStack");
00033     antEnvCreateEndpointMsg createMsg(EndpointType_DNS, 16*1024);
00034     createMsg.Call(ipstackRef, sizeof(createMsg));
00035     if (createMsg.error != ANT_SUCCESS) {
00036         OSYSLOG1((osyslogERROR, "%s : %s %d",
00037                   "DNSLookUp::DoStart()",
00038                   "createMsg.Call() FAILED", createMsg.error));
00039         return oFAIL;
00040     }
00041 
00042     dnsEndpoint = createMsg.moduleRef;
00043     GetDefaultServerByAddr();
00044 
00045     return oSUCCESS;
00046 }    
00047 
00048 OStatus
00049 DNSLookUp::DoStop(const OSystemEvent& event)
00050 {
00051     return oSUCCESS;
00052 }
00053 
00054 OStatus
00055 DNSLookUp::DoDestroy(const OSystemEvent& event)
00056 {
00057     return oSUCCESS;
00058 }
00059 
00060 void
00061 DNSLookUp::GetDefaultServerByAddrCont(ANTENVMSG msg)
00062 {
00063     DNSEndpointGetHostByAddrMsg* hostMsg
00064         = (DNSEndpointGetHostByAddrMsg*)antEnvMsg::Receive(msg);
00065     
00066     char addr[16];
00067     hostMsg->host_address.GetAsString(addr);
00068 
00069     if (hostMsg->n_address > 0) {
00070         OSYSPRINT(("Default Server : %s\n", hostMsg->name));
00071         OSYSPRINT(("Address        : %s\n", addr));
00072     } else {
00073         OSYSPRINT(("%s Non-existent host/domain\n", addr));
00074     }
00075 
00076     DNSEndpointGetDefaultDomainNameMsg getDomainMsg(dnsEndpoint);
00077     getDomainMsg.Call(ipstackRef, sizeof(getDomainMsg));
00078     if (getDomainMsg.error == DNS_SUCCESS) {
00079         OSYSPRINT(("Default domain : %s\n", getDomainMsg.name));
00080     }
00081 
00082     Prompt();
00083 }
00084 
00085 void
00086 DNSLookUp::GetHostByNameCont(ANTENVMSG msg)
00087 {
00088     DNSEndpointGetHostByNameMsg* hostMsg
00089         = (DNSEndpointGetHostByNameMsg*)antEnvMsg::Receive(msg);
00090 
00091     if (hostMsg->n_address > 0) {
00092         char addr[16];
00093         hostMsg->host_address.GetAsString(addr);
00094         OSYSPRINT(("Name    : %s\n", hostMsg->name));
00095         OSYSPRINT(("Address : %s\n", addr));
00096     } else {
00097         OSYSPRINT(("%s Non-existent host/domain\n", hostMsg->name));
00098     }
00099     
00100     Prompt();
00101 }
00102 
00103 void
00104 DNSLookUp::GetDefaultServerByAddr()
00105 {
00106     //
00107     // DNS server and default domain name are specified in WLANCONF.TXT
00108     // 
00109     //   DNS_SERVER_1=10.0.1.1
00110     //   DNS_DEFDNAME=example.net
00111     //
00112 
00113     DNSEndpointGetServerAddressesMsg getServerAddressesMsg(dnsEndpoint);
00114     getServerAddressesMsg.Call(ipstackRef, sizeof(getServerAddressesMsg));
00115     if (getServerAddressesMsg.error != DNS_SUCCESS) {
00116         OSYSLOG1((osyslogERROR, "%s : %s %d",
00117                   "DNSLookUp::GetDefaultServerByAddr()",
00118                   "getServerAddressesMsg.Call() DNSEndpointError",
00119                   getServerAddressesMsg.error));
00120         return;
00121     }
00122 
00123     antError error;
00124     DNSEndpointGetHostByAddrMsg getHostMsg(dnsEndpoint,
00125                                            getServerAddressesMsg.addrList[0]);
00126     error = getHostMsg.Send(ipstackRef, myOID_,
00127                             Extra_Entry[entryGetDefaultServerByAddrCont],
00128                             sizeof(getHostMsg));
00129     if (error != ANT_SUCCESS) {
00130         OSYSLOG1((osyslogERROR, "%s : %s %d",
00131                   "DNSLookUp::GetDefaultServerByAddr()",
00132                   "getHostMsg.Send() antError", error));
00133     }
00134 }
00135 
00136 void
00137 DNSLookUp::GetHostByName(char* hostname)
00138 {
00139     antError error;
00140     DNSEndpointGetHostByNameMsg getHostMsg(dnsEndpoint, hostname);
00141     error = getHostMsg.Send(ipstackRef,
00142                             myOID_, Extra_Entry[entryGetHostByNameCont],
00143                             sizeof(getHostMsg));
00144     if (error != ANT_SUCCESS) {
00145         OSYSLOG1((osyslogERROR, "%s : %s %d",
00146                   "DNSLookUp::GetHostByName()",
00147                   "getHostMsg.Send() FAILED", error));
00148     }
00149 }
00150 
00151 void
00152 DNSLookUp::Prompt()
00153 {
00154     OSYSPRINT(("DNSLookUp> "));
00155     char line[128];
00156     gets(line);
00157 
00158     if (strcmp(line, "shutdown") == 0) {
00159         OSYSPRINT(("SHUTDOWN ...\n"));
00160         OBootCondition bootCond(obcbPAUSE_SW);
00161         OPENR::Shutdown(bootCond);
00162     } else {
00163         //
00164         // If you don't put a '.' at the end of an FQDN, 
00165         // it's not recognized as an FQDN.
00166         //
00167         // See RFC1912.
00168         //
00169         char* ptr;
00170         if ((ptr = strrchr(line, '.')) != 0) {
00171             int len = strlen(line);
00172             char* last = line + len - 1;
00173             if (ptr != last) {
00174                 line[len]   = '.';
00175                 line[len+1] = 0;
00176             }
00177         }
00178 
00179         GetHostByName(line);
00180     }
00181 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1