-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.c
executable file
·176 lines (161 loc) · 3.83 KB
/
database.c
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
/***
*
*/
#include "database.h"
/******************************************************************************/
struct database * db;
/******************************************************************************/
/**
* Initialize database.
* @return 0 if everything is ok, -1 otherwise.
*/
int init_database()
{
db = (struct database *) malloc(sizeof(struct database));
if (db == NULL) {
fprintf(stderr, "(database) fail to allocate memory for the database\n");
return -1;
}
db->n_nodes = 0;
db->first = NULL;
return 0;
}
/******************************************************************************/
/**
* Insert a new record in the database.
* @param id: identifier of the record.
* @param port: client port.
* @param ip: client ip.
* @return 0 if everything is ok, -1 otherwise.
*/
int insert_record(char * id, unsigned short port, unsigned long ip)
{
struct node * new_node;
struct node * aux_node;
new_node = (struct node *) malloc(sizeof(struct node));
if (new_node == NULL) {
fprintf(stderr, "(database) fail to allocate memory for the new node\n");
return -1;
}
new_node->id = strdup(id);
new_node->port = port;
new_node->ip = ip;
/* Database is empty */
if (db->n_nodes == 0){
db->first = new_node;
db->first->next = NULL;
db->n_nodes++;
}
/* Insert in the first place */
else {
/* check if is already in database */
aux_node = db->first;
while (aux_node) {
if (strcmp(aux_node->id, id) == 0){
free(new_node->id);
free(new_node);
return -1;
}
aux_node = aux_node->next;
}
new_node->next = db->first;
db->first = new_node;
db->n_nodes++;
}
return 0;
}
/******************************************************************************/
/**
* Consult a record in the database.
* @param id: identifier of the record, take value after the call.
* @param port: client port, take value after the call.
* @param ip: client ip.
* @return 0 if everything is ok, -1 otherwise.
*/
int consult_record(char * id, unsigned short * port, unsigned long * ip)
{
struct node * aux_node;
aux_node = db->first;
while(aux_node) {
if (strcmp(aux_node->id, id) == 0) {
*port = aux_node->port;
*ip = aux_node->ip;
return 0;
}
aux_node = aux_node->next;
}
return -1;
}
/******************************************************************************/
/**
* Delete a record of the database.
* @param id: identifier of the record.
* @return 0 if everything is ok, -1 otherwise.
*/
int delete_record(char * id)
{
struct node * aux_node;
struct node * bef_node;
aux_node = bef_node = db->first;
while(aux_node) {
if (strcmp(aux_node->id, id) == 0) {
if (aux_node == db->first) {
db->first = db->first->next;
db->n_nodes--;
free(aux_node->id);
free(aux_node);
return 0;
}
else {
bef_node->next = aux_node->next;
db->n_nodes--;
free(aux_node->id);
free(aux_node);
return 0;
}
}
bef_node = aux_node;
aux_node = aux_node->next;
}
return -1;
}
/******************************************************************************/
/**
* Print the database.
*/
void print_database()
{
int i;
struct node * aux_node;
struct in_addr dir;
aux_node = db->first;
printf("Number of nodes: %d\n", db->n_nodes);
while (aux_node) {
printf("\tid: %s\n", aux_node->id);
printf("\tport: %d\n", aux_node->port);
dir.s_addr = aux_node->ip;
printf("\tip: %s\n\n", inet_ntoa(dir));
aux_node = aux_node->next;
}
}
/******************************************************************************/
/**
* Delete the database, free the memory.
*/
void delete_database()
{
struct node * to_delete;
struct node * aux_node;
int i;
aux_node = db->first;
i = 0;
while (aux_node) {
i++;
to_delete = aux_node;
aux_node = aux_node->next;
free(to_delete->id);
free(to_delete);
}
free(db);
printf("(database) deleted %d records\n", i);
}