Contiki 3.x
smtp-socket.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 #include "smtp.h"
35 
36 #include "smtp-strings.h"
37 #include "contiki-net.h"
38 
39 #include <string.h>
40 
41 struct smtp_state {
42 
43  char connected;
44 
45  struct psock psock;
46 
47  char inputbuffer[4];
48 
49  char *to;
50  char *cc;
51  char *from;
52  char *subject;
53  char *msg;
54  uint8_t msgwidth;
55  uint8_t msgheight;
56  uint8_t line;
57 };
58 
59 static struct smtp_state s;
60 
61 static char *localhostname;
62 static uip_ipaddr_t smtpserver;
63 
64 #define ISO_nl 0x0a
65 #define ISO_cr 0x0d
66 
67 #define ISO_period 0x2e
68 
69 #define ISO_2 0x32
70 #define ISO_3 0x33
71 #define ISO_4 0x34
72 #define ISO_5 0x35
73 
74 
75 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned char)strlen(str))
76 /*---------------------------------------------------------------------------*/
77 static
78 PT_THREAD(smtp_thread(void))
79 {
80  PSOCK_BEGIN(&s.psock);
81 
82  PSOCK_READTO(&s.psock, ISO_nl);
83 
84  if(strncmp(s.inputbuffer, smtp_220, 3) != 0) {
85  PSOCK_CLOSE(&s.psock);
86  smtp_done(2);
87  PSOCK_EXIT(&s.psock);
88  }
89 
90  SEND_STRING(&s.psock, (char *)smtp_helo);
91  SEND_STRING(&s.psock, localhostname);
92  SEND_STRING(&s.psock, (char *)smtp_crnl);
93 
94  PSOCK_READTO(&s.psock, ISO_nl);
95 
96  if(s.inputbuffer[0] != ISO_2) {
97  PSOCK_CLOSE(&s.psock);
98  smtp_done(3);
99  PSOCK_EXIT(&s.psock);
100  }
101 
102  SEND_STRING(&s.psock, (char *)smtp_mail_from);
103  SEND_STRING(&s.psock, s.from);
104  SEND_STRING(&s.psock, (char *)smtp_crnl);
105 
106  PSOCK_READTO(&s.psock, ISO_nl);
107 
108  if(s.inputbuffer[0] != ISO_2) {
109  PSOCK_CLOSE(&s.psock);
110  smtp_done(4);
111  PSOCK_EXIT(&s.psock);
112  }
113 
114  SEND_STRING(&s.psock, (char *)smtp_rcpt_to);
115  SEND_STRING(&s.psock, s.to);
116  SEND_STRING(&s.psock, (char *)smtp_crnl);
117 
118  PSOCK_READTO(&s.psock, ISO_nl);
119 
120  if(s.inputbuffer[0] != ISO_2) {
121  PSOCK_CLOSE(&s.psock);
122  smtp_done(5);
123  PSOCK_EXIT(&s.psock);
124  }
125 
126  if(*s.cc != 0) {
127  SEND_STRING(&s.psock, (char *)smtp_rcpt_to);
128  SEND_STRING(&s.psock, s.cc);
129  SEND_STRING(&s.psock, (char *)smtp_crnl);
130 
131  PSOCK_READTO(&s.psock, ISO_nl);
132 
133  if(s.inputbuffer[0] != ISO_2) {
134  PSOCK_CLOSE(&s.psock);
135  smtp_done(6);
136  PSOCK_EXIT(&s.psock);
137  }
138  }
139 
140  SEND_STRING(&s.psock, (char *)smtp_data);
141 
142  PSOCK_READTO(&s.psock, ISO_nl);
143 
144  if(s.inputbuffer[0] != ISO_3) {
145  PSOCK_CLOSE(&s.psock);
146  smtp_done(7);
147  PSOCK_EXIT(&s.psock);
148  }
149 
150  SEND_STRING(&s.psock, (char *)smtp_to);
151  SEND_STRING(&s.psock, s.to);
152  SEND_STRING(&s.psock, (char *)smtp_crnl);
153 
154  if(*s.cc != 0) {
155  SEND_STRING(&s.psock, (char *)smtp_cc);
156  SEND_STRING(&s.psock, s.cc);
157  SEND_STRING(&s.psock, (char *)smtp_crnl);
158  }
159 
160  SEND_STRING(&s.psock, (char *)smtp_from);
161  SEND_STRING(&s.psock, s.from);
162  SEND_STRING(&s.psock, (char *)smtp_crnl);
163 
164  SEND_STRING(&s.psock, (char *)smtp_subject);
165  SEND_STRING(&s.psock, s.subject);
166  SEND_STRING(&s.psock, (char *)smtp_crnl);
167 
168  for(s.line = 0; s.line < s.msgheight; ++s.line) {
169  SEND_STRING(&s.psock, (char *)smtp_crnl);
170  SEND_STRING(&s.psock, &s.msg[s.line * s.msgwidth]);
171  }
172 
173  SEND_STRING(&s.psock, (char *)smtp_crnlperiodcrnl);
174 
175  PSOCK_READTO(&s.psock, ISO_nl);
176  if(s.inputbuffer[0] != ISO_2) {
177  PSOCK_CLOSE(&s.psock);
178  smtp_done(8);
179  PSOCK_EXIT(&s.psock);
180  }
181 
182  SEND_STRING(&s.psock, (char *)smtp_quit);
183  smtp_done(SMTP_ERR_OK);
184  PSOCK_END(&s.psock);
185 }
186 /*---------------------------------------------------------------------------*/
187 void
188 smtp_appcall(void *state)
189 {
190  if(uip_closed()) {
191  s.connected = 0;
192  return;
193  }
194  if(uip_aborted() || uip_timedout()) {
195  s.connected = 0;
196  smtp_done(1);
197  return;
198  }
199  smtp_thread();
200 }
201 /*---------------------------------------------------------------------------*/
202 void
203 smtp_configure(char *lhostname, uip_ipaddr_t *server)
204 {
205  localhostname = lhostname;
206  uip_ipaddr_copy(&smtpserver, server);
207 }
208 /*---------------------------------------------------------------------------*/
209 unsigned char
210 smtp_send(char *to, char *cc, char *from, char *subject,
211  char *msg, uint8_t msgwidth, uint8_t msgheight)
212 {
213  struct uip_conn *conn;
214 
215  conn = tcp_connect(&smtpserver, UIP_HTONS(25), NULL);
216  if(conn == NULL) {
217  return 0;
218  }
219  s.connected = 1;
220  s.to = to;
221  s.cc = cc;
222  s.from = from;
223  s.subject = subject;
224  s.msg = msg;
225  s.msgwidth = msgwidth;
226  s.msgheight = msgheight;
227 
228  PSOCK_INIT(&s.psock, (uint8_t *)s.inputbuffer, sizeof(s.inputbuffer));
229 
230  return 1;
231 }
232 /*---------------------------------------------------------------------------*/
233 void
234 smtp_init(void)
235 {
236  s.connected = 0;
237 }
238 /*---------------------------------------------------------------------------*/
239 
#define PSOCK_READTO(psock, c)
Read data up to a specified character.
Definition: psock.h:291
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Definition: psock.h:164
Representation of a uIP TCP connection.
Definition: uip.h:1336
CCIF struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
#define NULL
The null pointer.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
#define PSOCK_INIT(psock, buffer, buffersize)
Initialize a protosocket.
Definition: psock.h:150
#define PSOCK_CLOSE(psock)
Close a protosocket.
Definition: psock.h:241
The representation of a protosocket.
Definition: psock.h:112
#define PSOCK_END(psock)
Declare the end of a protosocket&#39;s protothread.
Definition: psock.h:348
#define PSOCK_EXIT(psock)
Exit the protosocket&#39;s protothread.
Definition: psock.h:320
#define uip_timedout()
Has the connection timed out?
Definition: uip.h:791
#define uip_closed()
Has the connection been closed by the other end?
Definition: uip.h:771