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 | * |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 33 | * $Id: popc.c,v 1.3 2005/02/22 22:23:08 adamdunkels Exp $ |
adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 34 | */ |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 35 | |
| 36 | #include "contiki.h" |
| 37 | #include "popc.h" |
| 38 | #include "popc-strings.h" |
| 39 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 40 | #define SEND_STRING(s, str) PSOCK_SEND(s, str, strlen(str)) |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 41 | |
| 42 | enum { |
| 43 | COMMAND_NONE, |
| 44 | COMMAND_RETR, |
| 45 | COMMAND_TOP, |
| 46 | COMMAND_QUIT |
| 47 | }; |
| 48 | |
| 49 | /*---------------------------------------------------------------------------*/ |
| 50 | static |
| 51 | PT_THREAD(init_connection(struct popc_state *s)) |
| 52 | { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 53 | PSOCK_BEGIN(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 54 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 55 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 56 | if(s->inputbuf[0] != '+') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 57 | PSOCK_CLOSE_EXIT(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | SEND_STRING(&s->s, popc_strings_user); |
| 61 | SEND_STRING(&s->s, s->user); |
| 62 | SEND_STRING(&s->s, popc_strings_crnl); |
| 63 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 64 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 65 | if(s->inputbuf[0] != '+') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 66 | PSOCK_CLOSE_EXIT(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SEND_STRING(&s->s, popc_strings_pass); |
| 70 | SEND_STRING(&s->s, s->pass); |
| 71 | SEND_STRING(&s->s, popc_strings_crnl); |
| 72 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 73 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 74 | if(s->inputbuf[0] != '+') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 75 | PSOCK_CLOSE_EXIT(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | popc_connected(s); |
| 79 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 80 | PSOCK_END(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 81 | } |
| 82 | /*---------------------------------------------------------------------------*/ |
| 83 | static |
| 84 | PT_THREAD(stat(struct popc_state *s)) |
| 85 | { |
| 86 | unsigned short num; |
| 87 | unsigned long size; |
| 88 | char *ptr; |
| 89 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 90 | PSOCK_BEGIN(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 91 | |
| 92 | SEND_STRING(&s->s, popc_strings_stat); |
| 93 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 94 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 95 | if(s->inputbuf[0] != '+') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 96 | PSOCK_CLOSE_EXIT(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | num = 0; |
| 100 | for(ptr = &s->inputbuf[4]; *ptr >= '0' && *ptr <= '9'; ++ptr) { |
| 101 | num *= 10; |
| 102 | num += *ptr - '0'; |
| 103 | } |
| 104 | |
| 105 | size = 0; |
| 106 | for(ptr = ptr + 1; *ptr >= '0' && *ptr <= '9'; ++ptr) { |
| 107 | size *= 10; |
| 108 | size += *ptr - '0'; |
| 109 | } |
| 110 | |
| 111 | popc_messages(s, num, size); |
| 112 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 113 | PSOCK_END(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 114 | } |
| 115 | /*---------------------------------------------------------------------------*/ |
| 116 | static |
| 117 | PT_THREAD(retr(struct popc_state *s)) |
| 118 | { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 119 | PSOCK_BEGIN(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 120 | |
| 121 | SEND_STRING(&s->s, popc_strings_retr); |
| 122 | snprintf(s->outputbuf, sizeof(s->outputbuf), "%d", s->num); |
| 123 | SEND_STRING(&s->s, s->outputbuf); |
| 124 | SEND_STRING(&s->s, popc_strings_crnl); |
| 125 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 126 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 127 | if(s->inputbuf[0] != '+') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 128 | PSOCK_CLOSE_EXIT(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | popc_msgbegin(s); |
| 132 | while(s->inputbuf[0] != '.') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 133 | PSOCK_READTO(&s->s, '\n'); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 134 | if(s->inputbuf[0] != '.') { |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 135 | s->inputbuf[PSOCK_DATALEN(&s->s)] = 0; |
| 136 | popc_msgline(s, s->inputbuf, PSOCK_DATALEN(&s->s)); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | popc_msgend(s); |
| 140 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 141 | PSOCK_END(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 142 | |
| 143 | } |
| 144 | /*---------------------------------------------------------------------------*/ |
| 145 | static |
| 146 | PT_THREAD(handle_connection(struct popc_state *s)) |
| 147 | { |
| 148 | PT_BEGIN(&s->pt); |
| 149 | |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 150 | PSOCK_INIT(&s->s, s->inputbuf, sizeof(s->inputbuf) - 1); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 151 | |
| 152 | PT_WAIT_UNTIL(&s->pt, init_connection(s)); |
| 153 | PT_WAIT_UNTIL(&s->pt, stat(s)); |
| 154 | |
| 155 | timer_set(&s->timer, CLOCK_SECOND * 30); |
| 156 | |
| 157 | while(1) { |
| 158 | PT_WAIT_UNTIL(&s->pt, s->command != COMMAND_NONE || |
| 159 | timer_expired(&s->timer)); |
| 160 | |
| 161 | if(timer_expired(&s->timer)) { |
| 162 | PT_WAIT_UNTIL(&s->pt, stat(s)); |
| 163 | timer_set(&s->timer, CLOCK_SECOND * 30); |
| 164 | } |
| 165 | |
| 166 | switch(s->command) { |
| 167 | case COMMAND_RETR: |
| 168 | PT_WAIT_UNTIL(&s->pt, retr(s)); |
| 169 | break; |
| 170 | case COMMAND_QUIT: |
| 171 | tcp_markconn(uip_conn, NULL); |
adamdunkels | d0791bb | 2005-02-22 22:23:07 +0000 | [diff] [blame] | 172 | PSOCK_CLOSE(&s->s); |
adamdunkels | 6c19043 | 2004-09-09 20:51:18 +0000 | [diff] [blame] | 173 | PT_EXIT(&s->pt); |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | s->command = COMMAND_NONE; |
| 179 | |
| 180 | } |
| 181 | PT_END(&s->pt); |
| 182 | } |
| 183 | /*---------------------------------------------------------------------------*/ |
| 184 | void |
| 185 | popc_appcall(void *state) |
| 186 | { |
| 187 | struct popc_state *s = (struct popc_state *)state; |
| 188 | |
| 189 | if(uip_closed() || uip_aborted() || uip_timedout()) { |
| 190 | popc_closed(s); |
| 191 | } else if(uip_connected()) { |
| 192 | PT_INIT(&s->pt); |
| 193 | handle_connection(s); |
| 194 | } else if(s != NULL) { |
| 195 | handle_connection(s); |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | /*---------------------------------------------------------------------------*/ |
| 200 | void * |
| 201 | popc_connect(struct popc_state *s, u16_t *addr, |
| 202 | char *user, char *pass) |
| 203 | { |
| 204 | strncpy(s->user, user, sizeof(s->user)); |
| 205 | strncpy(s->pass, pass, sizeof(s->pass)); |
| 206 | s->conn = tcp_connect(addr, HTONS(110), s); |
| 207 | return s->conn; |
| 208 | } |
| 209 | /*---------------------------------------------------------------------------*/ |
| 210 | void |
| 211 | popc_retr(struct popc_state *s, unsigned short num) |
| 212 | { |
| 213 | s->command = COMMAND_RETR; |
| 214 | s->num = num; |
| 215 | } |
| 216 | /*---------------------------------------------------------------------------*/ |
| 217 | void |
| 218 | popc_top(struct popc_state *s, unsigned short num, unsigned short lines) |
| 219 | { |
| 220 | s->command = COMMAND_TOP; |
| 221 | s->num = num; |
| 222 | s->lines = lines; |
| 223 | } |
| 224 | /*---------------------------------------------------------------------------*/ |