-1

In our database, IP address is stored as Binary(16), and it is a ipv6 On the client side I'm getting it as string, which is a hybrid of octal codes and printable ASCII characters. A byte in the range of printable ASCII characters (the range [0x20, 0x7e]) is represented by the corresponding ASCII character, with the exception of the backslash ('\'), which is escaped as '\'. All other byte values are represented by their corresponding octal values. For example, the bytes {97,92,98,99}, which in ASCII are {a,\,b,c}, are translated to text as 'a\bc'.

" \001\015\270\000\000\000\000\000\010\010\000\014Az\000"

The problem is I would like to show it like a human readable IPv6. I tried some libraries but they require a byte arrays as an input.

I think I can solve my problem by converting the hybrid octal to a byte array and then use https://www.npmjs.com/package/ipaddr.js to convert to IPv6.

The string above translate to byte array in decimal values as: [32, 1, 13, 184, 0, 0, 0, 0, 0, 8, 8, 0, 12, 65, 122, 0] the blank space is 32 ascii, A=65 and z=122

Im working in a function to parse the hybrid octal to byte array. I will share when ready.

pxp
  • 5
  • 2
  • Possible duplicate of [How to convert IPv6 from binary for storage in MySQL](https://stackoverflow.com/questions/1120371/how-to-convert-ipv6-from-binary-for-storage-in-mysql) – DarkBee Nov 03 '17 at 19:43
  • 1
    So... what do you want from the volunteers at Stack Overflow? For us to write code that takes mystery meat data and coverts it to filet mignon? :) You need to tell us exactly what you're getting, what you want the output to be, etc.. Give a read to [ask] for information, especially around [mcve]. – Heretic Monkey Nov 03 '17 at 19:44
  • @MikeMcCaughan I wanted your help to find out what was the format of the string and how to convert to IPv6, but I already find what is the format, thanks – pxp Nov 04 '17 at 21:00
  • Well why do you send the addresses to the client in such a weird format in the first place? What code is formatting them like that? Better just fix that instead of trying to come up with a parser. – Bergi Nov 04 '17 at 21:08

2 Answers2

0

Check if IPv6 decoded first from Binary(16), looks like it store with inet6_pton() function but return without decoding it.

KorbenDallas
  • 944
  • 9
  • 16
  • IPv6 can be stored as binary(16). You can't store it as an integer as you would need 128 bits – DarkBee Nov 03 '17 at 19:44
  • @DarkBee, yes, I forgot about inet6_pton, updated answer. Thanks. – KorbenDallas Nov 03 '17 at 20:51
  • @KorbenDallas I cannot decode the binary at the query level, I have to work with what I'm getting in the client, that is a hybrid octal, thanks – pxp Nov 04 '17 at 21:08
0

A parser solution could be

const input = " \\001\\015\\270\\000\\000\\000\\000\\000\\010\\010\\000\\014Az\\000";
const output = Uint8Array.from(input.match(/\\(\d\d\d)|\\([ -~])|\\(\\)|([ -~])/g), x =>
    x.length == 1 ? x.charCodeAt(0) : x.length == 2 ? 92 : parseInt(x.slice(1), 8)
);
console.log(output); // Uint8Array(16) [32,1,13,184,0,0,0,0,0,8,8,0,12,65,122,0]

but I would really recommend to use a different (easier parseable) format such as a hex string, a base64-encoded string or just an array of numbers.

pxp
  • 5
  • 2
Bergi
  • 630,263
  • 148
  • 957
  • 1,375