#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <protocols/talkd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "extern.h"
#define MAX_ID 16000
#define NIL ((TABLE_ENTRY *)0)
static struct timespec ts;
typedef struct table_entry TABLE_ENTRY;
struct table_entry {
CTL_MSG request;
long time;
TABLE_ENTRY *next;
TABLE_ENTRY *last;
};
static void delete(TABLE_ENTRY *);
static TABLE_ENTRY *table = NIL;
CTL_MSG *
find_match(CTL_MSG *request)
{
TABLE_ENTRY *ptr, *next;
time_t current_time;
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
current_time = ts.tv_sec;
if (debug)
print_request("find_match", request);
for (ptr = table; ptr != NIL; ptr = next) {
next = ptr->next;
if ((ptr->time - current_time) > MAX_LIFE) {
if (debug)
print_request("deleting expired entry",
&ptr->request);
delete(ptr);
continue;
}
if (debug)
print_request("", &ptr->request);
if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
strcmp(request->r_name, ptr->request.l_name) == 0 &&
ptr->request.type == LEAVE_INVITE)
return (&ptr->request);
}
return ((CTL_MSG *)0);
}
CTL_MSG *
find_request(CTL_MSG *request)
{
TABLE_ENTRY *ptr, *next;
time_t current_time;
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
current_time = ts.tv_sec;
if (debug)
print_request("find_request", request);
for (ptr = table; ptr != NIL; ptr = next) {
next = ptr->next;
if ((ptr->time - current_time) > MAX_LIFE) {
if (debug)
print_request("deleting expired entry",
&ptr->request);
delete(ptr);
continue;
}
if (debug)
print_request("", &ptr->request);
if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
strcmp(request->l_name, ptr->request.l_name) == 0 &&
request->type == ptr->request.type &&
request->pid == ptr->request.pid) {
ptr->time = current_time;
return (&ptr->request);
}
}
return ((CTL_MSG *)0);
}
void
insert_table(CTL_MSG *request, CTL_RESPONSE *response)
{
TABLE_ENTRY *ptr;
time_t current_time;
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
current_time = ts.tv_sec;
request->id_num = new_id();
response->id_num = htonl(request->id_num);
ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
if (ptr == NIL) {
syslog(LOG_ERR, "insert_table: Out of memory");
_exit(1);
}
ptr->time = current_time;
ptr->request = *request;
ptr->next = table;
if (ptr->next != NIL)
ptr->next->last = ptr;
ptr->last = NIL;
table = ptr;
}
int
new_id(void)
{
static int current_id = 0;
current_id = (current_id + 1) % MAX_ID;
if (current_id == 0)
current_id = 1;
return (current_id);
}
int
delete_invite(u_int32_t id_num)
{
TABLE_ENTRY *ptr;
if (debug)
syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
for (ptr = table; ptr != NIL; ptr = ptr->next) {
if (ptr->request.id_num == id_num)
break;
if (debug)
print_request("", &ptr->request);
}
if (ptr != NIL) {
delete(ptr);
return (SUCCESS);
}
return (NOT_HERE);
}
static void
delete(TABLE_ENTRY *ptr)
{
if (debug)
print_request("delete", &ptr->request);
if (table == ptr)
table = ptr->next;
else if (ptr->last != NIL)
ptr->last->next = ptr->next;
if (ptr->next != NIL)
ptr->next->last = ptr->last;
free((char *)ptr);
}