def recvfd(socketfd): """ Receive a file descriptor from a L{send1msg} 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{str}) """ data, _ignore_flags, ancillary = recv1msg(socketfd) [(_ignore_cmsg_level, _ignore_cmsg_type, packedFD)] = ancillary # cmsg_level and cmsg_type really need to be SOL_SOCKET / SCM_RIGHTS, but # since those are the *only* standard values, there's not much point in # checking. unpackedFD = 0 int_size = calcsize("i") if len(packedFD) > int_size: # [ar]happens on 64 bit arch (FreeBSD) [unpackedFD] = unpack("i", packedFD[0:int_size]) else: [unpackedFD] = unpack("i", packedFD) return (unpackedFD, data)
def main(): foo, bar = socketpair() sent = send1msg(foo.fileno(), "Hello, world") print "Sent", sent, "bytes" (received, flags, ancillary) = recv1msg(bar.fileno(), 1024) print "Received", repr(received) print "Extra stuff, boring in this case", flags, ancillary
def test_roundtripEmptyAncillary(self): """ L{send1msg} treats an empty ancillary data list the same way it treats receiving no argument for the ancillary parameter at all. """ send1msg(self.input.fileno(), "hello, world!", 0, []) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, ("hello, world!", 0, []))
def test_roundtrip(self): """ L{recv1msg} will retrieve a message sent via L{send1msg}. """ message = "hello, world!" self.assertEqual(len(message), send1msg(self.input.fileno(), message, 0)) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, (message, 0, []))
def test_shortsend(self): """ L{send1msg} returns the number of bytes which it was able to send. """ message = "x" * 1024 * 1024 self.input.setblocking(False) sent = send1msg(self.input.fileno(), message) # Sanity check - make sure we did fill the send buffer and then some self.assertTrue(sent < len(message)) received = recv1msg(self.output.fileno(), 0, len(message)) self.assertEqual(len(received[0]), sent)
def test_roundtrip(self): """ L{recv1msg} will retrieve a message sent via L{send1msg}. """ message = "hello, world!" self.assertEqual( len(message), send1msg(self.input.fileno(), message, 0)) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, (message, 0, []))
def test_shortsend(self): """ L{send1msg} returns the number of bytes which it was able to send. """ message = "x" * 1024 * 1024 self.input.setblocking(False) sent = send1msg(self.input.fileno(), 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 = recv1msg(self.output.fileno(), 0, len(message)) self.assertEqual(len(received[0]), sent)
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 = send1msg(foo.fileno(), "\x00", 0, [(SOL_SOCKET, SCM_RIGHTS, pack("i", reader))]) # Receive the copy, including that one byte of normal data. data, flags, ancillary = recv1msg(bar.fileno(), 1024) duplicate = unpack("i", ancillary[0][2])[0] # Demonstrate that the copy works just like the original write(writer, "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): """ Receive a file descriptor from a L{send1msg} 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{str}) """ data, flags, ancillary = recv1msg(socketfd) [(cmsg_level, cmsg_type, packedFD)] = ancillary # cmsg_level and cmsg_type 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)