本文整理汇总了C++中CMSG_NXTHDR函数的典型用法代码示例。如果您正苦于以下问题:C++ CMSG_NXTHDR函数的具体用法?C++ CMSG_NXTHDR怎么用?C++ CMSG_NXTHDR使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CMSG_NXTHDR函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rxpacket
void rxpacket(int sock) {
Packet pkt;
memset(&pkt, 0, sizeof(pkt));
// using recvmsg
int size; // size of the received data
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
struct sockaddr_in from;
int fromlen=sizeof(from);
msg.msg_name = &from;
msg.msg_namelen = fromlen;
char anciliary[2048];
msg.msg_control = anciliary;
msg.msg_controllen = sizeof(anciliary);
struct iovec iov[1];
memset(iov, 0, sizeof(iov));
iov[0].iov_base = &pkt.data;
iov[0].iov_len = sizeof(pkt.data);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
struct cmsghdr *cmsg;
unsigned int ifindex; // interface index
struct in_addr hdraddr; // destination IP address in IP header
size = recvmsg(sock, &msg, 0);
if (size == -1) {
ASSERT(0);
rcpLog(muxsock, RCP_PROC_RIP, RLOG_ERR, RLOG_FC_RIP,
"cannot read data on socket, attempting recovery...");
exit(1);
}
// verify packet size
int sz = size - 4;
if (sz<= 0 || (sz % sizeof(RipRoute)) != 0) {
rcpLog(muxsock, RCP_PROC_RIP, RLOG_DEBUG, RLOG_FC_RIP,
"Invalid RIP packet size");
return;
}
int routes = sz / sizeof(RipRoute);
int found = 0;
for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
// struct in_pktinfo {
// unsigned int ipi_ifindex; /* Interface index */
// struct in_addr ipi_spec_dst; /* Local address */
// struct in_addr ipi_addr; /* Header Destination
// address */
// };
hdraddr = ((struct in_pktinfo*)CMSG_DATA(cmsg))->ipi_addr;
ifindex = ((struct in_pktinfo*)CMSG_DATA(cmsg))->ipi_ifindex;
found = 1;
}
}
if (!found)
return;
pkt.ip_source = ntohl(from.sin_addr.s_addr);
pkt.ip_dest = ntohl(hdraddr.s_addr);
pkt.if_index = ifindex;
// is the source ip address one of our addresses?
RcpInterface *rxif = rcpFindInterface(shm, pkt.ip_source);
if (rxif) {
// on Linux, packets we send to the multicast group will be received back on the socket
return;
}
char *cmd[] = {"", "request", "response"};
rcpLog(muxsock, RCP_PROC_RIP, RLOG_DEBUG, RLOG_FC_RIP,
"Receiving RIP packet of size %d, from %d.%d.%d.%d, destination %d.%d.%d.%d, RIP %s, protocol version %d",
size,
RCP_PRINT_IP(pkt.ip_source),
RCP_PRINT_IP(pkt.ip_dest),
cmd[(pkt.data.command <= 2) ? pkt.data.command: 0],
pkt.data.version);
// update neighbor list
RipNeighbor *neigh = neighbors;
while (neigh != NULL) {
if (neigh->ip == pkt.ip_source) {
neigh->rx_time = 0;
break;
}
neigh = neigh->next;
}
if (neigh == NULL) {
RipNeighbor *newneigh = malloc(sizeof(RipNeighbor));
if (newneigh != NULL) {
memset(newneigh, 0, sizeof(RipNeighbor));
newneigh->ip = pkt.ip_source;
newneigh->next = neighbors;
neighbors = newneigh;
neigh = newneigh;
}
//.........这里部分代码省略.........
示例2: imsg_read
ssize_t
imsg_read(struct imsgbuf *ibuf)
{
struct msghdr msg;
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int) * 1)];
} cmsgbuf;
struct iovec iov;
ssize_t n = -1;
int fd;
struct imsg_fd *ifd;
memset(&msg, 0, sizeof(msg));
memset(&cmsgbuf, 0, sizeof(cmsgbuf));
iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
return (-1);
again:
if (available_fds(imsg_fd_overhead +
(CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))) {
errno = EAGAIN;
free(ifd);
return (-1);
}
if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
if (errno == EMSGSIZE)
goto fail;
if (errno != EINTR && errno != EAGAIN)
goto fail;
goto again;
}
ibuf->r.wpos += n;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
int i;
int j;
/*
* We only accept one file descriptor. Due to C
* padding rules, our control buffer might contain
* more than one fd, and we must close them.
*/
j = ((char *)cmsg + cmsg->cmsg_len -
(char *)CMSG_DATA(cmsg)) / sizeof(int);
for (i = 0; i < j; i++) {
fd = ((int *)CMSG_DATA(cmsg))[i];
if (ifd != NULL) {
ifd->fd = fd;
TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
entry);
ifd = NULL;
} else
close(fd);
}
}
/* we do not handle other ctl data level */
}
fail:
if (ifd)
free(ifd);
return (n);
}
示例3: recvfromto
/*
* Receive packet, with src/dst information. It is assumed that necessary
* setsockopt() have already performed on socket.
*/
int
recvfromto(int s, void *buf, size_t buflen, int flags,
struct sockaddr *from, int *fromlen, struct sockaddr *to, int *tolen)
{
int otolen;
int len;
socklen_t sslen;
struct sockaddr_storage ss;
struct msghdr m;
struct cmsghdr *cm;
struct iovec iov[2];
unsigned char cmsgbuf[256];
#if defined(INET6) && defined(ADVAPI)
struct in6_pktinfo *pi;
#endif /*ADVAPI*/
#ifdef INET6
struct sockaddr_in6 *sin6;
#endif
sslen = sizeof(ss);
if (getsockname(s, (struct sockaddr *)&ss, &sslen) < 0) {
plog(PLOG_INTERR, PLOGLOC, NULL,
"getsockname (%s)\n", strerror(errno));
return -1;
}
#if 1
/* quick hack */
memcpy(to, &ss, sslen < *tolen ? sslen : *tolen);
#endif
m.msg_name = (caddr_t)from;
m.msg_namelen = *fromlen;
iov[0].iov_base = (caddr_t)buf;
iov[0].iov_len = buflen;
m.msg_iov = iov;
m.msg_iovlen = 1;
memset(cmsgbuf, 0, sizeof(cmsgbuf));
cm = (struct cmsghdr *)cmsgbuf;
m.msg_control = (caddr_t)cm;
m.msg_controllen = sizeof(cmsgbuf);
if ((len = recvmsg(s, &m, flags)) < 0) {
plog(PLOG_INTERR, PLOGLOC, NULL,
"recvmsg (%s)\n", strerror(errno));
return -1;
}
*fromlen = m.msg_namelen;
otolen = *tolen;
*tolen = 0;
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
m.msg_controllen != 0 && cm;
cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
#if 1
plog(PLOG_DEBUG, PLOGLOC, NULL,
"cmsg %d %d\n", cm->cmsg_level, cm->cmsg_type);
#endif
#if defined(INET6) && defined(ADVAPI)
if (SOCKADDR_FAMILY(&ss) == AF_INET6
&& cm->cmsg_level == IPPROTO_IPV6
&& cm->cmsg_type == IPV6_PKTINFO
&& otolen >= (int)sizeof(*sin6)) {
pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
*tolen = sizeof(*sin6);
sin6 = (struct sockaddr_in6 *)to;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
SET_SOCKADDR_LEN(sin6, sizeof(*sin6));
memcpy(&sin6->sin6_addr, &pi->ipi6_addr,
sizeof(sin6->sin6_addr));
/* XXX other cases, such as site-local? */
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
sin6->sin6_scope_id = pi->ipi6_ifindex;
else
sin6->sin6_scope_id = 0;
sin6->sin6_port =
((struct sockaddr_in6 *)&ss)->sin6_port;
otolen = -1; /* "to" already set */
continue;
}
#endif
#if defined(INET6) && defined(IPV6_RECVDSTADDR)
if (ss.ss_family == AF_INET6
&& cm->cmsg_level == IPPROTO_IPV6
&& cm->cmsg_type == IPV6_RECVDSTADDR
&& otolen >= sizeof(*sin6)) {
*tolen = sizeof(*sin6);
sin6 = (struct sockaddr_in6 *)to;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
memcpy(&sin6->sin6_addr, CMSG_DATA(cm),
sizeof(sin6->sin6_addr));
sin6->sin6_port =
((struct sockaddr_in6 *)&ss)->sin6_port;
otolen = -1; /* "to" already set */
continue;
//.........这里部分代码省略.........
示例4: afalg_start_cipher_sk
static int afalg_start_cipher_sk(afalg_ctx *actx, const unsigned char *in,
size_t inl, const unsigned char *iv,
unsigned int enc)
{
struct msghdr msg = { 0 };
struct cmsghdr *cmsg;
struct iovec iov;
ssize_t sbytes;
# ifdef ALG_ZERO_COPY
int ret;
# endif
char cbuf[CMSG_SPACE(ALG_IV_LEN(ALG_AES_IV_LEN)) + CMSG_SPACE(ALG_OP_LEN)];
memset(cbuf, 0, sizeof(cbuf));
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
/*
* cipher direction (i.e. encrypt or decrypt) and iv are sent to the
* kernel as part of sendmsg()'s ancillary data
*/
cmsg = CMSG_FIRSTHDR(&msg);
afalg_set_op_sk(cmsg, enc);
cmsg = CMSG_NXTHDR(&msg, cmsg);
afalg_set_iv_sk(cmsg, iv, ALG_AES_IV_LEN);
/* iov that describes input data */
iov.iov_base = (unsigned char *)in;
iov.iov_len = inl;
msg.msg_flags = MSG_MORE;
# ifdef ALG_ZERO_COPY
/*
* ZERO_COPY mode
* Works best when buffer is 4k aligned
* OPENS: out of place processing (i.e. out != in)
*/
/* Input data is not sent as part of call to sendmsg() */
msg.msg_iovlen = 0;
msg.msg_iov = NULL;
/* Sendmsg() sends iv and cipher direction to the kernel */
sbytes = sendmsg(actx->sfd, &msg, 0);
if (sbytes < 0) {
ALG_PERR("%s: sendmsg failed for zero copy cipher operation : ",
__func__);
return 0;
}
/*
* vmsplice and splice are used to pin the user space input buffer for
* kernel space processing avoiding copys from user to kernel space
*/
ret = vmsplice(actx->zc_pipe[1], &iov, 1, SPLICE_F_GIFT);
if (ret < 0) {
ALG_PERR("%s: vmsplice failed : ", __func__);
return 0;
}
ret = splice(actx->zc_pipe[0], NULL, actx->sfd, NULL, inl, 0);
if (ret < 0) {
ALG_PERR("%s: splice failed : ", __func__);
return 0;
}
# else
msg.msg_iovlen = 1;
msg.msg_iov = &iov;
/* Sendmsg() sends iv, cipher direction and input data to the kernel */
sbytes = sendmsg(actx->sfd, &msg, 0);
if (sbytes < 0) {
ALG_PERR("%s: sendmsg failed for cipher operation : ", __func__);
return 0;
}
if (sbytes != (ssize_t) inl) {
ALG_WARN("Cipher operation send bytes %zd != inlen %zd\n", sbytes,
inl);
return 0;
}
# endif
return 1;
}
示例5: red_recv_udp_pkt
int red_recv_udp_pkt(int fd, char *buf, size_t buflen, struct sockaddr_in *inaddr, struct sockaddr_in *toaddr)
{
socklen_t addrlen = sizeof(*inaddr);
ssize_t pktlen;
struct msghdr msg;
struct iovec io;
char control[1024];
memset(&msg, 0, sizeof(msg));
msg.msg_name = inaddr;
msg.msg_namelen = sizeof(*inaddr);
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
io.iov_base = buf;
io.iov_len = buflen;
pktlen = recvmsg(fd, &msg, 0);
if (pktlen == -1) {
log_errno(LOG_WARNING, "recvfrom");
return -1;
}
if (toaddr) {
memset(toaddr, 0, sizeof(*toaddr));
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (
cmsg->cmsg_level == SOL_IP &&
cmsg->cmsg_type == IP_ORIGDSTADDR &&
cmsg->cmsg_len >= CMSG_LEN(sizeof(*toaddr))
) {
struct sockaddr_in* cmsgaddr = (struct sockaddr_in*)CMSG_DATA(cmsg);
//char buf[RED_INET_ADDRSTRLEN];
//log_error(LOG_DEBUG, "IP_ORIGDSTADDR: %s", red_inet_ntop(cmsgaddr, buf, sizeof(buf)));
memcpy(toaddr, cmsgaddr, sizeof(*toaddr));
}
else {
log_error(LOG_WARNING, "unexepcted cmsg (level,type) = (%d,%d)",
cmsg->cmsg_level, cmsg->cmsg_type);
}
}
if (toaddr->sin_family != AF_INET) {
log_error(LOG_WARNING, "(SOL_IP, IP_ORIGDSTADDR) not found");
return -1;
}
}
if (addrlen != sizeof(*inaddr)) {
log_error(LOG_WARNING, "unexpected address length %u instead of %zu", addrlen, sizeof(*inaddr));
return -1;
}
if (pktlen >= buflen) {
char buf[RED_INET_ADDRSTRLEN];
log_error(LOG_WARNING, "wow! Truncated udp packet of size %zd from %s! impossible! dropping it...",
pktlen, red_inet_ntop(inaddr, buf, sizeof(buf)));
return -1;
}
return pktlen;
}
示例6: bt_monitor_read
static int
bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
{
struct cmsghdr *cmsg;
struct msghdr msg;
struct iovec iv[2];
ssize_t ret;
struct pcap_pkthdr pkth;
pcap_bluetooth_linux_monitor_header *bthdr;
struct mgmt_hdr hdr;
bthdr = (pcap_bluetooth_linux_monitor_header*) &handle->buffer[handle->offset];
iv[0].iov_base = &hdr;
iv[0].iov_len = MGMT_HDR_SIZE;
iv[1].iov_base = &handle->buffer[handle->offset + sizeof(pcap_bluetooth_linux_monitor_header)];
iv[1].iov_len = handle->snapshot;
memset(&pkth.ts, 0, sizeof(pkth.ts));
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iv;
msg.msg_iovlen = 2;
msg.msg_control = handle->buffer;
msg.msg_controllen = handle->offset;
do {
ret = recvmsg(handle->fd, &msg, 0);
if (handle->break_loop)
{
handle->break_loop = 0;
return -2;
}
} while ((ret == -1) && (errno == EINTR));
if (ret < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't receive packet: %s", strerror(errno));
return -1;
}
pkth.caplen = ret - MGMT_HDR_SIZE + sizeof(pcap_bluetooth_linux_monitor_header);
pkth.len = pkth.caplen;
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(&pkth.ts, CMSG_DATA(cmsg), sizeof(pkth.ts));
}
}
bthdr->adapter_id = htons(hdr.index);
bthdr->opcode = htons(hdr.opcode);
if (handle->fcode.bf_insns == NULL ||
bpf_filter(handle->fcode.bf_insns, &handle->buffer[handle->offset],
pkth.len, pkth.caplen)) {
callback(user, &pkth, &handle->buffer[handle->offset]);
return 1;
}
return 0; /* didn't pass filter */
}
示例7: tftp_request
void tftp_request(struct listener *listen, time_t now)
{
ssize_t len;
char *packet = daemon->packet;
char *filename, *mode, *p, *end, *opt;
union mysockaddr addr, peer;
struct msghdr msg;
struct iovec iov;
struct ifreq ifr;
int is_err = 1, if_index = 0, mtu = 0;
#ifdef HAVE_DHCP
struct iname *tmp;
#endif
struct tftp_transfer *transfer;
int port = daemon->start_tftp_port; /* may be zero to use ephemeral port */
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
int mtuflag = IP_PMTUDISC_DONT;
#endif
char namebuff[IF_NAMESIZE];
char *name = NULL;
char *prefix = daemon->tftp_prefix;
struct tftp_prefix *pref;
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(HAVE_SOLARIS_NETWORK)
char control[CMSG_SPACE(sizeof(unsigned int))];
#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
char control[CMSG_SPACE(sizeof(struct sockaddr_dl))];
#endif
} control_u;
msg.msg_controllen = sizeof(control_u);
msg.msg_control = control_u.control;
msg.msg_flags = 0;
msg.msg_name = &peer;
msg.msg_namelen = sizeof(peer);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = packet;
iov.iov_len = daemon->packet_buff_sz;
/* we overwrote the buffer... */
daemon->srv_save = NULL;
if ((len = recvmsg(listen->tftpfd, &msg, 0)) < 2)
return;
if (option_bool(OPT_NOWILD))
{
if (listen->iface)
{
addr = listen->iface->addr;
mtu = listen->iface->mtu;
name = listen->iface->name;
}
else
{
/* we're listening on an address that doesn't appear on an interface,
ask the kernel what the socket is bound to */
socklen_t tcp_len = sizeof(union mysockaddr);
if (getsockname(listen->tftpfd, (struct sockaddr *)&addr, &tcp_len) == -1)
return;
}
}
else
{
struct cmsghdr *cmptr;
if (msg.msg_controllen < sizeof(struct cmsghdr))
return;
addr.sa.sa_family = listen->family;
#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);
addr.in.sin_addr = p.p->ipi_spec_dst;
if_index = p.p->ipi_ifindex;
}
#elif defined(HAVE_SOLARIS_NETWORK)
if (listen->family == AF_INET)
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
{
union {
unsigned char *c;
//.........这里部分代码省略.........
示例8: __scm_send
int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
{
struct cmsghdr *cmsg;
int err;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
err = -EINVAL;
/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
/* The first check was omitted in <= 2.2.5. The reasoning was
that parser checks cmsg_len in any case, so that
additional check would be work duplication.
But if cmsg_level is not SOL_SOCKET, we do not check
for too short ancillary data object at all! Oops.
OK, let's add it...
*/
if (!CMSG_OK(msg, cmsg))
goto error;
if (cmsg->cmsg_level != SOL_SOCKET)
continue;
switch (cmsg->cmsg_type)
{
case SCM_RIGHTS:
if (!sock->ops || sock->ops->family != PF_UNIX)
goto error;
err=scm_fp_copy(cmsg, &p->fp);
if (err<0)
goto error;
break;
case SCM_CREDENTIALS:
if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
goto error;
memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
err = scm_check_creds(&p->creds);
if (err)
goto error;
if (!p->pid || pid_vnr(p->pid) != p->creds.pid) {
struct pid *pid;
err = -ESRCH;
pid = find_get_pid(p->creds.pid);
if (!pid)
goto error;
put_pid(p->pid);
p->pid = pid;
}
if (!p->cred ||
(p->cred->euid != p->creds.uid) ||
(p->cred->egid != p->creds.gid)) {
struct cred *cred;
err = -ENOMEM;
cred = prepare_creds();
if (!cred)
goto error;
cred->uid = cred->euid = p->creds.uid;
cred->gid = cred->egid = p->creds.gid;
if (p->cred)
put_cred(p->cred);
p->cred = cred;
}
break;
default:
goto error;
}
}
if (p->fp && !p->fp->count)
{
kfree(p->fp);
p->fp = NULL;
}
return 0;
error:
scm_destroy(p);
return err;
}
示例9: sizeof
AvahiDnsPacket *avahi_recv_dns_packet_ipv4(
int fd,
AvahiIPv4Address *ret_src_address,
uint16_t *ret_src_port,
AvahiIPv4Address *ret_dst_address,
AvahiIfIndex *ret_iface,
uint8_t *ret_ttl) {
AvahiDnsPacket *p= NULL;
struct msghdr msg;
struct iovec io;
size_t aux[1024 / sizeof(size_t)]; /* for alignment on ia64 ! */
ssize_t l;
struct cmsghdr *cmsg;
int found_addr = 0;
int ms;
struct sockaddr_in sa;
assert(fd >= 0);
if (ioctl(fd, FIONREAD, &ms) < 0) {
avahi_log_warn("ioctl(): %s", strerror(errno));
goto fail;
}
if (ms < 0) {
avahi_log_warn("FIONREAD returned negative value.");
goto fail;
}
p = avahi_dns_packet_new(ms + AVAHI_DNS_PACKET_EXTRA_SIZE);
io.iov_base = AVAHI_DNS_PACKET_DATA(p);
io.iov_len = p->max_size;
memset(&msg, 0, sizeof(msg));
msg.msg_name = &sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = aux;
msg.msg_controllen = sizeof(aux);
msg.msg_flags = 0;
if ((l = recvmsg(fd, &msg, 0)) < 0) {
/* Linux returns EAGAIN when an invalid IP packet has been
received. We suppress warnings in this case because this might
create quite a bit of log traffic on machines with unstable
links. (See #60) */
if (errno != EAGAIN)
avahi_log_warn("recvmsg(): %s", strerror(errno));
goto fail;
}
if (sa.sin_addr.s_addr == INADDR_ANY) {
/* Linux 2.4 behaves very strangely sometimes! */
goto fail;
}
assert(!(msg.msg_flags & MSG_CTRUNC));
assert(!(msg.msg_flags & MSG_TRUNC));
p->size = (size_t) l;
if (ret_src_port)
*ret_src_port = avahi_port_from_sockaddr((struct sockaddr*) &sa);
if (ret_src_address) {
AvahiAddress a;
avahi_address_from_sockaddr((struct sockaddr*) &sa, &a);
*ret_src_address = a.data.ipv4;
}
if (ret_ttl)
*ret_ttl = 255;
if (ret_iface)
*ret_iface = AVAHI_IF_UNSPEC;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == IPPROTO_IP) {
switch (cmsg->cmsg_type) {
#ifdef IP_RECVTTL
case IP_RECVTTL:
#endif
case IP_TTL:
if (ret_ttl)
*ret_ttl = (uint8_t) (*(int *) CMSG_DATA(cmsg));
break;
#ifdef IP_PKTINFO
case IP_PKTINFO: {
struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
//.........这里部分代码省略.........
示例10: recverr
int recverr(int fd, int ttl)
{
int res;
struct probehdr rcvbuf;
char cbuf[512];
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
struct sock_extended_err *e;
struct sockaddr_storage addr;
struct timeval tv;
struct timeval *rettv;
int slot = 0;
int rethops;
int sndhops;
int progress = -1;
int broken_router;
restart:
memset(&rcvbuf, -1, sizeof(rcvbuf));
iov.iov_base = &rcvbuf;
iov.iov_len = sizeof(rcvbuf);
msg.msg_name = (caddr_t)&addr;
msg.msg_namelen = sizeof(addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
gettimeofday(&tv, NULL);
res = recvmsg(fd, &msg, MSG_ERRQUEUE);
if (res < 0) {
if (errno == EAGAIN)
return progress;
goto restart;
}
progress = mtu;
rethops = -1;
sndhops = -1;
e = NULL;
rettv = NULL;
slot = -base_port;
switch (family) {
case AF_INET6:
slot += ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
break;
case AF_INET:
slot += ntohs(((struct sockaddr_in *)&addr)->sin_port);
break;
}
if (slot >= 0 && slot < 63 && his[slot].hops) {
sndhops = his[slot].hops;
rettv = &his[slot].sendtime;
his[slot].hops = 0;
}
broken_router = 0;
if (res == sizeof(rcvbuf)) {
if (rcvbuf.ttl == 0 || rcvbuf.tv.tv_sec == 0)
broken_router = 1;
else {
sndhops = rcvbuf.ttl;
rettv = &rcvbuf.tv;
}
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
switch (cmsg->cmsg_level) {
case SOL_IPV6:
switch(cmsg->cmsg_type) {
case IPV6_RECVERR:
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
break;
case IPV6_HOPLIMIT:
#ifdef IPV6_2292HOPLIMIT
case IPV6_2292HOPLIMIT:
#endif
memcpy(&rethops, CMSG_DATA(cmsg), sizeof(rethops));
break;
default:
printf("cmsg6:%d\n ", cmsg->cmsg_type);
}
break;
case SOL_IP:
switch(cmsg->cmsg_type) {
case IP_RECVERR:
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
break;
case IP_TTL:
rethops = *(__u8*)CMSG_DATA(cmsg);
break;
default:
printf("cmsg4:%d\n ", cmsg->cmsg_type);
}
}
}
//.........这里部分代码省略.........
示例11: process_frames
//.........这里部分代码省略.........
if (fds[i].fd == sock)
printf("device: disconnected\n");
else
printf("client: disconnect\n");
return 0;
}
}
if (mode == SERVER) {
len = recv(fd, buf, snap_len, MSG_DONTWAIT);
if (len == 0) {
printf("client: disconnect\n");
return 0;
}
if (len < 0 && errno != EAGAIN && errno != EINTR) {
perror("Connection read failure");
return -1;
}
}
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:
memcpy(&frm.ts, CMSG_DATA(cmsg),
sizeof(struct timeval));
break;
}
cmsg = CMSG_NXTHDR(&msg, cmsg);
}
frm.ptr = frm.data;
frm.len = frm.data_len;
switch (mode) {
case WRITE:
case SERVER:
/* Save or send dump */
if (flags & DUMP_BTSNOOP) {
uint64_t ts;
uint8_t pkt_type = ((uint8_t *) frm.data)[0];
dp->size = htonl(frm.data_len);
dp->len = dp->size;
dp->flags = ntohl(frm.in & 0x01);
dp->drops = 0;
ts = (frm.ts.tv_sec - 946684800ll) * 1000000ll + frm.ts.tv_usec;
dp->ts = hton64(ts + 0x00E03AB44A676000ll);
if (pkt_type == HCI_COMMAND_PKT ||
pkt_type == HCI_EVENT_PKT)
dp->flags |= ntohl(0x02);
} else {
dh->len = htobs(frm.data_len);
dh->in = frm.in;
dh->ts_sec = htobl(frm.ts.tv_sec);
dh->ts_usec = htobl(frm.ts.tv_usec);
}
if (write_n(fd, buf, frm.data_len + hdr_size) < 0) {
perror("Write error");
return -1;
}
break;
default:
/* Parse and print */
parse(&frm);
break;
}
}
return 0;
}
示例12: mcast_recv_igmp
void mcast_recv_igmp(int sockfd, int version)
{
unsigned char buf[MAX_RECV_BUF_LEN] = {0};
int num = 0;
struct iphdr* ip = NULL;
int iph_len = 0, igh_len = 0;
struct igmphdr *p_igh;
pi_addr pig, pia;
struct msghdr msg;
struct sockaddr sa;
struct iovec iov;
struct cmsghdr *cmsg;
char *ctrl = (char *) malloc(MAXCTRLSIZE);
struct in_pktinfo *info;
unsigned int if_index = 0;
imp_interface* p_if = NULL;
/*To get the unique index of the interfacethe packet was received on*/
bzero(&msg, sizeof(msg));
iov.iov_base = buf;
iov.iov_len = 2048;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = MAXCTRLSIZE;
msg.msg_name = &sa;
msg.msg_namelen = sizeof(struct sockaddr);
num = recvmsg(get_igmp_mld_socket(AF_INET), &msg, MSG_WAITALL);
if (num <= sizeof(*ip)) {
IMP_LOG_ERROR("BAD packet received n=%d \n", num);
free(ctrl);
return;
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_type == IP_PKTINFO) {
info = (struct in_pktinfo *) CMSG_DATA(cmsg);
if_index = info->ipi_ifindex;
}
}
free(ctrl);
/*return if the interface don't enable igmp*/
for(p_if = imp_interface_first();p_if;p_if = LIST_NEXT(p_if, link))
{
if(p_if->if_index == if_index && p_if->if_addr.ss.ss_family == AF_INET)
break;
}
if(p_if == NULL){
IMP_LOG_WARNING("Don't exist this VIF\n", if_index);
return;
}
IMP_LOG_DEBUG("src addr = %s\nreceived interface %d\n",
imp_inet_ntoa(((struct sockaddr_in*)&sa)->sin_addr.s_addr), if_index);
ip = (struct iphdr*)buf;
if (ip->protocol != IPPROTO_IGMP && ip->protocol != 0) {
IMP_LOG_ERROR("error protocol number %d\n", ip->protocol);
return;
}
imp_build_piaddr(AF_INET, &ip->daddr, &pig);
imp_build_piaddr(AF_INET, &ip->saddr, &pia);
if (mcast_if_get_by_addr(&pia) != NULL) {
IMP_LOG_DEBUG("Ignore igmp from myself\n");
return;
}
/*when protocol is zero, we need add MFC base one this kind of packet*/
if (ip->protocol == 0 && p_if->type == INTERFACE_UPSTREAM) {
if_set ttls;
bzero(&ttls, sizeof(ttls));
/*get ttls*/
if(imp_get_mfcc_ttls(&ttls, MAXVIFS, &pia, &pig) != 0){
IMP_LOG_DEBUG("add MFC:src -- %s group -- %s\n\n", imp_pi_ntoa(&pia), imp_pi_ntoa(&pig));
imp_membership_db_mfc_add(&pig, &pia, &ttls);
}
return;
} else if (ip->protocol == 0) {
return;
}
//.........这里部分代码省略.........
示例13: mcast_recv_mld
void mcast_recv_mld(int sockfd, int version)
{
unsigned char buf[MAX_RECV_BUF_LEN] = {0};
int num = 0;
pi_addr pig, pia;
struct msghdr msg;
struct sockaddr_in6 sa;
struct iovec iov;
struct cmsghdr *cmsg;
char *ctrl = (char *) malloc(MAXCTRLSIZE);
struct in6_pktinfo *info;
unsigned int if_index = 0;
struct mld_hdr *p_mldh;
imp_interface *p_if = NULL;
/*To get the unique index of the interfacethe packet was received on*/
bzero(&msg, sizeof(msg));
iov.iov_base = buf;
iov.iov_len = 2048;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = MAXCTRLSIZE;
msg.msg_name = &sa;
msg.msg_namelen = sizeof(struct sockaddr_in6);
num = recvmsg(get_igmp_mld_socket(AF_INET6), &msg, MSG_WAITALL);
if (num <= 20) {
IMP_LOG_ERROR("BAD packet received n=%d \n", num);
free(ctrl);
return;
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_type == IPV6_PKTINFO) {
info = (struct in6_pktinfo *) CMSG_DATA(cmsg);
if_index = info->ipi6_ifindex;
imp_build_piaddr(AF_INET6, &info->ipi6_addr, &pig);
imp_build_piaddr(AF_INET6, &sa.sin6_addr, &pia);
IMP_LOG_DEBUG("from %s %s %d\n", imp_pi_ntoa(&pig), imp_pi_ntoa(&pia), if_index);
}
}
free(ctrl);
if (if_index == 0) {
/*
* Structure used to communicate from kernel to multicast router.
* We'll overlay the structure onto an MLD header (not an IPv6 heder like igmpmsg{}
* used for IPv4 implementation). This is because this structure will be passed via an
* IPv6 raw socket, on wich an application will only receiver the payload i.e the data after
* the IPv6 header and all the extension headers. (See section 3 of RFC 3542)
*/
struct mrt6msg *p_mrmsg;
int mif = 0;
p_mrmsg = msg.msg_iov->iov_base;
IMP_LOG_DEBUG("DUMP mrmsg:\n\tmif %d\n\tmsgtype %d\n\tsrc %s\n\tgroup %s\n",
p_mrmsg->im6_mif, p_mrmsg->im6_msgtype, imp_inet6_ntoa(p_mrmsg->im6_src.s6_addr),
imp_inet6_ntoa(p_mrmsg->im6_dst.s6_addr));
imp_build_piaddr(AF_INET6, &p_mrmsg->im6_dst, &pig);
imp_build_piaddr(AF_INET6, &p_mrmsg->im6_src, &pia);
mif = k_get_vmif(get_up_if_index(), AF_INET6);
IMP_LOG_DEBUG("k_get_vmif = %d im6_mif %d\n", mif, p_mrmsg->im6_mif);
if (mif == p_mrmsg->im6_mif) {
if_set ttls;
bzero(&ttls, sizeof(ttls));
/*get ttls*/
if (imp_get_mfcc_ttls(&ttls, MAXVIFS, &pia, &pig) != 0) {
IMP_LOG_DEBUG("add MFC:src -- %s group -- %s\n\n", imp_pi_ntoa(&pia), imp_pi_ntoa(&pig));
imp_membership_db_mfc_add(&pig, &pia, &ttls);
}
}
return;
}
/* An MLDv2 Report MUST be sent with a valid IPv6 link-local source
* address, or the unspecified address (::), if the sending interface
* has not acquired a valid link-local address yet.
* [rfc 3810 5.2.13]
*/
if (!(IN6_IS_ADDR_LINKLOCAL(&sa.sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(&sa.sin6_addr))) {
IMP_LOG_ERROR("MLDv2 Report MUST be sent with a valid IPv6 link-local "
"source address, or the unspecified address (::).\n");
return;
}
//.........这里部分代码省略.........
示例14: nl_send_iovec
//.........这里部分代码省略.........
} else if (errno == EAGAIN) {
NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
goto abort;
} else {
free(msg.msg_control);
free(*buf);
return -nl_syserr2nlerr(errno);
}
}
if (iov.iov_len < n ||
msg.msg_flags & MSG_TRUNC) {
/* Provided buffer is not long enough, enlarge it
* and try again. */
iov.iov_len *= 2;
iov.iov_base = *buf = realloc(*buf, iov.iov_len);
goto retry;
} else if (msg.msg_flags & MSG_CTRUNC) {
msg.msg_controllen *= 2;
msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
goto retry;
} else if (flags != 0) {
/* Buffer is big enough, do the actual reading */
flags = 0;
goto retry;
}
if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
free(msg.msg_control);
free(*buf);
return -NLE_NOADDR;
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_CREDENTIALS) {
*creds = calloc(1, sizeof(struct ucred));
memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
break;
}
}
free(msg.msg_control);
return n;
abort:
free(msg.msg_control);
free(*buf);
return 0;
}
#define NL_CB_CALL(cb, type, msg) \
do { \
err = nl_cb_call(cb, type, msg); \
switch (err) { \
case NL_OK: \
err = 0; \
break; \
case NL_SKIP: \
goto skip; \
case NL_STOP: \
goto stop; \
default: \
goto out; \
} \
} while (0)
示例15: sock_ctrl_recv
/* Get a packet from the socket. It also returns the destination addres and
* source port of the packet */
int
sock_ctrl_recv(int sock, struct lbuf *buf, uconn_t *uc)
{
union control_data {
struct cmsghdr cmsg;
u_char data4[CMSG_SPACE(sizeof(struct in_pktinfo))];
u_char data6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
};
union sockunion su;
struct msghdr msg;
struct iovec iov[1];
union control_data cmsg;
struct cmsghdr *cmsgptr = NULL;
int nbytes = 0;
iov[0].iov_base = lbuf_data(buf);
iov[0].iov_len = lbuf_tailroom(buf);
memset(&msg, 0, sizeof msg);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = &cmsg;
msg.msg_controllen = sizeof cmsg;
msg.msg_name = &su;
msg.msg_namelen = sizeof(union sockunion);
nbytes = recvmsg(sock, &msg, 0);
if (nbytes == -1) {
LMLOG(LWRN, "sock_recv_ctrl: recvmsg error: %s", strerror(errno));
return (BAD);
}
lbuf_set_size(buf, lbuf_size(buf) + nbytes);
/* read local address, remote port and remote address */
if (su.s4.sin_family == AF_INET) {
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr;
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == IPPROTO_IP
&& cmsgptr->cmsg_type == IP_PKTINFO) {
lisp_addr_ip_init(&uc->la,
&(((struct in_pktinfo *) (CMSG_DATA(cmsgptr)))->ipi_addr),
AF_INET);
break;
}
}
lisp_addr_ip_init(&uc->ra, &su.s4.sin_addr, AF_INET);
uc->rp = ntohs(su.s4.sin_port);
} else {
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr;
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == IPPROTO_IPV6
&& cmsgptr->cmsg_type == IPV6_PKTINFO) {
lisp_addr_ip_init(&uc->la,
&(((struct in6_pktinfo *) (CMSG_DATA(cmsgptr)))->ipi6_addr),
AF_INET6);
break;
}
}
lisp_addr_ip_init(&uc->ra, &su.s6.sin6_addr, AF_INET6);
uc->rp = ntohs(su.s6.sin6_port);
}
return (GOOD);
}