summaryrefslogtreecommitdiff
path: root/nuttx/netutils/smtp/smtp.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-09-09 17:20:56 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-09-09 17:20:56 +0000
commit7a65f932826220c741ffbf5698b48692a787d915 (patch)
tree825e048f8fc18b7e69cb155ad96b7254566a3ffe /nuttx/netutils/smtp/smtp.c
parentd12e00bdd6ffbb39ab5d45d5d5a484d293108021 (diff)
downloadpx4-nuttx-7a65f932826220c741ffbf5698b48692a787d915.tar.gz
px4-nuttx-7a65f932826220c741ffbf5698b48692a787d915.tar.bz2
px4-nuttx-7a65f932826220c741ffbf5698b48692a787d915.zip
Implement TCP send; remove uIP proto-sockets
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@339 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/netutils/smtp/smtp.c')
-rw-r--r--nuttx/netutils/smtp/smtp.c212
1 files changed, 102 insertions, 110 deletions
diff --git a/nuttx/netutils/smtp/smtp.c b/nuttx/netutils/smtp/smtp.c
index 5d7a61aa6..be224590a 100644
--- a/nuttx/netutils/smtp/smtp.c
+++ b/nuttx/netutils/smtp/smtp.c
@@ -47,13 +47,14 @@
****************************************************************************/
#include <sys/types.h>
-#include <string.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <net/uip/uip.h>
-#include <net/uip/psock.h>
#include <net/uip/smtp.h>
#include "smtp-strings.h"
@@ -77,7 +78,6 @@ struct smtp_state
uint8 state;
boolean connected;
sem_t sem;
- struct psock psock;
uip_ipaddr_t smtpserver;
char *localhostname;
char *to;
@@ -89,146 +89,155 @@ struct smtp_state
int sentlen;
int textlen;
int sendptr;
- int result;
char buffer[SMTP_INPUT_BUFFER_SIZE];
};
-static volatile struct smtp_state *gpsmtp = 0;
-
-static void smtp_send_message(struct smtp_state *psmtp)
+static inline int smtp_send_message(int sockfd, struct smtp_state *psmtp)
{
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (strncmp(psmtp->buffer, smtp_220, 3) != 0)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 2;
- return;
+ return ERROR;
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_helo);
- PSOCK_SEND_STR(&psmtp->psock, psmtp->localhostname);
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_helo, psmtp->localhostname);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (psmtp->buffer[0] != ISO_2)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 3;
- return;
+ return ERROR;
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_mail_from);
- PSOCK_SEND_STR(&psmtp->psock, psmtp->from);
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_mail_from, psmtp->from);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (psmtp->buffer[0] != ISO_2)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 3;
- return;
+ return ERROR;
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_rcpt_to);
- PSOCK_SEND_STR(&psmtp->psock, psmtp->to);
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_rcpt_to, psmtp->to);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (psmtp->buffer[0] != ISO_2)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 5;
- return;
+ return ERROR;
}
if (psmtp->cc != 0)
{
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_rcpt_to);
- PSOCK_SEND_STR(&psmtp->psock, psmtp->cc);
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_rcpt_to, psmtp->cc);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (psmtp->buffer[0] != ISO_2)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 6;
- return;
+ return ERROR;
}
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_data);
+ if (send(sockfd, smtp_data, strlen(smtp_data), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
+ {
+ return ERROR;
+ }
if (psmtp->buffer[0] != ISO_3)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 7;
- return;
+ return ERROR;
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_to);
- PSOCK_SEND_STR(&psmtp->psock, psmtp->to);
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_crnl);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_to, psmtp->to);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
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);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_to, psmtp->cc);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
}
- 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);
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_from, psmtp->from);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
+ {
+ return ERROR;
+ }
- psock_readto(&psmtp->psock, ISO_nl);
- if (psmtp->buffer[0] != ISO_2)
+ snprintf(psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, "%s%s\r\n", smtp_subject, psmtp->subject);
+ if (send(sockfd, psmtp->buffer, strlen(psmtp->buffer), 0) < 0)
{
- PSOCK_CLOSE(&psmtp->psock);
- psmtp->result = 8;
- return;
+ return ERROR;
}
- PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_quit);
- psmtp->result = 0;
-}
+ if (send(sockfd, psmtp->msg, psmtp->msglen, 0) < 0)
+ {
+ return ERROR;
+ }
-/* This function is called by the UIP interrupt handling logic whenevent an
- * event of interest occurs.
- */
+ if (send(sockfd, smtp_crnlperiodcrnl, strlen(smtp_crnlperiodcrnl), 0) < 0)
+ {
+ return ERROR;
+ }
-void uip_interrupt_event(void)
-{
-#warning OBSOLETE -- needs to be redesigned
- if (gpsmtp)
+ if (recv(sockfd, psmtp->buffer, SMTP_INPUT_BUFFER_SIZE, 0) < 0)
{
- if (uip_closed())
- {
- gpsmtp->connected = FALSE;
- return;
- }
+ return ERROR;
+ }
- if (uip_aborted() || uip_timedout())
- {
- gpsmtp->connected = FALSE;
- }
+ if (psmtp->buffer[0] != ISO_2)
+ {
+ return ERROR;
+ }
- sem_post((sem_t*)&gpsmtp->sem);
+ if (send(sockfd, smtp_quit, strlen(smtp_quit), 0) < 0)
+ {
+ return ERROR;
}
+ return OK;
}
/* Specificy an SMTP server and hostname.
@@ -264,6 +273,7 @@ int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char
struct smtp_state *psmtp = (struct smtp_state *)handle;
struct sockaddr_in server;
int sockfd;
+ int ret;
/* Setup */
@@ -274,7 +284,6 @@ int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char
psmtp->subject = subject;
psmtp->msg = msg;
psmtp->msglen = msglen;
- psmtp->result = OK;
/* Create a socket */
@@ -284,12 +293,6 @@ int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char
return ERROR;
}
- /* Make this instance globally visible (we will get interrupts as
- * soon as we connect
- */
-
- gpsmtp = psmtp;
-
/* Connect to server. First we have to set some fields in the
* 'server' structure. The system will assign me an arbitrary
* local port that is not in use.
@@ -301,27 +304,16 @@ int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0)
{
+ close(sockfd);
return ERROR;
- }
-
- /* Initialize the psock structure inside the smtp state structure */
-
- 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? */
+ /* Send the message */
- if (psmtp->result == OK )
- {
- /* No... Send the message */
- smtp_send_message(psmtp);
- }
+ ret = smtp_send_message(sockfd, psmtp);
- return psmtp->result;
+ close(sockfd);
+ return ret;
}
void *smtp_open(void)