smtp now compiles

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@317 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2007-08-30 23:57:58 +00:00
parent 1344768825
commit c60f939f91
6 changed files with 280 additions and 224 deletions
+4
View File
@@ -33,6 +33,10 @@
* *
************************************************************/ ************************************************************/
#ifndef linux
# error "Sorry, this will only work with Linux"
#endif
/************************************************************ /************************************************************
* Included Files * Included Files
************************************************************/ ************************************************************/
+8 -14
View File
@@ -38,6 +38,10 @@
* *
****************************************************************************/ ****************************************************************************/
#ifndef linux
# error "Sorry, this will only work with Linux"
#endif
/**************************************************************************** /****************************************************************************
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
@@ -55,20 +59,8 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#if 0 #include <linux/if.h>
#include "uip/uip.h" #include <linux/if_tun.h>
#include <net/uip/uip-arch.h>
#include <net/uip/uip-arp.h>
#endif
#ifdef linux
# include <sys/ioctl.h>
# include <linux/if.h>
# include <linux/if_tun.h>
# define DEVTAP "/dev/net/tun"
#else /* linux */
# define DEVTAP "/dev/tap0"
#endif /* linux */
extern int lib_rawprintf(const char *format, ...); extern int lib_rawprintf(const char *format, ...);
@@ -78,6 +70,8 @@ extern int lib_rawprintf(const char *format, ...);
#define TAPDEV_DEBUG 1 #define TAPDEV_DEBUG 1
#define DEVTAP "/dev/net/tun"
#define UIP_DRIPADDR0 192 #define UIP_DRIPADDR0 192
#define UIP_DRIPADDR1 168 #define UIP_DRIPADDR1 168
#define UIP_DRIPADDR2 0 #define UIP_DRIPADDR2 0
+18 -16
View File
@@ -31,7 +31,7 @@
* *
* This file is part of the uIP TCP/IP stack. * This file is part of the uIP TCP/IP stack.
* *
* $Id: main.c,v 1.1.1.1 2007-08-26 23:10:37 patacongo Exp $ * $Id: main.c,v 1.2 2007-08-30 23:57:58 patacongo Exp $
* *
*/ */
@@ -66,14 +66,17 @@ int user_start(int argc, char *argv[])
int i; int i;
uip_ipaddr_t ipaddr; uip_ipaddr_t ipaddr;
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) #if defined(CONFIG_EXAMPLE_UIP_DHCPC)
uint16 mac[6] = {1,2,3,4,5,6}; uint16 mac[6] = {1, 2, 3, 4, 5, 6};
#endif
#ifdef CONFIG_EXAMPLE_UIP_SMTP
void *handle;
#endif #endif
uip_ipaddr(ipaddr, 192,168,0,2); uip_ipaddr(ipaddr, 192, 168, 0, 2);
uip_sethostaddr(ipaddr); uip_sethostaddr(ipaddr);
uip_ipaddr(ipaddr, 192,168,0,1); uip_ipaddr(ipaddr, 192, 168, 0, 1);
uip_setdraddr(ipaddr); uip_setdraddr(ipaddr);
uip_ipaddr(ipaddr, 255,255,255,0); uip_ipaddr(ipaddr, 255, 255, 255, 0);
uip_setnetmask(ipaddr); uip_setnetmask(ipaddr);
#if defined(CONFIG_EXAMPLE_UIP_WEBSERVER) #if defined(CONFIG_EXAMPLE_UIP_WEBSERVER)
@@ -83,15 +86,19 @@ int user_start(int argc, char *argv[])
#elif defined(CONFIG_EXAMPLE_UIP_DHCPC) #elif defined(CONFIG_EXAMPLE_UIP_DHCPC)
dhcpc_init(&mac, 6); dhcpc_init(&mac, 6);
#elif defined(CONFIG_EXAMPLE_UIP_SMTP) #elif defined(CONFIG_EXAMPLE_UIP_SMTP)
uip_ipaddr(ipaddr, 127,0,0,1); uip_ipaddr(ipaddr, 127, 0, 0, 1);
smtp_configure("localhost", ipaddr); handle = smtp_init();
SMTP_SEND("adam@sics.se", NULL, "uip-testing@example.com", if (handle)
"Testing SMTP from uIP", {
"Test message sent by uIP\r\n"); smtp_configure("localhost", ipaddr);
smtp_send("adam@sics.se", NULL, "uip-testing@example.com",
"Testing SMTP from uIP", "Test message sent by uIP\r\n");
smtp_close(handle);
}
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT) #elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
webclient_init(); webclient_init();
resolv_init(); resolv_init();
uip_ipaddr(ipaddr, 195,54,122,204); uip_ipaddr(ipaddr, 195, 54, 122, 204);
resolv_conf(ipaddr); resolv_conf(ipaddr);
resolv_query("www.sics.se"); resolv_query("www.sics.se");
#endif #endif
@@ -134,11 +141,6 @@ void dhcpc_configured(const struct dhcpc_state *s)
} }
#endif /* __DHCPC_H__ */ #endif /* __DHCPC_H__ */
void smtp_done(unsigned char code)
{
printf("SMTP done with code %d\n", code);
}
void webclient_closed(void) void webclient_closed(void)
{ {
printf("Webclient: connection closed\n"); printf("Webclient: connection closed\n");
+25 -36
View File
@@ -1,9 +1,15 @@
/****************************************************************************
/* smtp.h /* smtp.h
* SMTP header file * SMTP header file
* Author: Adam Dunkels <adam@dunkels.com>
* *
* Copyright (c) 2002, Adam Dunkels. * Copyright (C) 2007 Gregory Nutt. All rights reserved.
* All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Heavily leveraged from uIP 1.0 which also has a BSD-like license:
*
* Author: Adam Dunkels <adam@dunkels.com>
* Copyright (c) 2002, Adam Dunkels.
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -28,48 +34,31 @@
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ *
****************************************************************************/
#ifndef __SMTP_H__ #ifndef __SMTP_H__
#define __SMTP_H__ #define __SMTP_H__
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include <net/uip/uipopt.h> #include <net/uip/uipopt.h>
/* Error number that signifies a non-error condition. */ /****************************************************************************
* Type Definitions
****************************************************************************/
#define SMTP_ERR_OK 0 /****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Callback function that is called when an e-mail transmission is extern void *smtp_open(void);
* done. extern void smtp_configure(void *handle, char *localhostname, void *smtpserver);
* extern int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char *msg, int msglen);
* This function must be implemented by the module that uses the SMTP extern void smtp_close(void *handle);
* module.
*
* error The number of the error if an error occured, or
* SMTP_ERR_OK.
*/
void smtp_done(unsigned char error);
void smtp_init(void);
void smtp_configure(char *localhostname, uint16 *smtpserver);
unsigned char smtp_send(char *to, char *from,
char *subject, char *msg,
uint16 msglen);
struct smtp_state
{
uint8 state;
char *to;
char *from;
char *subject;
char *msg;
uint16 msglen;
uint16 sentlen, textlen;
uint16 sendptr;
};
#endif /* __SMTP_H__ */ #endif /* __SMTP_H__ */
+2 -3
View File
@@ -33,6 +33,5 @@
# #
############################################################################ ############################################################################
# Does not build SMTP_ASRCS =
# SMTP_ASRCS = SMTP_CSRCS = smtp.c smtp-strings.c
# SMTP_CSRCS = smtp.c smtp-strings.c
+223 -155
View File
@@ -1,6 +1,11 @@
/* smtp.c /****************************************************************************
* smtp.c
* smtp SMTP E-mail sender * smtp SMTP E-mail sender
* Author: Adam Dunkels <adam@dunkels.com> *
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Heavily leveraged from uIP 1.0 which also has a BSD-like license:
* *
* The Simple Mail Transfer Protocol (SMTP) as defined by RFC821 is * The Simple Mail Transfer Protocol (SMTP) as defined by RFC821 is
* the standard way of sending and transfering e-mail on the * the standard way of sending and transfering e-mail on the
@@ -8,8 +13,9 @@
* example of how to implement protocols in uIP, and is able to send * example of how to implement protocols in uIP, and is able to send
* out e-mail but has not been extensively tested. * out e-mail but has not been extensively tested.
* *
* Copyright (c) 2004, Adam Dunkels. * Author: Adam Dunkels <adam@dunkels.com>
* All rights reserved. * Copyright (c) 2004, Adam Dunkels.
* All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -34,16 +40,16 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* ****************************************************************************/
* This file is part of the uIP TCP/IP stack.
* /****************************************************************************
* Author: Adam Dunkels <adam@sics.se> * Included Files
* ****************************************************************************/
* $Id: smtp.c,v 1.1.1.1 2007-08-26 23:07:06 patacongo Exp $
*/
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <semaphore.h>
#include <net/uip/uip.h> #include <net/uip/uip.h>
#include <net/uip/psock.h> #include <net/uip/psock.h>
@@ -51,10 +57,7 @@
#include "smtp-strings.h" #include "smtp-strings.h"
static struct smtp_state s; #define SMTP_INPUT_BUFFER_SIZE 512
static char *localhostname;
static uip_ipaddr_t smtpserver;
#define ISO_nl 0x0a #define ISO_nl 0x0a
#define ISO_cr 0x0d #define ISO_cr 0x0d
@@ -66,110 +69,141 @@ static uip_ipaddr_t smtpserver;
#define ISO_4 0x34 #define ISO_4 0x34
#define ISO_5 0x35 #define ISO_5 0x35
/* This structure represents the state of a single SMTP transaction */
/*---------------------------------------------------------------------------*/ struct smtp_state
static
void smtp_thread(void)
{ {
psock_readto(&s.psock, ISO_nl); uint8 state;
boolean connected;
sem_t sem;
struct psock psock;
uip_ipaddr_t smtpserver;
char *localhostname;
char *to;
char *cc;
char *from;
char *subject;
char *msg;
int msglen;
int sentlen;
int textlen;
int sendptr;
int result;
char buffer[SMTP_INPUT_BUFFER_SIZE];
};
if(strncmp(s.inputbuffer, smtp_220, 3) != 0) { static volatile struct smtp_state *gpsmtp = 0;
PSOCK_CLOSE(&s.psock);
smtp_done(2);
pthread_exit(NULL);
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_helo); static void smtp_send_message(struct smtp_state *psmtp)
PSOCK_SEND_STR(&s.psock, localhostname); {
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl); psock_readto(&psmtp->psock, ISO_nl);
psock_readto(&s.psock, ISO_nl); if (strncmp(psmtp->buffer, smtp_220, 3) != 0)
{
if(s.inputbuffer[0] != ISO_2) { PSOCK_CLOSE(&psmtp->psock);
PSOCK_CLOSE(&s.psock); psmtp->result = 2;
smtp_done(3); return;
pthread_exit(NULL);
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_mail_from);
PSOCK_SEND_STR(&s.psock, s.from);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
psock_readto(&s.psock, ISO_nl);
if(s.inputbuffer[0] != ISO_2) {
PSOCK_CLOSE(&s.psock);
smtp_done(4);
pthread_exit(NULL);
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_rcpt_to);
PSOCK_SEND_STR(&s.psock, s.to);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
psock_readto(&s.psock, ISO_nl);
if(s.inputbuffer[0] != ISO_2) {
PSOCK_CLOSE(&s.psock);
smtp_done(5);
pthread_exit(NULL);
}
if(s.cc != 0) {
PSOCK_SEND_STR(&s.psock, (char *)smtp_rcpt_to);
PSOCK_SEND_STR(&s.psock, s.cc);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
psock_readto(&s.psock, ISO_nl);
if(s.inputbuffer[0] != ISO_2) {
PSOCK_CLOSE(&s.psock);
smtp_done(6);
pthread_exit(NULL);
} }
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_data); PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_helo);
PSOCK_SEND_STR(&psmtp->psock, psmtp->localhostname);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
psock_readto(&s.psock, ISO_nl); psock_readto(&psmtp->psock, ISO_nl);
if(s.inputbuffer[0] != ISO_3) { if (psmtp->buffer[0] != ISO_2)
PSOCK_CLOSE(&s.psock); {
smtp_done(7); PSOCK_CLOSE(&psmtp->psock);
pthread_exit(NULL); psmtp->result = 3;
} return;
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_to); PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_mail_from);
PSOCK_SEND_STR(&s.psock, s.to); PSOCK_SEND_STR(&psmtp->psock, psmtp->from);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl); PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
if(s.cc != 0) {
PSOCK_SEND_STR(&s.psock, (char *)smtp_cc);
PSOCK_SEND_STR(&s.psock, s.cc);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
}
PSOCK_SEND_STR(&s.psock, (char *)smtp_from); psock_readto(&psmtp->psock, ISO_nl);
PSOCK_SEND_STR(&s.psock, s.from);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
PSOCK_SEND_STR(&s.psock, (char *)smtp_subject); if (psmtp->buffer[0] != ISO_2)
PSOCK_SEND_STR(&s.psock, s.subject); {
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl); PSOCK_CLOSE(&psmtp->psock);
psmtp->result = 3;
return;
}
psock_send(&s.psock, s.msg, s.msglen); PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_rcpt_to);
PSOCK_SEND_STR(&psmtp->psock, psmtp->to);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
PSOCK_SEND_STR(&s.psock, (char *)smtp_crnlperiodcrnl); psock_readto(&psmtp->psock, ISO_nl);
psock_readto(&s.psock, ISO_nl); if (psmtp->buffer[0] != ISO_2)
if(s.inputbuffer[0] != ISO_2) { {
PSOCK_CLOSE(&s.psock); PSOCK_CLOSE(&psmtp->psock);
smtp_done(8); psmtp->result = 5;
pthread_exit(NULL); return;
} }
PSOCK_SEND_STR(&s.psock, (char *)smtp_quit); if (psmtp->cc != 0)
smtp_done(SMTP_ERR_OK); {
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_rcpt_to);
PSOCK_SEND_STR(&psmtp->psock, psmtp->cc);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
psock_readto(&psmtp->psock, ISO_nl);
if (psmtp->buffer[0] != ISO_2)
{
PSOCK_CLOSE(&psmtp->psock);
psmtp->result = 6;
return;
}
}
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_data);
psock_readto(&psmtp->psock, ISO_nl);
if (psmtp->buffer[0] != ISO_3)
{
PSOCK_CLOSE(&psmtp->psock);
psmtp->result = 7;
return;
}
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_to);
PSOCK_SEND_STR(&psmtp->psock, psmtp->to);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
if (psmtp->cc != 0)
{
PSOCK_SEND_STR(&psmtp->psock, (char *)psmtp->cc);
PSOCK_SEND_STR(&psmtp->psock, psmtp->cc);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
}
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_from);
PSOCK_SEND_STR(&psmtp->psock, psmtp->from);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_subject);
PSOCK_SEND_STR(&psmtp->psock, psmtp->subject);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
psock_send(&psmtp->psock, psmtp->msg, psmtp->msglen);
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnlperiodcrnl);
psock_readto(&psmtp->psock, ISO_nl);
if (psmtp->buffer[0] != ISO_2)
{
PSOCK_CLOSE(&psmtp->psock);
psmtp->result = 8;
return;
}
PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_quit);
psmtp->result = 0;
} }
/* This function is called by the UIP interrupt handling logic whenevent an /* This function is called by the UIP interrupt handling logic whenevent an
@@ -178,75 +212,109 @@ void smtp_thread(void)
void uip_interrupt_event(void) void uip_interrupt_event(void)
{ {
if(uip_closed()) { if (gpsmtp)
s.connected = 0; {
return; if (uip_closed())
} {
if(uip_aborted() || uip_timedout()) { gpsmtp->connected = FALSE;
s.connected = 0; return;
smtp_done(1); }
return;
} if (uip_aborted() || uip_timedout())
smtp_thread(); {
gpsmtp->connected = FALSE;
}
sem_post((sem_t*)&gpsmtp->sem);
}
} }
/*---------------------------------------------------------------------------*/ /* Specificy an SMTP server and hostname.
/**
* Specificy an SMTP server and hostname.
* *
* This function is used to configure the SMTP module with an SMTP * This function is used to configure the SMTP module with an SMTP
* server and the hostname of the host. * server and the hostname of the host.
* *
* \param lhostname The hostname of the uIP host. * lhostname The hostname of the uIP host.
* *
* \param server A pointer to a 4-byte array representing the IP * server A pointer to a 4-byte array representing the IP
* address of the SMTP server to be configured. * address of the SMTP server to be configured.
*/ */
void
smtp_configure(char *lhostname, void *server) void smtp_configure(void *handle, char *lhostname, void *server)
{ {
localhostname = lhostname; struct smtp_state *psmtp = (struct smtp_state *)handle;
uip_ipaddr_copy(smtpserver, server); psmtp->localhostname = lhostname;
uip_ipaddr_copy(psmtp->smtpserver, server);
} }
/*---------------------------------------------------------------------------*/
/** /* Send an e-mail.
* Send an e-mail.
* *
* \param to The e-mail address of the receiver of the e-mail. * to The e-mail address of the receiver of the e-mail.
* \param cc The e-mail address of the CC: receivers of the e-mail. * cc The e-mail address of the CC: receivers of the e-mail.
* \param from The e-mail address of the sender of the e-mail. * from The e-mail address of the sender of the e-mail.
* \param subject The subject of the e-mail. * subject The subject of the e-mail.
* \param msg The actual e-mail message. * msg The actual e-mail message.
* \param msglen The length of the e-mail message. * msglen The length of the e-mail message.
*/ */
unsigned char
smtp_send(char *to, char *cc, char *from, int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char *msg, int msglen)
char *subject, char *msg, uint16 msglen)
{ {
struct smtp_state *psmtp = (struct smtp_state *)handle;
struct uip_conn *conn; struct uip_conn *conn;
conn = uip_connect(smtpserver, HTONS(25)); conn = uip_connect(&psmtp->smtpserver, HTONS(25));
if(conn == NULL) { if (conn == NULL)
return 0; {
} return ERROR;
s.connected = 1; }
s.to = to;
s.cc = cc;
s.from = from;
s.subject = subject;
s.msg = msg;
s.msglen = msglen;
psock_init(&s.psock, s.inputbuffer, sizeof(s.inputbuffer)); psmtp->connected = TRUE;
psmtp->to = to;
return 1; psmtp->cc = cc;
psmtp->from = from;
psmtp->subject = subject;
psmtp->msg = msg;
psmtp->msglen = msglen;
psmtp->result = OK;
gpsmtp = psmtp;
psock_init(&psmtp->psock, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE);
/* And wait for the the socket to be connected */
sem_wait(&psmtp->sem);
gpsmtp = 0;
/* Was an error reported by interrupt handler? */
if (psmtp->result == OK )
{
/* No... Send the message */
smtp_send_message(psmtp);
}
return psmtp->result;
} }
/*---------------------------------------------------------------------------*/
void void *smtp_open(void)
smtp_init(void)
{ {
s.connected = 0; /* Allocate the handle */
struct smtp_state *psmtp = (struct smtp_state *)malloc(sizeof(struct smtp_state));
if (psmtp)
{
/* Initialize the handle */
memset(psmtp, 0, sizeof(struct smtp_state));
(void)sem_init(&psmtp->sem, 0, 0);
}
return (void*)psmtp;
} }
/*---------------------------------------------------------------------------*/
/** @} */ void smtp_close(void *handle)
/** @} */ {
struct smtp_state *psmtp = (struct smtp_state *)handle;
if (psmtp)
{
sem_destroy(&psmtp->sem);
free(psmtp);
}
}