Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of ipv6prefix attribute type #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/radius.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var fs = require("fs");
var util = require("util");
var crypto = require("crypto");
var path = require("path");
var net = require("net");
var v6 = require('ip-address').v6;

var Radius = {};

Expand Down Expand Up @@ -485,6 +487,19 @@ Radius.decode_attributes = function(data, attr_hash, vendor, raw_attrs) {
}
value = octets.join(".");
break;
case "ipv6prefix":
var prefix = value.readUInt8(1);
var octets = [];
for (var i = 1; i < value.length/2; i++) {
octets.push(parseInt(value.readUInt16BE(i*2)).toString(16));
}
value = octets.join(':')+'/'+prefix;
var addr = new v6.Address(octets.join(':'));
if (!addr.isValid()) {
throw new Error("IPv6 prefix is not valid");
}
value = addr.correctForm()+'/'+prefix;
break;
case "date":
value = new Date(value.readUInt32BE(0) * 1000);
break;
Expand Down Expand Up @@ -814,6 +829,26 @@ Radius.encode_attributes = function(packet, attributes, vendor) {
throw new Error("encode: invalid IP: " + in_value);
}
break;
case "ipv6prefix":
// rfc3162
// reserved (1 byte) / prefix-length (1 byte) / prefix (16 bytes)
out_value = new Buffer(18);
out_value.fill(0);
var addr = in_value.split('/');
if (addr.length != 2) {
throw new Error("encode: invalid IPv6 prefix, no /");
}
if (!net.isIPv6(addr[0])) {
throw new Error("encode: invalid IPv6 prefix");
}
var prefix = addr[1].slice(1);
out_value.writeUInt8(64, 1);
addrs = addr[0].split(':');
for (var j = 0; j < addrs.length; j++) {
if (addrs[j] != '')
out_value.writeUInt16BE(parseInt(addrs[j], 16), j*2+2);
}
break;
case "date":
in_value = Math.floor(in_value.getTime() / 1000);
case "time":
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
},
"engines": { "node": ">=0.8.0" },
"devDependencies": {
"nodeunit": "~0.8.6"
"nodeunit": "~0.8.6",
"ip-address": "*"
},
"scripts": {
"test": "nodeunit test"
},
"keywords": ["radius"]
"keywords": ["radius"],
"dependencies": {
"ip-address": "*"
}
}
30 changes: 30 additions & 0 deletions test/radius.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,36 @@ module.exports = testCase({
"User-Password": "降龙十八掌"
}, decoded.attributes );

test.done();
},

test_ipv6prefix_encode: function(test) {
var decoded = radius.decode({
packet: radius.encode({
code: 'Access-Request',
authenticator: new Buffer('426edca213c1bf6e005e90a64105ca3a', 'hex'),
attributes: [['Framed-IPv6-Prefix', '2a05:4680:4::/64']],
secret: secret
}),
secret: secret
});

test.equal( decoded.attributes['Framed-IPv6-Prefix'], '2a05:4680:4::/64' );

// bad ipv6prefix
test.throws(function() {
var decoded = radius.decode({
packet: radius.encode({
code: 'Access-Request',
authenticator: new Buffer('426edca213c1bf6e005e90a64105ca3a', 'hex'),
attributes: [['Framed-IPv6-Prefix', new Buffer('00402')]],
secret: secret
}),
secret: secret
});
});

test.done();
}

});