From 4fb5c2e10add325aa0167f20a3dee45d777eac16 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Wed, 16 Oct 2019 19:26:20 +0000 Subject: [server/registration] Automatic registration now sets the hostname (dhcp) as the name for the client --- server/api/registration.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'server/api') diff --git a/server/api/registration.js b/server/api/registration.js index 190bfeb..548e82d 100644 --- a/server/api/registration.js +++ b/server/api/registration.js @@ -146,15 +146,26 @@ noAuthRouter.postAsync('/clients', async (req, res) => { if (dhcp) { if (automatic) { // Set the name of the client if it's not set - if (!client.name) client.name = client.type + '_' + client.uuid - const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.ip, network.mac, undefined, true) - dhcp.ref = setIp.ref - // Check for errors. - if (!setIp.error) { - // Client ip set successfully - client.networks[0].ip = setIp.ip - } else { - log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}` }) + if (!client.name) { + // Check the ip state in the dhcp + const ipCheck = await dhcp.instance.checkIp(dhcp.backend.credentials, network.ip) + + // If it's not leased, set the hostname as clientname + if (!ipCheck.leased && ipCheck.name !== '') { + if (ipCheck.name) client.name = ipCheck.name + if (ipCheck.id) dhcp.ref = ipCheck.id + } else { + client.name = client.type + '_' + client.uuid + const setIp = await dhcp.instance.setIp(dhcp.backend.credentials, network.ip, network.mac, undefined, true) + dhcp.ref = setIp.ref + // Check for errors. + if (!setIp.error) { + // Client ip set successfully + client.networks[0].ip = setIp.ip + } else { + log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}` }) + } + } } } else if (network.dhcp) { // If networks.dhcp is set the user already choose the ip and we have to set it now. @@ -194,6 +205,10 @@ noAuthRouter.postAsync('/clients', async (req, res) => { if (ipCheck.id) dhcp.ref = ipCheck.id } } + } else { + if (automatic) { + client.name = client.type + '_' + client.uuid + } } } -- cgit v1.2.3-55-g7522