somnisoft / smtp-client

SMTP Client Library in C
Creative Commons Zero v1.0 Universal
109 stars 31 forks source link

connect(...) blocks for quite a while #4

Closed nidoro closed 5 years ago

nidoro commented 5 years ago

I'm calling smtp_open with these parameters:

smtp_open(
    "smtpout.secureserver.net",
    "25",
    SMTP_SECURITY_STARTTLS,
    (smtp_flag) (SMTP_DEBUG | SMTP_NO_CERT_VERIFY),
    0,
    &sender->smtp
);

Eventually it returns successfully but more often than not it takes 2 minutes or so. I did some digging to find out where exactly in the code it gets stuck and it is in connect(smtp->sock, res->ai_addr, res->ai_addrlen) inside smtp_connect.

Based on my experience using the python smtp client library I'm guessing the connection can be faster (even instantaneous), but I'm not a socket guy so I don't know exactly how.

Edit: I don't know if this is related or not, but the blocking happens especially if I just smtp_close'd the client. It also happens if it is the first time opening the connection, but less often. Maybe it is waiting for the last connection to the server to be closed properly? I don't know, just throwing ideas.

nidoro commented 5 years ago

I think I found out what the problem was. 1) The server is actually on port 465. 2) I need SMTP_SECURITY_TLS instead of SMTP_SECURITY_STARTTLS.

But now I have two questions: 1) The README page says SMTP_SECURITY_TLS is deprecated. But SMTP_SECURITY_STARTTLS doesn't work with this server. Do I have other options? 2) How did the connection work with the wrong port in the first place?

somnisoft commented 5 years ago
  1. You could try using port 587 with SMTP_SECURITY_STARTTLS instead of port 25. I removed the deprecated note. You must use the SMTP_SECURITY_TLS option if you connect to a mail server configured with a TLS port as you pointed out. Mail servers with port 465 typically require SMTP_SECURITY_TLS. And mail servers using port 25 and 587 would typically require SMTP_SECURITY_STARTTLS if using encryption, or SMTP_SECURITY_NONE if not using encryption.

  2. I do not have an account on that mail server so I cannot send test emails, but I can open the connection on both ports 25 and 587, issue a STARTTLS command, and then close the connection. The connect() does not block or delay. Maybe this is an issue with your ISP or the mail server blocking/throttling your IP address on port 25.


tss.c:

#include <assert.h>
#include "smtp.h"
int main(void){
  struct smtp *smtp;
  unsigned int i;
  enum smtp_status_code rc;

  for(i = 0; i < 4; i++){
    rc = smtp_open("smtpout.secureserver.net",
                   "587",
                   SMTP_SECURITY_STARTTLS,
                   SMTP_DEBUG,
                   NULL,
                   &smtp);
    assert(rc == SMTP_STATUS_OK);

    rc = smtp_close(smtp);
    assert(rc == SMTP_STATUS_OK);
  }
  return 0;
}
$ cc -c -DSMTP_OPENSSL -o smtp.o smtp.c
$ cc -c -DSMTP_OPENSSL -o tss.o tss.c
$ cc -o tss tss.o smtp.o -lssl -lcrypto
$ time ./tss
[smtp Server]: 220 p3plsmtpa09-02.prod.phx3.secureserver.net :SMTPAUTH: ESMTP
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa09-02.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250-STARTTLS
[smtp Server]: 250 OK
[smtp Client]: STARTTLS  
[smtp Server]: 220 Ready to start TLS
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa09-02.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250 OK
[smtp Client]: QUIT  
[smtp Server]: 220 p3plsmtpa11-03.prod.phx3.secureserver.net :SMTPAUTH: ESMTP
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa11-03.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250-STARTTLS
[smtp Server]: 250 OK
[smtp Client]: STARTTLS  
[smtp Server]: 220 Ready to start TLS
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa11-03.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250 OK
[smtp Client]: QUIT  
[smtp Server]: 220 p3plsmtpa06-06.prod.phx3.secureserver.net :SMTPAUTH: ESMTP
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa06-06.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250-STARTTLS
[smtp Server]: 250 OK
[smtp Client]: STARTTLS  
[smtp Server]: 220 Ready to start TLS
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa06-06.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250 OK
[smtp Client]: QUIT  
[smtp Server]: 220 p3plsmtpa09-05.prod.phx3.secureserver.net :SMTPAUTH: ESMTP
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa09-05.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250-STARTTLS
[smtp Server]: 250 OK
[smtp Client]: STARTTLS  
[smtp Server]: 220 Ready to start TLS
[smtp Client]: EHLO smtp  
[smtp Server]: 250-p3plsmtpa09-05.prod.phx3.secureserver.net hello [XXX.XXX.XXX.XXX], secureserver.net
[smtp Server]: 250-HELP
[smtp Server]: 250-AUTH LOGIN PLAIN
[smtp Server]: 250-SIZE 30000000
[smtp Server]: 250-PIPELINING
[smtp Server]: 250-8BITMIME
[smtp Server]: 250 OK
[smtp Client]: QUIT  

real    0m0.716s
user    0m0.013s
sys     0m0.010s
$ 
nidoro commented 5 years ago

I see. Thank you for taking your time to investigate this, and thanks for the library. It's been a good replacement for the old one I was using.