gap-dev-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gap-dev-discuss] GNUMail doesn't connect to IMAP Server


From: Sebastian Reitenbach
Subject: [Gap-dev-discuss] GNUMail doesn't connect to IMAP Server
Date: Tue, 07 Aug 2012 13:31:07 +0200
User-agent: SOGoMail 1.3.17

Hi,

sending this to the GAP list, since the gnustep-nonfsf doesn't have a m/l.

With the Pantomime/GNUMail from GNUstep-nonfsf I always had the problem that 
I was unable to connect to IMAP Server. It did not even tried to establish a 
connection
to the IMAP Server. I was looking into it, and found it was trying to find get 
the IP of 
the IMAP Server in the CSDNSManager. While parsing the resolv.conf file, and
trying to initialize DNS structure, it did not succeed, and therefore, in 
CSTCPConnection
it then did not tried to connect to the server.

I've seen Riccardo did a lot of things there, but I don't really understand what
he is trying to do. The ChangeLog entry tells me:
        Use res_ninit() on NetBSD to fix long-standing incompatibility

So, reverting the CWDNSManager.[h|m] and the CWTCPConnection.[h|m] to the 
files from Pantomime-1.2.0pre3 makes my GNUMail connect to my IMAP Server. 
But this may or may not break things again for Riccardo on NetBSD.

As always, I'm on OpenBSD.

Any objections to go back to the older versions, and restart looking into the
NetBSD issue again?

Sebastian


$OpenBSD$
--- Framework/Pantomime/CWDNSManager.h.orig     Tue Aug  7 13:11:19 2012
+++ Framework/Pantomime/CWDNSManager.h  Tue Aug  7 13:11:35 2012
@@ -1,7 +1,7 @@
 /*
 **  CWDNSManager.h
 **
-**  Copyright (c) 2004-2007
+**  Copyright (c) 2004
 **
 **  Author: Ludovic Marcotte <address@hidden>
 **
@@ -27,72 +27,29 @@
 
 #import <Foundation/NSArray.h>
 #import <Foundation/NSDictionary.h>
-#import <Foundation/NSNotification.h>
 #import <Foundation/NSObject.h>
-#import <Foundation/NSRunLoop.h>
-#import <Foundation/NSTimer.h>
 
-#ifdef MACOSX
-#import <Foundation/NSMapTable.h>
-#include <CoreFoundation/CoreFoundation.h>
-#endif
-
 /*!
-  @const PantomimeDNSResolutionCompleted
-  @discussion This notification is automatically posted when 
-              the DNS resolution has completed successfully.
-*/
-extern NSString* PantomimeDNSResolutionCompleted;
-
-/*!
-  @const PantomimeDNSResolutionFailed
-  @discussion This notification is automatically posted when
-              the DNS resolution has failed.
-*/
-extern NSString* PantomimeDNSResolutionFailed;
-
-#ifdef MACOSX
-typedef enum {ET_RDESC, ET_WDESC, ET_EDESC} RunLoopEventType;
-/*!
   @class CWDNSManager
   @discussion This class is used in Pantomime to perform DNS resolution.
               Currently, it does not do asynchronous lookups but implements
              a simple cache to speedup repetitive requets.
 */
 @interface CWDNSManager : NSObject
-#else
address@hidden CWDNSManager : NSObject <RunLoopEvents>
-#endif
 {
   @private
-    NSMutableArray *_servers, *_queue;
     NSMutableDictionary *_cache;
-    NSTimer *_timer;
-    id _delegate;
-
-#ifdef MACOSX
-    CFRunLoopSourceRef _runLoopSource;
-    CFSocketContext *_context;
-    CFSocketRef _cf_socket;
-#endif
-
-    unsigned short _packet_id;
-    BOOL _is_asynchronous;
-    int _socket;
 }
 
 /*!
-  @method addressesForName:background:
-  @discussion This method is used to obtain an array of IP
+  @method addressesForName:
+  @discussion This method is used to get obtain an array of IP
               addresses from a fully qualified domain name.
   @param theName The fully qualified domain name.
-  @param theBOOL If YES, the call is non-blocking and returns nil.
-                 Otherwise, the call is block and returns an
-                array of addresses, if any.
   @result The array of addresses encoded as NSData instances.
 */
