cURL / Mailing Lists / curl-users / Single Mail

curl-users

RE: curl-smtp-gmail commandline example, please?

From: Sune Ahlgren <sune_ahlgren_at_hotmail.com>
Date: Tue, 12 Oct 2010 21:44:51 +0200

Hi again,

no luck! :-(

I've edited 2 functions in connect.c and socket.c and I've included the traces they generate below.
You will find the 2 functions and 2 commented test cases below. If you need more information, please let me know.

I begin to feel mighty silly.

BRs
/Sune

int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
                      int timeout_ms)
{
#ifdef HAVE_POLL_FINE
  struct pollfd pfd[2];
  int num;
#else
  struct timeval pending_tv;
  struct timeval *ptimeout;
  fd_set fds_read;
  fd_set fds_write;
  fd_set fds_err;
  curl_socket_t maxfd;
#endif
  struct timeval initial_tv = {0,0};
  int pending_ms = 0;
  int error;
  int r;
  int ret;

  printf("f: %s l: %d\n", __FILE__, __LINE__); /* 181 */

  if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 184 */
    r = wait_ms(timeout_ms);
    return r;
  }

  /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
     time in this function does not need to be measured. This happens
     when function is called with a zero timeout or a negative timeout
     value indicating a blocking call should be performed. */

  if(timeout_ms > 0) {
    pending_ms = timeout_ms;
    initial_tv = curlx_tvnow();
  }

#ifdef HAVE_POLL_FINE
  printf("f: %s l: %d\n", __FILE__, __LINE__); /* 200 */

  num = 0;
  if(readfd != CURL_SOCKET_BAD) {
    pfd[num].fd = readfd;
    pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
    pfd[num].revents = 0;
    num++;
  }
  if(writefd != CURL_SOCKET_BAD) {
    pfd[num].fd = writefd;
    pfd[num].events = POLLWRNORM|POLLOUT;
    pfd[num].revents = 0;
    num++;
  }

  do {
    if(timeout_ms < 0)
      pending_ms = -1;
    else if(!timeout_ms)
      pending_ms = 0;
    r = poll(pfd, num, pending_ms);
    if(r != -1) {
      printf("f: %s l: %d err: %s\n", __FILE__, __LINE__, /* 223 */
         strerror(errno));
      break;
    }
    error = SOCKERRNO;
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 227 */
    if(error && error_not_EINTR)
      break;
    if(timeout_ms > 0) {
      pending_ms = timeout_ms - elapsed_ms;
      if(pending_ms <= 0)
        break;
    }
  } while(r == -1);

  printf("f: %s l: %d\n", __FILE__, __LINE__); /* 237 */

  if(r < 0) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 240 */
    return -1;
  }
  if(r == 0) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 244 */
    return 0;
  }
  ret = 0;
  num = 0;
  if(readfd != CURL_SOCKET_BAD) {
    if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
      ret |= CURL_CSELECT_IN;
    if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
      ret |= CURL_CSELECT_ERR;
    num++;
  }

  if(writefd != CURL_SOCKET_BAD) {
    if(pfd[num].revents & (POLLWRNORM|POLLOUT))
      ret |= CURL_CSELECT_OUT;
    if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
      ret |= CURL_CSELECT_ERR;
  }

  printf("f: %s l: %d\n", __FILE__, __LINE__);
  return ret;

#else /* HAVE_POLL_FINE */

  FD_ZERO(&fds_err);
  maxfd = (curl_socket_t)-1;

  FD_ZERO(&fds_read);
  if(readfd != CURL_SOCKET_BAD) {
    VERIFY_SOCK(readfd);
    FD_SET(readfd, &fds_read);
    FD_SET(readfd, &fds_err);
    maxfd = readfd;
  }

  FD_ZERO(&fds_write);
  if(writefd != CURL_SOCKET_BAD) {
    VERIFY_SOCK(writefd);
    FD_SET(writefd, &fds_write);
    FD_SET(writefd, &fds_err);
    if(writefd > maxfd)
      maxfd = writefd;
  }

  ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;

  do {
    if(timeout_ms > 0) {
      pending_tv.tv_sec = pending_ms / 1000;
      pending_tv.tv_usec = (pending_ms % 1000) * 1000;
    }
    else if(!timeout_ms) {
      pending_tv.tv_sec = 0;
      pending_tv.tv_usec = 0;
    }
    r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
    if(r != -1)
      break;
    error = SOCKERRNO;
    if(error && error_not_EINTR)
      break;
    if(timeout_ms > 0) {
      pending_ms = timeout_ms - elapsed_ms;
      if(pending_ms <= 0)
        break;
    }
  } while(r == -1);

  if(r < 0)
    return -1;
  if(r == 0)
    return 0;

  ret = 0;
  if(readfd != CURL_SOCKET_BAD) {
    if(FD_ISSET(readfd, &fds_read))
      ret |= CURL_CSELECT_IN;
    if(FD_ISSET(readfd, &fds_err))
      ret |= CURL_CSELECT_ERR;
  }
  if(writefd != CURL_SOCKET_BAD) {
    if(FD_ISSET(writefd, &fds_write))
      ret |= CURL_CSELECT_OUT;
    if(FD_ISSET(writefd, &fds_err))
      ret |= CURL_CSELECT_ERR;
  }

  return ret;

