+ return pid;
+}
+
+ssize_t recvfromto(int s, void *buf, size_t len, int flags,
+ struct sockaddr *from, socklen_t *fromlen, struct in_addr *toaddr)
+{
+ ssize_t r;
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ struct iovec vec;
+ char cbuf[128];
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_name = from;
+ msg.msg_namelen = *fromlen;
+
+ vec.iov_base = buf;
+ vec.iov_len = len;
+ msg.msg_iov = &vec;
+ msg.msg_iovlen = 1;
+ msg.msg_flags = 0;
+
+ msg.msg_control = cbuf;
+ msg.msg_controllen = sizeof(cbuf);
+
+ if ((r = recvmsg(s, &msg, flags)) < 0)
+ return r;
+
+ if (fromlen)
+ *fromlen = msg.msg_namelen;
+
+ memset(toaddr, 0, sizeof(*toaddr));
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
+ {
+ if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO)
+ {
+ struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
+ memcpy(toaddr, &i->ipi_addr, sizeof(*toaddr));
+ break;
+ }
+ }
+
+ return r;
+}
+
+ssize_t sendtofrom(int s, void const *buf, size_t len, int flags,
+ struct sockaddr const *to, socklen_t tolen, struct in_addr const *from)
+{
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ struct iovec vec;
+ struct in_pktinfo pktinfo;
+ char cbuf[CMSG_SPACE(sizeof(pktinfo))];
+
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_name = (struct sockaddr *) to;
+ msg.msg_namelen = tolen;
+
+ vec.iov_base = (void *) buf;
+ vec.iov_len = len;
+ msg.msg_iov = &vec;
+ msg.msg_iovlen = 1;
+ msg.msg_flags = 0;
+
+ msg.msg_control = cbuf;
+ msg.msg_controllen = sizeof(cbuf);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_IP;
+ cmsg->cmsg_type = IP_PKTINFO;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
+
+ memset(&pktinfo, 0, sizeof(pktinfo));
+ memcpy(&pktinfo.ipi_spec_dst, from, sizeof(*from));
+ memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
+
+ return sendmsg(s, &msg, flags);