[haiku-commits] r40880 - haiku/trunk/src/preferences/mail

  • From: clemens.zeidler@xxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 8 Mar 2011 21:56:37 +0100 (CET)

Author: czeidler
Date: 2011-03-08 21:56:37 +0100 (Tue, 08 Mar 2011)
New Revision: 40880
Changeset: http://dev.haiku-os.org/changeset/40880

Modified:
   haiku/trunk/src/preferences/mail/AutoConfig.cpp
   haiku/trunk/src/preferences/mail/DNSQuery.cpp
   haiku/trunk/src/preferences/mail/DNSQuery.h
Log:
Add some error checks. Fix memory leak in mx record list. Remove debug 
translation.



Modified: haiku/trunk/src/preferences/mail/AutoConfig.cpp
===================================================================
--- haiku/trunk/src/preferences/mail/AutoConfig.cpp     2011-03-08 20:55:01 UTC 
(rev 40879)
+++ haiku/trunk/src/preferences/mail/AutoConfig.cpp     2011-03-08 20:56:37 UTC 
(rev 40880)
@@ -31,7 +31,7 @@
 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;

Modified: haiku/trunk/src/preferences/mail/DNSQuery.cpp
===================================================================
--- haiku/trunk/src/preferences/mail/DNSQuery.cpp       2011-03-08 20:55:01 UTC 
(rev 40879)
+++ haiku/trunk/src/preferences/mail/DNSQuery.cpp       2011-03-08 20:56:37 UTC 
(rev 40880)
@@ -4,7 +4,6 @@
 #include <stdio.h>
 
 #include <ByteOrder.h>
-#include <Catalog.h>
 #include <FindDirectory.h>
 #include <NetAddress.h>
 #include <NetEndpoint.h>
@@ -19,10 +18,7 @@
 #define PRINT(a...)
 #endif
 
-#undef B_TRANSLATE_CONTEXT
-#define B_TRANSLATE_CONTEXT "E-Mail"
 
-
 static vint32 gID = 1;
 
 
@@ -98,12 +94,15 @@
 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 @@
 
        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 @@
        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 @@
        _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 @@
 
        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 @@
                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;

Modified: haiku/trunk/src/preferences/mail/DNSQuery.h
===================================================================
--- haiku/trunk/src/preferences/mail/DNSQuery.h 2011-03-08 20:55:01 UTC (rev 
40879)
+++ haiku/trunk/src/preferences/mail/DNSQuery.h 2011-03-08 20:56:37 UTC (rev 
40880)
@@ -58,6 +58,14 @@
 
 // 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 @@
                                                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);
                                  


Other related posts:

  • » [haiku-commits] r40880 - haiku/trunk/src/preferences/mail - clemens . zeidler