[Python] How to get socket Tx timestamps from python socket.sendmsg

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    While reading here, here and here I see that on Linux systems you can request timestamps from received and transmitted packets by setting socket options. I am currently able to use SO_TIMESTAMPNS and SO_TIMESTAMPING to get Rx timestamps with recvmsg. With sendmsg I don't know how to properly package the ancillary data for the control message and sendmsg doesn't even have a return that could hold the data I need.

    This is how I'm getting the Rx timestamp.

    import socket

    SO_TIMESTAMPING = 37
    SOF_TIMESTAMPING = 127

    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_sock.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPING, SO_TIMESTAMPING, SOF_TIMESTAMPING)

    server_sock.bind((_svr_ip, _svr_port))
    server_sock.listen(_num_clients)
    conn, client_addr = server_sock.accept()

    msg, ancdata, flags, address = conn.recvmsg(64, 1024)

    t_rx = ancdata[0][2]
    t_rx_s = int.from_bytes(t_rec[0:7], byteorder="little")
    t_rx_ns = int.from_bytes(t_rec[8:], byteorder="little")



    Section 1.3.4 mentions enabling timestamps via control messages without having to use socket.setsockopt to reserve overhead. I believe that means you are adding the flag to the control message while calling sendmsg instead of using socket.setsockopt.

    Continue reading...

Compartilhe esta Página