nsIUDPSocket.idl (15073B)
1 /* vim:set ts=4 sw=4 et cindent: */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "nsISupports.idl" 7 8 interface nsINetAddr; 9 interface nsIUDPSocketListener; 10 interface nsIUDPSocketSyncListener; 11 interface nsIUDPMessage; 12 interface nsISocketTransport; 13 interface nsIOutputStream; 14 interface nsIInputStream; 15 interface nsIPrincipal; 16 17 %{ C++ 18 #include "nsTArrayForwardDeclare.h" 19 namespace mozilla { 20 namespace net { 21 union NetAddr; 22 } 23 } 24 %} 25 native NetAddr(mozilla::net::NetAddr); 26 [ptr] native NetAddrPtr(mozilla::net::NetAddr); 27 [ref] native Uint8TArrayRef(FallibleTArray<uint8_t>); 28 29 /** 30 * nsIUDPSocket 31 * 32 * An interface to a UDP socket that can accept incoming connections. 33 */ 34 [scriptable, builtinclass, uuid(d423bf4e-4499-40cf-bc03-153e2bf206d1)] 35 interface nsIUDPSocket : nsISupports 36 { 37 /** 38 * init 39 * 40 * This method initializes a UDP socket. 41 * 42 * @param aPort 43 * The port of the UDP socket. Pass -1 to indicate no preference, 44 * and a port will be selected automatically. 45 * @param aLoopbackOnly 46 * If true, the UDP socket will only respond to connections on the 47 * local loopback interface. Otherwise, it will accept connections 48 * from any interface. To specify a particular network interface, 49 * use initWithAddress. 50 * @param aPrincipal 51 * The principal connected to this socket. 52 * @param aAddressReuse 53 * If true, the socket is allowed to be bound to an address that is 54 * already in use. Default is true. 55 */ 56 [optional_argc] void init(in long aPort, 57 in boolean aLoopbackOnly, 58 in nsIPrincipal aPrincipal, 59 [optional] in boolean aAddressReuse); 60 61 [optional_argc] void init2(in AUTF8String aAddr, 62 in long aPort, 63 in nsIPrincipal aPrincipal, 64 [optional] in boolean aAddressReuse); 65 66 /** 67 * initWithAddress 68 * 69 * This method initializes a UDP socket, and binds it to a particular 70 * local address (and hence a particular local network interface). 71 * 72 * @param aAddr 73 * The address to which this UDP socket should be bound. 74 * @param aPrincipal 75 * The principal connected to this socket. 76 * @param aAddressReuse 77 * If true, the socket is allowed to be bound to an address that is 78 * already in use. Default is true. 79 */ 80 [noscript, optional_argc] void initWithAddress([const] in NetAddrPtr aAddr, 81 in nsIPrincipal aPrincipal, 82 [optional] in boolean aAddressReuse); 83 84 /** 85 * close 86 * 87 * This method closes a UDP socket. This does not affect already 88 * connected client sockets (i.e., the nsISocketTransport instances 89 * created from this UDP socket). This will cause the onStopListening 90 * event to asynchronously fire with a status of NS_BINDING_ABORTED. 91 */ 92 void close(); 93 94 /** 95 * asyncListen 96 * 97 * This method puts the UDP socket in the listening state. It will 98 * asynchronously listen for and accept client connections. The listener 99 * will be notified once for each client connection that is accepted. The 100 * listener's onSocketAccepted method will be called on the same thread 101 * that called asyncListen (the calling thread must have a nsIEventTarget). 102 * 103 * The listener will be passed a reference to an already connected socket 104 * transport (nsISocketTransport). See below for more details. 105 * 106 * @param aListener 107 * The listener to be notified when client connections are accepted. 108 */ 109 void asyncListen(in nsIUDPSocketListener aListener); 110 111 /** 112 * This adds a nsIUDPSocketSyncListener listener (defined below). 113 * When data is available onPacketReceived is called and the listener uses 114 * recvWithAddr to actually retrieve data from the socket. 115 * The listener can be use only if it runs on the socket thread. 116 * If it is used off the socket thread there is a risk of triggering a bug 117 * in OS thatcan cause a crash. 118 */ 119 [noscript] void syncListen(in nsIUDPSocketSyncListener aListener); 120 121 /** 122 * connect 123 * 124 * This method connects the UDP socket to a remote UDP address. 125 * 126 * @param aRemoteAddr 127 * The remote address to connect to 128 */ 129 [noscript] void connect([const] in NetAddrPtr aAddr); 130 131 /** 132 * Returns the local address of this UDP socket 133 */ 134 readonly attribute nsINetAddr localAddr; 135 136 /** 137 * Returns the port of this UDP socket. 138 */ 139 readonly attribute long port; 140 141 /** 142 * Returns the address to which this UDP socket is bound. Since a 143 * UDP socket may be bound to multiple network devices, this address 144 * may not necessarily be specific to a single network device. In the 145 * case of an IP socket, the IP address field would be zerod out to 146 * indicate a UDP socket bound to all network devices. Therefore, 147 * this method cannot be used to determine the IP address of the local 148 * system. See nsIDNSService::myHostName if this is what you need. 149 */ 150 [noscript] NetAddr getAddress(); 151 152 /** 153 * send 154 * 155 * Send out the datagram to specified remote host and port. 156 * DNS lookup will be triggered. 157 * 158 * @param host The remote host name. 159 * @param port The remote port. 160 * @param data The buffer containing the data to be written. 161 * @return number of bytes written. (0 or length of data) 162 */ 163 unsigned long send(in AUTF8String host, in unsigned short port, 164 in Array<uint8_t> data); 165 166 /** 167 * sendWithAddr 168 * 169 * Send out the datagram to specified remote host and port. 170 * 171 * @param addr The remote host address. 172 * @param data The buffer containing the data to be written. 173 * @return number of bytes written. (0 or length of data) 174 */ 175 unsigned long sendWithAddr(in nsINetAddr addr, 176 in Array<uint8_t> data); 177 178 179 /** 180 * Receive a datagram. 181 * @param addr The remote host address. 182 * @param data The buffer to store received datagram. 183 */ 184 [noscript] void recvWithAddr(out NetAddr addr, 185 out Array<uint8_t> data); 186 187 /** 188 * sendWithAddress 189 * 190 * Send out the datagram to specified remote address and port. 191 * 192 * @param addr The remote host address. 193 * @param data The buffer containing the data to be written. 194 * @return number of bytes written. (0 or length of data) 195 */ 196 [noscript] unsigned long sendWithAddress([const] in NetAddrPtr addr, 197 [array, size_is(length), const] in uint8_t data, 198 in unsigned long length); 199 200 /** 201 * sendBinaryStream 202 * 203 * Send out the datagram to specified remote address and port. 204 * 205 * @param host The remote host name. 206 * @param port The remote port. 207 * @param stream The input stream to be sent. This must be a buffered stream implementation. 208 */ 209 void sendBinaryStream(in AUTF8String host, in unsigned short port, 210 in nsIInputStream stream); 211 212 /** 213 * sendBinaryStreamWithAddress 214 * 215 * Send out the datagram to specified remote address and port. 216 * 217 * @param addr The remote host address. 218 * @param stream The input stream to be sent. This must be a buffered stream implementation. 219 */ 220 [noscript] void sendBinaryStreamWithAddress([const] in NetAddrPtr addr, 221 in nsIInputStream stream); 222 223 /** 224 * joinMulticast 225 * 226 * Join the multicast group specified by |addr|. You are then able to 227 * receive future datagrams addressed to the group. 228 * 229 * @param addr 230 * The multicast group address. 231 * @param iface 232 * The local address of the interface on which to join the group. If 233 * this is not specified, the OS may join the group on all interfaces 234 * or only the primary interface. 235 */ 236 void joinMulticast(in AUTF8String addr, [optional] in AUTF8String iface); 237 [noscript] void joinMulticastAddr([const] in NetAddr addr, 238 [const, optional] in NetAddrPtr iface); 239 240 /** 241 * leaveMulticast 242 * 243 * Leave the multicast group specified by |addr|. You will no longer 244 * receive future datagrams addressed to the group. 245 * 246 * @param addr 247 * The multicast group address. 248 * @param iface 249 * The local address of the interface on which to leave the group. 250 * If this is not specified, the OS may leave the group on all 251 * interfaces or only the primary interface. 252 */ 253 void leaveMulticast(in AUTF8String addr, [optional] in AUTF8String iface); 254 [noscript] void leaveMulticastAddr([const] in NetAddr addr, 255 [const, optional] in NetAddrPtr iface); 256 257 /** 258 * getFileDescriptor 259 * 260 * Get the file descriptor of the socket. 261 * 262 * @return The file descriptor. 263 */ 264 [noscript, notxpcom] int64_t getFileDescriptor(); 265 266 /** 267 * enableWritePoll 268 * 269 * Request that the UDP socket polls for write-availability. 270 * Typically called after a non-blocking send returns WOULD_BLOCK. 271 * 272 * Note that the socket always polls for read-availability. 273 */ 274 [noscript, notxpcom] void enableWritePoll(); 275 276 /** 277 * isSocketClosed 278 * 279 * @return true when the socket is already closed. 280 */ 281 [noscript, notxpcom] boolean isSocketClosed(); 282 283 /** 284 * addOutputBytes 285 * 286 * Add number of bytes written to the socket. Used when sending data through 287 * file descriptor optained from getFileDescriptor instead of nsIUDPSocket 288 * methods. 289 * 290 * @param aBytes 291 * The number of bytes written. 292 */ 293 [noscript, notxpcom] void addOutputBytes(in uint32_t aBytes); 294 295 /** 296 * addInputBytes 297 * 298 * Add number of bytes read from the socket. Used when reading data through 299 * file descriptor optained from getFileDescriptor instead of nsIUDPSocket 300 * methods. 301 * 302 * @param aBytes 303 * The number of bytes read. 304 */ 305 [noscript, notxpcom] void addInputBytes(in uint32_t aBytes); 306 307 /** 308 * multicastLoopback 309 * 310 * Whether multicast datagrams sent via this socket should be looped back to 311 * this host (assuming this host has joined the relevant group). Defaults 312 * to true. 313 * Note: This is currently write-only. 314 */ 315 attribute boolean multicastLoopback; 316 317 /** 318 * multicastInterface 319 * 320 * The interface that should be used for sending future multicast datagrams. 321 * Note: This is currently write-only. 322 */ 323 attribute AUTF8String multicastInterface; 324 325 /** 326 * multicastInterfaceAddr 327 * 328 * The interface that should be used for sending future multicast datagrams. 329 * Note: This is currently write-only. 330 */ 331 [noscript] attribute NetAddr multicastInterfaceAddr; 332 333 /** 334 * recvBufferSize 335 * 336 * The size of the receive buffer. Default depends on the OS. 337 */ 338 [noscript] attribute long recvBufferSize; 339 340 /** 341 * sendBufferSize 342 * 343 * The size of the send buffer. Default depends on the OS. 344 */ 345 [noscript] attribute long sendBufferSize; 346 347 /** 348 * dontFragment 349 * 350 * The don't fragment flag. 351 * The socket must be initialized before calling this function. 352 */ 353 [noscript] attribute boolean dontFragment; 354 }; 355 356 /** 357 * nsIUDPSocketListener 358 * 359 * This interface is notified whenever a UDP socket accepts a new connection. 360 * The transport is in the connected state, and read/write streams can be opened 361 * using the normal nsITransport API. The address of the client can be found by 362 * calling the nsISocketTransport::GetAddress method or by inspecting 363 * nsISocketTransport::GetHost, which returns a string representation of the 364 * client's IP address (NOTE: this may be an IPv4 or IPv6 string literal). 365 */ 366 [scriptable, uuid(2E4B5DD3-7358-4281-B81F-10C62EF39CB5)] 367 interface nsIUDPSocketListener : nsISupports 368 { 369 /** 370 * onPacketReceived 371 * 372 * This method is called when a client sends a UDP packet. 373 * 374 * @param aSocket 375 * The UDP socket. 376 * @param aMessage 377 * The message. 378 */ 379 void onPacketReceived(in nsIUDPSocket aSocket, 380 in nsIUDPMessage aMessage); 381 382 /** 383 * onStopListening 384 * 385 * This method is called when the listening socket stops for some reason. 386 * The UDP socket is effectively dead after this notification. 387 * 388 * @param aSocket 389 * The UDP socket. 390 * @param aStatus 391 * The reason why the UDP socket stopped listening. If the 392 * UDP socket was manually closed, then this value will be 393 * NS_BINDING_ABORTED. 394 */ 395 void onStopListening(in nsIUDPSocket aSocket, in nsresult aStatus); 396 }; 397 398 /** 399 * nsIUDPMessage 400 * 401 * This interface is used to encapsulate an incomming UDP message 402 */ 403 [scriptable, builtinclass, uuid(afdc743f-9cc0-40d8-b442-695dc54bbb74)] 404 interface nsIUDPMessage : nsISupports 405 { 406 /** 407 * Address of the source of the message 408 */ 409 readonly attribute nsINetAddr fromAddr; 410 411 /** 412 * Data of the message 413 */ 414 readonly attribute ACString data; 415 416 /** 417 * Stream to send a response 418 */ 419 readonly attribute nsIOutputStream outputStream; 420 421 /** 422 * Raw Data of the message 423 */ 424 [implicit_jscontext] readonly attribute jsval rawData; 425 [noscript, notxpcom, nostdcall] Uint8TArrayRef getDataAsTArray(); 426 }; 427 428 [uuid(99f3d085-3d69-45da-a2c2-a6176af617cb)] 429 interface nsIUDPSocketSyncListener : nsISupports 430 { 431 /** 432 * onPacketReceived 433 * 434 * This method is called when a client sends an UDP packet. 435 * 436 * @param aSocket 437 * The UDP socket. 438 * @param aMessage 439 * The message. 440 */ 441 void onPacketReceived(in nsIUDPSocket aSocket); 442 443 /** 444 * onStopListening 445 * 446 * This method is called when the listening socket stops for some reason. 447 * The UDP socket is effectively dead after this notification. 448 * 449 * @param aSocket 450 * The UDP socket. 451 * @param aStatus 452 * The reason why the UDP socket stopped listening. If the 453 * UDP socket was manually closed, then this value will be 454 * NS_BINDING_ABORTED. 455 */ 456 void onStopListening(in nsIUDPSocket aSocket, in nsresult aStatus); 457 };