internal-connections.c 4.21 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
85
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
/*
 * 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"

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});

INTERNAL Connection *
alloc_connection(void)
{
    Connection *res;
    
    res = smalloc ( sizeof(Connection) );
    *res = EMPTY_CONNECTION;
    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;
    
    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;
    sfree(cp);
}

#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;
}