forked from iriscouch/dnsd
-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.js
247 lines (191 loc) · 6.15 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2012 Iris Couch, all rights reserved.
//
// Server routines
require('defaultable')(module,
{
}, function(module, exports, DEFS, require) {
var net = require('net')
var util = require('util')
var dgram = require('dgram')
var events = require('events')
var Message = require('./message')
var convenient = require('./convenient')
module.exports = createServer
function createServer(handler) {
return new Server(handler)
}
util.inherits(Server, events.EventEmitter)
function Server (handler) {
var self = this
events.EventEmitter.call(self)
self.log = console
self.zones = {}
if(handler)
self.on('request', handler)
self.udp = dgram.createSocket('udp4')
self.tcp = net.createServer()
self.udp.on('close', function() { self.close() })
self.tcp.on('close', function() { self.close() })
self.udp.on('error', function(er) { self.emit('error', er) })
self.tcp.on('error', function(er) { self.emit('error', er) })
self.tcp.on('connection', function(connection) { self.on_tcp_connection(connection) })
self.udp.on('message', function(msg, rinfo) { self.on_udp(msg, rinfo) })
var listening = {'tcp':false, 'udp':false}
self.udp.once('listening', function() {
listening.udp = true
if(listening.tcp)
self.emit('listening')
})
self.tcp.once('listening', function() {
listening.tcp = true
if(listening.udp)
self.emit('listening')
})
}
Server.prototype.zone = function(zone, server, admin, serial, refresh, retry, expire, ttl) {
var self = this
, record = zone
if(typeof record != 'object')
record = { 'class': 'IN'
, 'type' : 'SOA'
, 'name' : zone
, 'data' : { 'mname': server
, 'rname': admin
, 'serial': convenient.serial(serial)
, 'refresh': convenient.seconds(refresh)
, 'retry' : convenient.seconds(retry)
, 'expire' : convenient.seconds(expire)
, 'ttl' : convenient.seconds(ttl || 0)
}
}
self.zones[record.name] = record
return self
}
Server.prototype.listen = function(port, ip, callback) {
var self = this
if(typeof ip === 'function') {
callback = ip
ip = null
}
self.port = port
self.ip = ip || '0.0.0.0'
if(typeof callback === 'function')
self.on('listening', callback)
self.udp.bind(port, ip)
self.tcp.listen(port, ip)
return self
}
Server.prototype.close = function() {
var self = this
if(self.udp._receiving)
self.udp.close()
if(self.tcp._handle)
self.tcp.close(function() {
self.emit('close')
})
}
Server.prototype.unref = function() {
this.udp.unref()
this.tcp.unref()
}
Server.prototype.ref = function() {
this.udp.ref()
this.tcp.ref()
}
Server.prototype.on_tcp_connection = function(connection) {
var self = this
var length = null
, bufs = []
connection.type = 'tcp'
connection.server = self
connection.on('data', function(data) {
bufs.push(data)
var bytes_received = bufs.reduce(function(state, buf) { return state + buf.length }, 0)
if(length === null && bytes_received >= 2) {
var so_far = Buffer.concat(bufs) // Flatten them all together, it's probably not much data.
length = so_far.readUInt16BE(0)
bufs = [ so_far.slice(2) ]
}
if(length !== null && bytes_received == 2 + length) {
// All of the data (plus the 2-byte length prefix) is received.
try {
var data = Buffer.concat(bufs)
, req = new Request(data, connection)
, res = new Response(data, connection)
self.emit('request', req, res)
} catch(err) {
self.emit('error', 'Error processing request', err, connection)
}
}
})
}
Server.prototype.on_udp = function(data, rinfo) {
var self = this
// Provide something that feels like a net.Socket, which in turn feels like the http API.
var connection = { 'type' : self.udp.type
, 'remoteAddress': rinfo.address
, 'remotePort' : rinfo.port
, 'server' : self
, 'send' : function() { self.udp.send.apply(self.udp, arguments) }
, 'destroy' : function() {}
, 'end' : function() {}
}
try {
var req = new Request(data, connection)
, res = new Response(data, connection)
self.emit('request', req, res)
} catch (err) {
self.emit('error', 'Error processing request', err, connection)
}
}
util.inherits(Request, Message)
function Request (data, connection) {
var self = this
Message.call(self, data)
self.connection = connection
}
Request.prototype.toJSON = function() {
var self = this
var obj = {}
Object.keys(self).forEach(function(key) {
if(key != 'connection')
obj[key] = self[key]
})
return obj
}
util.inherits(Response, Message)
function Response (data, connection) {
var self = this
Message.call(self, data)
self.question = self.question || []
self.answer = self.answer || []
self.authority = self.authority || []
self.additional = self.additional || []
self.connection = connection
convenient.init_response(self)
}
Response.prototype.toJSON = Request.prototype.toJSON
Response.prototype.end = function(value) {
var self = this
var msg = convenient.final_response(self, value)
, data = msg.toBinary()
if(self.connection.type == 'udp4' && data.length > 512)
return self.emit('error', 'UDP responses greater than 512 bytes not yet implemented')
else if(self.connection.type == 'udp4')
self.connection.send(data, 0, data.length, self.connection.remotePort, self.connection.remoteAddress, function(er) {
if(er)
self.emit('error', er)
})
else if(self.connection.type == 'tcp') {
// Add the data length prefix.
var length = data.length
data = Buffer.concat([ new Buffer([length >> 8, length & 255]), data ])
self.connection.end(data, function(er) {
if(er)
self.emit('error', er)
})
}
else
self.emit('error', new Error('Unknown connection type: ' + self.connection.type))
}
}) // defaultable