def main(): foo, bar = socketpair() sent = sendmsg(foo, b"Hello, world") print("Sent", sent, "bytes") (received, ancillary, flags) = recvmsg(bar, 1024) print("Received", repr(received)) print("Extra stuff, boring in this case", flags, ancillary)
def fd_from_unix(): c, p = socketpair() SubProcess(c).start() data, an, f = recvmsg(p, 1024) r_copy = unpack('i', an[0][2])[0] print(r_copy) print(os.read(r_copy, 1024))
def test_roundtripEmptyAncillary(self): """ L{sendmsg} treats an empty ancillary data list the same way it treats receiving no argument for the ancillary parameter at all. """ sendmsg(self.input, b"hello, world!", [], 0) result = recvmsg(self.output) self.assertEqual(result, (b"hello, world!", [], 0))
def test_roundtrip(self): """ L{recvmsg} will retrieve a message sent via L{sendmsg}. """ message = b"hello, world!" self.assertEqual(len(message), sendmsg(self.input, message)) result = recvmsg(self.output) self.assertEqual(result.data, b"hello, world!") self.assertEqual(result.flags, 0) self.assertEqual(result.ancillary, [])
def from_handoff_socket(cls, sock, eliot_action=None): sock.sendall(READY_BYTE) description, ancillary, flags = recvmsg(sock, maxSize=1) # OOB data, like ancillary data, interrupts MSG_WAITALL. so # do this in two syscalls. description += sock.recv(DESCRIPTION_LENGTH - 1, socket.MSG_WAITALL) [fd] = struct.unpack('i', ancillary[0][2]) new_sock = reconstitute_socket(fd, description, eliot_action) new_sock.setblocking(True) ret = cls(new_sock) return ret
def test_shortsend(self): """ L{sendmsg} returns the number of bytes which it was able to send. """ message = b"x" * 1024 * 1024 self.input.setblocking(False) sent = sendmsg(self.input, message) # Sanity check - make sure the amount of data we sent was less than the # message, but not the whole message, as we should have filled the send # buffer. This won't work if the send buffer is more than 1MB, though. self.assertTrue(sent < len(message)) received = recvmsg(self.output, len(message)) self.assertEqual(len(received[0]), sent)
def test_roundtrip(self): """ L{recvmsg} will retrieve a message sent via L{sendmsg}. """ message = b"hello, world!" self.assertEqual( len(message), sendmsg(self.input, message)) result = recvmsg(self.output) self.assertEqual(result.data, b"hello, world!") self.assertEqual(result.flags, 0) self.assertEqual(result.ancillary, [])
def main(): foo, bar = socketpair() reader, writer = pipe() # Send a copy of the descriptor. Notice that there must be at least one # byte of normal data passed in. sent = sendmsg(foo, b"\x00", [(SOL_SOCKET, SCM_RIGHTS, pack("i", reader))]) # Receive the copy, including that one byte of normal data. data, ancillary, flags = recvmsg(bar, 1024) duplicate = unpack("i", ancillary[0][2])[0] # Demonstrate that the copy works just like the original write(writer, b"Hello, world") print("Read from original (%d): %r" % (reader, read(reader, 6))) print("Read from duplicate (%d): %r" % (duplicate, read(duplicate, 6)))
def main(): foo, bar = socketpair() reader, writer = pipe() # Send a copy of the descriptor. Notice that there must be at least one # byte of normal data passed in. sent = sendmsg( foo, b"\x00", [(SOL_SOCKET, SCM_RIGHTS, pack("i", reader))]) # Receive the copy, including that one byte of normal data. data, ancillary, flags = recvmsg(bar, 1024) duplicate = unpack("i", ancillary[0][2])[0] # Demonstrate that the copy works just like the original write(writer, b"Hello, world") print("Read from original (%d): %r" % (reader, read(reader, 6))) print("Read from duplicate (%d): %r" % (duplicate, read(duplicate, 6)))
def recvfd(socketfd: int) -> Tuple[int, bytes]: """ Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX} socket. @param socketfd: An C{AF_UNIX} socket, attached to another process waiting to send sockets via the ancillary data mechanism in L{send1msg}. @param fd: C{int} @return: a 2-tuple of (new file descriptor, description). @rtype: 2-tuple of (C{int}, C{bytes}) """ ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM) data, ancillary, flags = recvmsg(ourSocket) [(cmsgLevel, cmsgType, packedFD)] = ancillary # cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but # since those are the *only* standard values, there's not much point in # checking. [unpackedFD] = unpack("i", packedFD) return (unpackedFD, data)
def recvfd(socketfd): """ Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX} socket. @param socketfd: An C{AF_UNIX} socket, attached to another process waiting to send sockets via the ancillary data mechanism in L{send1msg}. @param fd: C{int} @return: a 2-tuple of (new file descriptor, description). @rtype: 2-tuple of (C{int}, C{bytes}) """ ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM) data, ancillary, flags = recvmsg(ourSocket) [(cmsgLevel, cmsgType, packedFD)] = ancillary # cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but # since those are the *only* standard values, there's not much point in # checking. [unpackedFD] = unpack("i", packedFD) return (unpackedFD, data)
fd_from_unix() ##################################################################################################3 # Copying File Descriptors import threading import time import socket def server(): sock = socket.socket(socket.AF_UNIX) if os.path.exists('unix_sock'): os.remove('unix_sock') sock.bind('unix_sock') sock.listen(11) conn, addr = sock.accept() r, w = os.pipe() sendmsg(conn, b'0', [(SOL_SOCKET, SCM_RIGHTS, pack('i', r))]) os.write(w, b'HYYYYY!') threading.Thread(target=server).start() time.sleep(2) cl = socket.socket(socket.AF_UNIX) cl.connect('unix_sock') d, an, f = recvmsg(cl, 1024) r_copy = unpack('i', an[0][2])[0] print(os.read(r_copy, 1024))