nsIServerSocket.idl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 nsIFile;
  7. interface nsIServerSocketListener;
  8. interface nsISocketTransport;
  9. native PRNetAddr(union PRNetAddr);
  10. [ptr] native PRNetAddrPtr(union PRNetAddr);
  11. typedef unsigned long nsServerSocketFlag;
  12. /**
  13. * nsIServerSocket
  14. *
  15. * An interface to a server socket that can accept incoming connections.
  16. */
  17. [scriptable, uuid(7a9c39cb-a13f-4eef-9bdf-a74301628742)]
  18. interface nsIServerSocket : nsISupports
  19. {
  20. /**
  21. * @name Server Socket Flags
  22. * These flags define various socket options.
  23. * @{
  24. */
  25. /// The server socket will only respond to connections on the
  26. /// local loopback interface. Otherwise, it will accept connections
  27. /// from any interface. To specify a particular network interface,
  28. /// use initWithAddress.
  29. const nsServerSocketFlag LoopbackOnly = 0x00000001;
  30. /// The server socket will not be closed when Gecko is set
  31. /// offline.
  32. const nsServerSocketFlag KeepWhenOffline = 0x00000002;
  33. /** @} */
  34. /**
  35. * init
  36. *
  37. * This method initializes a server socket.
  38. *
  39. * @param aPort
  40. * The port of the server socket. Pass -1 to indicate no preference,
  41. * and a port will be selected automatically.
  42. * @param aLoopbackOnly
  43. * If true, the server socket will only respond to connections on the
  44. * local loopback interface. Otherwise, it will accept connections
  45. * from any interface. To specify a particular network interface,
  46. * use initWithAddress.
  47. * @param aBackLog
  48. * The maximum length the queue of pending connections may grow to.
  49. * This parameter may be silently limited by the operating system.
  50. * Pass -1 to use the default value.
  51. */
  52. void init(in long aPort,
  53. in boolean aLoopbackOnly,
  54. in long aBackLog);
  55. /**
  56. * initSpecialConnection
  57. *
  58. * This method initializes a server socket and offers the ability to have
  59. * that socket not get terminated if Gecko is set offline.
  60. *
  61. * @param aPort
  62. * The port of the server socket. Pass -1 to indicate no preference,
  63. * and a port will be selected automatically.
  64. * @param aFlags
  65. * Flags for the socket.
  66. * @param aBackLog
  67. * The maximum length the queue of pending connections may grow to.
  68. * This parameter may be silently limited by the operating system.
  69. * Pass -1 to use the default value.
  70. */
  71. void initSpecialConnection(in long aPort,
  72. in nsServerSocketFlag aFlags,
  73. in long aBackLog);
  74. /**
  75. * initWithAddress
  76. *
  77. * This method initializes a server socket, and binds it to a particular
  78. * local address (and hence a particular local network interface).
  79. *
  80. * @param aAddr
  81. * The address to which this server socket should be bound.
  82. * @param aBackLog
  83. * The maximum length the queue of pending connections may grow to.
  84. * This parameter may be silently limited by the operating system.
  85. * Pass -1 to use the default value.
  86. */
  87. [noscript] void initWithAddress([const] in PRNetAddrPtr aAddr, in long aBackLog);
  88. /**
  89. * initWithFilename
  90. *
  91. * This method initializes a Unix domain or "local" server socket. Such
  92. * a socket has a name in the filesystem, like an ordinary file. To
  93. * connect, a client supplies the socket's filename, and the usual
  94. * permission checks on socket apply.
  95. *
  96. * This makes Unix domain sockets useful for communication between the
  97. * programs being run by a specific user on a single machine: the
  98. * operating system takes care of authentication, and the user's home
  99. * directory or profile directory provide natural per-user rendezvous
  100. * points.
  101. *
  102. * Since Unix domain sockets are always local to the machine, they are
  103. * not affected by the nsIIOService's 'offline' flag.
  104. *
  105. * The system-level socket API may impose restrictions on the length of
  106. * the filename that are stricter than those of the underlying
  107. * filesystem. If the file name is too long, this returns
  108. * NS_ERROR_FILE_NAME_TOO_LONG.
  109. *
  110. * All components of the path prefix of |aPath| must name directories;
  111. * otherwise, this returns NS_ERROR_FILE_NOT_DIRECTORY.
  112. *
  113. * This call requires execute permission on all directories containing
  114. * the one in which the socket is to be created, and write and execute
  115. * permission on the directory itself. Otherwise, this returns
  116. * NS_ERROR_CONNECTION_REFUSED.
  117. *
  118. * This call creates the socket's directory entry. There must not be
  119. * any existing entry with the given name. If there is, this returns
  120. * NS_ERROR_SOCKET_ADDRESS_IN_USE.
  121. *
  122. * On systems that don't support Unix domain sockets at all, this
  123. * returns NS_ERROR_SOCKET_ADDRESS_NOT_SUPPORTED.
  124. *
  125. * @param aPath nsIFile
  126. * The file name at which the socket should be created.
  127. *
  128. * @param aPermissions unsigned long
  129. * Unix-style permission bits to be applied to the new socket.
  130. *
  131. * Note about permissions: Linux's unix(7) man page claims that some
  132. * BSD-derived systems ignore permissions on UNIX-domain sockets;
  133. * NetBSD's bind(2) man page agrees, but says it does check now (dated
  134. * 2005). POSIX has required 'connect' to fail if write permission on
  135. * the socket itself is not granted since 2003 (Issue 6). NetBSD says
  136. * that the permissions on the containing directory (execute) have
  137. * always applied, so creating sockets in appropriately protected
  138. * directories should be secure on both old and new systems.
  139. */
  140. void initWithFilename(in nsIFile aPath, in unsigned long aPermissions,
  141. in long aBacklog);
  142. /**
  143. * close
  144. *
  145. * This method closes a server socket. This does not affect already
  146. * connected client sockets (i.e., the nsISocketTransport instances
  147. * created from this server socket). This will cause the onStopListening
  148. * event to asynchronously fire with a status of NS_BINDING_ABORTED.
  149. */
  150. void close();
  151. /**
  152. * asyncListen
  153. *
  154. * This method puts the server socket in the listening state. It will
  155. * asynchronously listen for and accept client connections. The listener
  156. * will be notified once for each client connection that is accepted. The
  157. * listener's onSocketAccepted method will be called on the same thread
  158. * that called asyncListen (the calling thread must have a nsIEventTarget).
  159. *
  160. * The listener will be passed a reference to an already connected socket
  161. * transport (nsISocketTransport). See below for more details.
  162. *
  163. * @param aListener
  164. * The listener to be notified when client connections are accepted.
  165. */
  166. void asyncListen(in nsIServerSocketListener aListener);
  167. /**
  168. * Returns the port of this server socket.
  169. */
  170. readonly attribute long port;
  171. /**
  172. * Returns the address to which this server socket is bound. Since a
  173. * server socket may be bound to multiple network devices, this address
  174. * may not necessarily be specific to a single network device. In the
  175. * case of an IP socket, the IP address field would be zerod out to
  176. * indicate a server socket bound to all network devices. Therefore,
  177. * this method cannot be used to determine the IP address of the local
  178. * system. See nsIDNSService::myHostName if this is what you need.
  179. */
  180. [noscript] PRNetAddr getAddress();
  181. };
  182. /**
  183. * nsIServerSocketListener
  184. *
  185. * This interface is notified whenever a server socket accepts a new connection.
  186. * The transport is in the connected state, and read/write streams can be opened
  187. * using the normal nsITransport API. The address of the client can be found by
  188. * calling the nsISocketTransport::GetAddress method or by inspecting
  189. * nsISocketTransport::GetHost, which returns a string representation of the
  190. * client's IP address (NOTE: this may be an IPv4 or IPv6 string literal).
  191. */
  192. [scriptable, uuid(836d98ec-fee2-4bde-b609-abd5e966eabd)]
  193. interface nsIServerSocketListener : nsISupports
  194. {
  195. /**
  196. * onSocketAccepted
  197. *
  198. * This method is called when a client connection is accepted.
  199. *
  200. * @param aServ
  201. * The server socket.
  202. * @param aTransport
  203. * The connected socket transport.
  204. */
  205. void onSocketAccepted(in nsIServerSocket aServ,
  206. in nsISocketTransport aTransport);
  207. /**
  208. * onStopListening
  209. *
  210. * This method is called when the listening socket stops for some reason.
  211. * The server socket is effectively dead after this notification.
  212. *
  213. * @param aServ
  214. * The server socket.
  215. * @param aStatus
  216. * The reason why the server socket stopped listening. If the
  217. * server socket was manually closed, then this value will be
  218. * NS_BINDING_ABORTED.
  219. */
  220. void onStopListening(in nsIServerSocket aServ, in nsresult aStatus);
  221. };