Browse Source

smtp: add SMTPS support (SMTP over TLS) using altcp_tls

master
goldsimon 9 years ago
parent
commit
5ca974e807
  1. 23
      apps/smtp/smtp.c
  2. 6
      apps/smtp/smtp.h

23
apps/smtp/smtp.c

@ -53,6 +53,7 @@
#include "lwip/dns.h"
#include "lwip/mem.h"
#include "lwip/altcp_tcp.h"
#include "lwip/apps/altcp_tls.h"
#include <string.h>
#include <stdlib.h>
@ -329,6 +330,10 @@ struct smtp_session {
static char smtp_server[SMTP_MAX_SERVERNAME_LEN + 1];
/** TCP port of the server to use for next SMTP request */
static u16_t smtp_server_port = SMTP_DEFAULT_PORT;
#if LWIP_ALTCP && LWIP_ALTCP_TLS
/** If this is set, mail is sent using SMTPS */
static struct altcp_tls_config *smtp_server_tls_config;
#endif
/** Username to use for the next SMTP request */
static char *smtp_username;
/** Password to use for the next SMTP request */
@ -410,6 +415,14 @@ smtp_set_server_port(u16_t port)
smtp_server_port = port;
}
#if LWIP_ALTCP && LWIP_ALTCP_TLS
void
smtp_set_tls_config(struct altcp_tls_config *tls_config)
{
smtp_server_tls_config = tls_config;
}
#endif
/** Set authentication parameters for next SMTP connection
*
* @param username login name as passed to the server
@ -476,6 +489,16 @@ smtp_setup_pcb(struct smtp_session *s, const ip_addr_t* remote_ip)
pcb = altcp_tcp_new_ip_type(IP_GET_TYPE(remote_ip));
if (pcb != NULL) {
#if LWIP_ALTCP_TLS
if (smtp_server_tls_config) {
struct altcp_pcb *pcb_tls = altcp_tls_new(smtp_server_tls_config, pcb);
if (pcb_tls == NULL) {
altcp_close(pcb);
return NULL;
}
pcb = pcb_tls;
}
#endif
altcp_arg(pcb, s);
altcp_recv(pcb, smtp_tcp_recv);
altcp_err(pcb, smtp_tcp_err);

6
apps/smtp/smtp.h

@ -10,6 +10,8 @@
/** The default TCP port used for SMTP */
#define SMTP_DEFAULT_PORT 25
/** The default TCP port used for SMTPS */
#define SMTPS_DEFAULT_PORT 465
/** Email successfully sent */
#define SMTP_RESULT_OK 0
@ -104,6 +106,10 @@ err_t smtp_send_mail_bodycback(const char *from, const char* to, const char* sub
err_t smtp_set_server_addr(const char* server);
void smtp_set_server_port(u16_t port);
#if LWIP_ALTCP && LWIP_ALTCP_TLS
struct altcp_tls_config;
void smtp_set_tls_config(struct altcp_tls_config *tls_config);
#endif
err_t smtp_set_auth(const char* username, const char* pass);
err_t smtp_send_mail(const char *from, const char* to, const char* subject, const char* body,
smtp_result_fn callback_fn, void* callback_arg);

Loading…
Cancel
Save