How to correctly receive MSG_FLAGS?

259 views Asked by At

After sending the message, I am waiting for MSG_CONFIRM. How to correctly receive MSG_FLAGS? I don't understand why temp is always equal -1.

struct msghdr msg;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr
setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &(int){ 1 }, sizeof(int));
setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &(int){ 1 }, sizeof(int));
setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, &(int){ 1 }, sizeof(int));

strcpy(ifr.ifr_name, ifname);
ioctl(s, SIOCGIFINDEX, &ifr);

addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

write(s, &frame, sizeof(struct can_frame));

temp = recvmsg(s, &msg, MSG_CONFIRM);

printf("%02X \n", temp);
printf("%02X \n", errno);

if (msg.msg_flags==MSG_CONFIRM){
   printf("MSG_CONFIRM"); }
1

There are 1 answers

0
Jos Seldenthuis On

MSG_CONFIRM is set by recvmsg() in msg.msg_flags if the CAN frame is a send confirmation, but you cannot pass the flag to recvmsg().

Unfortunately this makes handling send confirmations a bit complex. After write(), you may first receive CAN frames from other nodes, which you have to handle or buffer, before you receive the MSG_CONFIRM frame corresponding to write(). You cannot use recvmsg() to just wait for the confirmation and keep the other frames in the kernel buffer,