Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 18, 2016
0 parents commit e2643b0
Show file tree
Hide file tree
Showing 8 changed files with 1,108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Mathias Buus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
170 changes: 170 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# dns-packet

An [abstract-encoding](https://github.com/mafintosh/abstract-encoding) compliant module for encoding / decoding DNS packets.
Lifted out of [multicast-dns](https://github.com/mafintosh/multicast-dns) as a separate module.

```
npm install dns-packet
```

## Usage

``` js
var packet = require('dns-packet')
var dgram = require('dgram')

var socket = dgram.createSocket('udp4')

var buf = packet.encode({
type: 'query',
id: 1,
questions: [{
type: 'A',
name: 'google.com'
}]
})

socket.on('message', function (message) {
console.log(packet.decode(message)) // prints out a response from google dns
})

socket.send(buf, 0, buf.length, 53, '8.8.8.8')
```

## API

#### `var buf = packets.encode(packet, [buf], [offset])`

Encodes a DNS packet into a buffer.

#### `var packet = packets.decode(buf, [offset])`

Decode a DNS packet from a buffer

#### `var len = packets.encodingLength(packet)`

Returns how many bytes are needed to encode the DNS packet

## Packets

Packets look like this

``` js
{
type: 'query|response',
id: optionalIdNumber,
questions: [...],
answers: [...],
additionals: [...],
authorities: [...]
}
```

A question looks like this

``` js
{
type: 'A', // or SRV, AAAA, etc
name: 'google.com' // which record are you looking for
}
```

And an answers, additional, or authority looks like this

``` js
{
type: 'A', // or SRV, AAAA, etc
name: 'google.com', // which name is this record for
ttl: optionalTimeToLiveInSeconds,
(record specific data, see below)
}
```

Currently the different available records are

#### `A`

``` js
{
data: 'IPv4 address' // fx 127.0.0.1
}
```

#### `AAAA`

``` js
{
data: 'IPv6 address' // fx fe80::1
}
```

#### `TXT`

``` js
{
data: Buffer('some text')
}
```

#### `NULL`

``` js
{
data: Buffer('any binary data')
}
```

#### `SRV`

``` js
{
data: {
port: servicePort,
target: serviceHostName,
priority: optionalServicePriority,
weight: optionalServiceWeight
}
}
```

#### `HINFO`

``` js
{
data: {
cpu: 'cpu info',
os: 'os info'
}
}
```

#### `PTR`

``` js
{
data: 'points.to.another.record'
}
```

#### `CNAME`

``` js
{
data: 'cname.to.another.record'
}
```

#### `DNAME`

``` js
{
data: 'dname.to.another.record'
}
```

If you need another one, open an issue and we'll try to add it.

## License

MIT

20 changes: 20 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var packet = require('./')
var dgram = require('dgram')

var socket = dgram.createSocket('udp4')

var buf = packet.encode({
type: 'query',
id: 1,
questions: [{
type: 'A',
name: 'google.com'
}]
})

socket.on('message', function (message, rinfo) {
console.log(rinfo)
console.log(packet.decode(message)) // prints out a response from google dns
})

socket.send(buf, 0, buf.length, 53, '8.8.8.8')
Loading

0 comments on commit e2643b0

Please sign in to comment.