/[imapfilter]/imapfilter/socket.c
ViewVC logotype

Diff of /imapfilter/socket.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12 by lefcha, Sat Nov 10 19:39:22 2001 UTC revision 1.12.2.1 by lefcha, Wed Dec 5 09:56:52 2001 UTC
# Line 15  Line 15 
15  #include <openssl/ssl.h>  #include <openssl/ssl.h>
16  #include <openssl/err.h>  #include <openssl/err.h>
17  #include <openssl/x509.h>  #include <openssl/x509.h>
18    #endif
19    
20    
21  static SSL *ssl;  extern long timeout;
 #endif  
22    
23  static int sock;  static int sock;
24    
25    #ifdef SSL_TLS
26    static SSL *ssl;
27    #endif
28    
29    
30  /*  /*
31   * Connect to mail server.   * Connect to mail server.
# Line 29  static int sock; Line 33  static int sock;
33  #ifndef SSL_TLS  #ifndef SSL_TLS
34  int init_connection(char *serv, unsigned short int port)  int init_connection(char *serv, unsigned short int port)
35  #else  #else
36  int init_connection(char *serv, unsigned short int port, unsigned int protocol)  int init_connection(char *serv, unsigned short int port,
37                         unsigned int protocol)
38  #endif  #endif
39  {  {
40      struct sockaddr_in sa;      struct sockaddr_in sa;
# Line 43  int init_connection(char *serv, unsigned Line 48  int init_connection(char *serv, unsigned
48          error("imapfilter: create socket; %s\n", strerror(errno));          error("imapfilter: create socket; %s\n", strerror(errno));
49          return ERROR_NETWORK;          return ERROR_NETWORK;
50      }      }
   
51      if (!(he = gethostbyname(serv))) {      if (!(he = gethostbyname(serv))) {
52          error("imapfilter: get network host entry of %s; %s\n", serv,          error("imapfilter: get network host entry of %s; %s\n", serv,
53                strerror(errno));                strerror(errno));
# Line 57  int init_connection(char *serv, unsigned Line 61  int init_connection(char *serv, unsigned
61      xstrncpy(serv, he->h_name, SERVER_LEN - 1);      xstrncpy(serv, he->h_name, SERVER_LEN - 1);
62    
63      if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) {      if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) {
64          error("imapfilter: initiating connection to %s; %s\n", serv, strerror(errno));          error("imapfilter: initiating connection to %s; %s\n", serv,
65                  strerror(errno));
66          close_connection();          close_connection();
67          return ERROR_NETWORK;          return ERROR_NETWORK;
68      }      }
# Line 119  int ssl_init(unsigned int protocol) Line 124  int ssl_init(unsigned int protocol)
124    
125      e = SSL_connect(ssl);      e = SSL_connect(ssl);
126    
127      if (e <= 0) {      if (e < 0) {
128          error("imapfilter: initiating SSL connection; %s",          error("imapfilter: initiating SSL connection; %s",
129                ERR_error_string(e, NULL));                ERR_error_string(e, NULL));
130          return ERROR_SSL;          return ERROR_SSL;
# Line 183  int socket_read(char *buf) Line 188  int socket_read(char *buf)
188      int flags, r, s;      int flags, r, s;
189      fd_set fds;      fd_set fds;
190      struct timeval tv;      struct timeval tv;
191        struct timeval *tvp = NULL;
192    
193      r = 0;      r = 0;
194      s = 1;      s = 1;
195    
196      memset(buf, 0, RESPONSE_BUF);      memset(buf, 0, RESPONSE_BUF);
197    
198      tv.tv_sec = 60;      if (timeout >= 0) {
199      tv.tv_usec = 0;          tv.tv_sec = timeout;
200            tv.tv_usec = 0;
201            tvp = &tv;
202        }
203      flags = fcntl(sock, F_GETFL, 0);      flags = fcntl(sock, F_GETFL, 0);
204      fcntl(sock, F_SETFL, flags | O_NONBLOCK);      fcntl(sock, F_SETFL, flags | O_NONBLOCK);
205    
# Line 201  int socket_read(char *buf) Line 209  int socket_read(char *buf)
209  #ifdef SSL_TLS  #ifdef SSL_TLS
210      if (ssl) {      if (ssl) {
211          if (SSL_pending(ssl)          if (SSL_pending(ssl)
212              || ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0              || ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0
213                  && FD_ISSET(sock, &fds)))                  && FD_ISSET(sock, &fds)))
214              r = SSL_read(ssl, buf, RESPONSE_BUF - 1);              r = SSL_read(ssl, buf, RESPONSE_BUF - 1);
215      } else      } else
216  #endif  #endif
217          if ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0          if ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0
218              && FD_ISSET(sock, &fds))              && FD_ISSET(sock, &fds))
219          r = read(sock, buf, RESPONSE_BUF - 1);          r = read(sock, buf, RESPONSE_BUF - 1);
220    

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.12.2.1

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26