00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #define _GNU_SOURCE
00030
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <syslog.h>
00034 #include <errno.h>
00035 #include <pthread.h>
00036 #include <sys/wait.h>
00037 #include <sys/types.h>
00038 #include <sys/unistd.h>
00039
00040 #include <string.h>
00041
00042 #include "safe.h"
00043 #include "debug.h"
00044 #include "conf.h"
00045 #include "client_list.h"
00046
00048 pthread_mutex_t client_list_mutex = PTHREAD_MUTEX_INITIALIZER;
00049
00053 static t_client *firstclient = NULL;
00054
00057 t_client *
00058 client_get_first_client(void)
00059 {
00060 return firstclient;
00061 }
00062
00066 void
00067 client_list_init(void)
00068 {
00069 firstclient = NULL;
00070 }
00071
00079 t_client *
00080 client_list_append(char *ip, char *mac, char *token)
00081 {
00082 t_client *curclient, *prevclient;
00083
00084 prevclient = NULL;
00085 curclient = firstclient;
00086
00087 while (curclient != NULL) {
00088 prevclient = curclient;
00089 curclient = curclient->next;
00090 }
00091
00092 curclient = safe_malloc(sizeof(t_client));
00093 memset(curclient, 0, sizeof(t_client));
00094
00095 curclient->ip = safe_strdup(ip);
00096 curclient->mac = safe_strdup(mac);
00097 curclient->token = safe_strdup(token);
00098 curclient->counters.incoming = curclient->counters.outgoing = 0;
00099 curclient->counters.last_updated = time(NULL);
00100
00101 if (prevclient == NULL) {
00102 firstclient = curclient;
00103 } else {
00104 prevclient->next = curclient;
00105 }
00106
00107 debug(LOG_INFO, "Added a new client to linked list: IP: %s Token: %s",
00108 ip, token);
00109
00110 return curclient;
00111 }
00112
00119 t_client *
00120 client_list_find(char *ip, char *mac)
00121 {
00122 t_client *ptr;
00123
00124 ptr = firstclient;
00125 while (NULL != ptr) {
00126 if (0 == strcmp(ptr->ip, ip) && 0 == strcmp(ptr->mac, mac))
00127 return ptr;
00128 ptr = ptr->next;
00129 }
00130
00131 return NULL;
00132 }
00133
00140 t_client *
00141 client_list_find_by_ip(char *ip)
00142 {
00143 t_client *ptr;
00144
00145 ptr = firstclient;
00146 while (NULL != ptr) {
00147 if (0 == strcmp(ptr->ip, ip))
00148 return ptr;
00149 ptr = ptr->next;
00150 }
00151
00152 return NULL;
00153 }
00154
00161 t_client *
00162 client_list_find_by_mac(char *mac)
00163 {
00164 t_client *ptr;
00165
00166 ptr = firstclient;
00167 while (NULL != ptr) {
00168 if (0 == strcmp(ptr->mac, mac))
00169 return ptr;
00170 ptr = ptr->next;
00171 }
00172
00173 return NULL;
00174 }
00175
00180 t_client *
00181 client_list_find_by_token(char *token)
00182 {
00183 t_client *ptr;
00184
00185 ptr = firstclient;
00186 while (NULL != ptr) {
00187 if (0 == strcmp(ptr->token, token))
00188 return ptr;
00189 ptr = ptr->next;
00190 }
00191
00192 return NULL;
00193 }
00194
00201 void
00202 _client_list_free_node(t_client * client)
00203 {
00204
00205 if (client->mac != NULL)
00206 free(client->mac);
00207
00208 if (client->ip != NULL)
00209 free(client->ip);
00210
00211 if (client->token != NULL)
00212 free(client->token);
00213
00214 free(client);
00215 }
00216
00224 void
00225 client_list_delete(t_client * client)
00226 {
00227 t_client *ptr;
00228
00229 ptr = firstclient;
00230
00231 if (ptr == NULL) {
00232 debug(LOG_ERR, "Node list empty!");
00233 } else if (ptr == client) {
00234 firstclient = ptr->next;
00235 _client_list_free_node(client);
00236 } else {
00237
00238 while (ptr->next != NULL && ptr->next != client) {
00239 ptr = ptr->next;
00240 }
00241
00242 if (ptr->next == NULL) {
00243 debug(LOG_ERR, "Node to delete could not be found.");
00244
00245 } else {
00246 ptr->next = client->next;
00247 _client_list_free_node(client);
00248 }
00249 }
00250 }