/** DNS callback * If ipaddr is non-NULL, resolving succeeded, otherwise it failed. */ static void smtp_dns_found(const char* hostname, const ip_addr_t *ipaddr, void *arg) { struct smtp_session *s = (struct smtp_session*)arg; struct altcp_pcb *pcb; err_t err; u8_t result; LWIP_UNUSED_ARG(hostname); if (ipaddr != NULL) { pcb = smtp_setup_pcb(s, ipaddr); if (pcb != NULL) { LWIP_DEBUGF(SMTP_DEBUG_STATE, ("smtp_dns_found: hostname resolved, connecting\n")); err = altcp_connect(pcb, ipaddr, smtp_server_port, smtp_tcp_connected); if (err == ERR_OK) { return; } LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("tcp_connect failed: %d\n", (int)err)); result = SMTP_RESULT_ERR_CONNECT; } else { LWIP_DEBUGF(SMTP_DEBUG_STATE, ("smtp_dns_found: failed to allocate tcp pcb\n")); result = SMTP_RESULT_ERR_MEM; err = ERR_MEM; } } else { LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("smtp_dns_found: failed to resolve hostname: %s\n", hostname)); pcb = NULL; result = SMTP_RESULT_ERR_HOSTNAME; err = ERR_ARG; } smtp_close(s, pcb, result, 0, err); }
static err_t altcp_mbedtls_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected) { if (conn == NULL) { return ERR_VAL; } conn->connected = connected; return altcp_connect(conn->inner_conn, ipaddr, port, altcp_mbedtls_lower_connected); }
/** Start the http request when the server IP addr is known */ static err_t httpc_get_internal_addr(httpc_state_t* req, const ip_addr_t *ipaddr) { err_t err; LWIP_ASSERT("req != NULL", req != NULL); if (&req->remote_addr != ipaddr) { /* fill in remote addr if called externally */ req->remote_addr = *ipaddr; } err = altcp_connect(req->pcb, &req->remote_addr, req->remote_port, httpc_tcp_connected); if (err == ERR_OK) { return ERR_OK; } LWIP_DEBUGF(HTTPC_DEBUG_WARN_STATE, ("tcp_connect failed: %d\n", (int)err)); return err; }
/** The actual mail-sending function, called by smtp_send_mail and * smtp_send_mail_static after setting up the struct smtp_session. */ static err_t smtp_send_mail_alloced(struct smtp_session *s) { err_t err; struct altcp_pcb* pcb = NULL; ip_addr_t addr; LWIP_ASSERT("no smtp_session supplied", s != NULL); #if SMTP_CHECK_DATA /* check that body conforms to RFC: * - convert all single-CR or -LF in body to CRLF * - only 7-bit ASCII is allowed */ if (smtp_verify(s->to, s->to_len, 0) != ERR_OK) { err = ERR_ARG; goto leave; } if (smtp_verify(s->from, s->from_len, 0) != ERR_OK) { err = ERR_ARG; goto leave; } if (smtp_verify(s->subject, s->subject_len, 0) != ERR_OK) { err = ERR_ARG; goto leave; } #if SMTP_BODYDH if (s->bodydh == NULL) #endif /* SMTP_BODYDH */ { if (smtp_verify(s->body, s->body_len, 0) != ERR_OK) { err = ERR_ARG; goto leave; } } #endif /* SMTP_CHECK_DATA */ #if SMTP_COPY_AUTHDATA /* copy auth data, ensuring the first byte is always zero */ MEMCPY(s->auth_plain + 1, smtp_auth_plain + 1, smtp_auth_plain_len - 1); s->auth_plain_len = smtp_auth_plain_len; /* default username and pass is empty string */ s->username = s->auth_plain; s->pass = s->auth_plain; if (smtp_username != NULL) { s->username += smtp_username - smtp_auth_plain; } if (smtp_pass != NULL) { s->pass += smtp_pass - smtp_auth_plain; } #endif /* SMTP_COPY_AUTHDATA */ s->state = SMTP_NULL; s->timer = SMTP_TIMEOUT; #if LWIP_DNS err = dns_gethostbyname(smtp_server, &addr, smtp_dns_found, s); #else /* LWIP_DNS */ err = ipaddr_aton(smtp_server, &addr) ? ERR_OK : ERR_ARG; #endif /* LWIP_DNS */ if (err == ERR_OK) { pcb = smtp_setup_pcb(s, &addr); if (pcb == NULL) { err = ERR_MEM; goto leave; } err = altcp_connect(pcb, &addr, smtp_server_port, smtp_tcp_connected); if (err != ERR_OK) { LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("tcp_connect failed: %d\n", (int)err)); goto deallocate_and_leave; } } else if (err != ERR_INPROGRESS) { LWIP_DEBUGF(SMTP_DEBUG_WARN_STATE, ("dns_gethostbyname failed: %d\n", (int)err)); goto deallocate_and_leave; } return ERR_OK; deallocate_and_leave: if (pcb != NULL) { altcp_arg(pcb, NULL); altcp_close(pcb); } leave: smtp_free_struct(s); /* no need to call the callback here since we return != ERR_OK */ return err; }