wmauda: Fix installation dir
[dockapps.git] / wmnotify-1.0.0 / src / network.c
blobe264205525e34ca3432137938cc382be3dcebd25
1 /*
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 */
22 #define NETWORK_M 1
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #include <arpa/inet.h>
38 #include "common.h"
39 #include "wmnotify.h"
40 #if HAVE_SSL
41 # include "ssl.h"
42 #endif
43 #include "network.h"
46 #define SEND_FLAGS 0
47 #define RECV_FLAGS 0
50 /* Common buffers for IMAP4 and POP3. */
51 char tx_buffer[WMNOTIFY_BUFSIZE + 1];
52 char rx_buffer[WMNOTIFY_BUFSIZE + 1];
55 int
56 SocketOpen( char *server_name, int port )
58 int status;
59 int sock_fd = -1;
60 struct hostent *hostinfo;
61 struct sockaddr_in serv_addr;
63 hostinfo = gethostbyname(server_name);
64 if( hostinfo == NULL ) {
65 herror( PACKAGE );
66 ErrorLocation( __FILE__, __LINE__ );
67 goto error;
70 /* Open socket for Stream (TCP) */
71 sock_fd = socket( PF_INET, SOCK_STREAM, 0 );
72 if( sock_fd < 0 ) {
73 perror( PACKAGE );
74 ErrorLocation( __FILE__, __LINE__ );
75 goto error;
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) );
91 if( status < 0 ) {
92 perror( PACKAGE );
93 ErrorLocation( __FILE__, __LINE__ );
94 goto error;
97 end:
98 return sock_fd;
100 error:
101 if( sock_fd >= 0 ) {
102 status = close( sock_fd );
103 if( status < 0 ) {
104 perror( PACKAGE );
105 ErrorLocation( __FILE__, __LINE__ );
109 sock_fd = -1;
110 goto end;
115 ConnectionEstablish( char *server_name, int port )
117 int len;
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 ) {
122 goto error;
125 #if HAVE_SSL
126 if( wmnotify_infos.use_ssl == true ) {
127 int status;
128 status = InitSSL( wmnotify_infos.sock_fd );
129 if( status != EXIT_SUCCESS ) {
130 goto error;
133 #endif
135 /* Testing connection. */
136 len = WmnotifyGetResponse( rx_buffer, 1024 );
137 if( len < 0 ) {
138 goto error;
141 if( wmnotify_infos.debug ) {
142 rx_buffer[len] = 0;
143 printf(" Connect response:\n%s\n", rx_buffer );
146 return EXIT_SUCCESS;
148 error:
149 return EXIT_FAILURE;
154 ConnectionTerminate( void )
156 #if HAVE_SSL
157 if( wmnotify_infos.use_ssl == true ) {
158 SSL_free( ssl_infos.ssl ); /* release connection state */
160 #endif
162 close( wmnotify_infos.sock_fd ); /* close socket */
164 #if HAVE_SSL
165 if( wmnotify_infos.use_ssl == true ) {
166 SSL_CTX_free( ssl_infos.ctx ); /* release context */
168 #endif
170 return EXIT_SUCCESS;
175 WmnotifySendData( char *buffer, int size )
177 int len;
179 #if HAVE_SSL
180 if( wmnotify_infos.use_ssl == true ) {
181 len = SSL_write( ssl_infos.ssl, buffer, size ); /* Encrypt & send message */
182 if( len <= 0 ) {
183 SSL_get_error( ssl_infos.ssl, len );
184 len = -1;
187 return 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. */
193 do {
194 len = send( wmnotify_infos.sock_fd, buffer, size, SEND_FLAGS );
196 while( ( len < 0 ) && ( errno == EINTR ) );
198 if( len < 0 ) {
199 perror( PACKAGE );
200 ErrorLocation( __FILE__, __LINE__ );
203 return len;
208 WmnotifyGetResponse( char *buffer, int max_size )
210 int len;
212 #if HAVE_SSL
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 ) ) {
216 case SSL_ERROR_NONE:
217 /* Success. */
218 break;
219 case SSL_ERROR_ZERO_RETURN:
220 fprintf( stderr, "%s: SSL_read() connection closed.\n", PACKAGE );
221 break;
222 case SSL_ERROR_SYSCALL:
223 fprintf( stderr, "%s: SSL_read() I/O error.\n", PACKAGE );
224 goto ssl_error;
225 case SSL_ERROR_SSL:
226 fprintf( stderr, "%s: SSL_read() protocol error.\n", PACKAGE );
227 goto ssl_error;
228 default:
229 fprintf( stderr, "%s: SSL_read() error.\n", PACKAGE );
230 goto ssl_error;
233 return len;
235 ssl_error:
236 return -1;
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. */
242 do {
243 len = recv( wmnotify_infos.sock_fd, buffer, max_size, RECV_FLAGS );
245 while( ( len < 0 ) && ( errno == EINTR ) );
247 if( len < 0 ) {
248 perror( PACKAGE );
249 ErrorLocation( __FILE__, __LINE__ );
252 return len;