Initial revision
[melted] / src / valerie / valerie_socket.c
1 /*
2 * valerie_socket.c -- Client Socket
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 /* System header files */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <netdb.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <fcntl.h>
34 #include <errno.h>
35
36 /* Application header files */
37 #include "valerie_socket.h"
38
39 /** Initialise the socket.
40 */
41
42 valerie_socket valerie_socket_init( char *server, int port )
43 {
44 valerie_socket socket = malloc( sizeof( valerie_socket_t ) );
45 if ( socket != NULL )
46 {
47 memset( socket, 0, sizeof( valerie_socket_t ) );
48 socket->fd = -1;
49 socket->server = strdup( server );
50 socket->port = port;
51 }
52 return socket;
53 }
54
55 /** Connect to the server.
56 */
57
58 int valerie_socket_connect( valerie_socket connection )
59 {
60 int ret = 0;
61 struct hostent *host;
62 struct sockaddr_in sock;
63
64 if ( connection->server != NULL )
65 {
66 host = gethostbyname( connection->server );
67
68 memset( &sock, 0, sizeof( struct sockaddr_in ) );
69 memcpy( &sock.sin_addr, host->h_addr, host->h_length );
70 sock.sin_family = host->h_addrtype;
71 sock.sin_port = htons( connection->port );
72
73 if ( ( connection->fd = socket( AF_INET, SOCK_STREAM, 0 ) ) != -1 )
74 ret = connect( connection->fd, (const struct sockaddr *)&sock, sizeof( struct sockaddr_in ) );
75 else
76 ret = -1;
77 }
78
79 return ret;
80 }
81
82 /** Convenience constructor for a connected file descriptor.
83 */
84
85 valerie_socket valerie_socket_init_fd( int fd )
86 {
87 valerie_socket socket = malloc( sizeof( valerie_socket_t ) );
88 if ( socket != NULL )
89 {
90 memset( socket, 0, sizeof( valerie_socket_t ) );
91 socket->fd = fd;
92 socket->no_close = 1;
93 }
94 return socket;
95 }
96
97 /** Read an arbitrarily formatted block of data from the server.
98 */
99
100 int valerie_socket_read_data( valerie_socket socket, char *data, int length )
101 {
102 struct timeval tv = { 1, 0 };
103 fd_set rfds;
104 int used = 0;
105
106 data[ 0 ] = '\0';
107
108 FD_ZERO( &rfds );
109 FD_SET( socket->fd, &rfds );
110
111 if ( select( socket->fd + 1, &rfds, NULL, NULL, &tv ) )
112 {
113 used = read( socket->fd, data, length - 1 );
114 if ( used > 0 )
115 data[ used ] = '\0';
116 else
117 used = -1;
118 }
119
120 return used;
121 }
122
123 /** Write an arbitrarily formatted block of data to the server.
124 */
125
126 int valerie_socket_write_data( valerie_socket socket, char *data, int length )
127 {
128 int used = 0;
129
130 while ( used >=0 && used < length )
131 {
132 struct timeval tv = { 1, 0 };
133 fd_set rfds;
134 fd_set wfds;
135 fd_set efds;
136
137 FD_ZERO( &rfds );
138 FD_SET( socket->fd, &rfds );
139 FD_ZERO( &wfds );
140 FD_SET( socket->fd, &wfds );
141 FD_ZERO( &efds );
142 FD_SET( socket->fd, &efds );
143
144 errno = 0;
145
146 if ( select( socket->fd + 1, &rfds, &wfds, &efds, &tv ) )
147 {
148 if ( errno != 0 || FD_ISSET( socket->fd, &efds ) || FD_ISSET( socket->fd, &rfds ) )
149 {
150 used = -1;
151 }
152 else if ( FD_ISSET( socket->fd, &wfds ) )
153 {
154 int inc = write( socket->fd, data + used, length - used );
155 if ( inc > 0 )
156 used += inc;
157 else
158 used = -1;
159 }
160 }
161 }
162
163 return used;
164 }
165
166 /** Close the socket.
167 */
168
169 void valerie_socket_close( valerie_socket socket )
170 {
171 if ( socket->fd > 0 && !socket->no_close )
172 close( socket->fd );
173 free( socket->server );
174 free( socket );
175 }