--- parser3/src/lib/smtp/smtp.C 2004/02/11 15:33:15 1.2 +++ parser3/src/lib/smtp/smtp.C 2020/12/15 17:10:33 1.15 @@ -1,17 +1,22 @@ /** @file Parser: SMTP sender. - Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) Parts of the code here is based upon an early gensock and blat */ -static const char * const IDENT_SMTP_C="$Date: 2004/02/11 15:33:15 $"; - #include "pa_exception.h" #include "smtp.h" +volatile const char * IDENT_SMTP_C="$Id: smtp.C,v 1.15 2020/12/15 17:10:33 moko Exp $" IDENT_SMTP_H; + +// pa_common.C +#undef snprintf +extern int pa_snprintf(char *, size_t, const char* , ...); +#define snprintf pa_snprintf + //#define DEBUG_SHOW SMTP::SMTP() { @@ -22,15 +27,15 @@ SMTP::SMTP() { in_buffer_total = 0; out_buffer_total = 0; - in_buffer = (char *)malloc(SOCKET_BUFFER_SIZE); - out_buffer = (char *)malloc(SOCKET_BUFFER_SIZE); + in_buffer = (char *)pa_malloc(SOCKET_BUFFER_SIZE); + out_buffer = (char *)pa_malloc(SOCKET_BUFFER_SIZE); last_winsock_error = 0; } SMTP::~SMTP() { - free(in_buffer); - free(out_buffer); + pa_free(in_buffer); + pa_free(out_buffer); } @@ -39,6 +44,7 @@ void SMTP:: ConnectToHost(const char* hostname, const char* service) { struct sockaddr_in sa_in; + memset(&sa_in, 0, sizeof(sa_in)); u_short our_port; if( !ResolveService(service, &our_port) ) @@ -90,23 +96,19 @@ GetBuffer(int wait) timeout.tv_sec = 30; } - if( SOCKET_ERROR == (retval = select(0, &fds, NULL, NULL, &timeout)) ) + if( (retval = select(1+the_socket, &fds, NULL, NULL, &timeout))<0 ) { - //char what_error[256]; +#ifdef _MSC_VER int error_code = WSAGetLastError(); if( error_code == WSAEINPROGRESS && wait ) { return WAIT_A_BIT; } - - /* - wsprintf(what_error, - "GetBuffer() unexpected error from select: %d", - error_code); - - ShowError(what_error); - */ +#else + if( errno == EAGAIN && wait ) + return WAIT_A_BIT; +#endif } // if we don't want to wait @@ -128,7 +130,7 @@ GetBuffer(int wait) return WSAENOTCONN; } - if( SOCKET_ERROR == bytes_read ) + if( bytes_read <0 ) { //char what_error[256]; int ws_error = WSAGetLastError(); @@ -226,7 +228,7 @@ SendLine(const char* data, unsigned long while( length > 0 ) { - if( SOCKET_ERROR == select(0, NULL, &fds, NULL, &timeout) ) + if( select(1+the_socket, NULL, &fds, NULL, &timeout)<0 ) throw Exception("smtp.execute", 0, "connection::put_data() unexpected error from select: %d", @@ -237,7 +239,7 @@ SendLine(const char* data, unsigned long length > 1024 ? 1024 : (int)length, 0); - if( SOCKET_ERROR == num_sent ) + if( num_sent<0 ) { int ws_error = WSAGetLastError(); @@ -307,13 +309,13 @@ FlushBuffer() } //--------------------------------------------------------------------------- -BOOL SMTP:: +bool SMTP:: CloseConnect() { - if( closesocket(the_socket) == SOCKET_ERROR ) - return FALSE; + if( closesocket(the_socket) <0 ) + return false; - return TRUE; + return true; } //---------------------------------------------------------------------- @@ -338,9 +340,9 @@ transform_and_send_edit_data(const char* const char *index; char previous_char = 'x'; unsigned int send_len; - BOOL done = 0; + bool done = false; - send_len = lstrlen(editptr); + send_len = strlen(editptr); index = editptr; while( !done ) @@ -372,7 +374,7 @@ transform_and_send_edit_data(const char* } if( (unsigned int)(index - editptr) == send_len ) - done = 1; + done = true; } // this handles the case where the user doesn't end the last @@ -429,14 +431,14 @@ prepare_message(char *from, char *to, co if( 220 != get_line() ) SendSmtpError("SMTP server error"); - wsprintf(out_data, "HELO %s\r\n", my_hostname ); - SendLine(out_data, lstrlen(out_data) ); + snprintf(out_data, sizeof(out_data), "HELO %s\r\n", my_hostname ); + SendLine(out_data, strlen(out_data) ); if( 250 != get_line() ) SendSmtpError("SMTP server error"); - wsprintf(out_data, "MAIL From: <%s>\r\n", from); - SendLine(out_data, lstrlen(out_data) ); + snprintf(out_data, sizeof(out_data), "MAIL From: <%s>\r\n", from); + SendLine(out_data, strlen(out_data) ); if( 250 != get_line() ) SendSmtpError("The mail server doesn't like the sender name, have you set your mail address correctly?"); @@ -446,7 +448,7 @@ prepare_message(char *from, char *to, co { // if there's only one token left, then len will = startLen, // and we'll iterate once only - startLen = lstrlen(ptr); + startLen = strlen(ptr); if( (len = strcspn(ptr, " ,\n\t\r")) != startLen ) { ptr[len] = '\0'; // replace delim with NULL char @@ -454,8 +456,8 @@ prepare_message(char *from, char *to, co ptr[len++] = '\0'; } - wsprintf(out_data, "RCPT To: <%s>\r\n", ptr); - SendLine(out_data, lstrlen(out_data) ); + snprintf(out_data, sizeof(out_data), "RCPT To: <%s>\r\n", ptr); + SendLine(out_data, strlen(out_data) ); if( 250 != get_line() ) throw Exception("smtp.execute", @@ -467,35 +469,14 @@ prepare_message(char *from, char *to, co break; } - wsprintf(out_data, "DATA\r\n"); - SendLine(out_data, lstrlen(out_data)); + snprintf(out_data, sizeof(out_data), "DATA\r\n"); + SendLine(out_data, strlen(out_data)); if( 354 != get_line() ) SendSmtpError("Mail server error accepting message data"); } -static char *lsplit(char *string, char delim) { - if(string) { - char *v=strchr(string, delim); - if(v) { - *v=0; - return v+1; - } - } - return 0; -} -static char *rsplit(char *string, char delim) { - if(string) { - char *v=strrchr(string, delim); - if(v) { - *v=0; - return v+1; - } - } - return NULL; -} - //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- // returns 0 if all is OK