1 # Replacing use of host OSes' bind() and connect()
3 These are notes on how to address [issue 344]
4 (http://code.google.com/p/nativeclient/issues/detail?id=344).
6 ## What I thought the current protocol was
8 In the server process: * imc\_makeboundsock() does the following: * `token =
9 ChooseRandomToken()` * `fd = socket(AF_UNIX, SOCK_STREAM)` * `bind(fd, token)`
10 // An error here means two processes chose the same token, which is not supposed
11 to happen. * `listen(fd, backlog)` // Currently backlog=5 on Mac OS X. This is
12 bad because it exposes an arbitrary limit to NaCl processes. * `return
13 (SocketAddress(token), BoundSocket(fd))` * imc\_accept(BoundSocket(fd)) does: *
14 `sock_fd1 = accept(fd)` * `return ConnectedSocket(sock_fd1)`
16 In a client process: * imc\_connect(SocketAddress(token)) does: * `sock_fd2 =
17 socket(AF_UNIX, SOCK_STREAM)` * `connect(sock_fd2, token)` * `return
18 ConnectedSocket(sock_fd2)`
20 ## Current protocol on Linux
22 In the server process: * imc\_makeboundsock() does the following: * `token =
23 ChooseRandomToken()` * `fd = socket(AF_UNIX, SOCK_DGRAM)` * `bind(fd, token)` //
24 An error here means two processes chose the same token, which is not supposed to
25 happen. * `return (SocketAddress(token), BoundSocket(fd))` *
26 imc\_accept(BoundSocket(fd)) does: * `sock_fd1 = recvmsg(fd)` * `return
27 ConnectedSocket(sock_fd1)`
29 In a client process: * imc\_connect(SocketAddress(token)) does: * `sock_fd1,
30 sock_fd2 = socketpair(AF_UNIX, SOCK_SEQPACKET)` * `sendmsg(global_fd, "",
31 [sock_fd1], address=token)` // global\_fd is only available if sel\_ldr is run
32 with "-X -1" * `close(sock_fd2)` * // The problem with this is that it succeeds
33 immediately. We don't know if our connection request was received. * `return
34 ConnectedSocket(sock_fd2)`
36 ## Replacement protocol
38 In the server process: * imc\_makeboundsock() does the following: * `client_fd,
39 server_fd = socketpair(AF_UNIX, SOCK_STREAM)` * `return
40 (SocketAddress(client_fd), BoundSocket(server_fd))` *
41 imc\_accept(BoundSocket(server\_fd)) does: * `msg, sock_fd1 = recvmsg(server_fd,
42 single_byte_buffer)` * `assert msg == "c"` * `return ConnectedSocket(sock_fd1)`
43 // We assume that the FD that is sent to us behaves sanely.
45 In a client process: * imc\_connect(SocketAddress(client\_fd)) does: *
46 `sock_fd1, sock_fd2 = socketpair()` * `sendmsg(client_fd, "c", [sock_fd1])` *
47 `close(sock_fd1)` * // The problem with this is that it succeeds immediately. We
48 don't know if our connection request was received. * `return
49 ConnectedSocket(sock_fd2)`