adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 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 | * |
oliverschmidt | f9750b5 | 2005-05-05 23:02:01 +0000 | [diff] [blame] | 33 | * $Id: smtp-socket.c,v 1.6 2005/05/05 23:02:01 oliverschmidt Exp $ |
adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 34 | */ |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 35 | #include "smtp.h" |
| 36 | |
| 37 | #include "smtp-strings.h" |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 38 | #include "psock.h" |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | struct smtp_state { |
| 43 | |
| 44 | char connected; |
| 45 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 46 | struct psock psock; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 47 | |
| 48 | char inputbuffer[4]; |
| 49 | |
| 50 | char *to; |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 51 | char *cc; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 52 | char *from; |
| 53 | char *subject; |
| 54 | char *msg; |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 55 | u8_t msgwidth; |
| 56 | u8_t msgheight; |
| 57 | u8_t line; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static struct smtp_state s; |
| 61 | |
| 62 | static char *localhostname; |
| 63 | static u16_t smtpserver[2]; |
| 64 | |
| 65 | #define ISO_nl 0x0a |
| 66 | #define ISO_cr 0x0d |
| 67 | |
| 68 | #define ISO_period 0x2e |
| 69 | |
| 70 | #define ISO_2 0x32 |
| 71 | #define ISO_3 0x33 |
| 72 | #define ISO_4 0x34 |
| 73 | #define ISO_5 0x35 |
| 74 | |
| 75 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 76 | #define SEND_STRING(s, str) PSOCK_SEND(s, str, strlen(str)) |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 77 | /*---------------------------------------------------------------------------*/ |
| 78 | static |
| 79 | PT_THREAD(smtp_thread(void)) |
| 80 | { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 81 | PSOCK_BEGIN(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 82 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 83 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 84 | |
| 85 | if(strncmp(s.inputbuffer, smtp_220, 3) != 0) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 86 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 87 | smtp_done(2); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 88 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 89 | } |
| 90 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 91 | SEND_STRING(&s.psock, (char *)smtp_helo); |
| 92 | SEND_STRING(&s.psock, localhostname); |
| 93 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 94 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 95 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 96 | |
| 97 | if(s.inputbuffer[0] != ISO_2) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 98 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 99 | smtp_done(3); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 100 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 103 | SEND_STRING(&s.psock, (char *)smtp_mail_from); |
| 104 | SEND_STRING(&s.psock, s.from); |
| 105 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 106 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 107 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 108 | |
| 109 | if(s.inputbuffer[0] != ISO_2) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 110 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 111 | smtp_done(4); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 112 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 113 | } |
| 114 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 115 | SEND_STRING(&s.psock, (char *)smtp_rcpt_to); |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 116 | SEND_STRING(&s.psock, s.to); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 117 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 118 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 119 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 120 | |
| 121 | if(s.inputbuffer[0] != ISO_2) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 122 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 123 | smtp_done(5); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 124 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 125 | } |
| 126 | |
oliverschmidt | f9750b5 | 2005-05-05 23:02:01 +0000 | [diff] [blame] | 127 | if(*s.cc != 0) { |
| 128 | SEND_STRING(&s.psock, (char *)smtp_rcpt_to); |
| 129 | SEND_STRING(&s.psock, s.cc); |
| 130 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
| 131 | |
| 132 | PSOCK_READTO(&s.psock, ISO_nl); |
| 133 | |
| 134 | if(s.inputbuffer[0] != ISO_2) { |
| 135 | PSOCK_CLOSE(&s.psock); |
| 136 | smtp_done(6); |
| 137 | PSOCK_EXIT(&s.psock); |
| 138 | } |
| 139 | } |
| 140 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 141 | SEND_STRING(&s.psock, (char *)smtp_data); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 142 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 143 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 144 | |
| 145 | if(s.inputbuffer[0] != ISO_3) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 146 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | f9750b5 | 2005-05-05 23:02:01 +0000 | [diff] [blame] | 147 | smtp_done(7); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 148 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 149 | } |
| 150 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 151 | SEND_STRING(&s.psock, (char *)smtp_to); |
| 152 | SEND_STRING(&s.psock, s.to); |
| 153 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 154 | |
oliverschmidt | f9750b5 | 2005-05-05 23:02:01 +0000 | [diff] [blame] | 155 | if(*s.cc != 0) { |
| 156 | SEND_STRING(&s.psock, (char *)smtp_cc); |
| 157 | SEND_STRING(&s.psock, s.cc); |
| 158 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
| 159 | } |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 160 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 161 | SEND_STRING(&s.psock, (char *)smtp_from); |
| 162 | SEND_STRING(&s.psock, s.from); |
| 163 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 164 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 165 | SEND_STRING(&s.psock, (char *)smtp_subject); |
| 166 | SEND_STRING(&s.psock, s.subject); |
| 167 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 168 | |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 169 | for(s.line = 0; s.line < s.msgheight; ++s.line) { |
| 170 | SEND_STRING(&s.psock, (char *)smtp_crnl); |
| 171 | SEND_STRING(&s.psock, &s.msg[s.line * s.msgwidth]); |
| 172 | } |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 173 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 174 | SEND_STRING(&s.psock, (char *)smtp_crnlperiodcrnl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 175 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 176 | PSOCK_READTO(&s.psock, ISO_nl); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 177 | if(s.inputbuffer[0] != ISO_2) { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 178 | PSOCK_CLOSE(&s.psock); |
oliverschmidt | f9750b5 | 2005-05-05 23:02:01 +0000 | [diff] [blame] | 179 | smtp_done(8); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 180 | PSOCK_EXIT(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 181 | } |
| 182 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 183 | SEND_STRING(&s.psock, (char *)smtp_quit); |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 184 | smtp_done(SMTP_ERR_OK); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 185 | PSOCK_END(&s.psock); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 186 | } |
| 187 | /*---------------------------------------------------------------------------*/ |
| 188 | void |
| 189 | smtp_appcall(void *state) |
| 190 | { |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 191 | if(uip_closed()) { |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 192 | s.connected = 0; |
| 193 | return; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 194 | } |
oliverschmidt | 639f75d | 2005-05-05 22:15:19 +0000 | [diff] [blame] | 195 | if(uip_aborted() || uip_timedout()) { |
| 196 | s.connected = 0; |
| 197 | smtp_done(1); |
| 198 | return; |
| 199 | } |
| 200 | smtp_thread(); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 201 | } |
| 202 | /*---------------------------------------------------------------------------*/ |
| 203 | void |
| 204 | smtp_configure(char *lhostname, u16_t *server) |
| 205 | { |
| 206 | localhostname = lhostname; |
| 207 | smtpserver[0] = server[0]; |
| 208 | smtpserver[1] = server[1]; |
| 209 | } |
| 210 | /*---------------------------------------------------------------------------*/ |
| 211 | unsigned char |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 212 | smtp_send(char *to, char *cc, char *from, char *subject, |
| 213 | char *msg, u8_t msgwidth, u8_t msgheight) |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 214 | { |
| 215 | struct uip_conn *conn; |
| 216 | |
| 217 | conn = tcp_connect(smtpserver, HTONS(25), NULL); |
| 218 | if(conn == NULL) { |
| 219 | return 0; |
| 220 | } |
| 221 | s.connected = 1; |
| 222 | s.to = to; |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 223 | s.cc = cc; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 224 | s.from = from; |
| 225 | s.subject = subject; |
| 226 | s.msg = msg; |
oliverschmidt | 1263500 | 2005-05-04 23:51:09 +0000 | [diff] [blame] | 227 | s.msgwidth = msgwidth; |
| 228 | s.msgheight = msgheight; |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 229 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 230 | PSOCK_INIT(&s.psock, s.inputbuffer, sizeof(s.inputbuffer)); |
adamdunkels | 15c3a8b | 2004-09-01 20:42:03 +0000 | [diff] [blame] | 231 | |
| 232 | return 1; |
| 233 | } |
| 234 | /*---------------------------------------------------------------------------*/ |
| 235 | void |
| 236 | smtp_init(void) |
| 237 | { |
| 238 | s.connected = 0; |
| 239 | } |
| 240 | /*---------------------------------------------------------------------------*/ |
| 241 | |