These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers smtp.c Source File

smtp.c

Go to the documentation of this file.
00001 /** @addtogroup EMAC_uIP
00002  * @{
00003  */
00004 /**
00005  * \addtogroup apps
00006  * @{
00007  */
00008 
00009 /**
00010  * \defgroup smtp SMTP E-mail sender
00011  * @{
00012  *
00013  * The Simple Mail Transfer Protocol (SMTP) as defined by RFC821 is
00014  * the standard way of sending and transfering e-mail on the
00015  * Internet. This simple example implementation is intended as an
00016  * example of how to implement protocols in uIP, and is able to send
00017  * out e-mail but has not been extensively tested.
00018  */
00019 
00020 /**
00021  * \file
00022  * SMTP example implementation
00023  * \author Adam Dunkels <adam@dunkels.com>
00024  */
00025 
00026 /*
00027  * Copyright (c) 2004, Adam Dunkels.
00028  * All rights reserved.
00029  *
00030  * Redistribution and use in source and binary forms, with or without
00031  * modification, are permitted provided that the following conditions
00032  * are met:
00033  * 1. Redistributions of source code must retain the above copyright
00034  *    notice, this list of conditions and the following disclaimer.
00035  * 2. Redistributions in binary form must reproduce the above copyright
00036  *    notice, this list of conditions and the following disclaimer in the
00037  *    documentation and/or other materials provided with the distribution.
00038  * 3. Neither the name of the Institute nor the names of its contributors
00039  *    may be used to endorse or promote products derived from this software
00040  *    without specific prior written permission.
00041  *
00042  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00043  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00044  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00045  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00046  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00047  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00048  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00049  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00050  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00051  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  *
00054  * This file is part of the uIP TCP/IP stack.
00055  *
00056  * Author: Adam Dunkels <adam@sics.se>
00057  *
00058  * $Id: smtp.c,v 1.4 2006/06/11 21:46:37 adam Exp $
00059  */
00060 #include "smtp.h"
00061 
00062 #include "smtp-strings.h"
00063 #include "psock.h"
00064 #include "uip.h"
00065 
00066 #include <string.h>
00067 
00068 static struct smtp_state s;
00069 
00070 static char *localhostname;
00071 static uip_ipaddr_t smtpserver;
00072 
00073 #define ISO_nl 0x0a
00074 #define ISO_cr 0x0d
00075 
00076 #define ISO_period 0x2e
00077 
00078 #define ISO_2  0x32
00079 #define ISO_3  0x33
00080 #define ISO_4  0x34
00081 #define ISO_5  0x35
00082 
00083 
00084 /*---------------------------------------------------------------------------*/
00085 static
00086 PT_THREAD(smtp_thread(void))
00087 {
00088   PSOCK_BEGIN(&s.psock);
00089 
00090   PSOCK_READTO(&s.psock, ISO_nl);
00091 
00092   if(strncmp(s.inputbuffer, smtp_220, 3) != 0) {
00093     PSOCK_CLOSE(&s.psock);
00094     smtp_done(2);
00095     PSOCK_EXIT(&s.psock);
00096   }
00097 
00098   PSOCK_SEND_STR(&s.psock, (char *)smtp_helo);
00099   PSOCK_SEND_STR(&s.psock, localhostname);
00100   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00101 
00102   PSOCK_READTO(&s.psock, ISO_nl);
00103 
00104   if(s.inputbuffer[0] != ISO_2) {
00105     PSOCK_CLOSE(&s.psock);
00106     smtp_done(3);
00107     PSOCK_EXIT(&s.psock);
00108   }
00109 
00110   PSOCK_SEND_STR(&s.psock, (char *)smtp_mail_from);
00111   PSOCK_SEND_STR(&s.psock, s.from);
00112   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00113 
00114   PSOCK_READTO(&s.psock, ISO_nl);
00115 
00116   if(s.inputbuffer[0] != ISO_2) {
00117     PSOCK_CLOSE(&s.psock);
00118     smtp_done(4);
00119     PSOCK_EXIT(&s.psock);
00120   }
00121 
00122   PSOCK_SEND_STR(&s.psock, (char *)smtp_rcpt_to);
00123   PSOCK_SEND_STR(&s.psock, s.to);
00124   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00125 
00126   PSOCK_READTO(&s.psock, ISO_nl);
00127 
00128   if(s.inputbuffer[0] != ISO_2) {
00129     PSOCK_CLOSE(&s.psock);
00130     smtp_done(5);
00131     PSOCK_EXIT(&s.psock);
00132   }
00133 
00134   if(s.cc != 0) {
00135     PSOCK_SEND_STR(&s.psock, (char *)smtp_rcpt_to);
00136     PSOCK_SEND_STR(&s.psock, s.cc);
00137     PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00138 
00139     PSOCK_READTO(&s.psock, ISO_nl);
00140 
00141     if(s.inputbuffer[0] != ISO_2) {
00142       PSOCK_CLOSE(&s.psock);
00143       smtp_done(6);
00144       PSOCK_EXIT(&s.psock);
00145     }
00146   }
00147 
00148   PSOCK_SEND_STR(&s.psock, (char *)smtp_data);
00149 
00150   PSOCK_READTO(&s.psock, ISO_nl);
00151 
00152   if(s.inputbuffer[0] != ISO_3) {
00153     PSOCK_CLOSE(&s.psock);
00154     smtp_done(7);
00155     PSOCK_EXIT(&s.psock);
00156   }
00157 
00158   PSOCK_SEND_STR(&s.psock, (char *)smtp_to);
00159   PSOCK_SEND_STR(&s.psock, s.to);
00160   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00161 
00162   if(s.cc != 0) {
00163     PSOCK_SEND_STR(&s.psock, (char *)smtp_cc);
00164     PSOCK_SEND_STR(&s.psock, s.cc);
00165     PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00166   }
00167 
00168   PSOCK_SEND_STR(&s.psock, (char *)smtp_from);
00169   PSOCK_SEND_STR(&s.psock, s.from);
00170   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00171 
00172   PSOCK_SEND_STR(&s.psock, (char *)smtp_subject);
00173   PSOCK_SEND_STR(&s.psock, s.subject);
00174   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnl);
00175 
00176   PSOCK_SEND(&s.psock, s.msg, s.msglen);
00177 
00178   PSOCK_SEND_STR(&s.psock, (char *)smtp_crnlperiodcrnl);
00179 
00180   PSOCK_READTO(&s.psock, ISO_nl);
00181   if(s.inputbuffer[0] != ISO_2) {
00182     PSOCK_CLOSE(&s.psock);
00183     smtp_done(8);
00184     PSOCK_EXIT(&s.psock);
00185   }
00186 
00187   PSOCK_SEND_STR(&s.psock, (char *)smtp_quit);
00188   smtp_done(SMTP_ERR_OK);
00189   PSOCK_END(&s.psock);
00190 }
00191 /*---------------------------------------------------------------------------*/
00192 void
00193 smtp_appcall(void)
00194 {
00195   if(uip_closed()) {
00196     s.connected = 0;
00197     return;
00198   }
00199   if(uip_aborted() || uip_timedout()) {
00200     s.connected = 0;
00201     smtp_done(1);
00202     return;
00203   }
00204   smtp_thread();
00205 }
00206 /*---------------------------------------------------------------------------*/
00207 /**
00208  * Specificy an SMTP server and hostname.
00209  *
00210  * This function is used to configure the SMTP module with an SMTP
00211  * server and the hostname of the host.
00212  *
00213  * \param lhostname The hostname of the uIP host.
00214  *
00215  * \param server A pointer to a 4-byte array representing the IP
00216  * address of the SMTP server to be configured.
00217  */
00218 void
00219 smtp_configure(char *lhostname, void *server)
00220 {
00221   localhostname = lhostname;
00222   uip_ipaddr_copy(smtpserver, server);
00223 }
00224 /*---------------------------------------------------------------------------*/
00225 /**
00226  * Send an e-mail.
00227  *
00228  * \param to The e-mail address of the receiver of the e-mail.
00229  * \param cc The e-mail address of the CC: receivers of the e-mail.
00230  * \param from The e-mail address of the sender of the e-mail.
00231  * \param subject The subject of the e-mail.
00232  * \param msg The actual e-mail message.
00233  * \param msglen The length of the e-mail message.
00234  */
00235 unsigned char
00236 smtp_send(char *to, char *cc, char *from,
00237       char *subject, char *msg, u16_t msglen)
00238 {
00239   struct uip_conn *conn;
00240 
00241   conn = uip_connect(smtpserver, HTONS(25));
00242   if(conn == NULL) {
00243     return 0;
00244   }
00245   s.connected = 1;
00246   s.to = to;
00247   s.cc = cc;
00248   s.from = from;
00249   s.subject = subject;
00250   s.msg = msg;
00251   s.msglen = msglen;
00252 
00253   PSOCK_INIT(&s.psock, s.inputbuffer, sizeof(s.inputbuffer));
00254 
00255   return 1;
00256 }
00257 /*---------------------------------------------------------------------------*/
00258 void
00259 smtp_init(void)
00260 {
00261   s.connected = 0;
00262 }
00263 /*---------------------------------------------------------------------------*/
00264 /** @} */
00265 /** @} */
00266 /** @} */