Permalink
Browse files

full qualified domain name in HELO/EHLO smtp command

  • Loading branch information...
patrick84
patrick84 committed Mar 21, 2012
1 parent 858408b commit ad9d367fb75fc392804c92163e5ce783092a1613
@@ -504,7 +504,36 @@ static void mailstream_low_cfstream_free(mailstream_low * s)
static int mailstream_low_cfstream_get_fd(mailstream_low * s)
{
- return -1;
+ struct mailstream_cfstream_data * cfstream_data = NULL;
+ CFDataRef native_handle_data = NULL;
+ CFSocketNativeHandle native_handle_value = -1;
+ CFIndex native_data_len = 0;
+ CFIndex native_value_len = 0;
+
+ if (!s)
+ return -1;
+
+ cfstream_data = (struct mailstream_cfstream_data *) s->data;
+
+ if (!cfstream_data->readStream)
+ return -1;
+
+ native_handle_data = (CFDataRef)CFReadStreamCopyProperty(cfstream_data->readStream, kCFStreamPropertySocketNativeHandle);
+ if (!native_handle_data)
+ return -1;
+
+ native_data_len = CFDataGetLength(native_handle_data);
+ native_value_len = (CFIndex)sizeof(native_handle_value);
+
+ if (native_data_len != native_value_len) {
+ CFRelease(native_handle_data);
+ return -1;
+ }
+
+ CFDataGetBytes(native_handle_data, CFRangeMake(0, MIN(native_data_len, native_value_len)), (UInt8 *)&native_handle_value);
+ CFRelease(native_handle_data);
+
+ return native_handle_value;
}
#if HAVE_CFNETWORK
@@ -44,8 +44,19 @@
#include "base64.h"
#include "mail.h"
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+
+#ifdef HAVE_STRING_H
+# include <string.h>
+#endif
+
#ifdef WIN32
# include "win_etpan.h"
+#else
+# include <sys/types.h>
+# include <netdb.h>
#endif
#ifdef HAVE_NETINET_IN_H
@@ -157,6 +168,8 @@ static int send_command_private(mailsmtp * f, char * command, int can_be_publish
static int read_response(mailsmtp * session);
+static int get_hostname(mailsmtp * session, int useip, char * buf, int len);
+
/* smtp operations */
int mailsmtp_connect(mailsmtp * session, mailstream * s)
@@ -224,18 +237,57 @@ int mailsmtp_quit(mailsmtp * session)
}
-
#define HOSTNAME_SIZE 256
+static int get_hostname(mailsmtp * session, int useip, char * buf, int len)
+{
+ int r;
+ char hostname[HOSTNAME_SIZE];
+ struct sockaddr addr;
+ socklen_t addr_len = sizeof(addr);
+ int socket = -1;
+
+ if (!useip) {
+ r = gethostname(hostname, HOSTNAME_SIZE);
+ if (r != 0)
+ return MAILSMTP_ERROR_HOSTNAME;
+
+ if (snprintf(buf, len, "%s", hostname) >= len)
+ return MAILSMTP_ERROR_HOSTNAME;
+ } else {
+ socket = mailstream_low_get_fd(mailstream_get_low(session->stream));
+ if (socket < 0)
+ return MAILSMTP_ERROR_HOSTNAME;
+
+ r = getsockname(socket, &addr, &addr_len );
+ if (r != 0)
+ return MAILSMTP_ERROR_HOSTNAME;
+
+ r = getnameinfo(&addr, addr.sa_len, hostname, HOSTNAME_SIZE, NULL, 0, NI_NUMERICHOST);
+ if (r != 0)
+ return MAILSMTP_ERROR_HOSTNAME;
+
+ if (snprintf(buf, len, "[%s]", hostname) >= len)
+ return MAILSMTP_ERROR_HOSTNAME;
+ }
+ return MAILSMTP_NO_ERROR;
+}
+
+
int mailsmtp_helo(mailsmtp * session)
+{
+ return mailsmtp_helo_with_ip(session, 0);
+}
+
+int mailsmtp_helo_with_ip(mailsmtp * session, int useip)
{
int r;
char hostname[HOSTNAME_SIZE];
char command[SMTP_STRING_SIZE];
- r = gethostname(hostname, HOSTNAME_SIZE);
- if (r < 0)
- return MAILSMTP_ERROR_HOSTNAME;
+ r = get_hostname(session, useip, hostname, HOSTNAME_SIZE);
+ if (r != MAILSMTP_NO_ERROR)
+ return r;
snprintf(command, SMTP_STRING_SIZE, "HELO %s\r\n", hostname);
r = send_command(session, command);
@@ -555,14 +607,19 @@ int mailesmtp_parse_ehlo(mailsmtp * session)
int mailesmtp_ehlo(mailsmtp * session)
+{
+ return mailesmtp_ehlo_with_ip(session, 0);
+}
+
+int mailesmtp_ehlo_with_ip(mailsmtp * session, int useip)
{
int r;
char hostname[HOSTNAME_SIZE];
char command[SMTP_STRING_SIZE];
- r = gethostname(hostname, HOSTNAME_SIZE);
- if (r != 0)
- return MAILSMTP_ERROR_HOSTNAME;
+ r = get_hostname(session, useip, hostname, HOSTNAME_SIZE);
+ if (r != MAILSMTP_NO_ERROR)
+ return r;
snprintf(command, SMTP_STRING_SIZE, "EHLO %s\r\n", hostname);
r = send_command(session, command);
@@ -81,6 +81,9 @@ int mailsmtp_auth_type(mailsmtp * session,
LIBETPAN_EXPORT
int mailsmtp_helo(mailsmtp * session);
+LIBETPAN_EXPORT
+int mailsmtp_helo_with_ip(mailsmtp * session, int useip);
+
LIBETPAN_EXPORT
int mailsmtp_mail(mailsmtp * session, const char * from);
@@ -103,6 +106,9 @@ int mailsmtp_data_message_quit(mailsmtp * session,
LIBETPAN_EXPORT
int mailesmtp_ehlo(mailsmtp * session);
+LIBETPAN_EXPORT
+int mailesmtp_ehlo_with_ip(mailsmtp * session, int useip);
+
LIBETPAN_EXPORT
int mailesmtp_mail(mailsmtp * session,
const char * from,
@@ -43,23 +43,26 @@
#include "mail.h"
int mailsmtp_init(mailsmtp * session)
+{
+ return mailsmtp_init_with_ip(session, 0);
+}
+
+int mailsmtp_init_with_ip(mailsmtp * session, int useip)
{
int r;
- r = mailesmtp_ehlo(session);
+ r = mailesmtp_ehlo_with_ip(session, useip);
if (r == MAILSMTP_NO_ERROR)
return MAILSMTP_NO_ERROR;
- r = mailsmtp_helo(session);
+ r = mailsmtp_helo_with_ip(session, useip);
if (r == MAILSMTP_NO_ERROR)
return MAILSMTP_NO_ERROR;
return r;
}
-
-
int mailesmtp_send(mailsmtp * session,
const char * from,
int return_full,
@@ -45,6 +45,7 @@ extern "C" {
#include "clist.h"
int mailsmtp_init(mailsmtp * session);
+int mailsmtp_init_with_ip(mailsmtp * session, int useip);
int mailesmtp_send(mailsmtp * session,
const char * from,

0 comments on commit ad9d367

Please sign in to comment.