This module provide many funcions for leasing IP and getting IP from pools.
What this fucntion works ?
- pick up pool
- get first non-reserved ip and return
- if ip requested check on leases and :
- if exists send ip
- if does not exists search to leases and if this ip doesn't reserved, send ip
First of all we should get the first no reserved ip address with this query :
SELECT id, conf_id, ip FROM pool WHERE lease_flag = 0 LIMIT 1;
We need id for updating on ack, conf_id for sending typical options and ip for sending on replay packets.
Another fields like mac, host and lease_flag sets to zero or NULL.
- update lease flag to true
- fill mac address field
- fill hostname field
After getting request and when sending ack, we should update non reserved ip address on previous step.
We have an id and required parameters like mac, host and lease_flag :
UPDATE pool SET mac = "some-mac-address", host = "some-hostname", lease_flag = 1 WHERE id = 1;
CREATE TABLE config (
id INTEGER PRIMARY KEY AUTOINCREMENT,
mask TEXT NOT NULL,
router TEXT NOT NULL,
domain TEXT NOT NULL,
lease_time INTEGER NOT NULL DEFAULT 600
);
config table
---------------------------------------------------------
| NAME | TYPE |
---------------------------------------------------------
| id | INTEGER PRIMARY KEY AUTOINCREMENT |
---------------------------------------------------------
| mask | TEXT NOT NULL |
---------------------------------------------------------
| router | TEXT NOT NULL |
---------------------------------------------------------
| domain | TEXT NOT NULL UNIQUE |
---------------------------------------------------------
| lease_time | INTEGER NOT NULL DEFAULT 0 |
---------------------------------------------------------
CREATE TABLE pool (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conf_id INTEGER NOT NULL,
ip TEXT NOT NULL,
host TEXT NOT NULL,
mac TEXT NOT NULL,
lease_time INTEGER NOT NULL DEFAULT 600,
FOREIGN KEY (conf_id) REFERENCES config(id)
);
pool table
---------------------------------------------------------
| NAME | TYPE |
---------------------------------------------------------
| id | INTEGER PRIMARY KEY AUTOINCREMENT |
---------------------------------------------------------
| config_id | INTEGER NOT NULL |
---------------------------------------------------------
| hostname | TEXT NOT NULL |
---------------------------------------------------------
| mac | TEXT NOT NULL UNIQUE |
---------------------------------------------------------
| lease_flag | INTEGER NOT NULL DEFAULT 0 |
---------------------------------------------------------
| FOREIGN KEY | (conf_id) REFERENCES config (id) |
---------------------------------------------------------
pip3 install -r tests/scripts/requirements.txt
mkdir build
cd build
cmake ..
./tests/lease_test