This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.h
150 lines (130 loc) · 3.5 KB
/
db.h
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
#pragma once
#include <sqlite3.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include "iplist.h"
#include "dhcp.h"
#ifndef DHCPD_DB_H_
#define DHCPD_DB_H_
static const char DB_SCHEMA[] =
"CREATE TABLE IF NOT EXISTS leases (\n"
" 'id' INTEGER PRIMARY KEY,\n"
" 'address' STRING NOT NULL UNIQUE,\n"
" 'prefixlen' INTEGER,\n"
" 'hwaddr' STRING NOT NULL UNIQUE COLLATE NOCASE,\n"
" 'routers' STRING,\n"
" 'nameservers' STRING,\n"
" 'leasetime' INTEGER,\n"
" 'allocated' BOOLEAN DEFAULT 0,\n"
" 'allocated_at' INTEGER\n"
");\n"
"CREATE INDEX IF NOT EXISTS leases_idx_address ON leases (\n"
" 'address'\n"
");\n"
"CREATE INDEX IF NOT EXISTS leases_idx_hwaddr ON leases (\n"
" 'hwaddr' COLLATE NOCASE\n"
");\n"
"CREATE INDEX IF NOT EXISTS leases_idx_allocated_at ON leases (\n"
" 'allocated_at' ASC\n"
");\n";
#define DB_LEASE_EMPTY {\
.id = 0,\
.address = NULL,\
.prefixlen = 0,\
.hwaddr = NULL,\
.routers = NULL,\
.nameservers = NULL,\
.leasetime = 0,\
.allocated = false,\
.allocated_at = 0\
}
#define DB_COLUMNS "id, address, prefixlen, hwaddr, routers, nameservers,\n"\
"leasetime, allocated, allocated_at"
struct db_lease
{
unsigned int id;
char *address;
uint8_t prefixlen;
char *hwaddr;
char *routers;
char *nameservers;
uint32_t leasetime;
bool allocated;
time_t allocated_at;
};
/**
* Free any with a db_lease struct related memory areas
*
* @param[in] lease Struct which memory shall be freed
*/
static inline void db_lease_free(struct db_lease *lease)
{
if (lease->routers)
free(lease->routers);
if (lease->nameservers)
free(lease->nameservers);
if (lease->hwaddr)
free(lease->hwaddr);
if (lease->address)
free(lease->address);
}
/**
* Delete record defined by a specified lease from database
*
* @param[in] db Database descriptor
* @param[in] lease Struct which defines the lease which shall be deleted
*/
extern int db_lease_delete(sqlite3 *db, struct db_lease *lease);
/**
* Fetch record by a specified hwaddr from database
*
* @param[in] db Database descriptor
* @param[out] lease Struct which shall hold the database record
* @param[in] hwaddr Textual representation of hwaddr
*/
extern int db_lease_by_hwaddr(sqlite3 *db, struct db_lease *lease,
const char *hwaddr);
/**
* Fetch record by a specified address from database
*
* @param[in] db Database descriptor
* @param[out] lease Struct which shall hold the database record
* @param[in] address Textual representation of address
*/
extern int db_lease_by_address(sqlite3 *db, struct db_lease *lease,
const char *address);
/**
* Insert lease record into database
*
* @param[in] db Database descriptor
* @param[out] lease Struct which holds the record
*/
extern int db_insert(sqlite3 *db, struct db_lease *lease);
/**
* Convert binary representation struct dhcp_lease to text representation
* struct db_lease
*
* @param[out] dbl Struct which shall hold the text representation
* @param[in] l Struct which holds the binary representation
*/
extern void db_lease_from_lease(struct db_lease *dbl, struct dhcp_lease *l);
/**
* Put fetched row into struct db_lease from executed SQL statement
*
* @param[in] stmt Statement which was executed
* @param[out] l Strutc which shall hold the row information
*/
extern void db_lease_from_stmt(sqlite3_stmt *stmt, struct db_lease *l);
/**
* Initialize database with schema
*
* @param[in] db Database descriptor
*/
static inline void db_init(sqlite3 *db)
{
sqlite3_exec(db, DB_SCHEMA, NULL, NULL, NULL);
}
#endif