|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +var util = require('util'); |
| 23 | +var events = require('events'); |
| 24 | + |
| 25 | +var UDP = process.binding('udp_wrap').UDP; |
| 26 | + |
| 27 | +// lazily loaded |
| 28 | +var dns = null; |
| 29 | +var net = null; |
| 30 | + |
| 31 | + |
| 32 | +// no-op callback |
| 33 | +function noop() { |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +function isIP(address) { |
| 38 | + if (!net) |
| 39 | + net = require('net'); |
| 40 | + |
| 41 | + return net.isIP(address); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +function lookup(address, family, callback) { |
| 46 | + // implicit 'bind before send' needs to run on the same tick |
| 47 | + var matchedFamily = isIP(address); |
| 48 | + if (matchedFamily) |
| 49 | + return callback(null, address, matchedFamily); |
| 50 | + |
| 51 | + if (!dns) |
| 52 | + dns = require('dns'); |
| 53 | + |
| 54 | + return dns.lookup(address, family, callback); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +function lookup4(address, callback) { |
| 59 | + return lookup(address || '0.0.0.0', 4, callback); |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +function lookup6(address, callback) { |
| 64 | + return lookup(address || '::0', 6, callback); |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +function newHandle(type) { |
| 69 | + if (type == 'udp4') { |
| 70 | + var handle = new UDP; |
| 71 | + handle.lookup = lookup4; |
| 72 | + return handle; |
| 73 | + } |
| 74 | + |
| 75 | + if (type == 'udp6') { |
| 76 | + var handle = new UDP; |
| 77 | + handle.lookup = lookup6; |
| 78 | + handle.bind = handle.bind6; |
| 79 | + handle.send = handle.send6; |
| 80 | + return handle; |
| 81 | + } |
| 82 | + |
| 83 | + if (type == 'unix_dgram') |
| 84 | + throw new Error('unix_dgram sockets are not supported any more.'); |
| 85 | + |
| 86 | + throw new Error('Bad socket type specified. Valid types are: udp4, udp6'); |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +function Socket(type, listener) { |
| 91 | + events.EventEmitter.call(this); |
| 92 | + |
| 93 | + var handle = newHandle(type); |
| 94 | + handle.socket = this; |
| 95 | + |
| 96 | + this._handle = handle; |
| 97 | + this._receiving = false; |
| 98 | + this._bound = false; |
| 99 | + this.type = type; |
| 100 | + |
| 101 | + if (typeof listener === 'function') |
| 102 | + this.on('message', listener); |
| 103 | +} |
| 104 | +util.inherits(Socket, events.EventEmitter); |
| 105 | +exports.Socket = Socket; |
| 106 | + |
| 107 | + |
| 108 | +exports.createSocket = function(type, listener) { |
| 109 | + return new Socket(type, listener); |
| 110 | +}; |
| 111 | + |
| 112 | + |
| 113 | +Socket.prototype.bind = function(port, address) { |
| 114 | + var self = this; |
| 115 | + |
| 116 | + self._healthCheck(); |
| 117 | + |
| 118 | + // resolve address first |
| 119 | + self._handle.lookup(address, function(err, ip) { |
| 120 | + if (!err) { |
| 121 | + if (self._handle.bind(ip, port || 0, /*flags=*/0)) { |
| 122 | + err = errnoException(errno, 'bind'); |
| 123 | + } |
| 124 | + else { |
| 125 | + self._bound = true; |
| 126 | + self.emit('listening'); |
| 127 | + self._startReceiving(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (err) { |
| 132 | + // caller may not have had a chance yet to register its |
| 133 | + // error event listener so defer the error to the next tick |
| 134 | + process.nextTick(function() { |
| 135 | + self.emit('error', err); |
| 136 | + }); |
| 137 | + } |
| 138 | + }); |
| 139 | +}; |
| 140 | + |
| 141 | + |
| 142 | +// thin wrapper around `send`, here for compatibility with dgram_legacy.js |
| 143 | +Socket.prototype.sendto = function(buffer, |
| 144 | + offset, |
| 145 | + length, |
| 146 | + port, |
| 147 | + address, |
| 148 | + callback) { |
| 149 | + if (typeof offset !== 'number' || typeof length !== 'number') |
| 150 | + throw new Error('send takes offset and length as args 2 and 3'); |
| 151 | + |
| 152 | + if (typeof address !== 'string') |
| 153 | + throw new Error(this.type + ' sockets must send to port, address'); |
| 154 | + |
| 155 | + this.send(buffer, offset, length, port, address, callback); |
| 156 | +}; |
| 157 | + |
| 158 | + |
| 159 | +Socket.prototype.send = function(buffer, |
| 160 | + offset, |
| 161 | + length, |
| 162 | + port, |
| 163 | + address, |
| 164 | + callback) { |
| 165 | + var self = this; |
| 166 | + |
| 167 | + callback = callback || noop; |
| 168 | + |
| 169 | + self._healthCheck(); |
| 170 | + self._startReceiving(); |
| 171 | + |
| 172 | + self._handle.lookup(address, function(err, ip) { |
| 173 | + if (err) { |
| 174 | + if (callback) callback(err); |
| 175 | + self.emit('error', err); |
| 176 | + } |
| 177 | + else { |
| 178 | + var req = self._handle.send(buffer, offset, length, port, ip); |
| 179 | + if (req) { |
| 180 | + req.oncomplete = afterSend; |
| 181 | + req.cb = callback; |
| 182 | + } |
| 183 | + else { |
| 184 | + // don't emit as error, dgram_legacy.js compatibility |
| 185 | + callback(errnoException(errno, 'send')); |
| 186 | + } |
| 187 | + } |
| 188 | + }); |
| 189 | +}; |
| 190 | + |
| 191 | + |
| 192 | +function afterSend(status, handle, req, buffer) { |
| 193 | + var self = handle.socket; |
| 194 | + |
| 195 | + // CHECKME socket's been closed by user, don't call callback? |
| 196 | + if (handle !== self._handle) |
| 197 | + void(0); |
| 198 | + |
| 199 | + if (req.cb) |
| 200 | + req.cb(null, buffer.length); // compatibility with dgram_legacy.js |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +Socket.prototype.close = function() { |
| 205 | + this._healthCheck(); |
| 206 | + this._stopReceiving(); |
| 207 | + this._handle.close(); |
| 208 | + this._handle = null; |
| 209 | + this.emit('close'); |
| 210 | +}; |
| 211 | + |
| 212 | + |
| 213 | +Socket.prototype.address = function() { |
| 214 | + this._healthCheck(); |
| 215 | + |
| 216 | + var address = this._handle.getsockname(); |
| 217 | + if (!address) |
| 218 | + throw errnoException(errno, 'getsockname'); |
| 219 | + |
| 220 | + return address; |
| 221 | +}; |
| 222 | + |
| 223 | + |
| 224 | +Socket.prototype.setBroadcast = function(arg) { |
| 225 | + throw new Error('not yet implemented'); |
| 226 | +}; |
| 227 | + |
| 228 | + |
| 229 | +Socket.prototype.setTTL = function(arg) { |
| 230 | + throw new Error('not yet implemented'); |
| 231 | +}; |
| 232 | + |
| 233 | + |
| 234 | +Socket.prototype.setMulticastTTL = function(arg) { |
| 235 | + throw new Error('not yet implemented'); |
| 236 | +}; |
| 237 | + |
| 238 | + |
| 239 | +Socket.prototype.setMulticastLoopback = function(arg) { |
| 240 | + throw new Error('not yet implemented'); |
| 241 | +}; |
| 242 | + |
| 243 | + |
| 244 | +Socket.prototype.addMembership = function(multicastAddress, |
| 245 | + multicastInterface) { |
| 246 | + // are we ever going to support this in libuv? |
| 247 | + throw new Error('not yet implemented'); |
| 248 | +}; |
| 249 | + |
| 250 | + |
| 251 | +Socket.prototype.dropMembership = function(multicastAddress, |
| 252 | + multicastInterface) { |
| 253 | + // are we ever going to support this in libuv? |
| 254 | + throw new Error('not yet implemented'); |
| 255 | +}; |
| 256 | + |
| 257 | + |
| 258 | +Socket.prototype._healthCheck = function() { |
| 259 | + if (!this._handle) |
| 260 | + throw new Error('Not running'); // error message from dgram_legacy.js |
| 261 | +}; |
| 262 | + |
| 263 | + |
| 264 | +Socket.prototype._startReceiving = function() { |
| 265 | + if (this._receiving) |
| 266 | + return; |
| 267 | + |
| 268 | + if (!this._bound) { |
| 269 | + this.bind(); // bind to random port |
| 270 | + |
| 271 | + // sanity check |
| 272 | + if (!this._bound) |
| 273 | + throw new Error('implicit bind failed'); |
| 274 | + } |
| 275 | + |
| 276 | + this._handle.onmessage = onMessage; |
| 277 | + this._handle.recvStart(); |
| 278 | + this._receiving = true; |
| 279 | + this.fd = -42; // compatibility hack |
| 280 | +}; |
| 281 | + |
| 282 | + |
| 283 | +Socket.prototype._stopReceiving = function() { |
| 284 | + if (!this._receiving) |
| 285 | + return; |
| 286 | + |
| 287 | + this._handle.onmessage = null; |
| 288 | + this._handle.recvStop(); |
| 289 | + this._receiving = false; |
| 290 | +}; |
| 291 | + |
| 292 | + |
| 293 | +function onMessage(handle, nread, buf, rinfo) { |
| 294 | + var self = handle.socket; |
| 295 | + |
| 296 | + if (nread == -1) { |
| 297 | + self.emit('error', errnoException('recvmsg')); |
| 298 | + } |
| 299 | + else { |
| 300 | + rinfo.size = buf.length; // compatibility |
| 301 | + self.emit('message', buf, rinfo); |
| 302 | + } |
| 303 | +} |
| 304 | + |
| 305 | + |
| 306 | +// TODO share with net_uv and others |
| 307 | +function errnoException(errorno, syscall) { |
| 308 | + var e = new Error(syscall + ' ' + errorno); |
| 309 | + e.errno = e.code = errorno; |
| 310 | + e.syscall = syscall; |
| 311 | + return e; |
| 312 | +} |
0 commit comments