-- (NSArray *) addressesForName: (NSString *) theName
-                    background: (BOOL) theBOOL;
+- (NSArray *) addressesForName: (NSString *) theName;
+
 /*!
   @method singleInstance
   @discussion This method is used to obtain the shared
$OpenBSD$
--- Framework/Pantomime/CWDNSManager.m.orig     Tue Aug  7 13:10:56 2012
+++ Framework/Pantomime/CWDNSManager.m  Tue Aug  7 13:11:35 2012
@@ -2,10 +2,8 @@
 **  CWDNSManager.m
 **
 **  Copyright (c) 2004-2007
-**                2012 Riccardo Mottola
 **
 **  Author: Ludovic Marcotte <address@hidden>
-**          Riccardo Mottola <address@hidden>
 **
 **  This library is free software; you can redistribute it and/or
 **  modify it under the terms of the GNU Lesser General Public
@@ -25,131 +23,18 @@
 #include <Pantomime/CWDNSManager.h>
 
 #include <Pantomime/CWConstants.h>
-#include <Pantomime/NSData+Extensions.h>
-
-#import <Foundation/NSCharacterSet.h>
-#import <Foundation/NSValue.h>
-#import <Foundation/NSScanner.h>
-
 #ifdef __MINGW32__
 #include <winsock2.h>
-#elif defined (__NetBSD__)
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-#include <resolv.h>
-//#include <res_update.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#define HAS_RES_NINIT 1
 #else
-#include <sys/types.h>      // For u_char on Mac OS X
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #include <netdb.h>
-#include <resolv.h>
 #endif
-
 #include <unistd.h>
 
-#define MAX_PACKET_SIZE 512
-#define MAX_TIMEOUT 2
-
-
-
 static CWDNSManager *singleInstance = nil;
 
-typedef struct _dns_packet_header
-{
-  unsigned short packet_id;
-  unsigned short flags;
-  unsigned short qdcount;
-  unsigned short ancount;
-  unsigned short nscount;
-  unsigned short arcount;
-} dns_packet_header;
-
-typedef struct _dns_packet_question
-{
-  unsigned short qtype;
-  unsigned short qclass;
-} dns_packet_question;
-
-typedef struct _dns_resource_record
-{
-  unsigned short type;
-  unsigned short class;
-  unsigned int ttl;
-  unsigned short rdlength;
-} dns_resource_record;
-
-#ifdef MACOSX
-void dns_socket_callback(CFSocketRef s, CFSocketCallBackType type, CFDataRef 
address, const void* data, void* info)
-{
-  if (type&kCFSocketReadCallBack)
-    {
-      [(CWDNSManager *)info receivedEvent: (void*)CFSocketGetNative(s)
-                      type: ET_RDESC
-                      extra: 0
-                      forMode: nil];
-    }
-}
-#endif
-
 //
 //
 //
address@hidden CWDNSRequest : NSObject
-{
-  @public
-    NSMutableArray *servers;
-    NSData *name;
-    unsigned short packet_id, count;
-}
-
-- (id) initWithName: (NSString *) theName;
-
address@hidden
-
address@hidden CWDNSRequest
-
-- (id) initWithName: (NSString *) theName
-{
-  self = [super init];
-
-  servers = [[NSMutableArray alloc] init];
-  name = RETAIN([theName dataUsingEncoding: NSASCIIStringEncoding]);
-  count = 0;
-
-  return self;
-}
-
-- (void) dealloc
-{
-  RELEASE(servers);
-  RELEASE(name);
-  [super dealloc];
-}
-
address@hidden
-
-//
-//
-//
address@hidden CWDNSManager (Private)
-
-- (void) _parseHostsFile;
-- (void) _parseResolvFile;
-- (void) _processResponse;
-- (void) _sendRequest: (CWDNSRequest *) theRequest;
-- (void) _tick: (id) sender;
-
address@hidden
-
-//
-//
-//
 @implementation CWDNSManager
 
 - (id) init
@@ -157,141 +42,34 @@ void dns_socket_callback(CFSocketRef s, CFSocketCallBa
   self = [super init];
 
   _cache = [[NSMutableDictionary alloc] init];
-  _servers = [[NSMutableArray alloc] init];
-  _queue = [[NSMutableArray alloc] init];
-  _is_asynchronous = NO;
 
-#ifdef MACOSX
-  _runLoopSource = nil;
-  _context = nil;
-  _cf_socket = nil;
-#endif
-
-  [self _parseResolvFile];
-  [self _parseHostsFile];
-
-  if ([_servers count] && (_socket = socket(PF_INET, SOCK_DGRAM, 0)) >= 0)
-    {
-      _is_asynchronous = YES;
-      _packet_id = 1;
-      
-#ifdef MACOSX
-      _context = (CFSocketContext *)malloc(sizeof(CFSocketContext));
-      memset(_context, 0, sizeof(CFSocketContext));
-      _context->info = self;
-      
-      _cf_socket = CFSocketCreateWithNative(NULL, _socket, 
kCFSocketReadCallBack|kCFSocketWriteCallBack, dns_socket_callback, _context);
-      CFSocketDisableCallBacks(_cf_socket, 
kCFSocketReadCallBack|kCFSocketWriteCallBack);
-      
-      if (!_cf_socket)
-       {
-         _is_asynchronous = NO;
-         return self;
-       }
-      
-      _runLoopSource = CFSocketCreateRunLoopSource(NULL, _cf_socket, 1);
-      
-      if (!_runLoopSource)
-       {
-         CFSocketInvalidate(_cf_socket);
-         _is_asynchronous = NO;
-         return self;
-       }
-      
-      CFRunLoopAddSource(CFRunLoopGetCurrent(), _runLoopSource, 
kCFRunLoopCommonModes);
-#else
-      [[NSRunLoop currentRunLoop] addEvent: (void *)_socket
-#ifdef __MINGW32__
-                                 type: ET_HANDLE
-#else
-                                 type: ET_RDESC
-#endif
-                                 watcher: self
-                                 forMode: NSDefaultRunLoopMode];
-#endif
-
-      _timer = [NSTimer scheduledTimerWithTimeInterval: 1
-                       target: self
-                       selector: @selector(_tick:)
-                       userInfo: nil
-                       repeats: YES];
-      RETAIN(_timer);
-      [_timer fire];
-    }
-  
   return self;
 }
 
+
 //
 //
 //
 - (void) dealloc
 {
-  [_timer invalidate];
-  RELEASE(_timer);
-
-#ifdef MACOSX
-  if (CFRunLoopSourceIsValid(_runLoopSource))
-    {
-      CFRunLoopSourceInvalidate(_runLoopSource);
-      CFRelease(_runLoopSource);
-    }
-
-  if (CFSocketIsValid(_cf_socket))
-    {
-      CFSocketInvalidate(_cf_socket);
-    }
-
-  CFRelease(_cf_socket);
-  free(_context);
-#endif
-
   RELEASE(_cache);
-  RELEASE(_servers);
-  RELEASE(_queue);
   [super dealloc];
 }
 
+
 //
 //
 //
-- (NSArray *) addressesForName: (NSString *) theName  background: (BOOL) 
theBOOL
+- (NSArray *) addressesForName: (NSString *) theName
 {
   id o;
 
   o = [_cache objectForKey: theName];
-  
-  if (theBOOL)
-    {      
-      if (o)
-       {
-         POST_NOTIFICATION(PantomimeDNSResolutionCompleted, self, 
([NSDictionary dictionaryWithObjectsAndKeys: theName, @"Name", [o 
objectAtIndex: 0], @"Address", nil]));
-       }
-      else
-       {
-         CWDNSRequest *aRequest;
-         aRequest = AUTORELEASE([[CWDNSRequest alloc] initWithName: theName]);
-         aRequest->packet_id = _packet_id++;
-         aRequest->servers = [[NSMutableArray alloc] initWithArray: _servers];
-         aRequest->count = 0;
 
-         if ([_servers count])
-           {
-             [self _sendRequest: aRequest];
-           }
-       }
-
-      return nil;
-    }
-
   if (!o)
     {
       struct hostent *host_info;
 
-      //
-      // We don't compare with _servers as it might have more
-      // magic to obtain IP addresses from DNS names.
-      //
       host_info = gethostbyname([theName cString]);
       
       if (host_info)
@@ -308,19 +86,7 @@ void dns_socket_callback(CFSocketRef s, CFSocketCallBa
                }
              else
                {
-                 unsigned char c0, c1, c2, c3;
-                 char *buf;
-                 int r;
-                 
-                 buf = host_info->h_addr_list[i];
-                 c0 = *(buf);
-                 c1 = *(buf+1);
-                 c2 = *(buf+2);
-                 c3 = *(buf+3);
-                 
-                 r = ntohl((c0<<24)|(c1<<16)|(c2<<8)|c3);
-
-                 [o addObject: [NSNumber numberWithInt: r]];
+                 [o addObject: [NSData dataWithBytes: 
host_info->h_addr_list[i]  length: host_info->h_length]];
                }
            }
          
@@ -339,30 +105,7 @@ void dns_socket_callback(CFSocketRef s, CFSocketCallBa
   return o;
 }
 
-//
-//
-//
-- (void) receivedEvent: (void *) theData
-                  type: (RunLoopEventType) theType
-                 extra: (void *) theExtra
-               forMode: (NSString *) theMode
-{
-  switch (theType)
-    {
-#ifdef __MINGW32__
-    case ET_HANDLE:
-    case ET_TRIGGER:
-#else
-    case ET_RDESC:
-#endif
-      [self _processResponse];
-      break;
 
-    default:
-      break;
-    }
-}
-
 //
 //
 //
@@ -374,382 +117,6 @@ void dns_socket_callback(CFSocketRef s, CFSocketCallBa
     }
 
   return singleInstance;
-}
-
address@hidden
-
address@hidden CWDNSManager (Private)
-
-//
-// See man 5 hosts for all details
-// on the format of the /etc/hosts file.
-//
-- (void) _parseHostsFile
-{
-  NSData *aData;
-
-  aData = [NSData dataWithContentsOfFile: @"/etc/hosts"];
-  
-  if (aData)
-    {
-      NSArray *allLines;
-      NSString *aString;
-      BOOL b;
-      int i;
-      
-      allLines = [aData componentsSeparatedByCString: "\n"];
-      
-      for (i = 0; i < [allLines count]; i++)
-       {
-         aData = [allLines objectAtIndex: i];
-
-         if ([aData hasCPrefix: "#"]) continue;
-      
-         aString = [[NSString alloc] initWithData: aData  encoding: 
NSASCIIStringEncoding];
-         b = YES;
-
-         if (aString)
-           {
-             NSString *aWord;
-             NSString *theIP;
-             NSScanner *aScanner;
-
-             aScanner = [NSScanner scannerWithString: aString];
-             theIP = nil;
-             
-             [aScanner scanCharactersFromSet: [NSCharacterSet 
whitespaceAndNewlineCharacterSet]  intoString: NULL];
-             
-             while ([aScanner scanUpToCharactersFromSet: [NSCharacterSet 
whitespaceAndNewlineCharacterSet]  intoString: &aWord] == YES)
-               {
-                 if (b)
-                   {
-                     theIP = aWord;
-                     b = NO;
-                     continue;
-                   }
-
-                 [_cache setObject: [NSArray arrayWithObject: [NSNumber 
numberWithInt: inet_addr([theIP UTF8String])]]  forKey: aWord];
-                 [aScanner scanCharactersFromSet: [NSCharacterSet 
whitespaceAndNewlineCharacterSet]  intoString: NULL];
-
-               }
-
-             RELEASE(aString);
-           }
-       }
-    }
-}
-
-//
-// See man 5 resolv.conf for all details
-// on the format of the /etc/resolv.conf  file.
-//
-- (void) _parseResolvFile
-{
-  int i;
-#ifdef HAS_RES_NINIT
-  struct __res_state cw_res;
-#else
-#define cw_res _res
-#endif
-
-#ifdef HAS_RES_NINIT
-  memset(&cw_res, 0, sizeof(cw_res));
-
-  if (res_ninit(&cw_res) == -1)
-      return;
-#else
-  if (res_init() == -1)
-      return; 
-#endif
-
-  if ((cw_res.options & RES_INIT) == 0)
-    return;
-
-  for (i = 0 ; i < cw_res.nscount; i++ )
-  {
-    [_servers addObject: [NSNumber numberWithInt: 
cw_res.nsaddr_list[i].sin_addr.s_addr]];
-  }
-
-#ifdef HAS_RES_NINIT
-  res_ndestroy(&cw_res);
-#endif
-}
-
-
-//
-//
-//
-- (void) _processResponse
-{
-  CWDNSRequest *aRequest;
-  NSString *aString;
-  NSNumber *aNumber;
-
-  dns_resource_record *resource_record;
-  dns_packet_header *header;
-
-  char *buf, qr, ra, rcode, *start;
-  unsigned short flags, i, type;
-  unsigned char c0, c1, c2, c3;
-  int r;
-
-  start = buf = (char *)malloc(MAX_PACKET_SIZE);
-
-  if (recvfrom(_socket, buf, MAX_PACKET_SIZE, 0, NULL, NULL) == -1)
-    {
-      free(buf);
-      return;
-    }
-
-  // We build our packet header. We should get (~118 bytes in total):
-  //
-  // - packet identifier
-  // - flags (0x8180)
-  // - qdcount
-  // - ancount
-  // - nscount
-  // - arcount
-  header = (dns_packet_header *)buf;  
-
-  // We get the right DNSRequest object from the queue
-  // based on our packet ID.
-  aRequest = nil;
-
-  for (i = 0; i < [_queue count]; i++)
-    {
-      aRequest = [_queue objectAtIndex: i];
-      if (aRequest->packet_id == ntohs(header->packet_id)) break;
-    }
-
-  if (!aRequest)
-    {
-      return;
-    }
-
-  flags = ntohs(header->flags);  
-  
-  qr = (char)((flags & 0x8000) >> 15);
-  
-  // We check if we got a response from the server. If not,
-  // we try the next server, if any.
-  if (!qr) return;
-
-  ra = (char)((flags & 0x0080) >> 7);
-
-  // We check if recursive queries are supported by
-  // the server. If not, we try the next server, if any.
-  if (!ra) return;
-
-  rcode = (char)((flags & 0x000F) >> 0);
-
-  // We check if we got any errors...
-  // 2 - server failure
-  // 3 - domain does not exist
-  // 5 - the server refused to serve our query
-  if (rcode)
-    {
-      return;
-    }
-  
-  // We check if we got a response from the server. If not,
-  // we try the next server, if any.
-  if (!htons(header->ancount)) return;
-
-  buf += sizeof(dns_packet_header);
-
-  //
-  // We skip over the Question section.
-  //
-  while (*buf)
-    {
-      buf += (int)(*buf)+1;
-    }
-
-  buf += sizeof(dns_packet_question)+1;
-  
-
-  //
-  // We now read the Answer section of our packet
-  //
-  //                                  1  1  1  1  1  1
-  //    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  //  |                                               |
-  //  /                                               /
-  //  /                      NAME                     /
-  //  |                                               |
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  //  |                      TYPE                     |
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  //  |                     CLASS                     |
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  //  |                      TTL                      |
-  //  |                                               |
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  //  |                   RDLENGTH                    |
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
-  //  /                     RDATA                     /
-  //  /                                               /
-  //  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-  type = 0;
-
-  while (type != 1)
-    {
-      if (!((*buf) & 0xC0))
-       {      
-         while (*buf)  
-           {
-             buf+=(int)(*buf)+1;
-           }
-         
-         buf-=1;
-       }
-      
-      resource_record = (dns_resource_record *)(buf+=2); 
-      type = ntohs(resource_record->type);
-      buf += (sizeof(dns_resource_record)-2)+ntohs(resource_record->rdlength);
-    }
-  
-  buf -= ntohs(resource_record->rdlength);
-  c0 = *(buf);
-  c1 = *(buf+1);
-  c2 = *(buf+2);
-  c3 = *(buf+3);
-  
-  // We store our value in network byte order.
-#if BYTE_ORDER == BIG_ENDIAN
-  r = (c0<<24)|(c1<<16)|(c2<<8)|c3;
-#else
-  r = (c3<<24)|(c2<<16)|(c1<<8)|c0;
-#endif
-
-  aString = AUTORELEASE([[NSString alloc] initWithData: aRequest->name  
encoding: NSASCIIStringEncoding]);
-  aNumber = [NSNumber numberWithInt: r];
-
-  POST_NOTIFICATION(PantomimeDNSResolutionCompleted, self, ([NSDictionary 
dictionaryWithObjectsAndKeys: aString, @"Name", aNumber, @"Address", nil]));
-
-  [_cache setObject: [NSArray arrayWithObject: aNumber]  forKey: aString];
-
-  // We remove our request from the queue
-  [_queue removeObject: aRequest];
-
-  free(start);
-}
-
-//
-//
-//
-- (void) _sendRequest: (CWDNSRequest *) theRequest
-{
-  NSArray *subdomains;
-
-  struct sockaddr_in peer_address;
-  dns_packet_question *question;
-  dns_packet_header *header;
-  unsigned short len, i;
-  char *packet, *start;
-
-  peer_address.sin_family = PF_INET;
-  peer_address.sin_port = htons(53);
-
-  peer_address.sin_addr.s_addr = [[theRequest->servers objectAtIndex: 0] 
intValue];
-  start = packet = (char *)malloc(MAX_PACKET_SIZE);
-
-  // We build our packet header. We have to set:
-  //  
-  // - packet identifier
-  // - flags, we have something like this to fill:
-  //   |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
-  //    0      0000    0  0  1  0     000      0000
-  // - qdcount: 1 entry in the question section.
-  // - ancount: 0 resource record in the answer section
-  // - nscount: 0 name server resource record in the authority records section
-  // - arcount: 0 resource record in the additional records section
-  //
-  //NSLog(@"(0x0100:\t%d to %d\n1:\t\t%d to %d\n", 0x0100, htons(0x0100), 1, 
htons(1));
-  header = (dns_packet_header *)packet;
-  header->packet_id = htons(theRequest->packet_id);
-  header->flags = htons(0x0100);    
-  header->qdcount = htons(1);
-  header->ancount = header->nscount = header->arcount = 0;
-
-  // We build our packet question.
-  packet += sizeof(dns_packet_header);
-
-  // QNAME
-  // a domain name represented as a sequence of labels, where
-  // each label consists of a length octet followed by that
-  // number of octets.  The domain name terminates with the
-  // zero length octet for the null label of the root.  Note
-  // that this field may be an odd number of octets; no
-  // padding is used.
-  //
-  subdomains = [theRequest->name componentsSeparatedByCString: "."];
-  
-  for (i = 0; i < [subdomains count]; i++)
-    {
-      *packet = len = [[subdomains objectAtIndex: i] length];
-      memcpy(++packet, [[subdomains objectAtIndex: i] bytes], len);
-      packet += len;
-    }
-
-  *(packet++) = '\0'; 
-  question = (dns_packet_question *)packet;
-  question->qtype = htons(1);  // Type A: 1 a host address
-  question->qclass = htons(1); // IN: 1 the Internet
-  packet += sizeof(dns_packet_question);
-  
-  len = packet-(char *)header;
-
-  // We queue our DNS request
-  if (![_queue containsObject: theRequest]) [_queue addObject: theRequest];
-  
-  // We send our packet. If we failed to send it, we don't care since
-  // we'll try to send it to an other server shortly after.
-  sendto(_socket, start, len, 0, (struct sockaddr *)&peer_address, 
sizeof(struct sockaddr));
-  
-  free(start);
-}
-
-//
-//
-//
-- (void) _tick: (id) sender
-{
-  int c;
-
-  if ((c = [_queue count]))
-    {
-      CWDNSRequest *aRequest;
-
-      while (c--)
-       {
-         aRequest = [_queue objectAtIndex: c];
-         
-         if (aRequest->count == MAX_TIMEOUT)
-           {
-             if ([aRequest->servers count] > 1)
-               {
-                 [aRequest->servers removeObjectAtIndex: 0];
-                 aRequest->count = 0;
-                 
-                 [self _sendRequest: aRequest];
-               }
-             else
-               {
-                 NSDictionary *aDictionary;
-
-                 aDictionary = [NSDictionary dictionaryWithObject: 
AUTORELEASE([[NSString alloc] initWithData: aRequest->name  encoding: 
NSASCIIStringEncoding])
-                                             forKey: @"Name"];
-                 POST_NOTIFICATION(PantomimeDNSResolutionFailed, self, 
aDictionary);
-
-                 [_queue removeObject: aRequest];
-               }
-           }
-
-         aRequest->count++;
-       }
-    }
 }
 
 @end
$OpenBSD$
--- Framework/Pantomime/CWTCPConnection.h.orig  Tue Aug  7 13:11:09 2012
+++ Framework/Pantomime/CWTCPConnection.h       Tue Aug  7 13:11:29 2012
@@ -1,7 +1,7 @@
 /*
 **  CWTCPConnection.h
 **
-**  Copyright (c) 2001-2007
+**  Copyright (c) 2001-2006
 **
 **  Author: Ludovic Marcotte <address@hidden>
 **
@@ -48,13 +48,8 @@
     BOOL ssl_handshaking;
   
   @private
-    NSString *_name;
-
     unsigned int _connectionTimeout;
-    unsigned int _port;
     int _fd;
-
-    BOOL _dns_resolution_completed;
 
     SSL_CTX *_ctx;
     SSL *_ssl;
$OpenBSD$
--- Framework/Pantomime/CWTCPConnection.m.orig  Tue Aug  7 13:11:02 2012
+++ Framework/Pantomime/CWTCPConnection.m       Tue Aug  7 13:11:24 2012
@@ -1,7 +1,7 @@
 /*
 **  CWTCPConnection.m
 **
-**  Copyright (c) 2001-2007
+**  Copyright (c) 2001-2006
 **
 **  Author: Ludovic Marcotte <address@hidden>
 **
@@ -58,17 +58,6 @@
 //
 //
 //
address@hidden CWTCPConnection (Private)
-
-- (void) _DNSResolutionCompleted: (NSNotification *) theNotification;
-- (void) _DNSResolutionFailed: (NSNotification *) theNotification;
-
address@hidden
-
-
-//
-//
-//
 @implementation CWTCPConnection
 
 + (void) initialize
@@ -77,6 +66,7 @@
   SSL_load_error_strings();
 }
 
+
 //
 //
 //
@@ -113,20 +103,15 @@
   int nonblock = 1;
 #endif
 
+  ssl_handshaking = NO;
+  _ssl = NULL;
+
   if (theName == nil || thePort <= 0)
     {
       AUTORELEASE(self);
       return nil;
     }
-
-  _connectionTimeout = theConnectionTimeout;
  
-  ASSIGN(_name, theName);
-  _port = thePort;
-
-  _dns_resolution_completed = ssl_handshaking = NO;
-  _ssl = NULL;
- 
   // We get the file descriptor associated to a socket
   _fd = socket(PF_INET, SOCK_STREAM, 0);
 
@@ -136,42 +121,25 @@
       return nil;
     }
   
-  [[NSNotificationCenter defaultCenter]
-    addObserver: self
-    selector: @selector(_DNSResolutionCompleted:)
-    name: PantomimeDNSResolutionCompleted
-    object: nil];
+  addresses = [[CWDNSManager singleInstance] addressesForName: theName];
 
-  [[NSNotificationCenter defaultCenter]
-    addObserver: self
-    selector: @selector(_DNSResolutionFailed:)
-    name: PantomimeDNSResolutionFailed
-    object: nil];
-
-  if (!theBOOL)
+  if (!addresses)
     {
-      addresses = [[CWDNSManager singleInstance] addressesForName: theName  
background: NO];
-      
-      if (!addresses)
-       {
-         safe_close(_fd);
-         AUTORELEASE(self);
-         return nil;
-       }
+      safe_close(_fd);
+      AUTORELEASE(self);
+      return nil;
+    }
 
-      _dns_resolution_completed = YES;
+  server.sin_family = AF_INET;
+  memcpy((char *)&server.sin_addr, [[addresses objectAtIndex: 0] bytes], 
[[addresses objectAtIndex: 0] length]);
+  server.sin_port = htons(thePort);
 
-      server.sin_family = AF_INET;
-      server.sin_addr.s_addr = [[addresses objectAtIndex: 0] intValue];
-      server.sin_port = htons(thePort);
-      
-      // If we don't connect in background, we must try to connect right away
-      // and return on any errors.
-      if (connect(_fd, (struct sockaddr *)&server, sizeof(server)) != 0)
-       {
-         AUTORELEASE(self);
-         return nil;
-       }
+  // If we don't connect in background, we must try to connect right away
+  // and return on any errors.
+  if (!theBOOL && connect(_fd, (struct sockaddr *)&server, sizeof(server)) != 
0)
+    {
+      AUTORELEASE(self);
+      return nil;
     }
   
   // We set the non-blocking I/O flag on _fd
@@ -186,8 +154,39 @@
       return nil;
     }
 
-  if (theBOOL) [[CWDNSManager singleInstance] addressesForName: theName  
background: YES];
-
+  // If don't connect in background and we are connected, we return right away.
+  if (!theBOOL) return self;
+  
+  // We initiate our connection to the socket
+  if (connect(_fd, (struct sockaddr *)&server, sizeof(server)) == -1)
+    {
+#ifdef __MINGW32__
+      if (WSAGetLastError() == WSAEWOULDBLOCK)
+#else
+      if (errno == EINPROGRESS)
+#endif
+        {
+          // The socket is non-blocking and the connection cannot be completed 
immediately.
+         if (theBOOL)
+           {
+             return self;
+           }
+          
+         if (![self isConnected])
+           {
+             safe_close(_fd);
+             AUTORELEASE(self);
+             return nil;
+           }
+        } // if ( errno == EINPROGRESS ) ...
+      else
+        {
+         safe_close(_fd);
+         AUTORELEASE(self);
+         return nil;
+        }
+    } // if ( connect(...) )
+  
   return self;
 }
 
@@ -198,8 +197,6 @@
 - (void) dealloc
 {
   //NSLog(@"TCPConnection: -dealloc");
-  [[NSNotificationCenter defaultCenter] removeObserver: self];
-  RELEASE(_name);
 
   if (_ssl)
     {
@@ -234,11 +231,6 @@
   fd_set fdset;
   int value;
 
-  if (!_dns_resolution_completed)
-    {
-      return NO;
-    }
-
   // We remove all descriptors from the set fdset
   FD_ZERO(&fdset);
   
@@ -365,7 +357,7 @@
 - (int) startSSL
 {
   int ret;
-  
+
   // For now, we do not verify the certificates...
   _ctx = SSL_CTX_new(SSLv23_client_method());
   SSL_CTX_set_verify(_ctx, SSL_VERIFY_NONE, NULL);
@@ -437,61 +429,4 @@
   // Everything went all right, let's tell our caller.
   return 0;
 }
-
address@hidden
-
-//
-//
-//
address@hidden CWTCPConnection (Private)
-
-- (void) _DNSResolutionCompleted: (NSNotification *) theNotification
-{
-  struct sockaddr_in server;
-
-  if (![[[theNotification userInfo] objectForKey: @"Name"] isEqualToString: 
_name])
-    {
-      return;
-    }
-
-  //NSLog(@"DNS resolution completed for name |%@|", [[theNotification 
userInfo] objectForKey: @"Name"]);
-  [[NSNotificationCenter defaultCenter] removeObserver: self];
-  _dns_resolution_completed = YES;
-
-  server.sin_family = AF_INET;
-  server.sin_addr.s_addr = [[[theNotification userInfo] objectForKey: 
@"Address"] intValue];
-  server.sin_port = htons(_port);
-
-  // We initiate our connection to the socket
-  if (connect(_fd, (struct sockaddr *)&server, sizeof(server)) == -1)
-    {
-#ifdef __MINGW32__
-      if (WSAGetLastError() == WSAEWOULDBLOCK)
-#else
-      if (errno == EINPROGRESS)
-#endif
-       {
-         return;
-       } // if ( errno == EINPROGRESS ) ...
-      else
-       {
-         NSLog(@"Failed to connect asynchronously.");
-         safe_close(_fd);
-       }
-    } // if ( connect(...) )
-}
-
-//
-//
-//
-- (void) _DNSResolutionFailed: (NSNotification *) theNotification
-{
-  //NSLog(@"DNS resolution failed!");
- 
-  _dns_resolution_completed = YES;
-  safe_close(_fd);
-
-  [[NSNotificationCenter defaultCenter] removeObserver: self];
-}
-
 @end



reply via email to

[Prev in Thread] Current Thread [Next in Thread]