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