2 * miracle_connection.c -- DV Connection Handler
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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 Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 /* System header files */
30 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
37 #include <valerie/valerie_socket.h>
39 /* Application header files */
40 #include "miracle_commands.h"
41 #include "miracle_connection.h"
42 #include "miracle_server.h"
43 #include "miracle_log.h"
45 /** This is a generic replacement for fgets which operates on a file
46 descriptor. Unlike fgets, we can also specify a line terminator. Maximum
47 of (max - 1) chars can be read into buf from fd. If we reach the
48 end-of-file, *eof_chk is set to 1.
51 int fdgetline( int fd, char *buf, int max, char line_terminator, int *eof_chk )
58 while (count < max - 1) {
59 if (read (fd, tmp, 1) > 0) {
60 if (tmp [0] != line_terminator)
61 buf [count++] = tmp [0];
65 /* Is it an EOF character (ctrl-D, i.e. ascii 4)? If so we definitely want
83 static int connection_initiate( int );
84 static int connection_send( int, valerie_response );
85 static int connection_read( int, char *, int );
86 static void connection_close( int );
88 static int connection_initiate( int fd )
91 valerie_response response = valerie_response_init( );
92 valerie_response_set_error( response, 100, "VTR Ready" );
93 error = connection_send( fd, response );
94 valerie_response_close( response );
98 static int connection_send( int fd, valerie_response response )
102 int code = valerie_response_get_error_code( response );
106 int items = valerie_response_count( response );
109 valerie_response_set_error( response, 500, "Unknown error" );
111 if ( code == 200 && items > 2 )
112 valerie_response_set_error( response, 201, "OK" );
113 else if ( code == 200 && items > 1 )
114 valerie_response_set_error( response, 202, "OK" );
116 code = valerie_response_get_error_code( response );
117 items = valerie_response_count( response );
119 for ( index = 0; !error && index < items; index ++ )
121 char *line = valerie_response_get_line( response, index );
122 int length = strlen( line );
123 if ( length == 0 && index != valerie_response_count( response ) - 1 && write( fd, " ", 1 ) != 1 )
125 else if ( length > 0 && write( fd, line, length ) != length )
127 if ( write( fd, "\r\n", 2 ) != 2 )
131 if ( ( code == 201 || code == 500 ) && strcmp( valerie_response_get_line( response, items - 1 ), "" ) )
132 write( fd, "\r\n", 2 );
136 char *message = "500 Empty Response\r\n\r\n";
137 write( fd, message, strlen( message ) );
143 static int connection_read( int fd, char *command, int length )
146 int nchars = fdgetline( fd, command, length, '\n', &eof_chk );
147 char *cr = strchr( command, '\r');
150 if ( eof_chk || strncmp( command, "BYE", 3 ) == 0 )
155 int connection_status( int fd, valerie_notifier notifier )
159 valerie_status_t status;
161 valerie_socket socket = valerie_socket_init_fd( fd );
163 for ( index = 0; !error && index < MAX_UNITS; index ++ )
165 valerie_notifier_get( notifier, &status, index );
166 valerie_status_serialise( &status, text, sizeof( text ) );
167 error = valerie_socket_write_data( socket, text, strlen( text ) ) != strlen( text );
172 if ( valerie_notifier_wait( notifier, &status ) == 0 )
174 valerie_status_serialise( &status, text, sizeof( text ) );
175 error = valerie_socket_write_data( socket, text, strlen( text ) ) != strlen( text );
179 struct timeval tv = { 0, 0 };
185 if ( select( socket->fd + 1, &rfds, NULL, NULL, &tv ) )
190 valerie_socket_close( socket );
195 static void connection_close( int fd )
200 void *parser_thread( void *arg )
203 connection_t *connection = arg;
205 char command[ 1024 ];
206 int fd = connection->fd;
207 valerie_parser parser = connection->parser;
208 valerie_response response = NULL;
210 /* Get the connecting clients ip information */
211 he = gethostbyaddr( (char *) &( connection->sin.sin_addr.s_addr ), sizeof(u_int32_t), AF_INET);
213 strcpy( address, he->h_name );
215 inet_ntop( AF_INET, &( connection->sin.sin_addr.s_addr), address, 32 );
217 miracle_log( LOG_NOTICE, "Connection established with %s (%d)", address, fd );
219 /* Execute the commands received. */
220 if ( connection_initiate( fd ) == 0 )
224 while( !error && connection_read( fd, command, 1024 ) )
226 if ( strncmp( command, "STATUS", 6 ) )
228 response = valerie_parser_execute( parser, command );
229 miracle_log( LOG_INFO, "%s \"%s\" %d", address, command, valerie_response_get_error_code( response ) );
230 error = connection_send( fd, response );
231 valerie_response_close( response );
235 error = connection_status( fd, valerie_parser_get_notifier( parser ) );
240 /* Free the resources associated with this connection. */
241 connection_close( fd );
243 miracle_log( LOG_NOTICE, "Connection with %s (%d) closed", address, fd );