viewing paste Unknown #236 | Text

Posted on the
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
static int Ack(char **server_response)
{
    static char buf[MAIL_BUFFER_SIZE];
    int rlen;
    int Index = 0;
    int Received = 0;
 
again:
 
    if ((rlen = recv(sc, buf + Index, ((MAIL_BUFFER_SIZE) - 1) - Received, 0)) < 1) {
        return (FAILED_TO_RECEIVE);
    }
    Received += rlen;
    buf[Received] = 0;
    /*err_msg   fprintf(stderr,"Received: (%d bytes) %s", rlen, buf + Index); */
 
    /* Check for newline */
    Index += rlen;
    
    /* SMPT RFC says \r\n is the only valid line ending, who are we to argue ;)
     * The response code must contain at least 5 characters ex. 220\r\n */
    if (Received < 5 || buf[Received - 1] != '\n' || buf[Received - 2] != '\r') {
        goto again;
    }
 
    if (buf[0] > '3') {
        /* If we've a valid pointer, return the SMTP server response so the error message contains more information */
        if (server_response) {
            int dec = 0;
            /* See if we have something like \r, \n, \r\n or \n\r at the end of the message and chop it off */
            if (Received > 2) {
                if (buf[Received-1] == '\n' || buf[Received-1] == '\r') {
                    dec++;
                    if (buf[Received-2] == '\r' || buf[Received-2] == '\n') {
                        dec++;
                    }
                }
 
            }
            *server_response = estrndup(buf, Received - dec);
        }
        return (SMTP_SERVER_ERROR);
    }
 
    return (SUCCESS);
}
Viewed 748 times, submitted by Guest.