The msghdr.msg_iov->iov_base and msghdr.msg_iov->iov_len of ff_sendmsg() and ff_recvmsg() compatible with the Linux.

Note: linux2freebsd_msghdr and freebsd2linux_msghdr must be called in sequence and in pairs.
This commit is contained in:
fengbojiang 2024-10-11 17:21:05 +08:00
parent 615b66a344
commit 1152067e93
1 changed files with 12 additions and 0 deletions

View File

@ -185,6 +185,9 @@
/* msghdr define start */ /* msghdr define start */
static /*__thread*/ struct iovec msg_iov_tmp[UIO_MAXIOV];
static /*__thread*/ size_t msg_iovlen_tmp;
struct linux_msghdr { struct linux_msghdr {
void *msg_name; /* Address to send to/receive from. */ void *msg_name; /* Address to send to/receive from. */
socklen_t msg_namelen; /* Length of address data. */ socklen_t msg_namelen; /* Length of address data. */
@ -642,6 +645,7 @@ linux2freebsd_cmsg(const struct linux_msghdr *linux_msg, struct msghdr *freebsd_
/* /*
* While sendmsg, need convert msg_name and msg_control from Linux to FreeBSD. * While sendmsg, need convert msg_name and msg_control from Linux to FreeBSD.
* While recvmsg, need convert msg_name and msg_control from FreeBSD to Linux. * While recvmsg, need convert msg_name and msg_control from FreeBSD to Linux.
* Note: linux2freebsd_msghdr and freebsd2linux_msghdr must be called in sequence and in pairs.
*/ */
static int static int
freebsd2linux_msghdr(struct linux_msghdr *linux_msg, struct msghdr *freebsd_msg, int send_flag) freebsd2linux_msghdr(struct linux_msghdr *linux_msg, struct msghdr *freebsd_msg, int send_flag)
@ -658,6 +662,8 @@ freebsd2linux_msghdr(struct linux_msghdr *linux_msg, struct msghdr *freebsd_msg,
linux_msg->msg_iov = freebsd_msg->msg_iov; linux_msg->msg_iov = freebsd_msg->msg_iov;
linux_msg->msg_iovlen = freebsd_msg->msg_iovlen; linux_msg->msg_iovlen = freebsd_msg->msg_iovlen;
/* Restore the old iov pointer, compatible with the Linux interface */
memcpy(linux_msg->msg_iov, msg_iov_tmp, msg_iovlen_tmp * sizeof(struct iovec));
if(freebsd_msg->msg_control && linux_msg->msg_control && !send_flag) { if(freebsd_msg->msg_control && linux_msg->msg_control && !send_flag) {
freebsd2linux_cmsghdr(linux_msg, freebsd_msg); freebsd2linux_cmsghdr(linux_msg, freebsd_msg);
@ -685,6 +691,12 @@ linux2freebsd_msghdr(const struct linux_msghdr *linux_msg, struct msghdr *freebs
} }
freebsd_msg->msg_namelen = linux_msg->msg_namelen; freebsd_msg->msg_namelen = linux_msg->msg_namelen;
/* Save the old iov pointer, compatible with the Linux interface */
msg_iovlen_tmp = linux_msg->msg_iovlen;
if (msg_iovlen_tmp > UIO_MAXIOV) {
return -1; // EMSGSIZE;
}
memcpy(msg_iov_tmp, linux_msg->msg_iov, msg_iovlen_tmp * sizeof(struct iovec));
freebsd_msg->msg_iov = linux_msg->msg_iov; freebsd_msg->msg_iov = linux_msg->msg_iov;
freebsd_msg->msg_iovlen = linux_msg->msg_iovlen; freebsd_msg->msg_iovlen = linux_msg->msg_iovlen;