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

Diff of /imapfilter/socket.c

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

revision 1.17 by lefcha, Fri Feb 8 22:15:43 2002 UTC revision 1.18 by lefcha, Tue Jun 18 21:21:58 2002 UTC
# Line 20  Line 20 
20    
21  long timeout = -1;              /* Server non-response timeout in seconds. */  long timeout = -1;              /* Server non-response timeout in seconds. */
22    
23  static int sock;  int sockpri = -1;               /* Main socket used mostly. */
24    int sockaux = -1;               /* Auxiliary socked used when another
25                                       connection is needed. */
26  #ifdef SSL_TLS  #ifdef SSL_TLS
27  static SSL *ssl;  static SSL *ssl;
28  #endif  #endif
# Line 30  static SSL *ssl; Line 31  static SSL *ssl;
31  /*  /*
32   * Connect to mail server.   * Connect to mail server.
33   */   */
34  int init_connection(char *serv, unsigned short int port,  int init_connection(int *sock, char *serv, unsigned short int port,
35                       unsigned int protocol)                       unsigned int protocol)
36  {  {
37      struct sockaddr_in sa;      struct sockaddr_in sa;
# Line 45  int init_connection(char *serv, unsigned Line 46  int init_connection(char *serv, unsigned
46    
47      memset((char *) &sa, 0, sizeof(struct sockaddr_in));      memset((char *) &sa, 0, sizeof(struct sockaddr_in));
48    
49      sock = socket(PF_INET, SOCK_STREAM, 0);      *sock = socket(PF_INET, SOCK_STREAM, 0);
50    
51      if (sock < 0) {      if (*sock < 0) {
52          error("imapfilter: create socket; %s\n", strerror(errno));          error("imapfilter: create socket; %s\n", strerror(errno));
53          return ERROR_NETWORK;          return ERROR_NETWORK;
54      }      }
55      if (!(he = gethostbyname(serv))) {      if (!(he = gethostbyname(serv))) {
56          error("imapfilter: get network host entry of %s; %s\n", serv,          error("imapfilter: get network host entry of %s; %s\n", serv,
57                strerror(errno));                strerror(errno));
58          close_connection();          close_connection(sock);
59          return ERROR_NETWORK;          return ERROR_NETWORK;
60      }      }
61      sa.sin_family = AF_INET;      sa.sin_family = AF_INET;
# Line 63  int init_connection(char *serv, unsigned Line 64  int init_connection(char *serv, unsigned
64    
65      xstrncpy(serv, he->h_name, SERVER_LEN - 1);      xstrncpy(serv, he->h_name, SERVER_LEN - 1);
66    
67      if (connect(sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) {      if (connect(*sock, (struct sockaddr *) & sa, sizeof(struct sockaddr))) {
68          error("imapfilter: initiating connection to %s; %s\n", serv,          error("imapfilter: initiating connection to %s; %s\n", serv,
69                strerror(errno));                strerror(errno));
70          close_connection();          close_connection(sock);
71          return ERROR_NETWORK;          return ERROR_NETWORK;
72      }      }
73      log_info(LOG_SERVER, serv);      log_info(LOG_SERVER, serv);
74    
75  #ifdef SSL_TLS  #ifdef SSL_TLS
76      if (protocol != SSL_DISABLED)      if (protocol != SSL_DISABLED)
77          if (!ssl_init(protocol)) {          if (!ssl_init(sock, protocol)) {
78              info("Connected to %s using %s.\n", serv, SSL_get_cipher(ssl));              info("Connected to %s using %s.\n", serv, SSL_get_cipher(ssl));
79              return 0;              return 0;
80          } else          } else
# Line 92  int init_connection(char *serv, unsigned Line 93  int init_connection(char *serv, unsigned
93  /*  /*
94   * Initialize Secure Socket Layer connection.   * Initialize Secure Socket Layer connection.
95   */   */
96  int ssl_init(unsigned int protocol)  int ssl_init(int *sock, unsigned int protocol)
97  {  {
98      int e;      int e;
99      unsigned int i, n = 0;      unsigned int i, n = 0;
# Line 123  int ssl_init(unsigned int protocol) Line 124  int ssl_init(unsigned int protocol)
124      if (!(ssl = SSL_new(ctx)))      if (!(ssl = SSL_new(ctx)))
125          return ERROR_SSL;          return ERROR_SSL;
126    
127      SSL_set_fd(ssl, sock);      SSL_set_fd(ssl, *sock);
128    
129      e = SSL_connect(ssl);      e = SSL_connect(ssl);
130    
# Line 166  int ssl_init(unsigned int protocol) Line 167  int ssl_init(unsigned int protocol)
167  /*  /*
168   * Disconnect from mail server.   * Disconnect from mail server.
169   */   */
170  int close_connection(void)  int close_connection(int *sock)
171  {  {
172  #ifdef SSL_TLS  #ifdef SSL_TLS
173      if (ssl) {      if (ssl) {
# Line 175  int close_connection(void) Line 176  int close_connection(void)
176      }      }
177  #endif  #endif
178    
179      if (sock >= 0 && close(sock)) {      if (*sock >= 0 && close(*sock)) {
180          error("imapfilter: closing socket; %s\n", strerror(errno));          error("imapfilter: closing socket; %s\n", strerror(errno));
181          return ERROR_NETWORK;          return ERROR_NETWORK;
182      } else {      } else {
183          sock = -1;          *sock = -1;
184          return 0;          return 0;
185      }      }
186  }  }
# Line 188  int close_connection(void) Line 189  int close_connection(void)
189  /*  /*
190   * Read data from socket.   * Read data from socket.
191   */   */
192  int socket_read(char *buf)  int socket_read(int *sock, char *buf)
193  {  {
194      int flags, r, s;      int f, r, s;
195      fd_set fds;      fd_set fds;
196      struct timeval tv;      struct timeval tv;
197      struct timeval *tvp = NULL;      struct timeval *tvp = NULL;
# Line 205  int socket_read(char *buf) Line 206  int socket_read(char *buf)
206          tv.tv_usec = 0;          tv.tv_usec = 0;
207          tvp = &tv;          tvp = &tv;
208      }      }
209      flags = fcntl(sock, F_GETFL, 0);      f = fcntl(*sock, F_GETFL, 0);
210      fcntl(sock, F_SETFL, flags | O_NONBLOCK);      fcntl(*sock, F_SETFL, f | O_NONBLOCK);
211    
212      FD_ZERO(&fds);      FD_ZERO(&fds);
213      FD_SET(sock, &fds);      FD_SET(*sock, &fds);
214    
215  #ifdef SSL_TLS  #ifdef SSL_TLS
216      if (ssl) {      if (ssl) {
217          if (SSL_pending(ssl)          if (SSL_pending(ssl)
218              || ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0              || ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0
219                  && FD_ISSET(sock, &fds)))                  && FD_ISSET(*sock, &fds)))
220              r = SSL_read(ssl, buf, RESPONSE_BUF - 1);              r = SSL_read(ssl, buf, RESPONSE_BUF - 1);
221      } else      } else
222  #endif  #endif
223          if ((s = select(sock + 1, &fds, NULL, NULL, tvp)) > 0          if ((s = select(*sock + 1, &fds, NULL, NULL, tvp)) > 0
224              && FD_ISSET(sock, &fds))              && FD_ISSET(*sock, &fds))
225          r = read(sock, buf, RESPONSE_BUF - 1);          r = read(*sock, buf, RESPONSE_BUF - 1);
226    
227      fcntl(sock, F_SETFL, flags);      fcntl(*sock, F_SETFL, f);
228    
229      if (s == -1)      if (s == -1)
230          fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n",          fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n",
# Line 250  int socket_read(char *buf) Line 251  int socket_read(char *buf)
251  /*  /*
252   * Write data to socket.   * Write data to socket.
253   */   */
254  int socket_write(char *data)  int socket_write(int *sock, char *data)
255  {  {
256  #ifdef SSL_TLS  #ifdef SSL_TLS
257      int e;      int e;
# Line 263  int socket_write(char *data) Line 264  int socket_write(char *data)
264                    ERR_error_string(e, NULL));                    ERR_error_string(e, NULL));
265      } else      } else
266  #endif  #endif
267      if (write(sock, data, strlen(data)) == -1)      if (write(*sock, data, strlen(data)) == -1)
268          fatal(ERROR_NETWORK, "imapfilter: sending data; %s",          fatal(ERROR_NETWORK, "imapfilter: sending data; %s",
269                strerror(errno));                strerror(errno));
270    

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26