Exemple #1
0
static mailstream_low * mailstream_low_ssl_open_full(int fd, int starttls, time_t timeout,
  void (* callback)(struct mailstream_ssl_context * ssl_context, void * cb_data), void * cb_data)
{
#ifdef USE_SSL
  mailstream_low * s;
  struct mailstream_ssl_data * ssl_data;

  if (starttls)
    ssl_data = tls_data_new(fd, timeout, callback, cb_data);
  else
    ssl_data = ssl_data_new(fd, timeout, callback, cb_data);

  if (ssl_data == NULL)
    goto err;

  s = mailstream_low_new(ssl_data, mailstream_ssl_driver);
  if (s == NULL)
    goto free_ssl_data;
	mailstream_low_set_timeout(s, timeout);

  return s;

 free_ssl_data:
  ssl_data_free(ssl_data);
 err:
  return NULL;
#else
  return NULL;
#endif
}
mailstream_low * mailstream_low_compress_open(mailstream_low * ms)
{
  mailstream_low * s;
    
  /* stores the original mailstream */
  struct mailstream_compress_data * compress_data = malloc(sizeof(* compress_data));
  if (compress_data == NULL)
    goto err;

  /* allocate deflate state */
  compress_data->compress_stream = malloc(sizeof(z_stream));
  compress_data->compress_stream->zalloc = Z_NULL;
  compress_data->compress_stream->zfree = Z_NULL;
  compress_data->compress_stream->opaque = Z_NULL;
  /* these specific settings are very important - don't change without looking at the COMPRESS RFC */
  int ret = deflateInit2(compress_data->compress_stream, Z_BEST_SPEED, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
  if (ret != Z_OK) {
    goto free_compress_data;
  }
  compress_data->compress_stream->avail_in = 0;
  compress_data->compress_stream->avail_out = 0;

  /* allocate inflate state */
  compress_data->decompress_stream = malloc(sizeof(z_stream));
  compress_data->decompress_stream->zalloc = Z_NULL;
  compress_data->decompress_stream->zfree = Z_NULL;
  compress_data->decompress_stream->opaque = Z_NULL;
  /* these specific settings are very important - don't change without looking at the COMPRESS RFC */
  ret = inflateInit2(compress_data->decompress_stream, -15);
  if (ret != Z_OK) {
    goto free_compress_data;
  }
  compress_data->decompress_stream->avail_in = 0;
  compress_data->decompress_stream->avail_out = 0;

  compress_data->ms = ms;

  s = mailstream_low_new(compress_data, mailstream_compress_driver);
  if (s == NULL)
    goto free_compress_data;
    
  return s;
    
  free_compress_data:
  if (compress_data->compress_stream) {
    deflateEnd(compress_data->compress_stream);
    free(compress_data->compress_stream);
  }
  if (compress_data->decompress_stream) {
    inflateEnd(compress_data->decompress_stream);
    free(compress_data->decompress_stream);
  }
  free(compress_data);
  err:
  return NULL;
}
Exemple #3
0
mailstream_low * mailstream_low_socket_open(int fd)
{
  mailstream_low * s;
  struct mailstream_socket_data * socket_data;

  socket_data = socket_data_new(fd);
  if (socket_data == NULL)
    goto err;

  s = mailstream_low_new(socket_data, mailstream_socket_driver);
  if (s == NULL)
    goto free_socket_data;

  return s;

 free_socket_data:
  socket_data_free(socket_data);
 err:
  return NULL;
}
mailstream_low * mailstream_low_cfstream_open_voip_timeout(const char * hostname, int16_t port,
  int voip_enabled, time_t timeout)
{
#if HAVE_CFNETWORK
  mailstream_low * s;
  struct mailstream_cfstream_data * cfstream_data;
  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;
  CFStringRef hostString;
  CFOptionFlags readFlags;
  CFOptionFlags writeFlags;
  int r;
  
  hostString = CFStringCreateWithCString(NULL, hostname, kCFStringEncodingUTF8);
  CFStreamCreatePairWithSocketToHost(NULL, hostString, port, &readStream, &writeStream);
  CFRelease(hostString);

#if TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR  
  if (voip_enabled) {
    CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
  }
#endif
  
  cfstream_data = cfstream_data_new(readStream, writeStream);
  s = mailstream_low_new(cfstream_data, mailstream_cfstream_driver);
	mailstream_low_set_timeout(s, timeout);  

  //fprintf(stderr, "open %s %i -> %p\n", hostname, port, s);
  
  /* setup streams */
  cfstream_data->streamContext.info = s;
  
  readFlags = kCFStreamEventOpenCompleted |
  kCFStreamEventHasBytesAvailable |
  kCFStreamEventErrorOccurred |
  kCFStreamEventEndEncountered;
  
  writeFlags = kCFStreamEventOpenCompleted |
  kCFStreamEventCanAcceptBytes |
  kCFStreamEventErrorOccurred |
  kCFStreamEventEndEncountered;
  
  CFReadStreamSetClient(cfstream_data->readStream, readFlags, readStreamCallback, &cfstream_data->streamContext);
  CFWriteStreamSetClient(cfstream_data->writeStream, writeFlags, writeStreamCallback, &cfstream_data->streamContext);
  
  CFRelease(readStream);
  CFRelease(writeStream);
  readStream = NULL;
  writeStream = NULL;
  
  /* setup cancel */
  cfstream_data->cancelContext.info = s;
  cfstream_data->cancelContext.perform = cancelPerform;
  cfstream_data->cancelSource = CFRunLoopSourceCreate(NULL, 0, &cfstream_data->cancelContext);
  
  r = low_open(s);
  if (r < 0) {
    mailstream_low_cfstream_close(s);
    return NULL;
  }
  
  return s;
#else
  return NULL;
#endif
}