nsIUDPSocket.idl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include "nsISupports.idl"
  6. interface nsINetAddr;
  7. interface nsIUDPSocketListener;
  8. interface nsIUDPMessage;
  9. interface nsISocketTransport;
  10. interface nsIOutputStream;
  11. interface nsIInputStream;
  12. interface nsIPrincipal;
  13. %{ C++
  14. #include "nsTArrayForwardDeclare.h"
  15. namespace mozilla {
  16. namespace net {
  17. union NetAddr;
  18. }
  19. }
  20. %}
  21. native NetAddr(mozilla::net::NetAddr);
  22. [ptr] native NetAddrPtr(mozilla::net::NetAddr);
  23. [ref] native Uint8TArrayRef(FallibleTArray<uint8_t>);
  24. /**
  25. * nsIUDPSocket
  26. *
  27. * An interface to a UDP socket that can accept incoming connections.
  28. */
  29. [scriptable, uuid(d423bf4e-4499-40cf-bc03-153e2bf206d1)]
  30. interface nsIUDPSocket : nsISupports
  31. {
  32. /**
  33. * init
  34. *
  35. * This method initializes a UDP socket.
  36. *
  37. * @param aPort
  38. * The port of the UDP socket. Pass -1 to indicate no preference,
  39. * and a port will be selected automatically.
  40. * @param aLoopbackOnly
  41. * If true, the UDP socket will only respond to connections on the
  42. * local loopback interface. Otherwise, it will accept connections
  43. * from any interface. To specify a particular network interface,
  44. * use initWithAddress.
  45. * @param aPrincipal
  46. * The principal connected to this socket.
  47. * @param aAddressReuse
  48. * If true, the socket is allowed to be bound to an address that is
  49. * already in use. Default is true.
  50. */
  51. [optional_argc] void init(in long aPort,
  52. in boolean aLoopbackOnly,
  53. in nsIPrincipal aPrincipal,
  54. [optional] in boolean aAddressReuse);
  55. [optional_argc] void init2(in AUTF8String aAddr,
  56. in long aPort,
  57. in nsIPrincipal aPrincipal,
  58. [optional] in boolean aAddressReuse);
  59. /**
  60. * initWithAddress
  61. *
  62. * This method initializes a UDP socket, and binds it to a particular
  63. * local address (and hence a particular local network interface).
  64. *
  65. * @param aAddr
  66. * The address to which this UDP socket should be bound.
  67. * @param aPrincipal
  68. * The principal connected to this socket.
  69. * @param aAddressReuse
  70. * If true, the socket is allowed to be bound to an address that is
  71. * already in use. Default is true.
  72. */
  73. [noscript, optional_argc] void initWithAddress([const] in NetAddrPtr aAddr,
  74. in nsIPrincipal aPrincipal,
  75. [optional] in boolean aAddressReuse);
  76. /**
  77. * close
  78. *
  79. * This method closes a UDP socket. This does not affect already
  80. * connected client sockets (i.e., the nsISocketTransport instances
  81. * created from this UDP socket). This will cause the onStopListening
  82. * event to asynchronously fire with a status of NS_BINDING_ABORTED.
  83. */
  84. void close();
  85. /**
  86. * asyncListen
  87. *
  88. * This method puts the UDP socket in the listening state. It will
  89. * asynchronously listen for and accept client connections. The listener
  90. * will be notified once for each client connection that is accepted. The
  91. * listener's onSocketAccepted method will be called on the same thread
  92. * that called asyncListen (the calling thread must have a nsIEventTarget).
  93. *
  94. * The listener will be passed a reference to an already connected socket
  95. * transport (nsISocketTransport). See below for more details.
  96. *
  97. * @param aListener
  98. * The listener to be notified when client connections are accepted.
  99. */
  100. void asyncListen(in nsIUDPSocketListener aListener);
  101. /**
  102. * connect
  103. *
  104. * This method connects the UDP socket to a remote UDP address.
  105. *
  106. * @param aRemoteAddr
  107. * The remote address to connect to
  108. */
  109. void connect([const] in NetAddrPtr aAddr);
  110. /**
  111. * Returns the local address of this UDP socket
  112. */
  113. readonly attribute nsINetAddr localAddr;
  114. /**
  115. * Returns the port of this UDP socket.
  116. */
  117. readonly attribute long port;
  118. /**
  119. * Returns the address to which this UDP socket is bound. Since a
  120. * UDP socket may be bound to multiple network devices, this address
  121. * may not necessarily be specific to a single network device. In the
  122. * case of an IP socket, the IP address field would be zerod out to
  123. * indicate a UDP socket bound to all network devices. Therefore,
  124. * this method cannot be used to determine the IP address of the local
  125. * system. See nsIDNSService::myHostName if this is what you need.
  126. */
  127. [noscript] NetAddr getAddress();
  128. /**
  129. * send
  130. *
  131. * Send out the datagram to specified remote host and port.
  132. * DNS lookup will be triggered.
  133. *
  134. * @param host The remote host name.
  135. * @param port The remote port.
  136. * @param data The buffer containing the data to be written.
  137. * @param dataLength The maximum number of bytes to be written.
  138. * @return number of bytes written. (0 or dataLength)
  139. */
  140. unsigned long send(in AUTF8String host, in unsigned short port,
  141. [const, array, size_is(dataLength)]in uint8_t data,
  142. in unsigned long dataLength);
  143. /**
  144. * sendWithAddr
  145. *
  146. * Send out the datagram to specified remote host and port.
  147. *
  148. * @param addr The remote host address.
  149. * @param data The buffer containing the data to be written.
  150. * @param dataLength The maximum number of bytes to be written.
  151. * @return number of bytes written. (0 or dataLength)
  152. */
  153. unsigned long sendWithAddr(in nsINetAddr addr,
  154. [const, array, size_is(dataLength)]in uint8_t data,
  155. in unsigned long dataLength);
  156. /**
  157. * sendWithAddress
  158. *
  159. * Send out the datagram to specified remote address and port.
  160. *
  161. * @param addr The remote host address.
  162. * @param data The buffer containing the data to be written.
  163. * @param dataLength The maximum number of bytes to be written.
  164. * @return number of bytes written. (0 or dataLength)
  165. */
  166. [noscript] unsigned long sendWithAddress([const] in NetAddrPtr addr,
  167. [const, array, size_is(dataLength)]in uint8_t data,
  168. in unsigned long dataLength);
  169. /**
  170. * sendBinaryStream
  171. *
  172. * Send out the datagram to specified remote address and port.
  173. *
  174. * @param host The remote host name.
  175. * @param port The remote port.
  176. * @param stream The input stream to be sent. This must be a buffered stream implementation.
  177. */
  178. void sendBinaryStream(in AUTF8String host, in unsigned short port,
  179. in nsIInputStream stream);
  180. /**
  181. * sendBinaryStreamWithAddress
  182. *
  183. * Send out the datagram to specified remote address and port.
  184. *
  185. * @param addr The remote host address.
  186. * @param stream The input stream to be sent. This must be a buffered stream implementation.
  187. */
  188. void sendBinaryStreamWithAddress([const] in NetAddrPtr addr,
  189. in nsIInputStream stream);
  190. /**
  191. * joinMulticast
  192. *
  193. * Join the multicast group specified by |addr|. You are then able to
  194. * receive future datagrams addressed to the group.
  195. *
  196. * @param addr
  197. * The multicast group address.
  198. * @param iface
  199. * The local address of the interface on which to join the group. If
  200. * this is not specified, the OS may join the group on all interfaces
  201. * or only the primary interface.
  202. */
  203. void joinMulticast(in AUTF8String addr, [optional] in AUTF8String iface);
  204. [noscript] void joinMulticastAddr([const] in NetAddr addr,
  205. [const, optional] in NetAddrPtr iface);
  206. /**
  207. * leaveMulticast
  208. *
  209. * Leave the multicast group specified by |addr|. You will no longer
  210. * receive future datagrams addressed to the group.
  211. *
  212. * @param addr
  213. * The multicast group address.
  214. * @param iface
  215. * The local address of the interface on which to leave the group.
  216. * If this is not specified, the OS may leave the group on all
  217. * interfaces or only the primary interface.
  218. */
  219. void leaveMulticast(in AUTF8String addr, [optional] in AUTF8String iface);
  220. [noscript] void leaveMulticastAddr([const] in NetAddr addr,
  221. [const, optional] in NetAddrPtr iface);
  222. /**
  223. * multicastLoopback
  224. *
  225. * Whether multicast datagrams sent via this socket should be looped back to
  226. * this host (assuming this host has joined the relevant group). Defaults
  227. * to true.
  228. * Note: This is currently write-only.
  229. */
  230. attribute boolean multicastLoopback;
  231. /**
  232. * multicastInterface
  233. *
  234. * The interface that should be used for sending future multicast datagrams.
  235. * Note: This is currently write-only.
  236. */
  237. attribute AUTF8String multicastInterface;
  238. /**
  239. * multicastInterfaceAddr
  240. *
  241. * The interface that should be used for sending future multicast datagrams.
  242. * Note: This is currently write-only.
  243. */
  244. [noscript] attribute NetAddr multicastInterfaceAddr;
  245. /**
  246. * recvBufferSize
  247. *
  248. * The size of the receive buffer. Default depends on the OS.
  249. */
  250. [noscript] attribute long recvBufferSize;
  251. /**
  252. * sendBufferSize
  253. *
  254. * The size of the send buffer. Default depends on the OS.
  255. */
  256. [noscript] attribute long sendBufferSize;
  257. };
  258. /**
  259. * nsIUDPSocketListener
  260. *
  261. * This interface is notified whenever a UDP socket accepts a new connection.
  262. * The transport is in the connected state, and read/write streams can be opened
  263. * using the normal nsITransport API. The address of the client can be found by
  264. * calling the nsISocketTransport::GetAddress method or by inspecting
  265. * nsISocketTransport::GetHost, which returns a string representation of the
  266. * client's IP address (NOTE: this may be an IPv4 or IPv6 string literal).
  267. */
  268. [scriptable, uuid(2E4B5DD3-7358-4281-B81F-10C62EF39CB5)]
  269. interface nsIUDPSocketListener : nsISupports
  270. {
  271. /**
  272. * onPacketReceived
  273. *
  274. * This method is called when a client sends an UDP packet.
  275. *
  276. * @param aSocket
  277. * The UDP socket.
  278. * @param aMessage
  279. * The message.
  280. */
  281. void onPacketReceived(in nsIUDPSocket aSocket,
  282. in nsIUDPMessage aMessage);
  283. /**
  284. * onStopListening
  285. *
  286. * This method is called when the listening socket stops for some reason.
  287. * The UDP socket is effectively dead after this notification.
  288. *
  289. * @param aSocket
  290. * The UDP socket.
  291. * @param aStatus
  292. * The reason why the UDP socket stopped listening. If the
  293. * UDP socket was manually closed, then this value will be
  294. * NS_BINDING_ABORTED.
  295. */
  296. void onStopListening(in nsIUDPSocket aSocket, in nsresult aStatus);
  297. };
  298. /**
  299. * nsIUDPMessage
  300. *
  301. * This interface is used to encapsulate an incomming UDP message
  302. */
  303. [scriptable, uuid(afdc743f-9cc0-40d8-b442-695dc54bbb74)]
  304. interface nsIUDPMessage : nsISupports
  305. {
  306. /**
  307. * Address of the source of the message
  308. */
  309. readonly attribute nsINetAddr fromAddr;
  310. /**
  311. * Data of the message
  312. */
  313. readonly attribute ACString data;
  314. /**
  315. * Stream to send a response
  316. */
  317. readonly attribute nsIOutputStream outputStream;
  318. /**
  319. * Raw Data of the message
  320. */
  321. [implicit_jscontext] readonly attribute jsval rawData;
  322. [noscript, notxpcom, nostdcall] Uint8TArrayRef getDataAsTArray();
  323. };