Commit | Line | Data |
---|---|---|
d0aeaf5a DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
963ce42e | 7 | #ifndef lint |
d0aeaf5a DF |
8 | static char sccsid[] = "@(#)table.c 5.1 (Berkeley) %G%"; |
9 | #endif not lint | |
963ce42e MK |
10 | |
11 | /* | |
12 | * Routines to handle insertion, deletion, etc on the table | |
13 | * of requests kept by the daemon. Nothing fancy here, linear | |
14 | * search on a double-linked list. A time is kept with each | |
15 | * entry so that overly old invitations can be eliminated. | |
16 | * | |
17 | * Consider this a mis-guided attempt at modularity | |
1d0f8542 | 18 | */ |
963ce42e MK |
19 | #include <stdio.h> |
20 | #include <sys/time.h> | |
1d0f8542 MK |
21 | |
22 | #include "ctl.h" | |
1d0f8542 | 23 | |
963ce42e | 24 | #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ |
1d0f8542 | 25 | |
963ce42e | 26 | #define NIL ((TABLE_ENTRY *)0) |
1d0f8542 | 27 | |
963ce42e MK |
28 | extern int debug; |
29 | struct timeval tp; | |
30 | struct timezone *txp; | |
1d0f8542 MK |
31 | |
32 | typedef struct table_entry TABLE_ENTRY; | |
33 | ||
34 | struct table_entry { | |
963ce42e MK |
35 | CTL_MSG request; |
36 | long time; | |
37 | TABLE_ENTRY *next; | |
38 | TABLE_ENTRY *last; | |
1d0f8542 MK |
39 | }; |
40 | ||
963ce42e | 41 | TABLE_ENTRY *table = NIL; |
1d0f8542 MK |
42 | CTL_MSG *find_request(); |
43 | CTL_MSG *find_match(); | |
963ce42e | 44 | char *malloc(); |
1d0f8542 | 45 | |
963ce42e MK |
46 | /* |
47 | * Look in the table for an invitation that matches the current | |
48 | * request looking for an invitation | |
49 | */ | |
50 | CTL_MSG * | |
51 | find_match(request) | |
52 | CTL_MSG *request; | |
1d0f8542 | 53 | { |
963ce42e | 54 | TABLE_ENTRY *ptr; |
81379f80 | 55 | extern FILE *debugout; |
963ce42e MK |
56 | long current_time; |
57 | ||
58 | gettimeofday(&tp, &txp); | |
59 | current_time = tp.tv_sec; | |
60 | if (debug) { | |
81379f80 | 61 | fprintf(debugout, "Entering Look-Up with : \n"); |
963ce42e | 62 | print_request(request); |
1d0f8542 | 63 | } |
963ce42e MK |
64 | for (ptr = table; ptr != NIL; ptr = ptr->next) { |
65 | if ((ptr->time - current_time) > MAX_LIFE) { | |
66 | /* the entry is too old */ | |
81379f80 KL |
67 | if (debug) { |
68 | fprintf(debugout | |
69 | ,"Deleting expired entry : \n"); | |
963ce42e | 70 | print_request(&ptr->request); |
81379f80 | 71 | } |
963ce42e MK |
72 | delete(ptr); |
73 | continue; | |
74 | } | |
75 | if (debug) | |
76 | print_request(&ptr->request); | |
77 | if (strcmp(request->l_name, ptr->request.r_name) == 0 && | |
78 | strcmp(request->r_name, ptr->request.l_name) == 0 && | |
79 | ptr->request.type == LEAVE_INVITE) | |
80 | return (&ptr->request); | |
1d0f8542 | 81 | } |
963ce42e | 82 | return ((CTL_MSG *)0); |
1d0f8542 MK |
83 | } |
84 | ||
963ce42e MK |
85 | /* |
86 | * Look for an identical request, as opposed to a complimentary | |
87 | * one as find_match does | |
88 | */ | |
89 | CTL_MSG * | |
90 | find_request(request) | |
91 | CTL_MSG *request; | |
1d0f8542 | 92 | { |
963ce42e | 93 | TABLE_ENTRY *ptr; |
81379f80 | 94 | extern FILE *debugout; |
963ce42e MK |
95 | long current_time; |
96 | ||
97 | gettimeofday(&tp, &txp); | |
98 | current_time = tp.tv_sec; | |
99 | /* | |
100 | * See if this is a repeated message, and check for | |
101 | * out of date entries in the table while we are it. | |
1d0f8542 | 102 | */ |
963ce42e | 103 | if (debug) { |
81379f80 | 104 | fprintf(debugout, "Entering find_request with : \n"); |
963ce42e | 105 | print_request(request); |
1d0f8542 | 106 | } |
963ce42e MK |
107 | for (ptr = table; ptr != NIL; ptr = ptr->next) { |
108 | if ((ptr->time - current_time) > MAX_LIFE) { | |
109 | /* the entry is too old */ | |
110 | if (debug) { | |
81379f80 KL |
111 | fprintf(debugout |
112 | , "Deleting expired entry : \n"); | |
963ce42e MK |
113 | print_request(&ptr->request); |
114 | } | |
115 | delete(ptr); | |
116 | continue; | |
117 | } | |
118 | if (debug) | |
119 | print_request(&ptr->request); | |
120 | if (strcmp(request->r_name, ptr->request.r_name) == 0 && | |
121 | strcmp(request->l_name, ptr->request.l_name) == 0 && | |
122 | request->type == ptr->request.type && | |
123 | request->pid == ptr->request.pid) { | |
124 | /* update the time if we 'touch' it */ | |
125 | ptr->time = current_time; | |
126 | return (&ptr->request); | |
127 | } | |
1d0f8542 | 128 | } |
963ce42e | 129 | return ((CTL_MSG *)0); |
1d0f8542 MK |
130 | } |
131 | ||
132 | insert_table(request, response) | |
963ce42e MK |
133 | CTL_MSG *request; |
134 | CTL_RESPONSE *response; | |
1d0f8542 | 135 | { |
963ce42e MK |
136 | TABLE_ENTRY *ptr; |
137 | long current_time; | |
1d0f8542 | 138 | |
963ce42e MK |
139 | gettimeofday(&tp, &txp); |
140 | current_time = tp.tv_sec; | |
141 | response->id_num = request->id_num = new_id(); | |
1d0f8542 | 142 | /* insert a new entry into the top of the list */ |
963ce42e MK |
143 | ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY)); |
144 | if (ptr == NIL) { | |
145 | fprintf(stderr, "malloc in insert_table"); | |
146 | exit(1); | |
147 | } | |
148 | ptr->time = current_time; | |
149 | ptr->request = *request; | |
150 | ptr->next = table; | |
151 | if (ptr->next != NIL) | |
152 | ptr->next->last = ptr; | |
153 | ptr->last = NIL; | |
154 | table = ptr; | |
1d0f8542 MK |
155 | } |
156 | ||
963ce42e MK |
157 | /* |
158 | * Generate a unique non-zero sequence number | |
159 | */ | |
1d0f8542 MK |
160 | new_id() |
161 | { | |
963ce42e | 162 | static int current_id = 0; |
1d0f8542 | 163 | |
963ce42e | 164 | current_id = (current_id + 1) % MAX_ID; |
1d0f8542 | 165 | /* 0 is reserved, helps to pick up bugs */ |
963ce42e MK |
166 | if (current_id == 0) |
167 | current_id = 1; | |
168 | return (current_id); | |
1d0f8542 MK |
169 | } |
170 | ||
963ce42e MK |
171 | /* |
172 | * Delete the invitation with id 'id_num' | |
173 | */ | |
1d0f8542 | 174 | delete_invite(id_num) |
963ce42e | 175 | int id_num; |
1d0f8542 | 176 | { |
963ce42e | 177 | TABLE_ENTRY *ptr; |
81379f80 | 178 | extern FILE *debugout; |
1d0f8542 | 179 | |
963ce42e | 180 | ptr = table; |
1d0f8542 | 181 | |
963ce42e | 182 | if (debug) |
81379f80 | 183 | fprintf(debugout,"Entering delete_invite with %d\n", id_num); |
963ce42e MK |
184 | for (ptr = table; ptr != NIL; ptr = ptr->next) { |
185 | if (ptr->request.id_num == id_num) | |
186 | break; | |
187 | if (debug) | |
188 | print_request(&ptr->request); | |
189 | } | |
190 | if (ptr != NIL) { | |
191 | delete(ptr); | |
192 | return (SUCCESS); | |
193 | } | |
194 | return (NOT_HERE); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Classic delete from a double-linked list | |
199 | */ | |
1d0f8542 | 200 | delete(ptr) |
963ce42e | 201 | TABLE_ENTRY *ptr; |
1d0f8542 | 202 | { |
81379f80 | 203 | extern FILE *debugout; |
1d0f8542 | 204 | |
963ce42e | 205 | if (debug) { |
81379f80 | 206 | fprintf(debugout, "Deleting : "); |
963ce42e MK |
207 | print_request(&ptr->request); |
208 | } | |
209 | if (table == ptr) | |
210 | table = ptr->next; | |
211 | else if (ptr->last != NIL) | |
212 | ptr->last->next = ptr->next; | |
213 | if (ptr->next != NIL) | |
214 | ptr->next->last = ptr->last; | |
215 | free((char *)ptr); | |
1d0f8542 | 216 | } |