2 * network.c -- common routines for POP3 and IMAP protocols
4 * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* Define filename_M */
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include <arpa/inet.h>
50 /* Common buffers for IMAP4 and POP3. */
51 char tx_buffer
[WMNOTIFY_BUFSIZE
+ 1];
52 char rx_buffer
[WMNOTIFY_BUFSIZE
+ 1];
56 SocketOpen( char *server_name
, int port
)
60 struct hostent
*hostinfo
;
61 struct sockaddr_in serv_addr
;
63 hostinfo
= gethostbyname(server_name
);
64 if( hostinfo
== NULL
) {
66 ErrorLocation( __FILE__
, __LINE__
);
70 /* Open socket for Stream (TCP) */
71 sock_fd
= socket( PF_INET
, SOCK_STREAM
, 0 );
74 ErrorLocation( __FILE__
, __LINE__
);
78 /*---Initialize server address/port struct---*/
79 serv_addr
.sin_family
= AF_INET
;
80 serv_addr
.sin_port
= htons(port
);
81 serv_addr
.sin_addr
= *((struct in_addr
*) hostinfo
->h_addr
);
82 memset( &( serv_addr
.sin_zero
), '\0', 8 ); /* Clear the rest of the structure. */
84 if( wmnotify_infos
.debug
) {
85 printf( " Server IP = %s\n", inet_ntoa( serv_addr
.sin_addr
) );
86 printf( " Server port = %d\n", ntohs(serv_addr
.sin_port
) );
89 /* Establishing connection. */
90 status
= connect( sock_fd
, (struct sockaddr
*) &(serv_addr
), sizeof(serv_addr
) );
93 ErrorLocation( __FILE__
, __LINE__
);
102 status
= close( sock_fd
);
105 ErrorLocation( __FILE__
, __LINE__
);
115 ConnectionEstablish( char *server_name
, int port
)
118 char rx_buffer
[1024]; /* Temporary... */
120 wmnotify_infos
.sock_fd
= SocketOpen( wmnotify_infos
.server_name
, wmnotify_infos
.port
);
121 if( wmnotify_infos
.sock_fd
< 0 ) {
126 if( wmnotify_infos
.use_ssl
== true ) {
128 status
= InitSSL( wmnotify_infos
.sock_fd
);
129 if( status
!= EXIT_SUCCESS
) {
135 /* Testing connection. */
136 len
= WmnotifyGetResponse( rx_buffer
, 1024 );
141 if( wmnotify_infos
.debug
) {
143 printf(" Connect response:\n%s\n", rx_buffer
);
154 ConnectionTerminate( void )
157 if( wmnotify_infos
.use_ssl
== true ) {
158 SSL_free( ssl_infos
.ssl
); /* release connection state */
162 close( wmnotify_infos
.sock_fd
); /* close socket */
165 if( wmnotify_infos
.use_ssl
== true ) {
166 SSL_CTX_free( ssl_infos
.ctx
); /* release context */
175 WmnotifySendData( char *buffer
, int size
)
180 if( wmnotify_infos
.use_ssl
== true ) {
181 len
= SSL_write( ssl_infos
.ssl
, buffer
, size
); /* Encrypt & send message */
183 SSL_get_error( ssl_infos
.ssl
, len
);
189 #endif /* HAVE_SSL */
191 /* if errno = EINTR, it means the operation was interrupted by a signal before any data was
192 * sent. We must retry the operation in this case. */
194 len
= send( wmnotify_infos
.sock_fd
, buffer
, size
, SEND_FLAGS
);
196 while( ( len
< 0 ) && ( errno
== EINTR
) );
200 ErrorLocation( __FILE__
, __LINE__
);
208 WmnotifyGetResponse( char *buffer
, int max_size
)
213 if( wmnotify_infos
.use_ssl
== true ) {
214 len
= SSL_read( ssl_infos
.ssl
, buffer
, max_size
); /* Get reply & decrypt. */
215 switch( SSL_get_error( ssl_infos
.ssl
, len
) ) {
219 case SSL_ERROR_ZERO_RETURN
:
220 fprintf( stderr
, "%s: SSL_read() connection closed.\n", PACKAGE
);
222 case SSL_ERROR_SYSCALL
:
223 fprintf( stderr
, "%s: SSL_read() I/O error.\n", PACKAGE
);
226 fprintf( stderr
, "%s: SSL_read() protocol error.\n", PACKAGE
);
229 fprintf( stderr
, "%s: SSL_read() error.\n", PACKAGE
);
238 #endif /* HAVE_SSL */
240 /* if errno = EINTR, it means the operation was interrupted by a signal before any data was
241 * read. We must retry the operation in this case. */
243 len
= recv( wmnotify_infos
.sock_fd
, buffer
, max_size
, RECV_FLAGS
);
245 while( ( len
< 0 ) && ( errno
== EINTR
) );
249 ErrorLocation( __FILE__
, __LINE__
);