Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

lwip_smtp.c File Reference

lwip_smtp.c File Reference

SMTP client module. More...

Go to the source code of this file.

Enumerations

enum  smtp_session_state
 

State for SMTP client state machine.

More...

Functions

static err_t smtp_verify (const char *data, size_t data_len, u8_t linebreaks_allowed)
 Verify that a given string conforms to the SMTP rules (7-bit only, no single CR or LF,.
static err_t smtp_tcp_recv (void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
 Raw API TCP recv callback.
static void smtp_tcp_err (void *arg, err_t err)
 Raw API TCP err callback: pcb is already deallocated.
static err_t smtp_tcp_poll (void *arg, struct altcp_pcb *pcb)
 Raw API TCP poll callback.
static err_t smtp_tcp_sent (void *arg, struct altcp_pcb *pcb, u16_t len)
 Raw API TCP sent callback.
static void smtp_dns_found (const char *hostname, const ip_addr_t *ipaddr, void *arg)
 DNS callback If ipaddr is non-NULL, resolving succeeded, otherwise it failed.
static size_t smtp_base64_encode (char *target, size_t target_len, const char *source, size_t source_len)
 Base64 encoding.
static enum smtp_session_state smtp_prepare_mail (struct smtp_session *s, u16_t *tx_buf_len)
 Prepare MAIL message.
static void smtp_send_body (struct smtp_session *s, struct altcp_pcb *pcb)
 If in state SMTP_BODY, try to send more body data.
static void smtp_process (void *arg, struct altcp_pcb *pcb, struct pbuf *p)
 State machine-like implementation of an SMTP client.
const char * smtp_result_str (u8_t smtp_result)
 Convert an smtp result to a string.
static const char * smtp_pbuf_str (struct pbuf *p)
 Null-terminates the payload of p for printing out messages.
err_t smtp_set_server_addr (const char *server)
 Set IP address or DNS name for next SMTP connection.
void smtp_set_server_port (u16_t port)
 Set TCP port for next SMTP connection.
void smtp_set_tls_config (struct altcp_tls_config *tls_config)
 Set TLS configuration for next SMTP connection.
err_t smtp_set_auth (const char *username, const char *pass)
 Set authentication parameters for next SMTP connection.
static err_t smtp_send_mail_alloced (struct smtp_session *s)
 The actual mail-sending function, called by smtp_send_mail and smtp_send_mail_static after setting up the struct smtp_session.
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)
 Send an email via the currently selected server, username and password.
err_t smtp_send_mail_static (const char *from, const char *to, const char *subject, const char *body, smtp_result_fn callback_fn, void *callback_arg)
 Same as smtp_send_mail, but doesn't copy from, to, subject and body into an internal buffer to save memory.
void smtp_send_mail_int (void *arg)
 Same as smtp_send_mail but takes a struct smtp_send_request as single parameter which contains all the other parameters.
static void smtp_free (struct smtp_session *s, u8_t result, u16_t srv_err, err_t err)
 Frees the smtp_session and calls the callback function.
static void smtp_close (struct smtp_session *s, struct altcp_pcb *pcb, u8_t result, u16_t srv_err, err_t err)
 Try to close a pcb and free the arg if successful.
static u16_t smtp_is_response (struct smtp_session *s)
 Parse pbuf to see if it contains the beginning of an answer.
static err_t smtp_is_response_finished (struct smtp_session *s)
 Parse pbuf to see if it contains a fully received answer.
static enum smtp_session_state smtp_prepare_helo (struct smtp_session *s, u16_t *tx_buf_len, struct altcp_pcb *pcb)
 Prepare HELO/EHLO message.
static enum smtp_session_state smtp_prepare_auth_or_mail (struct smtp_session *s, u16_t *tx_buf_len)
 Parse last server response (in rx_buf) for supported authentication method, create data to send out (to tx_buf), set tx_data_len correctly and return the next state.
static enum smtp_session_state smtp_prepare_auth_login_uname (struct smtp_session *s, u16_t *tx_buf_len)
 Send base64-encoded username.
static enum smtp_session_state smtp_prepare_auth_login_pass (struct smtp_session *s, u16_t *tx_buf_len)
 Send base64-encoded password.
static enum smtp_session_state smtp_prepare_rcpt (struct smtp_session *s, u16_t *tx_buf_len)
 Prepare RCPT message.
static enum smtp_session_state smtp_prepare_header (struct smtp_session *s, u16_t *tx_buf_len)
 Prepare header of body.
static enum smtp_session_state smtp_prepare_quit (struct smtp_session *s, u16_t *tx_buf_len)
 Prepare QUIT message.
static int smtp_send_bodyh_data (struct altcp_pcb *pcb, const char **from, u16_t *howmany)
 Elementary sub-function to send data.
err_t smtp_send_mail_bodycback (const char *from, const char *to, const char *subject, smtp_bodycback_fn bodycback_fn, smtp_result_fn callback_fn, void *callback_arg)
 Same as smtp_send_mail_static, but uses a callback function to send body data.

Variables

static const char * smtp_state_str []
 State-to-string table for debugging.
static char smtp_server [SMTP_MAX_SERVERNAME_LEN+1]
 IP address or DNS name of the server to use for next SMTP request.
static u16_t smtp_server_port = SMTP_DEFAULT_PORT
 TCP port of the server to use for next SMTP request.
static struct altcp_tls_config * smtp_server_tls_config
 If this is set, mail is sent using SMTPS.
static char * smtp_username
 Username to use for the next SMTP request.
static char * smtp_pass
 Password to use for the next SMTP request.
static char smtp_auth_plain [SMTP_MAX_USERNAME_LEN+SMTP_MAX_PASS_LEN+3]
 Username and password combined as necessary for PLAIN authentication.
static size_t smtp_auth_plain_len
 Length of smtp_auth_plain string (cannot use strlen since it includes \0)
static const char base64_table []
 Table 6-bit-index-to-ASCII used for base64-encoding.

Detailed Description

SMTP client module.

Author: Simon Goldschmidt

Definition in file lwip_smtp.c.


Enumeration Type Documentation

State for SMTP client state machine.

Definition at line 180 of file lwip_smtp.c.


Function Documentation

static size_t smtp_base64_encode ( char *  target,
size_t  target_len,
const char *  source,
size_t  source_len 
) [static]

Base64 encoding.

Definition at line 914 of file lwip_smtp.c.

static void smtp_close ( struct smtp_session *  s,
struct altcp_pcb *  pcb,
u8_t  result,
u16_t  srv_err,
err_t  err 
) [static]

Try to close a pcb and free the arg if successful.

Definition at line 772 of file lwip_smtp.c.

static void smtp_dns_found ( const char *  hostname,
const ip_addr_t ipaddr,
void *  arg 
) [static]

DNS callback If ipaddr is non-NULL, resolving succeeded, otherwise it failed.

Definition at line 863 of file lwip_smtp.c.

static void smtp_free ( struct smtp_session *  s,
u8_t  result,
u16_t  srv_err,
err_t  err 
) [static]

Frees the smtp_session and calls the callback function.

Definition at line 757 of file lwip_smtp.c.

static u16_t smtp_is_response ( struct smtp_session *  s ) [static]

Parse pbuf to see if it contains the beginning of an answer.

If so, it returns the contained response code as number between 1 and 999. If not, zero is returned.

Parameters:
ssmtp session struct

Definition at line 955 of file lwip_smtp.c.

static err_t smtp_is_response_finished ( struct smtp_session *  s ) [static]

Parse pbuf to see if it contains a fully received answer.

If one is found, ERR_OK is returned. If none is found, ERR_VAL is returned.

A fully received answer is a 3-digit number followed by a space, some string and a CRLF as line ending.

Parameters:
ssmtp session struct

Definition at line 987 of file lwip_smtp.c.

static const char* smtp_pbuf_str ( struct pbuf p ) [static]

Null-terminates the payload of p for printing out messages.

WARNING: use this only if p is not needed any more as the last byte of payload is deleted!

Definition at line 333 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_auth_login_pass ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Send base64-encoded password.

Definition at line 1124 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_auth_login_uname ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Send base64-encoded username.

Definition at line 1108 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_auth_or_mail ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Parse last server response (in rx_buf) for supported authentication method, create data to send out (to tx_buf), set tx_data_len correctly and return the next state.

Definition at line 1054 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_header ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Prepare header of body.

Definition at line 1175 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_helo ( struct smtp_session *  s,
u16_t *  tx_buf_len,
struct altcp_pcb *  pcb 
) [static]

Prepare HELO/EHLO message.

Definition at line 1031 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_mail ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Prepare MAIL message.

Definition at line 1141 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_quit ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Prepare QUIT message.

Definition at line 1204 of file lwip_smtp.c.

static enum smtp_session_state smtp_prepare_rcpt ( struct smtp_session *  s,
u16_t *  tx_buf_len 
) [static]

Prepare RCPT message.

Definition at line 1158 of file lwip_smtp.c.

static void smtp_process ( void *  arg,
struct altcp_pcb *  pcb,
struct pbuf p 
) [static]

State machine-like implementation of an SMTP client.

Definition at line 1265 of file lwip_smtp.c.

const char* smtp_result_str ( u8_t  smtp_result )

Convert an smtp result to a string.

Definition at line 320 of file lwip_smtp.c.

static void smtp_send_body ( struct smtp_session *  s,
struct altcp_pcb *  pcb 
) [static]

If in state SMTP_BODY, try to send more body data.

Definition at line 1215 of file lwip_smtp.c.

static int smtp_send_bodyh_data ( struct altcp_pcb *  pcb,
const char **  from,
u16_t *  howmany 
) [static]

Elementary sub-function to send data.

Returns:
: BDHALLDATASENT all data has been written BDHSOMEDATASENT some data has been written 0 no data has been written

Definition at line 1448 of file lwip_smtp.c.

static err_t smtp_send_mail_alloced ( struct smtp_session *  s ) [static]

The actual mail-sending function, called by smtp_send_mail and smtp_send_mail_static after setting up the struct smtp_session.

Definition at line 484 of file lwip_smtp.c.

err_t smtp_send_mail_bodycback ( const char *  from,
const char *  to,
const char *  subject,
smtp_bodycback_fn  bodycback_fn,
smtp_result_fn  callback_fn,
void *  callback_arg 
)

Same as smtp_send_mail_static, but uses a callback function to send body data.

Definition at line 1468 of file lwip_smtp.c.

static void smtp_tcp_err ( void *  arg,
err_t  err 
) [static]

Raw API TCP err callback: pcb is already deallocated.

Definition at line 794 of file lwip_smtp.c.

static err_t smtp_tcp_poll ( void *  arg,
struct altcp_pcb *  pcb 
) [static]

Raw API TCP poll callback.

Definition at line 805 of file lwip_smtp.c.

static err_t smtp_tcp_recv ( void *  arg,
struct altcp_pcb *  pcb,
struct pbuf p,
err_t  err 
) [static]

Raw API TCP recv callback.

Definition at line 830 of file lwip_smtp.c.

static err_t smtp_tcp_sent ( void *  arg,
struct altcp_pcb *  pcb,
u16_t  len 
) [static]

Raw API TCP sent callback.

Definition at line 819 of file lwip_smtp.c.

static err_t smtp_verify ( const char *  data,
size_t  data_len,
u8_t  linebreaks_allowed 
) [static]

Verify that a given string conforms to the SMTP rules (7-bit only, no single CR or LF,.

Definition at line 721 of file lwip_smtp.c.


Variable Documentation

const char base64_table[] [static]
Initial value:
 {
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
  'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  '+', '/'
}

Table 6-bit-index-to-ASCII used for base64-encoding.

Definition at line 901 of file lwip_smtp.c.

char smtp_auth_plain[SMTP_MAX_USERNAME_LEN+SMTP_MAX_PASS_LEN+3] [static]

Username and password combined as necessary for PLAIN authentication.

Definition at line 291 of file lwip_smtp.c.

size_t smtp_auth_plain_len [static]

Length of smtp_auth_plain string (cannot use strlen since it includes \0)

Definition at line 293 of file lwip_smtp.c.

char* smtp_pass [static]

Password to use for the next SMTP request.

Definition at line 289 of file lwip_smtp.c.

char smtp_server[SMTP_MAX_SERVERNAME_LEN+1] [static]

IP address or DNS name of the server to use for next SMTP request.

Definition at line 279 of file lwip_smtp.c.

u16_t smtp_server_port = SMTP_DEFAULT_PORT [static]

TCP port of the server to use for next SMTP request.

Definition at line 281 of file lwip_smtp.c.

struct altcp_tls_config* smtp_server_tls_config [static]

If this is set, mail is sent using SMTPS.

Definition at line 284 of file lwip_smtp.c.

const char* smtp_state_str[] [static]
Initial value:
 {
  "SMTP_NULL",
  "SMTP_HELO",
  "SMTP_AUTH_PLAIN",
  "SMTP_AUTH_LOGIN_UNAME",
  "SMTP_AUTH_LOGIN_PASS",
  "SMTP_AUTH_LOGIN",
  "SMTP_MAIL",
  "SMTP_RCPT",
  "SMTP_DATA",
  "SMTP_BODY",
  "SMTP_QUIT",
  "SMTP_CLOSED",
}

State-to-string table for debugging.

Definition at line 197 of file lwip_smtp.c.

char* smtp_username [static]

Username to use for the next SMTP request.

Definition at line 287 of file lwip_smtp.c.