mirror of
https://github.com/adtools/clib2.git
synced 2025-12-08 14:59:05 +00:00
git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14789 87f5fb63-7c3d-0410-a384-fd976d0f7a62
1011 lines
23 KiB
C
1011 lines
23 KiB
C
/*
|
|
* $Id: socket_select.c,v 1.3 2005-01-02 09:07:08 obarthel Exp $
|
|
*
|
|
* :ts=4
|
|
*
|
|
* Portable ISO 'C' (1994) runtime library for the Amiga computer
|
|
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* - Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* - Neither the name of Olaf Barthel nor the names of contributors
|
|
* may be used to endorse or promote products derived from this
|
|
* software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#if defined(SOCKET_SUPPORT)
|
|
|
|
/****************************************************************************/
|
|
|
|
#ifndef _STDLIB_MEM_DEBUG_H
|
|
#include "stdlib_mem_debug.h"
|
|
#endif /* _STDLIB_MEM_DEBUG_H */
|
|
|
|
/****************************************************************************/
|
|
|
|
#ifndef _SOCKET_HEADERS_H
|
|
#include "socket_headers.h"
|
|
#endif /* _SOCKET_HEADERS_H */
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
copy_fd_set(fd_set * to,fd_set * from,int num_fds)
|
|
{
|
|
ENTER();
|
|
|
|
SHOWPOINTER(to);
|
|
SHOWPOINTER(from);
|
|
SHOWVALUE(num_fds);
|
|
|
|
if(to != NULL && from != NULL && num_fds > 0)
|
|
{
|
|
size_t num_bytes;
|
|
|
|
num_bytes = sizeof(unsigned long) * ((num_fds + 31) / 32);
|
|
|
|
SHOWVALUE(num_bytes);
|
|
|
|
memmove(to,from,num_bytes);
|
|
}
|
|
|
|
LEAVE();
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
zero_fd_set(fd_set * set,int num_fds)
|
|
{
|
|
ENTER();
|
|
|
|
SHOWPOINTER(set);
|
|
SHOWVALUE(num_fds);
|
|
|
|
if(set != NULL && num_fds > 0)
|
|
{
|
|
size_t num_bytes;
|
|
|
|
num_bytes = sizeof(unsigned long) * ((num_fds + 31) / 32);
|
|
|
|
SHOWVALUE(num_bytes);
|
|
|
|
memset(set,0,num_bytes);
|
|
}
|
|
|
|
LEAVE();
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static fd_set *
|
|
allocate_fd_set(int num_fds,fd_set * duplicate_this_set)
|
|
{
|
|
fd_set * result = NULL;
|
|
size_t num_bytes;
|
|
fd_set * set;
|
|
|
|
ENTER();
|
|
|
|
assert( num_fds > 0 );
|
|
|
|
SHOWVALUE(num_fds);
|
|
|
|
num_bytes = sizeof(unsigned long) * ((num_fds + 31) / 32);
|
|
|
|
SHOWVALUE(num_bytes);
|
|
|
|
set = (fd_set *)malloc(num_bytes);
|
|
if(set != NULL)
|
|
{
|
|
if(duplicate_this_set != NULL)
|
|
copy_fd_set(set,duplicate_this_set,num_fds);
|
|
else
|
|
zero_fd_set(set,num_fds);
|
|
|
|
result = set;
|
|
}
|
|
|
|
RETURN(result);
|
|
return(result);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
free_fd_set(fd_set * set)
|
|
{
|
|
if(set != NULL)
|
|
free(set);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static struct fd *
|
|
get_file_descriptor(int file_descriptor)
|
|
{
|
|
struct fd * result = NULL;
|
|
struct fd * fd;
|
|
|
|
if(file_descriptor < 0 || file_descriptor >= __num_fd)
|
|
goto out;
|
|
|
|
assert( __fd != NULL );
|
|
|
|
fd = __fd[file_descriptor];
|
|
|
|
assert( fd != NULL );
|
|
|
|
if(FLAG_IS_CLEAR(fd->fd_Flags,FDF_IN_USE))
|
|
goto out;
|
|
|
|
result = fd;
|
|
|
|
out:
|
|
|
|
return(result);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
fix_datestamp(struct DateStamp * ds)
|
|
{
|
|
const LONG ticks_per_minute = 60 * TICKS_PER_SECOND;
|
|
const LONG minutes_per_day = 24 * 60;
|
|
|
|
assert( ds != NULL );
|
|
|
|
while(ds->ds_Minute >= minutes_per_day ||
|
|
ds->ds_Tick >= ticks_per_minute)
|
|
{
|
|
if(ds->ds_Minute >= minutes_per_day)
|
|
{
|
|
ds->ds_Days++;
|
|
|
|
ds->ds_Minute -= minutes_per_day;
|
|
}
|
|
|
|
if(ds->ds_Tick >= ticks_per_minute)
|
|
{
|
|
ds->ds_Minute++;
|
|
|
|
ds->ds_Tick -= ticks_per_minute;
|
|
}
|
|
}
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static struct DateStamp *
|
|
timeval_to_datestamp(struct DateStamp * ds,const struct timeval * tv)
|
|
{
|
|
assert( ds != NULL && tv != NULL );
|
|
|
|
ds->ds_Days = (tv->tv_secs / (24 * 60 * 60));
|
|
ds->ds_Minute = (tv->tv_secs % (24 * 60 * 60)) / 60;
|
|
ds->ds_Tick = (tv->tv_secs % 60) * TICKS_PER_SECOND + (TICKS_PER_SECOND * tv->tv_micro) / 1000000;
|
|
|
|
fix_datestamp(ds);
|
|
|
|
return(ds);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
add_dates(struct DateStamp * to,const struct DateStamp * from)
|
|
{
|
|
assert( to != NULL && from != NULL );
|
|
|
|
to->ds_Tick += from->ds_Tick;
|
|
to->ds_Minute += from->ds_Minute;
|
|
to->ds_Days += from->ds_Days;
|
|
|
|
fix_datestamp(to);
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
map_descriptor_sets(
|
|
const fd_set * input_fds,
|
|
int num_input_fds,
|
|
|
|
fd_set * socket_fds,
|
|
int num_socket_fds,
|
|
int * total_socket_fd_ptr,
|
|
|
|
fd_set * file_fds,
|
|
int num_file_fds,
|
|
int * total_file_fd_ptr)
|
|
{
|
|
ENTER();
|
|
|
|
SHOWPOINTER(input_fds);
|
|
SHOWVALUE(num_input_fds);
|
|
|
|
SHOWPOINTER(socket_fds);
|
|
SHOWVALUE(num_socket_fds);
|
|
|
|
SHOWPOINTER(file_fds);
|
|
SHOWVALUE(num_file_fds);
|
|
|
|
/* This routine maps file descriptor sets
|
|
* from one format to another. We map
|
|
* socket descriptors and regular file
|
|
* descriptor sets.
|
|
*/
|
|
if(input_fds != NULL && num_input_fds > 0)
|
|
{
|
|
int total_socket_fd;
|
|
int total_file_fd;
|
|
struct fd * fd;
|
|
int file_fd;
|
|
|
|
total_socket_fd = (*total_socket_fd_ptr);
|
|
total_file_fd = (*total_file_fd_ptr);
|
|
|
|
SHOWVALUE(total_socket_fd);
|
|
SHOWVALUE(total_file_fd);
|
|
|
|
for(file_fd = 0 ; file_fd < num_input_fds ; file_fd++)
|
|
{
|
|
if(NOT FD_ISSET(file_fd,input_fds))
|
|
continue;
|
|
|
|
D(("descriptor %ld is set",file_fd));
|
|
|
|
fd = get_file_descriptor(file_fd);
|
|
if(fd == NULL)
|
|
{
|
|
SHOWMSG("but no file is attached to it");
|
|
continue;
|
|
}
|
|
|
|
/* Is this a socket descriptor? */
|
|
if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET))
|
|
{
|
|
int socket_fd = (int)fd->fd_DefaultFile;
|
|
|
|
D(("corresponds to socket #%ld",socket_fd));
|
|
|
|
if(socket_fds != NULL && socket_fd < num_socket_fds)
|
|
{
|
|
SHOWMSG("setting it");
|
|
|
|
FD_SET(socket_fd,socket_fds);
|
|
|
|
if(total_socket_fd < socket_fd+1)
|
|
total_socket_fd = socket_fd+1;
|
|
}
|
|
else
|
|
{
|
|
SHOWMSG("can't set it, though");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* We only watch files bound to console streams. */
|
|
if(FLAG_IS_CLEAR(fd->fd_Flags,FDF_IS_INTERACTIVE))
|
|
{
|
|
SHOWMSG("this is a file");
|
|
continue;
|
|
}
|
|
|
|
SHOWMSG("this is an interactive stream");
|
|
|
|
if(file_fds != NULL && file_fd < num_file_fds)
|
|
{
|
|
SHOWMSG("setting it");
|
|
|
|
FD_SET(file_fd,file_fds);
|
|
|
|
if(total_file_fd < file_fd+1)
|
|
total_file_fd = file_fd+1;
|
|
}
|
|
else
|
|
{
|
|
SHOWMSG("can't set it, though");
|
|
}
|
|
}
|
|
}
|
|
|
|
(*total_socket_fd_ptr) = total_socket_fd;
|
|
(*total_file_fd_ptr) = total_file_fd;
|
|
|
|
SHOWVALUE(total_socket_fd);
|
|
SHOWVALUE(total_file_fd);
|
|
}
|
|
|
|
LEAVE();
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
remap_descriptor_sets(
|
|
const fd_set * socket_fds,
|
|
int num_socket_fds,
|
|
|
|
const fd_set * file_fds,
|
|
int num_file_fds,
|
|
|
|
fd_set * output_fds,
|
|
int num_output_fds)
|
|
{
|
|
ENTER();
|
|
|
|
SHOWPOINTER(socket_fds);
|
|
SHOWVALUE(num_socket_fds);
|
|
|
|
SHOWPOINTER(file_fds);
|
|
SHOWVALUE(num_file_fds);
|
|
|
|
SHOWPOINTER(output_fds);
|
|
SHOWVALUE(num_output_fds);
|
|
|
|
/* This routine reverses the mapping established
|
|
* above. We map the file and socket descriptor
|
|
* sets back into the original set.
|
|
*/
|
|
if(output_fds != NULL && num_output_fds > 0)
|
|
{
|
|
zero_fd_set(output_fds,num_output_fds);
|
|
|
|
if(socket_fds != NULL && num_socket_fds > 0)
|
|
{
|
|
struct fd * fd;
|
|
int output_fd;
|
|
int socket_fd;
|
|
|
|
SHOWMSG("taking care of the sockets");
|
|
|
|
for(socket_fd = 0 ; socket_fd < num_socket_fds ; socket_fd++)
|
|
{
|
|
if(NOT FD_ISSET(socket_fd,socket_fds))
|
|
continue;
|
|
|
|
for(output_fd = 0 ; output_fd < num_output_fds ; output_fd++)
|
|
{
|
|
fd = get_file_descriptor(output_fd);
|
|
if(fd != NULL && FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET) && (int)fd->fd_DefaultFile == socket_fd)
|
|
{
|
|
assert( output_fd < num_output_fds );
|
|
assert( FLAG_IS_SET(__fd[output_fd]->fd_Flags,FDF_IS_SOCKET) );
|
|
|
|
D(("setting file %ld for socket #%ld",output_fd,socket_fd));
|
|
|
|
FD_SET(output_fd,output_fds);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(file_fds != NULL && num_file_fds > 0)
|
|
{
|
|
int file_fd;
|
|
|
|
SHOWMSG("taking care of the files");
|
|
|
|
for(file_fd = 0 ; file_fd < num_file_fds ; file_fd++)
|
|
{
|
|
if(FD_ISSET(file_fd,file_fds))
|
|
{
|
|
int output_fd = file_fd;
|
|
|
|
assert( output_fd < num_output_fds );
|
|
assert( FLAG_IS_CLEAR(__fd[output_fd]->fd_Flags,FDF_IS_SOCKET) );
|
|
|
|
D(("setting file %ld",file_fd));
|
|
|
|
FD_SET(output_fd,output_fds);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SHOWMSG("no output necessary");
|
|
}
|
|
|
|
LEAVE();
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
get_num_descriptors_used(int num_fds,int * num_socket_used_ptr,int * num_file_used_ptr)
|
|
{
|
|
int num_socket_used = 0;
|
|
int num_file_used = 0;
|
|
int which_file_fd;
|
|
struct fd * fd;
|
|
|
|
assert( num_socket_used_ptr != NULL );
|
|
assert( num_file_used_ptr != NULL );
|
|
|
|
SHOWMSG("figuring out which file descriptors are in use");
|
|
|
|
for(which_file_fd = 0 ; which_file_fd < num_fds ; which_file_fd++)
|
|
{
|
|
fd = get_file_descriptor(which_file_fd);
|
|
if(fd != NULL)
|
|
{
|
|
if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_SOCKET))
|
|
{
|
|
int which_socket_fd = (int)fd->fd_DefaultFile;
|
|
|
|
if(num_socket_used < which_socket_fd+1)
|
|
num_socket_used = which_socket_fd+1;
|
|
}
|
|
else
|
|
{
|
|
if(num_file_used < which_file_fd+1)
|
|
num_file_used = which_file_fd+1;
|
|