Skip to content

Commit

Permalink
Fix duid uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
xinnige committed Sep 6, 2024
1 parent 89b1933 commit d14d35e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
43 changes: 28 additions & 15 deletions plugins/interface/intf_base_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Plugin = require('../plugin.js');
const pl = require('../plugin_loader.js');
const _ = require('lodash');
const url = require('url');
const { v4: uuidv4 } = require("uuid");

const r = require('../../util/firerouter');

Expand Down Expand Up @@ -382,7 +383,7 @@ class InterfaceBasePlugin extends Plugin {
if (this.networkConfig.dhcp6.duidType) {
await this._genDuid(this.networkConfig.dhcp6.duidType);
} else {
await this._unsetDuid();
await this._resetDuid();
}
// start dhcpcd for SLAAC and stateful DHCPv6 if necessary
await exec(`sudo systemctl restart firerouter_dhcpcd6@${this.name}`).catch((err) => {
Expand Down Expand Up @@ -482,15 +483,24 @@ class InterfaceBasePlugin extends Plugin {
return await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid`, {encoding: 'utf8'}).then(content => content.trim()).catch((err) => null);
}

async _unsetDuid() {
const lastDuid = await this.getLastDuid();
if (!lastDuid) {
return;
async _resetDuid() {
let duidType = 'DUID-UUID';
const arch = await exec("uname -m", {encoding: 'utf8'}).then(result => result.stdout.trim()).catch((err) => {
this.log.error(`Failed to get architecture`, err.message);
return null;
});
if (arch) {
switch (arch) {
case 'x86_64':
break;
case 'aarch64':
duidType = 'DUID-LLT';
break;
default:
break;
}
}
const currentDuid = await this._getDuid();
this.log.info("unset duid", this.name, currentDuid);
await rclient.delAsync(`duid_record`);
await exec(`cat /dev/null | sudo tee ${r.getRuntimeFolder()}/dhcpcd.duid`).catch((err) => {});
return await this._genDuid(duidType);
}

// Generate DHCP Unique Identifier (DUID), see RFC8415
Expand Down Expand Up @@ -523,7 +533,7 @@ class InterfaceBasePlugin extends Plugin {
break;
case 'DUID-UUID':
// 00:04 DUID-Type (DUID-UUID), DUID Based on UUID, see rfc6355
const uuid = await fs.readFileAsync("/var/lib/dbus/machine-id", {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null);
const uuid = await this._genDuidUuid();
if (uuid) {
duid = '00:04:' + this._formatDuid(uuid);
}
Expand All @@ -540,12 +550,15 @@ class InterfaceBasePlugin extends Plugin {
return duid;
}

async getLastDuid(){
const results = await rclient.zrevrangeAsync(`duid_record`, 0, 0);
if (results.length > 0) {
return results[0];
async _genDuidUuid() {
const existUuid = await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null);
if (existUuid) {
return existUuid;
}
return null;

const newUuid = uuidv4();
await fs.writeFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, newUuid).catch((err) => {this.log.warn("fail to persistently save duid uuid", err.message)});
return newUuid;
}

async saveDuidRecord(record){
Expand Down
34 changes: 22 additions & 12 deletions tests/plugins/test_intf_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
let chai = require('chai');
let expect = chai.expect;

const fs = require('fs');
const exec = require('child-process-promise').exec;

const r = require('../../util/firerouter');
let log = require('../../util/logger.js')(__filename, 'info');

let InterfaceBasePlugin = require('../../plugins/interface/intf_base_plugin.js');
Expand All @@ -30,7 +33,7 @@ describe('Test interface base dhcp6', function(){
async() => {
this.plugin = new InterfaceBasePlugin("eth0");
this.plugin.configure({dhcp6:{}});
await this.plugin._unsetDuid();
await exec(`cat /dev/null | sudo tee ${r.getRuntimeFolder()}/dhcpcd.duid`).catch((err) => {});
done();
})()
);
Expand Down Expand Up @@ -63,19 +66,26 @@ describe('Test interface base dhcp6', function(){
expect(duid).to.contains('00:04:');
});

it('should get last duid', async() => {
let duid = await this.plugin._getDuid();
log.debug("current duid", duid);
let lastDuid = await this.plugin.getLastDuid();
log.debug("last duid", lastDuid);
expect(lastDuid).to.be.contains("00:03:");
it('should gen duid uuid', async() => {
const t1 = await this.plugin._genDuidUuid();
const t2 = await this.plugin._genDuidUuid();
const duuuid = await fs.readFileAsync(`${r.getRuntimeFolder()}/dhcpcd.duid_uuid`, {encoding: "utf8"}).then((content) => content.trim()).catch((err) => null);
log.debug("duid uuid generated", duuuid);
expect(t1).to.be.equal(duuuid);
expect(t2).to.be.equal(duuuid);
});

it('should unset duid', async() => {
await this.plugin._unsetDuid();
expect(await this.plugin.getLastDuid()).to.be.equal(null);
let duid = await this.plugin._getDuid();
expect(duid).to.be.empty;
it('should reset duid', async() => {
await this.plugin._resetDuid();
let duidType = await this.plugin._getDuidType(await this.plugin._getDuid());
const arch = await exec("uname -m", {encoding: 'utf8'}).then(result => result.stdout.trim()).catch((err) => {}); switch (arch) {
case 'x86_64':
expect(duidType).to.equal('DUID-UUID');
break;
case 'aarch64':
expect(duidType).to.equal('DUID-LLT');
break;
}
});

it('should get duid type', async() => {
Expand Down

0 comments on commit d14d35e

Please sign in to comment.