sendmsg.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- test-case-name: twisted.test.test_sendmsg -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. sendmsg(2) and recvmsg(2) support for Python.
  6. """
  7. from __future__ import absolute_import, division
  8. from collections import namedtuple
  9. from twisted.python.compat import _PY3
  10. __all__ = ["sendmsg", "recvmsg", "getSocketFamily", "SCM_RIGHTS"]
  11. if not _PY3:
  12. from twisted.python._sendmsg import send1msg, recv1msg
  13. from twisted.python._sendmsg import getsockfam, SCM_RIGHTS
  14. __all__ += ["send1msg", "recv1msg", "getsockfam"]
  15. else:
  16. from socket import SCM_RIGHTS, CMSG_SPACE
  17. RecievedMessage = namedtuple('RecievedMessage', ['data', 'ancillary', 'flags'])
  18. def sendmsg(socket, data, ancillary=[], flags=0):
  19. """
  20. Send a message on a socket.
  21. @param socket: The socket to send the message on.
  22. @type socket: L{socket.socket}
  23. @param data: Bytes to write to the socket.
  24. @type data: bytes
  25. @param ancillary: Extra data to send over the socket outside of the normal
  26. datagram or stream mechanism. By default no ancillary data is sent.
  27. @type ancillary: C{list} of C{tuple} of C{int}, C{int}, and C{bytes}.
  28. @param flags: Flags to affect how the message is sent. See the C{MSG_}
  29. constants in the sendmsg(2) manual page. By default no flags are set.
  30. @type flags: C{int}
  31. @return: The return value of the underlying syscall, if it succeeds.
  32. """
  33. if _PY3:
  34. return socket.sendmsg([data], ancillary, flags)
  35. else:
  36. return send1msg(socket.fileno(), data, flags, ancillary)
  37. def recvmsg(socket, maxSize=8192, cmsgSize=4096, flags=0):
  38. """
  39. Receive a message on a socket.
  40. @param socket: The socket to receive the message on.
  41. @type socket: L{socket.socket}
  42. @param maxSize: The maximum number of bytes to receive from the socket using
  43. the datagram or stream mechanism. The default maximum is 8192.
  44. @type maxSize: L{int}
  45. @param cmsgSize: The maximum number of bytes to receive from the socket
  46. outside of the normal datagram or stream mechanism. The default maximum
  47. is 4096.
  48. @type cmsgSize: L{int}
  49. @param flags: Flags to affect how the message is sent. See the C{MSG_}
  50. constants in the sendmsg(2) manual page. By default no flags are set.
  51. @type flags: L{int}
  52. @return: A named 3-tuple of the bytes received using the datagram/stream
  53. mechanism, a L{list} of L{tuple}s giving ancillary received data, and
  54. flags as an L{int} describing the data received.
  55. """
  56. if _PY3:
  57. # In Twisted's sendmsg.c, the csmg_space is defined as:
  58. # int cmsg_size = 4096;
  59. # cmsg_space = CMSG_SPACE(cmsg_size);
  60. # Since the default in Python 3's socket is 0, we need to define our
  61. # own default of 4096. -hawkie
  62. data, ancillary, flags = socket.recvmsg(
  63. maxSize, CMSG_SPACE(cmsgSize), flags)[0:3]
  64. else:
  65. data, flags, ancillary = recv1msg(
  66. socket.fileno(), flags, maxSize, cmsgSize)
  67. return RecievedMessage(data=data, ancillary=ancillary, flags=flags)
  68. def getSocketFamily(socket):
  69. """
  70. Return the family of the given socket.
  71. @param socket: The socket to get the family of.
  72. @type socket: L{socket.socket}
  73. @rtype: L{int}
  74. """
  75. if _PY3:
  76. return socket.family
  77. else:
  78. return getsockfam(socket.fileno())