summaryrefslogtreecommitdiff
path: root/3rdParty/libquickmail/smtpsocket.h
blob: 8aed1e50e8e5136dcc02c9c653a4cc4ec915d6be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*! \file      smtpsocket.h
 *  \brief     header file for TCP/IP socket and SMTP communication functions
 *  \author    Brecht Sanders
 *  \date      2012-2013
 *  \copyright GPL
 */
/*
    This file is part of libquickmail.

    libquickmail is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libquickmail is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libquickmail.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __INCLUDED_SMTPSOCKET_H
#define __INCLUDED_SMTPSOCKET_H

#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifndef SOCKET
#define SOCKET int
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
#endif
#include <stdio.h>
#include <stdarg.h>

#define READ_BUFFER_CHUNK_SIZE 128
#define WRITE_BUFFER_CHUNK_SIZE 128

#ifdef __cplusplus
extern "C" {
#endif

/*! \brief connect network socket
 * \param  smtpserver      hostname or IP address of server
 * \param  smtpport        TCP port to connect to
 * \param  errmsg          optional pointer to where error message will be stored (must not be freed by caller)
 * \return open network socket or INVALID_SOCKET on error
*/
SOCKET socket_open (const char* smtpserver, unsigned int smtpport, char** errmsg);

/*! \brief disconnect network socket
 * \param  sock        open network socket
*/
void socket_close (SOCKET sock);

/*! \brief send data to a network socket
 * \param  sock        open network socket
 * \param  buf         buffer containing data
 * \param  len         size of buffer in bytes
 * \return number of bytes sent
*/
int socket_send (SOCKET sock, const char* buf, int len);

/*! \brief check if data is waiting to be read from network socket
 * \param  sock            open network socket
 * \param  timeoutseconds  number of seconds to wait (0 to return immediately)
 * \return nonzero if data is waiting
*/
int socket_data_waiting (SOCKET sock, int timeoutseconds);

/*! \brief read SMTP response from network socket
 * \param  sock            open network socket
 * \return null-terminated string containing received data (must be freed by caller), or NULL
*/
char* socket_receive_stmp (SOCKET sock);

/*! \brief read SMTP response from network socket
 * \param  sock            open network socket
 * \param  errmsg          optional pointer to where error message will be stored (must be freed by caller)
 * \return SMTP status code (code >= 400 means error)
*/
int socket_get_smtp_code (SOCKET sock, char** errmsg);

/*! \brief send SMTP command and return status code
 * \param  sock            open network socket
 * \param  debuglog        file handle to write debugging information to (NULL for no debugging)
 * \param  template        printf style formatting template
 * \return SMTP status code (code >= 400 means error)
*/
int socket_smtp_command (SOCKET sock, FILE* debuglog, const char* template, ...);

#ifdef __cplusplus
}
#endif

#endif //__INCLUDED_SMTPSOCKET_H