summaryrefslogtreecommitdiff
path: root/nuttx/netutils/smtp/smtp.c
blob: d4d8feee0f23574c1a212578f7ad0f1b9ab12189 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/****************************************************************************
 * smtp.c
 * smtp SMTP E-mail sender
 *
 *   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 standard way of sending and transfering e-mail on the
 * Internet. This simple example implementation is intended as an
 * example of how to implement protocols in uIP, and is able to send
 * out e-mail but has not been extensively tested.
 *
 *   Author: Adam Dunkels <adam@dunkels.com>
 *   Copyright (c) 2004, Adam Dunkels.
 *   All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * 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
 * SUCH DAMAGE.
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <semaphore.h>

#include <net/uip/uip.h>
#include <net/uip/psock.h>
#include <net/uip/smtp.h>

#include "smtp-strings.h"

#define SMTP_INPUT_BUFFER_SIZE 512

#define ISO_nl 0x0a
#define ISO_cr 0x0d

#define ISO_period 0x2e

#define ISO_2  0x32
#define ISO_3  0x33
#define ISO_4  0x34
#define ISO_5  0x35

/* This structure represents the state of a single SMTP transaction */

struct smtp_state
{
  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];
};

static volatile struct smtp_state *gpsmtp = 0;

static void smtp_send_message(struct smtp_state *psmtp)
{
  psock_readto(&psmtp->psock, ISO_nl);

  if (strncmp(psmtp->buffer, smtp_220, 3) != 0)
    {
      PSOCK_CLOSE(&psmtp->psock);
      psmtp->result = 2;
      return;
    }

  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(&psmtp->psock, ISO_nl);

  if (psmtp->buffer[0] != ISO_2)
    {
      PSOCK_CLOSE(&psmtp->psock);
      psmtp->result = 3;
      return;
    }

  PSOCK_SEND_STR(&psmtp->psock, (char *)smtp_mail_from);
  PSOCK_SEND_STR(&psmtp->psock, psmtp->from);
  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 = 3;
      return;
    }

  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_readto(&psmtp->psock, ISO_nl);

  if (psmtp->buffer[0] != ISO_2)
    {
      PSOCK_CLOSE(&psmtp->psock);
      psmtp->result = 5;
      return;
    }

  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);

      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
 * event of interest occurs.
 */

void uip_interrupt_event(void)
{
  if (gpsmtp)
    {
      if (uip_closed())
        {
          gpsmtp->connected = FALSE;
          return;
        }

      if (uip_aborted() || uip_timedout())
        {
          gpsmtp->connected = FALSE;
        }

      sem_post((sem_t*)&gpsmtp->sem);
    }
}

/* Specificy an SMTP server and hostname.
 *
 * This function is used to configure the SMTP module with an SMTP
 * server and the hostname of the host.
 *
 * lhostname The hostname of the uIP host.
 *
 * server A pointer to a 4-byte array representing the IP
 * address of the SMTP server to be configured.
 */

void smtp_configure(void *handle, char *lhostname, void *server)
{
  struct smtp_state *psmtp = (struct smtp_state *)handle;
  psmtp->localhostname = lhostname;
  uip_ipaddr_copy(psmtp->smtpserver, server);
}

/* Send an e-mail.
 *
 * to The e-mail address of the receiver of the e-mail.
 * cc The e-mail address of the CC: receivers of the e-mail.
 * from The e-mail address of the sender of the e-mail.
 * subject The subject of the e-mail.
 * msg The actual e-mail message.
 * msglen The length of the e-mail message.
 */

int smtp_send(void *handle, char *to, char *cc, char *from, char *subject, char *msg, int msglen)
{
  struct smtp_state *psmtp = (struct smtp_state *)handle;
  struct uip_conn *conn;

  conn = uip_connect(&psmtp->smtpserver, HTONS(25));
  if (conn == NULL)
    {
      return ERROR;
    }

  psmtp->connected = TRUE;
  psmtp->to        = to;
  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 *smtp_open(void)
{
  /* 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);
    }
}