tsnlab / libtsn

TSN library for C
GNU General Public License v3.0
5 stars 3 forks source link

get HW/SW tx_timestamp apply to latency #31

Closed junppyo closed 10 months ago

junppyo commented 1 year ago

just solve lifetime problem

junppyo commented 1 year ago
fn main() {
    let sock = unsafe {
        libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0)
    };

    let mut addr = libc::sockaddr_in {
        sin_family: libc::AF_INET as libc::sa_family_t,
        sin_port: 12345_u16.to_be(),
        sin_addr: libc::in_addr {
            s_addr: u32::from_be_bytes([192, 168, 0, 1])
        },
        sin_zero: [0; 8],
    };

    let ret = unsafe {
        libc::connect(sock, &addr as *const _ as *const _, std::mem::size_of_val(&addr) as u32)
    };

    let mut buffer = [0u8; 1514];
    let mut iov = libc::iovec {
        iov_base: buffer.as_mut_ptr() as *mut libc::c_void,
        iov_len: buffer.len(),
    };

    let mut control = [0u8; 1024];
    let mut msg = libc::msghdr {
        msg_name: std::ptr::null_mut(),
        msg_namelen: 0,
        msg_iov: &mut iov,
        msg_iovlen: 1,
        msg_control: control.as_mut_ptr() as *mut libc::c_void,
        msg_controllen: control.len(),
        msg_flags: 0,
    };

    let ret = unsafe {
        libc::sendmsg(sock, &msg, 0)
    };
    if ret < 0 {
        eprintln!("Failed to send message: {}", std::io::Error::last_os_error());
    }
}

가장 간단한 코드입니다.

junppyo commented 1 year ago
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <spawn.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
int SendMsg(int fd, const void *buf, size_t count)
{
    struct msghdr msg;
    struct iovec iv;
    int n;

    memset(&msg, 0, sizeof(msg));
    iv.iov_base = (void*)buf;
    iv.iov_len  = count;
    msg.msg_iov = &iv;
    msg.msg_iovlen = 1;
    n = sendmsg(fd, &msg, MSG_NOSIGNAL);
    if (n < 0) {
        printf("errno: %d\n", errno);

    } else {
        printf("send %d\n", n);
    }
    return 0;
}
int main(int argc, char* argv[]) {
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
        printf("error: %d\n", errno);
        return -1;
    }
    if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &(int){1, 64, 16}, sizeof(int)) < 0) {
        printf("set error: %d\n", errno);
        return -1;
    }
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(12345);
    addr.sin_addr.s_addr = inet_addr("22.220.220.21");
    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        printf("error: %d\n", errno);
        return -1;
    }
    SendMsg(fd, "hello", 5);

}
junppyo commented 1 year ago

#include <net/if.h>
#include <arpa/inet.h>
 #include <sys/ioctl.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <spawn.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>

int SendMsg(int fd, const void *buf, size_t count, struct sockaddr_ll addr)
{
    struct msghdr msg;
    struct iovec iv;
    char *ctlbuf;
    int n;

    ctlbuf = (char*) malloc(1024);
    // ctlbuf = (char*) malloc(CMSG_SPACE(sizeof(struct timespec)));
    memset(&msg, 0, sizeof(msg));
    iv.iov_base = (void*)buf;
    iv.iov_len  = count;
    msg.msg_iov = &iv;
    msg.msg_iovlen = 1;
    msg.msg_control =  &ctlbuf;
    msg.msg_controllen = sizeof(ctlbuf);
    msg.msg_name = &addr;
    msg.msg_namelen = sizeof(addr);
    n = sendmsg(fd, &msg, 0);
    if (n < 0) {
        printf("send error: %s\n", strerror(errno));

    } else {
        printf("send %dbyte\n", n);
    }
    return 0;
}

int main(int argc, char* argv[]) {
    int fd = socket(AF_PACKET, SOCK_RAW, 0);
    const unsigned char mac[6] = {0x00, 0xe0, 0x4d, 0x3c, 0x02, 0x0d};
    struct timespec tx = {0, 0};
    if (fd < 0) {
        printf("fd error: %s\n", strerror(errno));
        return -1;
    }
    if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &(int){1, 64, 16}, sizeof(int)) < 0) {
        printf("set error: %s\n", strerror(errno));

        return -1;
    }

    struct sockaddr_ll sock_ll = {
        sll_family: AF_PACKET,
        sll_ifindex: 2, // need to change
        sll_addr: {0, 0, 0, 0, 0, 0, 0, 0},
        sll_halen: 0,
        sll_hatype: 0,
        sll_protocol: 0,
        sll_pkttype: 0,
    };

    int res = bind(fd, &sock_ll, sizeof(sock_ll));
    if (res < 0) {
        printf("Bind error: %s\n", strerror(errno));
    }

    struct sockaddr_ll addr = {
        .sll_family = AF_PACKET,
        .sll_ifindex = 2,
        .sll_halen = 0,
        .sll_protocol = 0
    };
    memcpy(addr.sll_addr, mac, ETH_ALEN);

    SendMsg(fd, "hello", 5, addr);
}