#endif /* HAVE_POLL_FINE */

}

static
int waitconnect(struct connectdata *conn,
                curl_socket_t sockfd, /* socket */
                long timeout_msec)
{
  int rc;
#ifdef mpeix
  /* Call this function once now, and ignore the results. We do this to
     "clear" the error state on the socket so that we can later read it
     reliably. This is reported necessary on the MPE/iX operating system. */
  (void)verifyconnect(sockfd, NULL);
#endif
      printf("f: %s l: %d\n", __FILE__, __LINE__); /* 204 */

  for(;;) {

    /* now select() until we get connect or timeout */
    rc = Curl_socket_ready(CURL_SOCKET_BAD, sockfd, (int)(timeout_msec>1000?
                                                          1000:timeout_msec));
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 211 */
      if(Curl_pgrsUpdate(conn)) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 213 */
    return WAITCONN_ABORTED;
      }

      if(-1 == rc) {
      /* error, no connect here, try next */
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 219 */
    return WAITCONN_SELECT_ERROR;

      } else if(0 == rc) {
      /* timeout */
      printf("f: %s l: %d\n", __FILE__, __LINE__); /* 224 */
      timeout_msec -= 1000;
      if(timeout_msec <= 0) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 227 */
        return WAITCONN_TIMEOUT;
      }
      printf("f: %s l: %d\n", __FILE__, __LINE__); /* 230 */
      continue;
    }

      if(rc & CURL_CSELECT_ERR) {
    printf("f: %s l: %d\n", __FILE__, __LINE__); /* 235 */
    /* error condition caught */
    return WAITCONN_FDSET_ERROR;
      }
    break;
  }
  printf("f: %s l: %d\n", __FILE__, __LINE__); /* 241 */
  return WAITCONN_CONNECTED;
}

1)
No --ssl and no port number
==============================
f: url.c l: 3204
* About to connect() to smtp.gmail.com port 25 (#0)
f: connect.c l: 1048
f: connect.c l: 827
f: connect.c l: 866
f: connect.c l: 895
f: connect.c l: 903
f: connect.c l: 909
* Trying 74.125.77.109... f: connect.c l: 915
f: connect.c l: 934
f: connect.c l: 946
f: connect.c l: 950
f: connect.c l: 971
f: connect.c l: 204
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
f: connect.c l: 211
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0f: connect.c l: 224
f: connect.c l: 230
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
f: connect.c l: 211
  0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0f: connect.c l: 224
f: connect.c l: 230
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
f: connect.c l: 211
  0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0

With --ssl and port number 587
================================
f: connect.c l: 971
f: connect.c l: 204
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 265
f: connect.c l: 211
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0f: connect.c l: 241
f: connect.c l: 987
f: connect.c l: 1087
f: connect.c l: 1120
f: url.c l: 3221
f: url.c l: 3166
f: url.c l: 3322
f: url.c l: 3242
f: smtp.c l: 1072
f: url.c l: 5427
f: smtp.c l: 1034
f: smtp.c l: 1015
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 265
f: smtp.c l: 911
f: smtp.c l: 225
f: smtp.c l: 312
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 265
f: smtp.c l: 911
f: smtp.c l: 225
f: smtp.c l: 225
f: smtp.c l: 225
f: smtp.c l: 225
f: smtp.c l: 225
f: smtp.c l: 493
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 265
f: smtp.c l: 911
f: smtp.c l: 225
f: smtp.c l: 466
f: url.c l: 2563
f: smtp.c l: 1341
f: smtp.c l: 1319
f: smtp.c l: 1015
f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
  0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
  0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0f: select.c l: 181
f: select.c l: 200
f: select.c l: 223 err: Operation now in progress
f: select.c l: 238
f: select.c l: 245
  0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0f: select.c l: 181
f: select.c l: 200

> Date: Mon, 11 Oct 2010 22:58:57 +0200
> From: daniel_at_haxx.se
> To: curl-users_at_cool.haxx.se
> Subject: RE: curl-smtp-gmail commandline example, please?
>
> On Mon, 11 Oct 2010, Sune Ahlgren wrote:
>
> > Ok, the thing is that Bredbandsbolaget, my ISP blocks port 25 (which is
> > quite common) in an effort to minimize spam originating from their network.
> > Could you please try the other two gmail ports and test if they work? (465,
> > 587)
>
> Funnily, I am a Bredbandsbolaget customer myself but I have no such port
> block... But yes, I just redid the exact same thing with the URL
> smtp://smtp.gmail.com:587 and I got the mail through just fine.
>
> So, if that doesn't work for you I suggest you check the output closely and
> possibly the source and ponder why things are like they are.
>
> --
>
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-users
> FAQ: http://curl.haxx.se/docs/faq.html
> Etiquette: http://curl.haxx.se/mail/etiquette.html
                                               

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-10-12