| 1 | 
 /*! \file      smtpsocket.h | 
 
 
 
 
 
 | 2 | 
  *  \brief     header file for TCP/IP socket and SMTP communication functions | 
 
 
 
 
 
 | 3 | 
  *  \author    Brecht Sanders | 
 
 
 
 
 
 | 4 | 
  *  \date      2012-2013 | 
 
 
 
 
 
 | 5 | 
  *  \copyright GPL | 
 
 
 
 
 
 | 6 | 
  */ | 
 
 
 
 
 
 | 7 | 
 /* | 
 
 
 
 
 
 | 8 | 
     This file is part of libquickmail. | 
 
 
 
 
 
 | 9 | 
  | 
 
 
 
 
 
 | 10 | 
     libquickmail is free software: you can redistribute it and/or modify | 
 
 
 
 
 
 | 11 | 
     it under the terms of the GNU General Public License as published by | 
 
 
 
 
 
 | 12 | 
     the Free Software Foundation, either version 3 of the License, or | 
 
 
 
 
 
 | 13 | 
     (at your option) any later version. | 
 
 
 
 
 
 | 14 | 
  | 
 
 
 
 
 
 | 15 | 
     libquickmail is distributed in the hope that it will be useful, | 
 
 
 
 
 
 | 16 | 
     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 
 
 
 
 
 | 17 | 
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 
 
 
 
 
 | 18 | 
     GNU General Public License for more details. | 
 
 
 
 
 
 | 19 | 
  | 
 
 
 
 
 
 | 20 | 
     You should have received a copy of the GNU General Public License | 
 
 
 
 
 
 | 21 | 
     along with libquickmail.  If not, see <http://www.gnu.org/licenses/>. | 
 
 
 
 
 
 | 22 | 
 */ | 
 
 
 
 
 
 | 23 | 
  | 
 
 
 
 
 
 | 24 | 
 #ifndef __INCLUDED_SMTPSOCKET_H | 
 
 
 
 
 
 | 25 | 
 #define __INCLUDED_SMTPSOCKET_H | 
 
 
 
 
 
 | 26 | 
  | 
 
 
 
 
 
 | 27 | 
 #ifdef _WIN32 | 
 
 
 
 
 
 | 28 | 
 #include <winsock2.h> | 
 
 
 
 
 
 | 29 | 
 #else | 
 
 
 
 
 
 | 30 | 
 #include <sys/socket.h> | 
 
 
 
 
 
 | 31 | 
 #include <netinet/in.h> | 
 
 
 
 
 
 | 32 | 
 #include <arpa/inet.h> | 
 
 
 
 
 
 | 33 | 
 #include <netdb.h> | 
 
 
 
 
 
 | 34 | 
 #ifndef SOCKET | 
 
 
 
 
 
 | 35 | 
 #define SOCKET int | 
 
 
 
 
 
 | 36 | 
 #endif | 
 
 
 
 
 
 | 37 | 
 #ifndef INVALID_SOCKET | 
 
 
 
 
 
 | 38 | 
 #define INVALID_SOCKET -1 | 
 
 
 
 
 
 | 39 | 
 #endif | 
 
 
 
 
 
 | 40 | 
 #ifndef SOCKET_ERROR | 
 
 
 
 
 
 | 41 | 
 #define SOCKET_ERROR -1 | 
 
 
 
 
 
 | 42 | 
 #endif | 
 
 
 
 
 
 | 43 | 
 #endif | 
 
 
 
 
 
 | 44 | 
 #include <stdio.h> | 
 
 
 
 
 
 | 45 | 
 #include <stdarg.h> | 
 
 
 
 
 
 | 46 | 
  | 
 
 
 
 
 
 | 47 | 
 #define READ_BUFFER_CHUNK_SIZE 128 | 
 
 
 
 
 
 | 48 | 
 #define WRITE_BUFFER_CHUNK_SIZE 128 | 
 
 
 
 
 
 | 49 | 
  | 
 
 
 
 
 
 | 50 | 
 #ifdef __cplusplus | 
 
 
 
 
 
 | 51 | 
 extern "C" { | 
 
 
 
 
 
 | 52 | 
 #endif | 
 
 
 
 
 
 | 53 | 
  | 
 
 
 
 
 
 | 54 | 
 /*! \brief connect network socket | 
 
 
 
 
 
 | 55 | 
  * \param  smtpserver      hostname or IP address of server | 
 
 
 
 
 
 | 56 | 
  * \param  smtpport        TCP port to connect to | 
 
 
 
 
 
 | 57 | 
  * \param  errmsg          optional pointer to where error message will be stored (must not be freed by caller) | 
 
 
 
 
 
 | 58 | 
  * \return open network socket or INVALID_SOCKET on error | 
 
 
 
 
 
 | 59 | 
 */ | 
 
 
 
 
 
 | 60 | 
 SOCKET socket_open (const char* smtpserver, unsigned int smtpport, char** errmsg); | 
 
 
 
 
 
 | 61 | 
  | 
 
 
 
 
 
 | 62 | 
 /*! \brief disconnect network socket | 
 
 
 
 
 
 | 63 | 
  * \param  sock        open network socket | 
 
 
 
 
 
 | 64 | 
 */ | 
 
 
 
 
 
 | 65 | 
 void socket_close (SOCKET sock); | 
 
 
 
 
 
 | 66 | 
  | 
 
 
 
 
 
 | 67 | 
 /*! \brief send data to a network socket | 
 
 
 
 
 
 | 68 | 
  * \param  sock        open network socket | 
 
 
 
 
 
 | 69 | 
  * \param  buf         buffer containing data | 
 
 
 
 
 
 | 70 | 
  * \param  len         size of buffer in bytes | 
 
 
 
 
 
 | 71 | 
  * \return number of bytes sent | 
 
 
 
 
 
 | 72 | 
 */ | 
 
 
 
 
 
 | 73 | 
 int socket_send (SOCKET sock, const char* buf, int len); | 
 
 
 
 
 
 | 74 | 
  | 
 
 
 
 
 
 | 75 | 
 /*! \brief check if data is waiting to be read from network socket | 
 
 
 
 
 
 | 76 | 
  * \param  sock            open network socket | 
 
 
 
 
 
 | 77 | 
  * \param  timeoutseconds  number of seconds to wait (0 to return immediately) | 
 
 
 
 
 
 | 78 | 
  * \return nonzero if data is waiting | 
 
 
 
 
 
 | 79 | 
 */ | 
 
 
 
 
 
 | 80 | 
 int socket_data_waiting (SOCKET sock, int timeoutseconds); | 
 
 
 
 
 
 | 81 | 
  | 
 
 
 
 
 
 | 82 | 
 /*! \brief read SMTP response from network socket | 
 
 
 
 
 
 | 83 | 
  * \param  sock            open network socket | 
 
 
 
 
 
 | 84 | 
  * \return null-terminated string containing received data (must be freed by caller), or NULL | 
 
 
 
 
 
 | 85 | 
 */ | 
 
 
 
 
 
 | 86 | 
 char* socket_receive_stmp (SOCKET sock); | 
 
 
 
 
 
 | 87 | 
  | 
 
 
 
 
 
 | 88 | 
 /*! \brief read SMTP response from network socket | 
 
 
 
 
 
 | 89 | 
  * \param  sock            open network socket | 
 
 
 
 
 
 | 90 | 
  * \param  errmsg          optional pointer to where error message will be stored (must be freed by caller) | 
 
 
 
 
 
 | 91 | 
  * \return SMTP status code (code >= 400 means error) | 
 
 
 
 
 
 | 92 | 
 */ | 
 
 
 
 
 
 | 93 | 
 int socket_get_smtp_code (SOCKET sock, char** errmsg); | 
 
 
 
 
 
 | 94 | 
  | 
 
 
 
 
 
 | 95 | 
 /*! \brief send SMTP command and return status code | 
 
 
 
 
 
 | 96 | 
  * \param  sock            open network socket | 
 
 
 
 
 
 | 97 | 
  * \param  debuglog        file handle to write debugging information to (NULL for no debugging) | 
 
 
 
 
 
 | 98 | 
  * \param  template        printf style formatting template | 
 
 
 
 
 
 | 99 | 
  * \return SMTP status code (code >= 400 means error) | 
 
 
 
 
 
 | 100 | 
 */ | 
 
 
 
 
 
 | 101 | 
 int socket_smtp_command (SOCKET sock, FILE* debuglog, const char* template, ...); | 
 
 
 
 
 
 | 102 | 
  | 
 
 
 
 
 
 | 103 | 
 #ifdef __cplusplus | 
 
 
 
 
 
 | 104 | 
 } | 
 
 
 
 
 
 | 105 | 
 #endif | 
 
 
 
 
 
 | 106 | 
  | 
 
 
 
 
 
 | 107 | 
 #endif //__INCLUDED_SMTPSOCKET_H |