From 5c62a551c431e05ab7a80cf48985870ee54c1a68 Mon Sep 17 00:00:00 2001 From: Steve Holme Date: Sat, 24 Mar 2012 11:55:34 +0000 Subject: email: Moved server greeting responses into separate functions Moved the server greeting response handling code from the statemach_act functions to separate response functions. This makes the code simpler to follow and provides consistency with the other responses that are handled here. --- lib/imap.c | 56 +++++++++++++++++++++++++++++++++++++------------------- lib/pop3.c | 50 ++++++++++++++++++++++++++++++++++---------------- lib/smtp.c | 35 +++++++++++++++++++++++++---------- 3 files changed, 96 insertions(+), 45 deletions(-) (limited to 'lib') diff --git a/lib/imap.c b/lib/imap.c index 2aeda785b..42b76fe1b 100644 --- a/lib/imap.c +++ b/lib/imap.c @@ -343,6 +343,36 @@ static void imap_to_imaps(struct connectdata *conn) #define imap_to_imaps(x) Curl_nop_stmt #endif +/* for the initial server greeting */ +static CURLcode imap_state_servergreet_resp(struct connectdata *conn, + int imapcode, + imapstate instate) +{ + CURLcode result = CURLE_OK; + struct SessionHandle *data = conn->data; + + (void)instate; /* no use for this yet */ + + if(imapcode != 'O') { + failf(data, "Got unexpected imap-server response"); + return CURLE_FTP_WEIRD_SERVER_REPLY; + } + + if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { + /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch + to TLS connection now */ + const char *str; + + str = getcmdid(conn); + result = imapsendf(conn, str, "%s STARTTLS", str); + state(conn, IMAP_STARTTLS); + } + else + result = imap_state_login(conn); + + return result; +} + /* for STARTTLS responses */ static CURLcode imap_state_starttls_resp(struct connectdata *conn, int imapcode, @@ -373,7 +403,9 @@ static CURLcode imap_state_starttls_resp(struct connectdata *conn, } } } + state(conn, IMAP_STOP); + return result; } @@ -400,6 +432,7 @@ static CURLcode imap_state_login_resp(struct connectdata *conn, { CURLcode result = CURLE_OK; struct SessionHandle *data = conn->data; + (void)instate; /* no use for this yet */ if(imapcode != 'O') { @@ -408,6 +441,7 @@ static CURLcode imap_state_login_resp(struct connectdata *conn, } state(conn, IMAP_STOP); + return result; } @@ -422,6 +456,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn, struct FTP *imap = data->state.proto.imap; struct pingpong *pp = &imapc->pp; const char *ptr = data->state.buffer; + (void)instate; /* no use for this yet */ if('*' != imapcode) { @@ -489,6 +524,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn, result = CURLE_FTP_WEIRD_SERVER_REPLY; /* TODO: fix this code */ state(conn, IMAP_STOP); + return result; } @@ -558,7 +594,6 @@ static CURLcode imap_statemach_act(struct connectdata *conn) { CURLcode result; curl_socket_t sock = conn->sock[FIRSTSOCKET]; - struct SessionHandle *data=conn->data; int imapcode; struct imap_conn *imapc = &conn->proto.imapc; struct pingpong *pp = &imapc->pp; @@ -580,24 +615,7 @@ static CURLcode imap_statemach_act(struct connectdata *conn) /* we have now received a full IMAP server response */ switch(imapc->state) { case IMAP_SERVERGREET: - if(imapcode != 'O') { - failf(data, "Got unexpected imap-server response"); - return CURLE_FTP_WEIRD_SERVER_REPLY; - } - - if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { - /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch - to TLS connection now */ - const char *str; - - str = getcmdid(conn); - result = imapsendf(conn, str, "%s STARTTLS", str); - state(conn, IMAP_STARTTLS); - } - else - result = imap_state_login(conn); - if(result) - return result; + result = imap_state_servergreet_resp(conn, imapcode, imapc->state); break; case IMAP_LOGIN: diff --git a/lib/pop3.c b/lib/pop3.c index c95f45e19..6b7e01a60 100644 --- a/lib/pop3.c +++ b/lib/pop3.c @@ -287,6 +287,34 @@ static void pop3_to_pop3s(struct connectdata *conn) #define pop3_to_pop3s(x) Curl_nop_stmt #endif +/* for the initial server greeting */ +static CURLcode pop3_state_servergreet_resp(struct connectdata *conn, + int pop3code, + pop3state instate) +{ + CURLcode result = CURLE_OK; + struct SessionHandle *data = conn->data; + struct pop3_conn *pop3c = &conn->proto.pop3c; + + (void)instate; /* no use for this yet */ + + if(pop3code != 'O') { + failf(data, "Got unexpected pop3-server response"); + return CURLE_FTP_WEIRD_SERVER_REPLY; + } + + if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { + /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch + to TLS connection now */ + result = Curl_pp_sendf(&pop3c->pp, "STLS"); + state(conn, POP3_STARTTLS); + } + else + result = pop3_state_user(conn); + + return result; +} + /* for STARTTLS responses */ static CURLcode pop3_state_starttls_resp(struct connectdata *conn, int pop3code, @@ -294,6 +322,7 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn, { CURLcode result = CURLE_OK; struct SessionHandle *data = conn->data; + (void)instate; /* no use for this yet */ if(pop3code != 'O') { @@ -316,6 +345,7 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn, state(conn, POP3_STOP); } } + return result; } @@ -342,6 +372,7 @@ static CURLcode pop3_state_user_resp(struct connectdata *conn, return result; state(conn, POP3_PASS); + return result; } @@ -360,6 +391,7 @@ static CURLcode pop3_state_pass_resp(struct connectdata *conn, } state(conn, POP3_STOP); + return result; } @@ -518,7 +550,6 @@ static CURLcode pop3_statemach_act(struct connectdata *conn) { CURLcode result; curl_socket_t sock = conn->sock[FIRSTSOCKET]; - struct SessionHandle *data=conn->data; int pop3code; struct pop3_conn *pop3c = &conn->proto.pop3c; struct pingpong *pp = &pop3c->pp; @@ -536,21 +567,7 @@ static CURLcode pop3_statemach_act(struct connectdata *conn) /* we have now received a full POP3 server response */ switch(pop3c->state) { case POP3_SERVERGREET: - if(pop3code != 'O') { - failf(data, "Got unexpected pop3-server response"); - return CURLE_FTP_WEIRD_SERVER_REPLY; - } - - if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { - /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch - to TLS connection now */ - result = Curl_pp_sendf(&pop3c->pp, "STLS"); - state(conn, POP3_STARTTLS); - } - else - result = pop3_state_user(conn); - if(result) - return result; + result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state); break; case POP3_USER: @@ -585,6 +602,7 @@ static CURLcode pop3_statemach_act(struct connectdata *conn) break; } } + return result; } diff --git a/lib/smtp.c b/lib/smtp.c index 83edb42b8..08fe86f24 100644 --- a/lib/smtp.c +++ b/lib/smtp.c @@ -499,6 +499,26 @@ static void smtp_to_smtps(struct connectdata *conn) #define smtp_to_smtps(x) Curl_nop_stmt #endif +/* for the initial server greeting */ +static CURLcode smtp_state_servergreet_resp(struct connectdata *conn, + int smtpcode, + smtpstate instate) +{ + CURLcode result = CURLE_OK; + struct SessionHandle *data = conn->data; + + (void)instate; /* no use for this yet */ + + if(smtpcode/100 != 2) { + failf(data, "Got unexpected smtp-server response: %d", smtpcode); + return CURLE_FTP_WEIRD_SERVER_REPLY; + } + + result = smtp_state_ehlo(conn); + + return result; +} + /* for STARTTLS responses */ static CURLcode smtp_state_starttls_resp(struct connectdata *conn, int smtpcode, @@ -506,6 +526,7 @@ static CURLcode smtp_state_starttls_resp(struct connectdata *conn, { CURLcode result = CURLE_OK; struct SessionHandle *data = conn->data; + (void)instate; /* no use for this yet */ if(smtpcode != 220) { @@ -1066,14 +1087,15 @@ static CURLcode smtp_state_data_resp(struct connectdata *conn, FIRSTSOCKET, smtp->bytecountp); state(conn, SMTP_STOP); + return CURLE_OK; } /* for the POSTDATA response, which is received after the entire DATA part has been sent off to the server */ static CURLcode smtp_state_postdata_resp(struct connectdata *conn, - int smtpcode, - smtpstate instate) + int smtpcode, + smtpstate instate) { CURLcode result = CURLE_OK; @@ -1117,14 +1139,7 @@ static CURLcode smtp_statemach_act(struct connectdata *conn) /* we have now received a full SMTP server response */ switch(smtpc->state) { case SMTP_SERVERGREET: - if(smtpcode/100 != 2) { - failf(data, "Got unexpected smtp-server response: %d", smtpcode); - return CURLE_FTP_WEIRD_SERVER_REPLY; - } - - result = smtp_state_ehlo(conn); - if(result) - return result; + result = smtp_state_servergreet_resp(conn, smtpcode, smtpc->state); break; case SMTP_EHLO: -- cgit v1.2.3