Add some error checks. Fix memory leak in mx record list. Remove debug translation.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40880 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2011-03-08 20:56:37 +00:00
parent f1aceeca70
commit 3e3735a84c
3 changed files with 41 additions and 34 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ AutoConfig::GetInfoFromMailAddress(const char* email, provider_info *info)
status_t
AutoConfig::GetMXRecord(const char* provider, provider_info *info)
{
BObjectList<mx_record> mxList;
BObjectList<mx_record> mxList(5, true);
DNSQuery dnsQuery;
if (dnsQuery.GetMXRecords(provider, &mxList) != B_OK)
return B_ERROR;
+31 -32
View File
@@ -4,7 +4,6 @@
#include <stdio.h>
#include <ByteOrder.h>
#include <Catalog.h>
#include <FindDirectory.h>
#include <NetAddress.h>
#include <NetEndpoint.h>
@@ -19,9 +18,6 @@
#define PRINT(a...)
#endif
#undef B_TRANSLATE_CONTEXT
#define B_TRANSLATE_CONTEXT "E-Mail"
static vint32 gID = 1;
@@ -98,12 +94,15 @@ BRawNetBuffer::ReadUint32(uint32& value)
status_t
BRawNetBuffer::ReadString(BString& string)
{
if (fReadPosition >= fBuffer.BufferLength())
return B_ERROR;
char* buffer = (char*)fBuffer.Buffer();
buffer = &buffer[fReadPosition];
// if the string is compressed we have to follow the links to the
// sub strings
while (*buffer != 0) {
while (fReadPosition < fBuffer.BufferLength() && *buffer != 0) {
if (uint8(*buffer) == 192) {
// found a pointer mark
buffer++;
@@ -172,8 +171,8 @@ DNSTools::GetDNSServers(BObjectList<BString>* serverList)
register FILE* fp = fopen(path.Path(), "r");
if (fp == NULL) {
fprintf(stderr, B_TRANSLATE("failed to open '%s' to read "
"nameservers: %s\n"), path.Path(), strerror(errno));
fprintf(stderr, "failed to open '%s' to read nameservers: %s\n",
path.Path(), strerror(errno));
return B_ENTRY_NOT_FOUND;
}
@@ -293,14 +292,14 @@ DNSQuery::ReadDNSServer(in_addr* add)
if (firstDNS == NULL || inet_aton(firstDNS->String(), add) != 1)
return B_ERROR;
PRINT(B_TRANSLATE("dns server found: %s \n"), firstDNS->String());
PRINT("dns server found: %s \n", firstDNS->String());
return B_OK;
}
status_t
DNSQuery::GetMXRecords(BString serverName, BObjectList<mx_record>* mxList,
bigtime_t timeout)
DNSQuery::GetMXRecords(const BString& serverName,
BObjectList<mx_record>* mxList, bigtime_t timeout)
{
// get the DNS server to ask for the mx record
in_addr dnsAddress;
@@ -314,12 +313,12 @@ DNSQuery::GetMXRecords(BString serverName, BObjectList<mx_record>* mxList,
_AppendQueryHeader(buffer, &header);
BString serverNameConv = DNSTools::ConvertToDNSName(serverName);
buffer.AppendString(serverNameConv.String());
buffer.AppendString(serverNameConv);
buffer.AppendUint16(uint16(MX_RECORD));
buffer.AppendUint16(uint16(1));
// send the buffer
PRINT(B_TRANSLATE("send buffer\n"));
PRINT("send buffer\n");
BNetAddress netAddress(dnsAddress, 53);
BNetEndpoint netEndpoint(SOCK_DGRAM);
if (netEndpoint.InitCheck() != B_OK)
@@ -327,30 +326,31 @@ DNSQuery::GetMXRecords(BString serverName, BObjectList<mx_record>* mxList,
if (netEndpoint.Connect(netAddress) != B_OK)
return B_ERROR;
PRINT(B_TRANSLATE("Connected\n"));
PRINT("Connected\n");
#ifdef DEBUG
int32 bytesSend =
#endif
netEndpoint.Send(buffer.Data(), buffer.Size());
PRINT(B_TRANSLATE("bytes send %i\n"), int(bytesSend));
int32 bytesSend = netEndpoint.Send(buffer.Data(), buffer.Size());
if (bytesSend == B_ERROR)
return B_ERROR;
PRINT("bytes send %i\n", int(bytesSend));
// receive buffer
BRawNetBuffer receiBuffer(512);
netEndpoint.SetTimeout(timeout);
#ifdef DEBUG
int32 bytesRecei =
#endif
netEndpoint.ReceiveFrom(receiBuffer.Data(), 512, netAddress);
PRINT(B_TRANSLATE("bytes received %i\n"), int(bytesRecei));
int32 bytesRecei = netEndpoint.ReceiveFrom(receiBuffer.Data(), 512,
netAddress);
if (bytesRecei == B_ERROR)
return B_ERROR;
PRINT("bytes received %i\n", int(bytesRecei));
dns_header receiHeader;
_ReadQueryHeader(receiBuffer, &receiHeader);
PRINT(B_TRANSLATE("Package contains :"));
PRINT(B_TRANSLATE("%d Questions, "), receiHeader.q_count);
PRINT(B_TRANSLATE("%d Answers, "), receiHeader.ans_count);
PRINT(B_TRANSLATE("%d Authoritative Servers, "), receiHeader.auth_count);
PRINT(B_TRANSLATE("%d Additional records\n"), receiHeader.add_count);
PRINT("Package contains :");
PRINT("%d Questions, ", receiHeader.q_count);
PRINT("%d Answers, ", receiHeader.ans_count);
PRINT("%d Authoritative Servers, ", receiHeader.auth_count);
PRINT("%d Additional records\n", receiHeader.add_count);
// remove name and Question
BString dummyS;
@@ -364,11 +364,10 @@ DNSQuery::GetMXRecords(BString serverName, BObjectList<mx_record>* mxList,
resource_record_head rrHead;
_ReadResourceRecord(receiBuffer, &rrHead);
if (rrHead.type == MX_RECORD) {
mx_record *mxRec = new mx_record;
mx_record* mxRec = new mx_record;
_ReadMXRecord(receiBuffer, mxRec);
PRINT(B_TRANSLATE("MX record found pri %i, name %s\n"),
mxRec->priority,
mxRec->serverName.String());
PRINT("MX record found pri %i, name %s\n",
mxRec->priority, mxRec->serverName.String());
// Add mx record to the list
mxList->AddItem(mxRec);
mxRecordFound = true;
+9 -1
View File
@@ -58,6 +58,14 @@ public:
// see also http://prasshhant.blogspot.com/2007/03/dns-query.html
struct dns_header {
dns_header()
{
q_count = 0;
ans_count = 0;
auth_count = 0;
add_count = 0;
}
uint16 id; // A 16 bit identifier
unsigned char qr :1; // query (0), or a response (1)
@@ -90,7 +98,7 @@ public:
DNSQuery();
~DNSQuery();
status_t ReadDNSServer(in_addr* add);
status_t GetMXRecords(BString serverName,
status_t GetMXRecords(const BString& serverName,
BObjectList<mx_record>* mxList,
bigtime_t timeout = 500000);