lib/misc/response_read.cc
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include <unistd.h>
       
    19 #include "response.h"
       
    20 
       
    21 static mystring receive_string(int fd)
       
    22 {
       
    23   unsigned char lenbuf[2];
       
    24   if(read(fd, lenbuf, 2) != 2)
       
    25     return "";
       
    26   ssize_t len = lenbuf[0] << 8 | lenbuf[1];
       
    27   char buf[len+1];
       
    28   if(read(fd, buf, len) != len)
       
    29     return "";
       
    30   return mystring(buf, len);
       
    31 }
       
    32 
       
    33 static response read_response(int fd)
       
    34 {
       
    35   response::response_code code;
       
    36   mystring msg;
       
    37   unsigned char c;
       
    38   if(read(fd, &c, 1) != 1) {
       
    39     code = response::econn;
       
    40     msg = "Sender aborted the data connection";
       
    41   }
       
    42   else {
       
    43     msg = receive_string(fd);
       
    44     switch(c) {
       
    45     case 0:
       
    46     case 1:
       
    47     case 2:
       
    48       code = (response::response_code)c;
       
    49       break;
       
    50     default:
       
    51       code = response::econn;
       
    52       msg = "Invalid data received from sender";
       
    53     }
       
    54   }
       
    55   return response(code, msg);
       
    56 }
       
    57 
       
    58 response response::read(int fd)
       
    59 {
       
    60   return read_response(fd);
       
    61 }