internal-connections.c 4.98 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * internal-connections.c
 *
 * Abstract routines on the data type Connection.
 */

#include "exp.h"
#include <kom-types.h>
#include "smalloc.h"
#include "com.h"
#include "connections.h"
#include "internal-connections.h"
#include "lyskomd.h"
#include "config.h"
15
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

INTERNAL  Connection  *all_connections   = NULL;

/* Used in get_conn_by_number to speed things up. */

INTERNAL  Connection  *last_conn = NULL;

INTERNAL const Connection EMPTY_CONNECTION =
    ((Connection){0, NULL, NULL, 0, NULL, NO_TIME, 0,
		      EMPTY_STRING_i, 0, EMPTY_STRING_i, EMPTY_STRING_i,
		      NULL, '\0',
		      0, 0, 0, 0, 0, 0, 0,
		      0, 0, 0, 0, EMPTY_STRING_i, EMPTY_STRING_i,
		      EMPTY_STRING_i, NULL, NULL, DEFAULT_PRIV_BITS_i,
		      NULL_CONF_TYPE_i, EMPTY_STRING_i, 0, FALSE, NO_TIME,
		      0, FALSE});

33 34
static int  no_of_allocated_connections = 0;

Per Cederqvist's avatar
Per Cederqvist committed
35 36 37 38 39 40 41
INTERNAL Connection *
alloc_connection(void)
{
    Connection *res;
    
    res = smalloc ( sizeof(Connection) );
    *res = EMPTY_CONNECTION;
42
    ++no_of_allocated_connections;
Per Cederqvist's avatar
Per Cederqvist committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    return res;
}

EXPORT  Connection *
new_client(void)
{
    Connection		*c;
    static Session_no 	 no_of_connection_attempts = 0;

    c = alloc_connection();

    c->magic = CONN_MAGIC_ALLOC;
    
    if (all_connections != NULL)
    {
	all_connections->prev->next = c;
	c->prev = all_connections->prev;
	all_connections->prev = c;
	c->next = all_connections;
    }
    else
    {
	c->prev = c;
	c->next = c;
    }

    all_connections = c;
    
    c->session_no = ++no_of_connection_attempts;
    c->protocol = '\0';		/* Reserved to mean 'no protocol specified yet' */
    c->unparsed = EMPTY_STRING;
    c->first_to_parse = 0;
    c->more_to_parse = TRUE;
    c->last_request = NO_TIME;
    c->pers_no = 0;
    c->person = NULL;
    time( & c->session_start );
    c->cwc = 0;
    c->what_am_i_doing = EMPTY_STRING;
    c->ena_level = 0;
    c->username = EMPTY_STRING;
    c->kill_me = FALSE;
85

Per Cederqvist's avatar
Per Cederqvist committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    return c;
}

/*
 * Only used from logout_client
 */
EXPORT  void
kill_client(Connection *cp)    
{
    if ( all_connections == NULL )
	restart_kom("kill_client(): No clients in all_connections list\n");
    
    if (all_connections == all_connections->next)
    {
	if (all_connections->prev != all_connections)
	    restart_kom("kill_client(): all_connections corrupt (LINK)\n");

	if (all_connections != cp)
	    restart_kom("kill_client(): all_connections corrupt (SINGLE)\n");
      
	all_connections = NULL;
    }
    else
    {
	if (cp->prev == NULL || cp->next == NULL)
	    restart_kom("kill_client(): all_connections corrupt (NULL)\n");
      
	cp->prev->next = cp->next;
	cp->next->prev = cp->prev;
	
	if (all_connections == cp)
	    all_connections = cp->next;
    }

    if ( last_conn == cp )
	last_conn = NULL;

    cp->magic = CONN_MAGIC_FREE;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    s_clear(&cp->unparsed);
    s_clear(&cp->what_am_i_doing);
    s_clear(&cp->username);
    s_clear(&cp->hostname);

    if ( cp->mux != NULL )
    {
	log("kill_client(): client %d has mux != NULL.\n",
	    cp->session_no);
    }

    if ( !s_empty(cp->c_string0) || !s_empty(cp->c_string1)
	|| !s_empty(cp->string0) )
    {
	log("kill_client(): unexpected string remains.\n");
    }

    if ( cp->c_misc_info_p != NULL || cp->c_local_text_no_p != NULL )
	log("kill_client(): unexpected remaining data.\n");

Per Cederqvist's avatar
Per Cederqvist committed
145
    sfree(cp);
146
    --no_of_allocated_connections;
Per Cederqvist's avatar
Per Cederqvist committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

#ifdef DEFENSIVE_CHECKS

INTERNAL  void
check_conn_exists(Connection *foo)
{
    /* Check that foo really is active. */

    Connection *c = all_connections;
    Bool found = FALSE;

    do
    {
	if ( c == foo )
	    found = TRUE;

	c = c->next;
    }
    while ( c != all_connections && found == FALSE );

    if ( found == FALSE )
	restart_kom("get_conn_by_number: foo == %d not found in"
		    " all_connections.\n", last_conn->session_no);
}

#endif

/*
 * Session_nos must NOT be recycled, or this code might break!
 */
EXPORT  Connection *
get_conn_by_number (Session_no session_no)
{
    Connection *end;

    if ( last_conn == NULL )
	last_conn = all_connections;
    else if ( all_connections == NULL )
    {
	restart_kom("get_conn_by_number(%d): last_conn = %d and "
		    "all_connections == NULL", session_no,
		    last_conn->session_no);
    }
#ifdef DEFENSIVE_CHECKS
    else
	check_conn_exists(last_conn);
#endif

    end = last_conn;

    do
    {
	if ( last_conn->session_no == session_no )
	    return last_conn;

	last_conn = last_conn->next;
    }
    while ( last_conn != end);

    return NULL;
}


EXPORT  Session_no
traverse_connections (Session_no session_no)
{
    Connection *prev = NULL;

    if ( all_connections == NULL )
	return 0;

    prev = get_conn_by_number ( session_no );

    /* prev is NULL if session_no is 0, or if session_no was logged out. */

    if ( prev == NULL )
	return all_connections->session_no;
    else if ( prev->next == all_connections )
	return 0;		/* Full circle. */
    else
	return prev->next->session_no;
}
230 231 232 233 234 235 236

void
dump_allocated_connections(FILE *fp)
{
    fprintf(fp, "---" __FILE__ ":\n\tConnections:      %d\n",
	    no_of_allocated_connections);
}