本文整理汇总了C++中CMSG_FIRSTHDR函数的典型用法代码示例。如果您正苦于以下问题:C++ CMSG_FIRSTHDR函数的具体用法?C++ CMSG_FIRSTHDR怎么用?C++ CMSG_FIRSTHDR使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CMSG_FIRSTHDR函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ping
static void ping(const char *host)
{
char packet[datalen + MAXIPLEN + MAXICMPLEN];
char buf[INET6_ADDRSTRLEN];
int sockopt;
struct msghdr msg;
struct sockaddr_in6 from;
struct iovec iov;
char control_buf[CMSG_SPACE(36)];
pingsock = create_icmp6_socket();
memset(&pingaddr, 0, sizeof(struct sockaddr_in));
pingaddr.sin6_family = AF_INET6;
hostent = xgethostbyname2(host, AF_INET6);
if (hostent->h_addrtype != AF_INET6)
bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
#ifdef ICMP6_FILTER
{
struct icmp6_filter filt;
if (!(options & O_VERBOSE)) {
ICMP6_FILTER_SETBLOCKALL(&filt);
#if 0
if ((options & F_FQDN) || (options & F_FQDNOLD) ||
(options & F_NODEADDR) || (options & F_SUPTYPES))
ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
else
#endif
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
} else {
ICMP6_FILTER_SETPASSALL(&filt);
}
if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
sizeof(filt)) < 0)
bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
}
#endif /*ICMP6_FILTER*/
/* enable broadcast pings */
sockopt = 1;
setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
sizeof(sockopt));
/* set recv buf for broadcast pings */
sockopt = 48 * 1024;
setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
sizeof(sockopt));
sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
sizeof(sockopt));
sockopt = 1;
setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
sizeof(sockopt));
if (ifname) {
if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
bb_error_msg_and_die("%s: invalid interface name", ifname);
}
printf("PING %s (%s): %d data bytes\n",
hostent->h_name,
inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
buf, sizeof(buf)),
datalen);
signal(SIGINT, pingstats);
/* start the ping's going ... */
sendping(0);
/* listen for replies */
msg.msg_name=&from;
msg.msg_namelen=sizeof(from);
msg.msg_iov=&iov;
msg.msg_iovlen=1;
msg.msg_control=control_buf;
iov.iov_base=packet;
iov.iov_len=sizeof(packet);
while (1) {
int c;
struct cmsghdr *cmsgptr = NULL;
int hoplimit=-1;
msg.msg_controllen=sizeof(control_buf);
if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
if (errno == EINTR)
continue;
bb_perror_msg("recvfrom");
continue;
}
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == SOL_IPV6 &&
cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
//.........这里部分代码省略.........
示例2: recv_fd
/*
* Receive a file descriptor from a server process. Also, any data
* receive is passed to (*userfunc)(STDERR_FILENO, buf, nbytes).
* We have a 2-byte protocol for receiving the fd from send_fd().
*/
int recv_fd(int fd, ssize_t (*userfunc)(int, const void *, size_t))
{
struct cmsghdr *cmp;
struct CREDSTRUCT *credp;
int newfd, nr, status;
char *ptr;
char buf[MAXLINE];
struct iovec iov[1];
struct msghdr msg;
const int on = 1;
status = -1;
newfd = -1;
if (setsockopt(fd, SOL_SOCKET, &on, sizeof(int)) < 0) {
err_ret("setsockopt failed");
return(-1);
}
for ( ; ; ) {
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
if (cmptr == NULL && (cmptr = malloc(CONTROLEN)) == NULL)
return(-1);
msg.msg_control = cmptr;
msg.msg_controllen = CONTROLEN;
if ((nr = recvmsg(fd, &msg, 0)) < 0) {
err_sys("recvmsg error");
} else if (nr == 0) {
err_ret("connection closed by server");
return(-1);
}
/*
* See if this is the final data with null & status. NULL
* is next to last byte of buffer; status byte is last byte.
* Zero status means there is a file descriptor to receive.
*/
for (ptr = buf; ptr < &buf[nr]; ) {
if (*ptr++ >= 0) {
if (ptr != &buf[n-1])
err_dump("message format error");
status = *ptr & 0xFF; /* prevent sign extension */
if (status == 0) {
if (msg.msg_controllen != CONTROLLEN)
err_dump("status = 0 but no fd");
/* process the control data */
for (cmp = CMSG_FIRSTHDR(&msg);
cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp)) {
if (cmp->cmsg_level != SOL_SOCKET)
continue;
switch (cmp->cmsg_type) {
case SCM_RIGHTS:
newfd = *(int *)CMSG_DATA(cmp);
break;
case SCM_CREDTYPE:
credp = (struct CREDSTRUCT *)CMSG_DATA(cmp);
*uidptr = credp->CR_UID;
}
}
} else {
newfd = -status;
}
nr -= 2;
}
}
if (nr > 0 && (*userfunc)(STDERR_FILENO, buf, nr) != nr)
return(-1);
if (status >= 0) /* final data has arrived */
return(newfd);
}
}
示例3: recvfromto
//.........这里部分代码省略.........
#if !defined(IP_PKTINFO) && !defined(IP_RECVDSTADDR)
return recvfrom(s, buf, len, flags, from, fromlen);
#else
struct sockaddr_in *dst = (struct sockaddr_in *) to;
struct sockaddr_in *src = (struct sockaddr_in *) &si;
if (*tolen < sizeof(*dst)) {
errno = EINVAL;
return -1;
}
*tolen = sizeof(*dst);
*dst = *src;
#endif
}
#ifdef AF_INET6
else if (si.ss_family == AF_INET6) {
#if !defined(IPV6_PKTINFO)
return recvfrom(s, buf, len, flags, from, fromlen);
#else
struct sockaddr_in6 *dst = (struct sockaddr_in6 *) to;
struct sockaddr_in6 *src = (struct sockaddr_in6 *) &si;
if (*tolen < sizeof(*dst)) {
errno = EINVAL;
return -1;
}
*tolen = sizeof(*dst);
*dst = *src;
#endif
}
#endif
/*
* Unknown address family.
*/
else {
errno = EINVAL;
return -1;
}
/* Set up iov and msgh structures. */
memset(&cbuf, 0, sizeof(cbuf));
memset(&msgh, 0, sizeof(struct msghdr));
iov.iov_base = buf;
iov.iov_len = len;
msgh.msg_control = cbuf;
msgh.msg_controllen = sizeof(cbuf);
msgh.msg_name = from;
msgh.msg_namelen = fromlen ? *fromlen : 0;
msgh.msg_iov = &iov;
msgh.msg_iovlen = 1;
msgh.msg_flags = 0;
/* Receive one packet. */
if ((err = recvmsg(s, &msgh, flags)) < 0) {
return err;
}
if (fromlen) *fromlen = msgh.msg_namelen;
/* Process auxiliary received data in msgh */
for (cmsg = CMSG_FIRSTHDR(&msgh);
cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
#ifdef IP_PKTINFO
if ((cmsg->cmsg_level == SOL_IP) &&
(cmsg->cmsg_type == IP_PKTINFO)) {
struct in_pktinfo *i =
(struct in_pktinfo *) CMSG_DATA(cmsg);
((struct sockaddr_in *)to)->sin_addr = i->ipi_addr;
*tolen = sizeof(struct sockaddr_in);
break;
}
#endif
#ifdef IP_RECVDSTADDR
if ((cmsg->cmsg_level == IPPROTO_IP) &&
(cmsg->cmsg_type == IP_RECVDSTADDR)) {
struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
((struct sockaddr_in *)to)->sin_addr = *i;
*tolen = sizeof(struct sockaddr_in);
break;
}
#endif
#ifdef IPV6_PKTINFO
if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
(cmsg->cmsg_type == IPV6_PKTINFO)) {
struct in6_pktinfo *i =
(struct in6_pktinfo *) CMSG_DATA(cmsg);
((struct sockaddr_in6 *)to)->sin6_addr = i->ipi6_addr;
*tolen = sizeof(struct sockaddr_in6);
break;
}
#endif
}
return err;
}
示例4: uv__write
/* On success returns NULL. On error returns a pointer to the write request
* which had the error.
*/
static void uv__write(uv_stream_t* stream) {
uv_write_t* req;
struct iovec* iov;
int iovcnt;
ssize_t n;
if (stream->flags & UV_CLOSING) {
/* Handle was closed this tick. We've received a stale
* 'is writable' callback from the event loop, ignore.
*/
return;
}
start:
assert(stream->fd >= 0);
/* Get the request at the head of the queue. */
req = uv_write_queue_head(stream);
if (!req) {
assert(stream->write_queue_size == 0);
return;
}
assert(req->handle == stream);
/*
* Cast to iovec. We had to have our own uv_buf_t instead of iovec
* because Windows's WSABUF is not an iovec.
*/
assert(sizeof(uv_buf_t) == sizeof(struct iovec));
iov = (struct iovec*) &(req->bufs[req->write_index]);
iovcnt = req->bufcnt - req->write_index;
/*
* Now do the actual writev. Note that we've been updating the pointers
* inside the iov each time we write. So there is no need to offset it.
*/
if (req->send_handle) {
struct msghdr msg;
char scratch[64];
struct cmsghdr *cmsg;
int fd_to_send = req->send_handle->fd;
assert(fd_to_send >= 0);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
msg.msg_flags = 0;
msg.msg_control = (void*) scratch;
msg.msg_controllen = CMSG_LEN(sizeof(fd_to_send));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = msg.msg_controllen;
*(int*) CMSG_DATA(cmsg) = fd_to_send;
do {
n = sendmsg(stream->fd, &msg, 0);
}
while (n == -1 && errno == EINTR);
} else {
do {
if (iovcnt == 1) {
n = write(stream->fd, iov[0].iov_base, iov[0].iov_len);
} else {
n = writev(stream->fd, iov, iovcnt);
}
}
while (n == -1 && errno == EINTR);
}
if (n < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
/* Error */
req->error = errno;
stream->write_queue_size -= uv__write_req_size(req);
uv__write_req_finish(req);
return;
} else if (stream->flags & UV_STREAM_BLOCKING) {
/* If this is a blocking stream, try again. */
goto start;
}
} else {
/* Successful write */
/* Update the counters. */
while (n >= 0) {
uv_buf_t* buf = &(req->bufs[req->write_index]);
size_t len = buf->len;
assert(req->write_index < req->bufcnt);
//.........这里部分代码省略.........
示例5: mm_receive_fd
int
mm_receive_fd(int sock)
{
#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
struct msghdr msg;
#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
struct cmsghdr *cmsg;
#endif
struct iovec vec;
ssize_t n;
char ch;
int fd;
struct pollfd pfd;
memset(&msg, 0, sizeof(msg));
vec.iov_base = &ch;
vec.iov_len = 1;
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
msg.msg_accrights = (caddr_t)&fd;
msg.msg_accrightslen = sizeof(fd);
#else
memset(&cmsgbuf, 0, sizeof(cmsgbuf));
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
#endif
pfd.fd = sock;
pfd.events = POLLIN;
while ((n = recvmsg(sock, &msg, 0)) == -1 &&
(errno == EAGAIN || errno == EINTR)) {
debug3("%s: recvmsg: %s", __func__, strerror(errno));
(void)poll(&pfd, 1, -1);
}
if (n == -1) {
error("%s: recvmsg: %s", __func__, strerror(errno));
return -1;
}
if (n != 1) {
error("%s: recvmsg: expected received 1 got %ld",
__func__, (long)n);
return -1;
}
#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
if (msg.msg_accrightslen != sizeof(fd)) {
error("%s: no fd", __func__);
return -1;
}
#else
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL) {
error("%s: no message header", __func__);
return -1;
}
#ifndef BROKEN_CMSG_TYPE
if (cmsg->cmsg_type != SCM_RIGHTS) {
error("%s: expected type %d got %d", __func__,
SCM_RIGHTS, cmsg->cmsg_type);
return -1;
}
#endif
fd = (*(int *)CMSG_DATA(cmsg));
#endif
return fd;
#else
error("%s: file descriptor passing not supported", __func__);
return -1;
#endif
}
示例6: data_callback
static void data_callback(int fd, uint32_t events, void *user_data)
{
struct control_data *data = user_data;
unsigned char control[64];
struct mgmt_hdr hdr;
struct msghdr msg;
struct iovec iov[2];
if (events & (EPOLLERR | EPOLLHUP)) {
mainloop_remove_fd(data->fd);
return;
}
iov[0].iov_base = &hdr;
iov[0].iov_len = MGMT_HDR_SIZE;
iov[1].iov_base = data->buf;
iov[1].iov_len = sizeof(data->buf);
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 2;
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
while (1) {
struct cmsghdr *cmsg;
struct timeval *tv = NULL;
struct timeval ctv;
struct ucred *cred = NULL;
struct ucred ccred;
uint16_t opcode, index, pktlen;
ssize_t len;
len = recvmsg(data->fd, &msg, MSG_DONTWAIT);
if (len < 0)
break;
if (len < MGMT_HDR_SIZE)
break;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET)
continue;
if (cmsg->cmsg_type == SCM_TIMESTAMP) {
memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
tv = &ctv;
}
if (cmsg->cmsg_type == SCM_CREDENTIALS) {
memcpy(&ccred, CMSG_DATA(cmsg), sizeof(ccred));
cred = &ccred;
}
}
opcode = le16_to_cpu(hdr.opcode);
index = le16_to_cpu(hdr.index);
pktlen = le16_to_cpu(hdr.len);
switch (data->channel) {
case HCI_CHANNEL_CONTROL:
packet_control(tv, cred, index, opcode,
data->buf, pktlen);
break;
case HCI_CHANNEL_MONITOR:
btsnoop_write_hci(btsnoop_file, tv, index, opcode,
data->buf, pktlen);
ellisys_inject_hci(tv, index, opcode,
data->buf, pktlen);
packet_monitor(tv, cred, index, opcode,
data->buf, pktlen);
break;
}
}
}
示例7: send_from
/* Send a UDP packet with its source address set as "source"
unless nowild is true, when we just send it with the kernel default */
int send_from(int fd, int nowild, char *packet, size_t len,
union mysockaddr *to, struct all_addr *source,
unsigned int iface, int *errp)
{
struct msghdr msg;
struct iovec iov[1];
union {
struct cmsghdr align; /* this ensures alignment */
#if defined(HAVE_LINUX_NETWORK)
char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
#elif defined(IP_SENDSRCADDR)
char control[CMSG_SPACE(sizeof(struct in_addr))];
#endif
#ifdef HAVE_IPV6
char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
#endif
} control_u;
iov[0].iov_base = packet;
iov[0].iov_len = len;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
msg.msg_name = to;
msg.msg_namelen = sa_len(to);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if (!nowild)
{
struct cmsghdr *cmptr;
msg.msg_control = &control_u;
msg.msg_controllen = sizeof(control_u);
cmptr = CMSG_FIRSTHDR(&msg);
if (to->sa.sa_family == AF_INET)
{
#if defined(HAVE_LINUX_NETWORK)
struct in_pktinfo p;
p.ipi_ifindex = 0;
p.ipi_spec_dst = source->addr.addr4;
memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
cmptr->cmsg_level = IPPROTO_IP;
cmptr->cmsg_type = IP_PKTINFO;
#elif defined(IP_SENDSRCADDR)
memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
cmptr->cmsg_level = IPPROTO_IP;
cmptr->cmsg_type = IP_SENDSRCADDR;
#endif
}
else
#ifdef HAVE_IPV6
{
struct in6_pktinfo p;
p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */
p.ipi6_addr = source->addr.addr6;
memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
cmptr->cmsg_type = daemon->v6pktinfo;
cmptr->cmsg_level = IPPROTO_IPV6;
}
#else
(void)iface; /* eliminate warning */
#endif
}
retry:
if (sendmsg(fd, &msg, 0) == -1)
{
/* certain Linux kernels seem to object to setting the source address in the IPv6 stack
by returning EINVAL from sendmsg. In that case, try again without setting the
source address, since it will nearly alway be correct anyway. IPv6 stinks. */
if (errno == EINVAL && msg.msg_controllen)
{
msg.msg_controllen = 0;
goto retry;
}
if (retry_send())
goto retry;
if (errp)
*errp = errno;
else
my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
return 0;
}
return 1;
}
示例8: jdwp_process_event
//.........这里部分代码省略.........
if (len <= 0) {
if (len < 0 && errno == EINTR)
continue;
if (len < 0 && errno == EAGAIN)
return;
else {
D("terminating JDWP %d connection: %s\n", proc->pid,
strerror(errno));
break;
}
}
else {
D( "ignoring unexpected JDWP %d control socket activity (%d bytes)\n",
proc->pid, len );
}
}
CloseProcess:
if (proc->pid >= 0)
D( "remove pid %d to jdwp process list\n", proc->pid );
jdwp_process_free(proc);
return;
}
}
if (events & FDE_WRITE) {
D("trying to write to JDWP pid controli (count=%d first=%d) %d\n",
proc->pid, proc->out_count, proc->out_fds[0]);
if (proc->out_count > 0) {
int fd = proc->out_fds[0];
int n, ret;
struct cmsghdr* cmsg;
struct msghdr msg;
struct iovec iov;
char dummy = '!';
char buffer[sizeof(struct cmsghdr) + sizeof(int)];
int flags;
iov.iov_base = &dummy;
iov.iov_len = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = buffer;
msg.msg_controllen = sizeof(buffer);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = msg.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
((int*)CMSG_DATA(cmsg))[0] = fd;
flags = fcntl(proc->socket,F_GETFL,0);
if (flags == -1) {
D("failed to get cntl flags for socket %d: %s\n",
proc->pid, strerror(errno));
goto CloseProcess;
}
if (fcntl(proc->socket, F_SETFL, flags & ~O_NONBLOCK) == -1) {
D("failed to remove O_NONBLOCK flag for socket %d: %s\n",
proc->pid, strerror(errno));
goto CloseProcess;
}
for (;;) {
ret = sendmsg(proc->socket, &msg, 0);
if (ret >= 0) {
adb_close(fd);
break;
}
if (errno == EINTR)
continue;
D("sending new file descriptor to JDWP %d failed: %s\n",
proc->pid, strerror(errno));
goto CloseProcess;
}
D("sent file descriptor %d to JDWP process %d\n",
fd, proc->pid);
for (n = 1; n < proc->out_count; n++)
proc->out_fds[n-1] = proc->out_fds[n];
if (fcntl(proc->socket, F_SETFL, flags) == -1) {
D("failed to set O_NONBLOCK flag for socket %d: %s\n",
proc->pid, strerror(errno));
goto CloseProcess;
}
if (--proc->out_count == 0)
fdevent_del( proc->fde, FDE_WRITE );
}
}
}
示例9: arcan_pushhandle
bool arcan_pushhandle(file_handle source, int channel)
{
char empty = '!';
struct cmsgbuf {
struct cmsghdr hdr;
int fd[1];
} msgbuf;
struct iovec nothing_ptr = {
.iov_base = &empty,
.iov_len = 1
};
struct msghdr msg = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = ¬hing_ptr,
.msg_iovlen = 1,
.msg_flags = 0,
};
if (-1 == source){
}
else {
msg.msg_control = &msgbuf;
msg.msg_controllen = CMSG_LEN(sizeof(int));
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = msg.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
int* dptr = (int*) CMSG_DATA(cmsg);
*dptr = source;
}
#ifdef __APPLE__
int set = 1;
setsockopt(channel, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
#endif
int rv = sendmsg(channel, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
return rv >= 0;
}
file_handle arcan_fetchhandle(int sockin_fd, bool block)
{
if (sockin_fd == -1)
return -1;
char empty;
struct cmsgbuf {
struct cmsghdr hdr;
int fd[1];
} msgbuf;
struct iovec nothing_ptr = {
.iov_base = &empty,
.iov_len = 1
};
struct msghdr msg = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = ¬hing_ptr,
.msg_iovlen = 1,
.msg_flags = 0,
.msg_control = &msgbuf,
.msg_controllen = sizeof(struct cmsghdr) + sizeof(int)
};
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = msg.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
int* dfd = (int*) CMSG_DATA(cmsg);
*dfd = -1;
if (-1 == recvmsg(sockin_fd, &msg,
(!block ? MSG_DONTWAIT : 0)| MSG_NOSIGNAL))
;
int nd = msgbuf.fd[0];
return nd;
}
示例10: main
//.........这里部分代码省略.........
return (EXIT_FAILURE);
}
// Set the ICMPv6 ns header option.
option[0] = 1; // Option Type - "source link layer address" (Section 4.6 of RFC 4861)
option[1] = 8 / 8; // Option Length - units of 8 octets (RFC 4861)
for (i=0; i<6; i++) {
option[i+2] = (uint8_t) ifr.ifr_hwaddr.sa_data[i];
}
printf ("Hardware address is %02X:%02X:%02X:%02X:%02X:%02X\n", option[2], option[3], option[4], option[5], option[6], option[7]);
// Use ioctl() to find interface index.
memset (&ifr, 0, sizeof(struct ifreq));
snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), "%s", argv[1]);
if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
perror ("ioctl() failed to find interface ");
return (EXIT_FAILURE);
}
printf ("Index for interface %s is %i\n", argv[1], ifr.ifr_ifindex);
// Close sd.
close (sd);
// Create a ICMPv6 raw socket.
// The kernel will fill the IPv6 header automatically.
if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
perror ("socket() ");
exit (EXIT_FAILURE);
}
// Convert the hostname to network bytes.
if (inet_pton (AF_INET6, argv[2], &(dst.sin6_addr)) < 0) {
perror ("inet_pton() ");
exit (EXIT_FAILURE);
}
// Bind the interface using setsockopt()
if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(struct ifreq)) < 0) {
perror ("setsockopt() failed to bind interface ");
return (EXIT_FAILURE);
}
// Send ICMPv6 neighbor advertisement.
// 1. Construct ICMPv6 header and payload.
memset (&na, 0, sizeof(na));
// Set header type.
na.nd_na_hdr.icmp6_type = ND_NEIGHBOR_ADVERT; // 136 (RFC 4861)
// Code is 0 for ns.
na.nd_na_hdr.icmp6_code = 0;
// When you create a ICMPv6 raw socket, the kernel will calculate and
// insert the ICMPv6 checksum automatically.
na.nd_na_hdr.icmp6_cksum = htons (0);
// Set R/S/O flags as: R(router)=0, S(Solicited)=1, O(override)=1.
na.nd_na_flags_reserved = htonl ((1 << 30) + (1 << 29));
na.nd_na_target = src.sin6_addr; // Target address (as type in6_addr)
// 2. Assign msghdr's field "msg_name" to destination address.
memset (&msghdr, 0, sizeof (msghdr));
msghdr.msg_name = &dst;
msghdr.msg_namelen = sizeof (dst);
// 3. Assign the packet to the io vector.
iov[0].iov_base = &na;
iov[0].iov_len = sizeof (struct nd_neighbor_advert);
iov[1].iov_base = &option;
iov[1].iov_len = sizeof (option);
msghdr.msg_iov = iov;
msghdr.msg_iovlen = 2;
// 4. Set the TTL in cmsghdr.
memset (&cmsghdr_buf, 0, sizeof (cmsghdr_buf));
msghdr.msg_control = cmsghdr_buf;
msghdr.msg_controllen = sizeof (cmsghdr_buf);
// Set the time-to-live value 255.
cmsghdr = CMSG_FIRSTHDR (&msghdr);
cmsghdr->cmsg_level = IPPROTO_IPV6;
cmsghdr->cmsg_type = IPV6_HOPLIMIT;
cmsghdr->cmsg_len = CMSG_LEN (sizeof (hoplimit));
*(int *)CMSG_DATA (cmsghdr) = hoplimit;
// 5. Send the message.
if (sendmsg (sd, &msghdr, 0) <= 0) {
perror ("sendmsg() ");
exit (EXIT_FAILURE);
}
// Close sd.
close (sd);
// Use "tcpdump -nnvvvXS -i eth1 -s0 icmp6" to check neighbor advertisement progress.
}
示例11: process_frames
static int process_frames(int dev, int sock, int fd, unsigned long flags)
{
struct cmsghdr *cmsg;
struct msghdr msg;
struct iovec iv;
struct hcidump_hdr *dh;
struct btsnoop_pkt *dp;
struct frame frm;
struct pollfd fds[2];
int nfds = 0;
char *buf, *ctrl;
int len, hdr_size = HCIDUMP_HDR_SIZE;
if (sock < 0)
return -1;
if (snap_len < SNAP_LEN)
snap_len = SNAP_LEN;
if (flags & DUMP_BTSNOOP)
hdr_size = BTSNOOP_PKT_SIZE;
buf = malloc(snap_len + hdr_size);
if (!buf) {
perror("Can't allocate data buffer");
return -1;
}
dh = (void *) buf;
dp = (void *) buf;
frm.data = buf + hdr_size;
ctrl = malloc(100);
if (!ctrl) {
free(buf);
perror("Can't allocate control buffer");
return -1;
}
if (dev == HCI_DEV_NONE)
printf("system: ");
else
printf("device: hci%d ", dev);
printf("snap_len: %d filter: 0x%lx\n", snap_len, parser.filter);
memset(&msg, 0, sizeof(msg));
fds[nfds].fd = sock;
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
while (1) {
int i, n = poll(fds, nfds, -1);
if (n <= 0)
continue;
for (i = 0; i < nfds; i++) {
if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
if (fds[i].fd == sock)
printf("device: disconnected\n");
else
printf("client: disconnect\n");
return 0;
}
}
iv.iov_base = frm.data;
iv.iov_len = snap_len;
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = 100;
len = recvmsg(sock, &msg, MSG_DONTWAIT);
if (len < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
perror("Receive failed");
return -1;
}
/* Process control message */
frm.data_len = len;
frm.dev_id = dev;
frm.in = 0;
frm.pppdump_fd = parser.pppdump_fd;
frm.audio_fd = parser.audio_fd;
cmsg = CMSG_FIRSTHDR(&msg);
while (cmsg) {
int dir;
switch (cmsg->cmsg_type) {
case HCI_CMSG_DIR:
memcpy(&dir, CMSG_DATA(cmsg), sizeof(int));
frm.in = (uint8_t) dir;
break;
case HCI_CMSG_TSTAMP:
//.........这里部分代码省略.........
示例12: vnode_recvmsg
/*
* return the number of bytes received
* return 0 if the message should be ignored
* return a negative value if i/o should stop
*/
ssize_t vnode_recvmsg(vnode_msgio_t *msgio)
{
ssize_t recvlen;
struct msghdr msg = {};
struct iovec iov[1];
char buf[CMSG_SPACE(3 * sizeof(int))];
struct cmsghdr *cmsg;
if (msgio->msgbuf.msgbufsize < VNODE_MSGSIZMAX)
{
if (vnode_resizemsgbuf(&msgio->msgbuf, VNODE_MSGSIZMAX))
return -1;
}
msgio->msgbuf.infd = msgio->msgbuf.outfd = msgio->msgbuf.errfd = -1;
iov[0].iov_base = msgio->msgbuf.msg;
iov[0].iov_len = msgio->msgbuf.msgbufsize;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);
recvlen = recvmsg(msgio->fd, &msg, 0);
if (recvlen == 0)
return -1;
else if (recvlen < 0)
{
if (errno == EAGAIN)
return 0;
WARN("recvmsg() failed");
return -1;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg != NULL && cmsg->cmsg_type == SCM_RIGHTS)
{
int *fdptr;
fdptr = (int *)CMSG_DATA(cmsg);
msgio->msgbuf.infd = fdptr[0];
msgio->msgbuf.outfd = fdptr[1];
msgio->msgbuf.errfd = fdptr[2];
}
if (recvlen < sizeof(msgio->msgbuf.msg->hdr))
{
WARNX("message header truncated: received %d of %d bytes",
recvlen, sizeof(msgio->msgbuf.msg->hdr));
return 0;
}
if (msgio->msgbuf.msg->hdr.type == VNODE_MSG_NONE ||
msgio->msgbuf.msg->hdr.type >= VNODE_MSG_MAX)
{
WARNX("invalid message type: %u", msgio->msgbuf.msg->hdr.type);
return 0;
}
if (recvlen - sizeof(msgio->msgbuf.msg->hdr) !=
msgio->msgbuf.msg->hdr.datalen)
{
WARNX("message length mismatch: received %d bytes; expected %d bytes",
recvlen - sizeof(msgio->msgbuf.msg->hdr),
msgio->msgbuf.msg->hdr.datalen);
return 0;
}
return recvlen;
}
示例13: main_loop
//.........这里部分代码省略.........
int recv_expected = in_flight();
/* If we are here, recvmsg() is unable to wait for
* required timeout. */
if (1000 % HZ == 0 ? next <= 1000 / HZ : (next < INT_MAX / HZ && next * HZ <= 1000)) {
/* Very short timeout... So, if we wait for
* something, we sleep for MININTERVAL.
* Otherwise, spin! */
if (recv_expected) {
next = MININTERVAL;
} else {
next = 0;
/* When spinning, no reasons to poll.
* Use nonblocking recvmsg() instead. */
polling = MSG_DONTWAIT;
/* But yield yet. */
sched_yield();
}
}
if (!polling &&
((options & (F_ADAPTIVE|F_FLOOD_POLL)) || interval)) {
struct pollfd pset;
pset.fd = sock->fd;
pset.events = POLLIN|POLLERR;
pset.revents = 0;
if (poll(&pset, 1, next) < 1 ||
!(pset.revents&(POLLIN|POLLERR)))
continue;
polling = MSG_DONTWAIT;
}
}
for (;;) {
struct timeval *recv_timep = NULL;
struct timeval recv_time;
int not_ours = 0; /* Raw socket can receive messages
* destined to other running pings. */
iov.iov_len = packlen;
memset(&msg, 0, sizeof(msg));
msg.msg_name = addrbuf;
msg.msg_namelen = sizeof(addrbuf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ans_data;
msg.msg_controllen = sizeof(ans_data);
cc = recvmsg(sock->fd, &msg, polling);
polling = MSG_DONTWAIT;
if (cc < 0) {
if (errno == EAGAIN || errno == EINTR)
break;
if (!fset->receive_error_msg(sock)) {
if (errno) {
perror("ping: recvmsg");
break;
}
not_ours = 1;
}
} else {
#ifdef SO_TIMESTAMP
for (c = CMSG_FIRSTHDR(&msg); c; c = CMSG_NXTHDR(&msg, c)) {
if (c->cmsg_level != SOL_SOCKET ||
c->cmsg_type != SO_TIMESTAMP)
continue;
if (c->cmsg_len < CMSG_LEN(sizeof(struct timeval)))
continue;
recv_timep = (struct timeval*)CMSG_DATA(c);
}
#endif
if ((options&F_LATENCY) || recv_timep == NULL) {
if ((options&F_LATENCY) ||
ioctl(sock->fd, SIOCGSTAMP, &recv_time))
gettimeofday(&recv_time, NULL);
recv_timep = &recv_time;
}
not_ours = fset->parse_reply(sock, &msg, cc, addrbuf, recv_timep);
}
/* See? ... someone runs another ping on this host. */
if (not_ours && sock->socktype == SOCK_RAW)
fset->install_filter(sock);
/* If nothing is in flight, "break" returns us to pinger. */
if (in_flight() == 0)
break;
/* Otherwise, try to recvmsg() again. recvmsg()
* is nonblocking after the first iteration, so that
* if nothing is queued, it will receive EAGAIN
* and return to pinger. */
}
}
finish();
}
示例14: receive_query
void receive_query(struct listener *listen, time_t now)
{
struct dns_header *header = (struct dns_header *)daemon->packet;
union mysockaddr source_addr;
unsigned short type;
struct all_addr dst_addr;
struct in_addr netmask, dst_addr_4;
size_t m;
ssize_t n;
int if_index = 0;
struct iovec iov[1];
struct msghdr msg;
struct cmsghdr *cmptr;
union {
struct cmsghdr align; /* this ensures alignment */
#ifdef HAVE_IPV6
char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
#endif
#if defined(HAVE_LINUX_NETWORK)
char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
#elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
char control[CMSG_SPACE(sizeof(struct in_addr)) +
CMSG_SPACE(sizeof(unsigned int))];
#elif defined(IP_RECVDSTADDR)
char control[CMSG_SPACE(sizeof(struct in_addr)) +
CMSG_SPACE(sizeof(struct sockaddr_dl))];
#endif
} control_u;
/* packet buffer overwritten */
daemon->srv_save = NULL;
if (listen->iface && listen->family == AF_INET && option_bool(OPT_NOWILD))
{
dst_addr_4 = listen->iface->addr.in.sin_addr;
netmask = listen->iface->netmask;
}
else
{
dst_addr_4.s_addr = 0;
netmask.s_addr = 0;
}
iov[0].iov_base = daemon->packet;
iov[0].iov_len = daemon->edns_pktsz;
msg.msg_control = control_u.control;
msg.msg_controllen = sizeof(control_u);
msg.msg_flags = 0;
msg.msg_name = &source_addr;
msg.msg_namelen = sizeof(source_addr);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
return;
if (n < (int)sizeof(struct dns_header) ||
(msg.msg_flags & MSG_TRUNC) ||
(header->hb3 & HB3_QR))
return;
source_addr.sa.sa_family = listen->family;
#ifdef HAVE_IPV6
if (listen->family == AF_INET6)
source_addr.in6.sin6_flowinfo = 0;
#endif
if (!option_bool(OPT_NOWILD))
{
struct ifreq ifr;
if (msg.msg_controllen < sizeof(struct cmsghdr))
return;
#if defined(HAVE_LINUX_NETWORK)
if (listen->family == AF_INET)
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
{
union {
unsigned char *c;
struct in_pktinfo *p;
} p;
p.c = CMSG_DATA(cmptr);
dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
if_index = p.p->ipi_ifindex;
}
#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
if (listen->family == AF_INET)
{
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
{
union {
unsigned char *c;
unsigned int *i;
struct in_addr *a;
#ifndef HAVE_SOLARIS_NETWORK
struct sockaddr_dl *s;
#endif
//.........这里部分代码省略.........
示例15: NaClDescConnCapFdConnectAddr
static int NaClDescConnCapFdConnectAddr(struct NaClDesc *vself,
struct NaClDesc **out_desc) {
struct NaClDescConnCapFd *self = (struct NaClDescConnCapFd *) vself;
NaClHandle sock_pair[2];
struct NaClDescImcDesc *connected_socket;
char control_buf[CMSG_SPACE_KHANDLE_COUNT_MAX_INTS];
struct iovec iovec;
struct msghdr connect_msg;
struct cmsghdr *cmsg;
int sent;
int retval;
assert(CMSG_SPACE(sizeof(int)) <= CMSG_SPACE_KHANDLE_COUNT_MAX_INTS);
sock_pair[0] = NACL_INVALID_HANDLE;
sock_pair[1] = NACL_INVALID_HANDLE;
connected_socket = (struct NaClDescImcDesc *) NULL;
retval = -NACL_ABI_EINVAL;
if (0 != NaClSocketPair(sock_pair)) {
retval = -NACL_ABI_EMFILE;
goto cleanup;
}
iovec.iov_base = "c";
iovec.iov_len = 1;
connect_msg.msg_iov = &iovec;
connect_msg.msg_iovlen = 1;
connect_msg.msg_name = NULL;
connect_msg.msg_namelen = 0;
connect_msg.msg_control = control_buf;
connect_msg.msg_controllen = sizeof(control_buf);
connect_msg.msg_flags = 0;
cmsg = CMSG_FIRSTHDR(&connect_msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
/*
* We use memcpy() rather than assignment through a cast to avoid
* strict-aliasing warnings
*/
memcpy(CMSG_DATA(cmsg), &sock_pair[0], sizeof(int));
/* Set msg_controllen to the actual size of the cmsg. */
connect_msg.msg_controllen = cmsg->cmsg_len;
sent = sendmsg(self->connect_fd, &connect_msg, 0);
if (1 != sent) {
retval = -NACL_ABI_EIO;
goto cleanup;
}
if (NACL_OSX) {
/*
* Mac OS X has a kernel bug in which a socket descriptor that is
* referenced only from the message queue of another socket can
* get garbage collected. This causes the socket descriptor not
* to work properly. To work around this, we don't close our
* reference to the socket until we receive an acknowledgement
* that it has been successfully received.
*
* We cannot receive the acknowledgement through self->connect_fd
* because this FD could be shared between multiple processes. So
* we receive the acknowledgement through the socket pair that we
* have just created.
*
* However, this creates a risk that we are left hanging if the
* other process dies after our sendmsg() call, because we are
* holding on to the socket that it would use to send the ack. To
* avoid this problem, we use poll() so that we will be notified
* if self->connect_fd becomes unwritable.
* TODO(mseaborn): Add a test case to simulate that scenario.
*
* See http://code.google.com/p/nativeclient/issues/detail?id=1796
*
* Note that we are relying on a corner case of poll() here.
* Using POLLHUP in "events" is not meaningful on Linux, which is
* documented as ignoring POLLHUP as an input argument and will
* return POLLHUP in "revents" even if it not present in "events".
* On Mac OS X, however, passing events == 0 does not work if we
* want to get POLLHUP. We are in the unusual situation of
* waiting for a socket to become *un*writable.
*/
struct pollfd poll_fds[2];
poll_fds[0].fd = self->connect_fd;
poll_fds[0].events = POLLHUP;
poll_fds[1].fd = sock_pair[1];
poll_fds[1].events = POLLIN;
if (poll(poll_fds, 2, -1) < 0) {
NaClLog(LOG_ERROR,
"NaClDescConnCapFdConnectAddr: poll() failed, errno %d\n", errno);
retval = -NACL_ABI_EIO;
goto cleanup;
}
/*
* It is not necessarily an error if POLLHUP fires on
* self->connect_fd: The other process could have done
* imc_accept(S) and then closed S. This means it will have
* received sock_pair[0] successfully, so we can close our
//.........这里部分代码省略.........