本文整理汇总了C++中recvmsg函数的典型用法代码示例。如果您正苦于以下问题:C++ recvmsg函数的具体用法?C++ recvmsg怎么用?C++ recvmsg使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了recvmsg函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: BIO_dgram_sctp_wait_for_dry
int BIO_dgram_sctp_wait_for_dry(BIO *b)
{
int is_dry = 0;
int n, sockflags, ret;
union sctp_notification snp;
struct msghdr msg;
struct iovec iov;
#ifdef SCTP_EVENT
struct sctp_event event;
#else
struct sctp_event_subscribe event;
socklen_t eventsize;
#endif
bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
/* set sender dry event */
#ifdef SCTP_EVENT
memset(&event, 0, sizeof(struct sctp_event));
event.se_assoc_id = 0;
event.se_type = SCTP_SENDER_DRY_EVENT;
event.se_on = 1;
ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
#else
eventsize = sizeof(struct sctp_event_subscribe);
ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
if (ret < 0)
return -1;
event.sctp_sender_dry_event = 1;
ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
#endif
if (ret < 0)
return -1;
/* peek for notification */
memset(&snp, 0x00, sizeof(union sctp_notification));
iov.iov_base = (char *)&snp;
iov.iov_len = sizeof(union sctp_notification);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
n = recvmsg(b->num, &msg, MSG_PEEK);
if (n <= 0)
{
if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
return -1;
else
return 0;
}
/* if we find a notification, process it and try again if necessary */
while (msg.msg_flags & MSG_NOTIFICATION)
{
memset(&snp, 0x00, sizeof(union sctp_notification));
iov.iov_base = (char *)&snp;
iov.iov_len = sizeof(union sctp_notification);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
n = recvmsg(b->num, &msg, 0);
if (n <= 0)
{
if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
return -1;
else
return is_dry;
}
if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
{
is_dry = 1;
/* disable sender dry event */
#ifdef SCTP_EVENT
memset(&event, 0, sizeof(struct sctp_event));
event.se_assoc_id = 0;
event.se_type = SCTP_SENDER_DRY_EVENT;
event.se_on = 0;
ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
#else
eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
if (ret < 0)
return -1;
event.sctp_sender_dry_event = 0;
ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
#endif
//.........这里部分代码省略.........
示例2: send_netlink
static int send_netlink(struct nlmsghdr *hdr)
{
int s;
pid_t mypid = getpid ();
struct sockaddr_nl nl;
struct iovec iov;
struct msghdr msg;
static unsigned int seq;
char *buffer;
int bytes;
union
{
char *buffer;
struct nlmsghdr *nlm;
} h;
if ((s = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
logger (LOG_ERR, "socket: %s", strerror (errno));
return -1;
}
memset (&nl, 0, sizeof (struct sockaddr_nl));
nl.nl_family = AF_NETLINK;
if (bind (s, (struct sockaddr *) &nl, sizeof (nl)) == -1) {
logger (LOG_ERR, "bind: %s", strerror (errno));
close (s);
return -1;
}
memset (&iov, 0, sizeof (struct iovec));
iov.iov_base = hdr;
iov.iov_len = hdr->nlmsg_len;
memset (&msg, 0, sizeof (struct msghdr));
msg.msg_name = &nl;
msg.msg_namelen = sizeof (nl);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* Request a reply */
hdr->nlmsg_flags |= NLM_F_ACK;
hdr->nlmsg_seq = ++seq;
if (sendmsg (s, &msg, 0) == -1) {
logger (LOG_ERR, "write: %s", strerror (errno));
close (s);
return -1;
}
buffer = xmalloc (sizeof (char) * BUFFERLEN);
memset (buffer, 0, BUFFERLEN);
iov.iov_base = buffer;
while (1) {
iov.iov_len = BUFFERLEN;
bytes = recvmsg (s, &msg, 0);
if (bytes == -1) {
if (errno != EINTR)
logger (LOG_ERR, "netlink: overrun");
continue;
}
if (bytes == 0) {
logger (LOG_ERR, "netlink: EOF");
goto eexit;
}
if (msg.msg_namelen != sizeof (nl)) {
logger (LOG_ERR, "netlink: sender address length mismatch");
goto eexit;
}
for (h.buffer = buffer; bytes >= (signed) sizeof (*h.nlm); ) {
int len = h.nlm->nlmsg_len;
int l = len - sizeof (*h.nlm);
if (l < 0 || len > bytes) {
if (msg.msg_flags & MSG_TRUNC)
logger (LOG_ERR, "netlink: truncated message");
else
logger (LOG_ERR, "netlink: malformed message");
goto eexit;
}
if (nl.nl_pid != 0 ||
(pid_t) h.nlm->nlmsg_pid != mypid ||
h.nlm->nlmsg_seq != seq)
/* Message isn't for us, so skip it */
goto next;
/* We get an NLMSG_ERROR back with a code of zero for success */
if (h.nlm->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h.nlm);
if ((unsigned) l < sizeof (struct nlmsgerr))
logger (LOG_ERR, "netlink: truncated error message");
else {
errno = -err->error;
if (errno == 0) {
close (s);
//.........这里部分代码省略.........
示例3: main
void main(int argc, char * argv[])
{
/* this program is command line tool to help us communicate with the kernel
* it needs the following arguments to run: 1. filename_to_save_to, 2. message string
or istead of "message string" have a -f tag, and then filename for input */
char * file_name;
char message[MAX_NETCONNECT_INPUT];
char * input_file_name;
FILE * input_file;
FILE * file;
FILE * bytes;
char payload[MAX_PAYLOAD];
int payload_number = 0;
int i;
unsigned long recieved = 0;
memset(message, 0, MAX_NETCONNECT_INPUT);
if (argc != 3 && argc != 4) //thired argument is the programs name
{
//printf("Wrong parameters supplied. [LEAVING]\n");
return;
}
if (argc == 3)
{
//use regular input
file_name = argv[1];
strncpy(message, argv[2], MAX_NETCONNECT_INPUT);
}
else if(argc == 4)
{
//check if -f tag given
if (!strcmp(argv[2], FILE_TAG))
{
input_file_name = argv[3];
input_file = fopen(input_file_name, "rt");
if (!input_file)
{
printf("WHAT");
}
printf("GOT HERE\n");
cpy_from_file(input_file, message, MAX_NETCONNECT_INPUT);
fclose(input_file);
}
}
else {printf ("WRONG PARAMETERS SUPPLIED [LEAVING]\n"); return;}
file_name = argv[1];
if (!strstr(file_name,".txt"))
{
printf("BAD parameters, file_name needs to be supplied with .txt ending.\n");
return;
}
file = fopen(file_name,"a+");
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_LISTEN_PROTOCOL);
if (sock_fd < 0)
return;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
/* message header */
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), message);
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("Letting kernel know our PID\n");
sendmsg(sock_fd, &msg, 0);
printf("Waiting for report from kernel\n");
//delay to avoid rereading buffer
recvmsg(sock_fd, &msg, 0);
if (!strcmp(NLMSG_DATA(nlh),message))
{
//delay -> and read again ignoring first
recvmsg(sock_fd, &msg,0);
printf("Buffer reread, avoiding crash...\n" );
}
//.........这里部分代码省略.........
示例4: main
//.........这里部分代码省略.........
int len;
int i = 0;
while (1) {
i++;
if (1) {
printf("(%d) Input msg (q or Q to quit):", i);
fflush(stdout);
gets(send_data);
//len = strlen(send_data);
len = 1000;
printf("\nlen=%d, str='%s'\n", len, send_data);
fflush(stdout);
if (len > 0 && len < send_len) {
gettimeofday(&curr, 0);
numbytes = sendto(sock, send_data, len, 0, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
//sleep(1);
ret = poll(fds, nfds, time);
if (ret || 0) {
if (1) {
printf("poll: ret=%d, revents=%x\n", ret, fds[ret].revents);
printf(
"POLLIN=%x POLLPRI=%x POLLOUT=%x POLLERR=%x POLLHUP=%x POLLNVAL=%x POLLRDNORM=%x POLLRDBAND=%x POLLWRNORM=%x POLLWRBAND=%x\n",
(fds[ret].revents & POLLIN) > 0, (fds[ret].revents & POLLPRI) > 0, (fds[ret].revents & POLLOUT) > 0, (fds[ret].revents
& POLLERR) > 0, (fds[ret].revents & POLLHUP) > 0, (fds[ret].revents & POLLNVAL) > 0,
(fds[ret].revents & POLLRDNORM) > 0, (fds[ret].revents & POLLRDBAND) > 0, (fds[ret].revents & POLLWRNORM) > 0,
(fds[ret].revents & POLLWRBAND) > 0);
fflush(stdout);
}
int recv_bytes;
if ((fds[ret].revents & (POLLERR)) || 0) {
recv_bytes = recvmsg(sock, &recv_msg, MSG_ERRQUEUE);
if (recv_bytes > 0) {
printf("recv_bytes=%d, msg_controllen=%d\n", recv_bytes, recv_msg.msg_controllen);
/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&recv_msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&recv_msg, cmsg)) {
printf("cmsg_len=%u, cmsg_level=%u, cmsg_type=%u\n", cmsg->cmsg_len, cmsg->cmsg_level, cmsg->cmsg_type);
if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_TTL) {
received_ttl = *(int *) CMSG_DATA(cmsg);
printf("received_ttl=%d\n", received_ttl);
//break;
} else if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) {
struct sock_extended_err *err = (struct sock_extended_err *) CMSG_DATA(cmsg);
printf("ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u, ee_pad=%u, ee_info=%u, ee_data=%u\n", err->ee_errno,
err->ee_origin, err->ee_type, err->ee_code, err->ee_pad, err->ee_info, err->ee_data);
struct sockaddr_in *offender = (struct sockaddr_in *) SO_EE_OFFENDER(err);
printf("family=%u, addr=%s/%u\n", offender->sin_family, inet_ntoa(offender->sin_addr), ntohs(offender->sin_port));
} else if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMP) {
struct timeval *stamp = (struct timeval *) CMSG_DATA(cmsg);
printf("stamp=%u.%u\n", (uint32_t) stamp->tv_sec, (uint32_t) stamp->tv_usec);
printf("diff=%f\n", time_diff(&curr, stamp));
}
}
if (cmsg == NULL) {
/*
* Error: IP_TTL not enabled or small buffer
* or I/O error.
*/
}
} else {
示例5: 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;
}
//.........这里部分代码省略.........
示例6: uv__udp_recvmsg
static void uv__udp_recvmsg(uv_loop_t* loop,
uv__io_t* w,
unsigned int revents) {
struct sockaddr_storage peer;
struct msghdr h;
uv_udp_t* handle;
ssize_t nread;
uv_buf_t buf;
int flags;
int count;
handle = container_of(w, uv_udp_t, io_watcher);
assert(handle->type == UV_UDP);
assert(revents & UV__POLLIN);
assert(handle->recv_cb != NULL);
assert(handle->alloc_cb != NULL);
/* Prevent loop starvation when the data comes in as fast as (or faster than)
* we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
*/
count = 32;
memset(&h, 0, sizeof(h));
h.msg_name = &peer;
do {
buf = handle->alloc_cb((uv_handle_t*)handle, 64 * 1024);
assert(buf.len > 0);
assert(buf.base != NULL);
h.msg_namelen = sizeof(peer);
h.msg_iov = (void*) &buf;
h.msg_iovlen = 1;
do {
nread = recvmsg(handle->io_watcher.fd, &h, 0);
}
while (nread == -1 && errno == EINTR);
if (nread == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
uv__set_sys_error(handle->loop, EAGAIN);
handle->recv_cb(handle, 0, buf, NULL, 0);
}
else {
uv__set_sys_error(handle->loop, errno);
handle->recv_cb(handle, -1, buf, NULL, 0);
}
}
else {
flags = 0;
if (h.msg_flags & MSG_TRUNC)
flags |= UV_UDP_PARTIAL;
handle->recv_cb(handle,
nread,
buf,
(struct sockaddr*)&peer,
flags);
}
}
/* recv_cb callback may decide to pause or close the handle */
while (nread != -1
&& count-- > 0
&& handle->io_watcher.fd != -1
&& handle->recv_cb != NULL);
}
示例7: recv_fd
static int recv_fd(int c)
{
int fd;
uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
struct msghdr msg = {
.msg_control = msgbuf,
.msg_controllen = sizeof(msgbuf),
};
struct cmsghdr *cmsg;
struct iovec iov;
uint8_t req[1];
ssize_t len;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
msg.msg_controllen = cmsg->cmsg_len;
iov.iov_base = req;
iov.iov_len = sizeof(req);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
len = recvmsg(c, &msg, 0);
if (len > 0) {
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
return fd;
}
return len;
}
static int net_bridge_run_helper(const char *helper, const char *bridge)
{
sigset_t oldmask, mask;
int pid, status;
char *args[5];
char **parg;
int sv[2];
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
return -1;
}
/* try to launch bridge helper */
pid = fork();
if (pid == 0) {
int open_max = sysconf(_SC_OPEN_MAX), i;
char fd_buf[6+10];
char br_buf[6+IFNAMSIZ] = {0};
char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
for (i = 0; i < open_max; i++) {
if (i != STDIN_FILENO &&
i != STDOUT_FILENO &&
i != STDERR_FILENO &&
i != sv[1]) {
close(i);
}
}
snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
/* assume helper is a command */
if (strstr(helper, "--br=") == NULL) {
snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
}
snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
helper, "--use-vnet", fd_buf, br_buf);
parg = args;
*parg++ = (char *)"sh";
*parg++ = (char *)"-c";
*parg++ = helper_cmd;
*parg++ = NULL;
execv("/bin/sh", args);
} else {
/* assume helper is just the executable path name */
snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
parg = args;
*parg++ = (char *)helper;
*parg++ = (char *)"--use-vnet";
*parg++ = fd_buf;
*parg++ = br_buf;
*parg++ = NULL;
execv(helper, args);
}
//.........这里部分代码省略.........
示例8: sco_ipc_cmd
static int sco_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
void *param, size_t *rsp_len, void *rsp, int *fd)
{
ssize_t ret;
struct msghdr msg;
struct iovec iv[2];
struct ipc_hdr cmd;
char cmsgbuf[CMSG_SPACE(sizeof(int))];
struct ipc_status s;
size_t s_len = sizeof(s);
pthread_mutex_lock(&sk_mutex);
if (ipc_sk < 0) {
error("sco: Invalid cmd socket passed to sco_ipc_cmd");
goto failed;
}
if (!rsp || !rsp_len) {
memset(&s, 0, s_len);
rsp_len = &s_len;
rsp = &s;
}
memset(&msg, 0, sizeof(msg));
memset(&cmd, 0, sizeof(cmd));
cmd.service_id = service_id;
cmd.opcode = opcode;
cmd.len = len;
iv[0].iov_base = &cmd;
iv[0].iov_len = sizeof(cmd);
iv[1].iov_base = param;
iv[1].iov_len = len;
msg.msg_iov = iv;
msg.msg_iovlen = 2;
ret = sendmsg(ipc_sk, &msg, 0);
if (ret < 0) {
error("sco: Sending command failed:%s", strerror(errno));
goto failed;
}
/* socket was shutdown */
if (ret == 0) {
error("sco: Command socket closed");
goto failed;
}
memset(&msg, 0, sizeof(msg));
memset(&cmd, 0, sizeof(cmd));
iv[0].iov_base = &cmd;
iv[0].iov_len = sizeof(cmd);
iv[1].iov_base = rsp;
iv[1].iov_len = *rsp_len;
msg.msg_iov = iv;
msg.msg_iovlen = 2;
if (fd) {
memset(cmsgbuf, 0, sizeof(cmsgbuf));
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
}
ret = recvmsg(ipc_sk, &msg, 0);
if (ret < 0) {
error("sco: Receiving command response failed:%s",
strerror(errno));
goto failed;
}
if (ret < (ssize_t) sizeof(cmd)) {
error("sco: Too small response received(%zd bytes)", ret);
goto failed;
}
if (cmd.service_id != service_id) {
error("sco: Invalid service id (%u vs %u)", cmd.service_id,
service_id);
goto failed;
}
if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
error("sco: Malformed response received(%zd bytes)", ret);
goto failed;
}
if (cmd.opcode != opcode && cmd.opcode != SCO_OP_STATUS) {
error("sco: Invalid opcode received (%u vs %u)",
cmd.opcode, opcode);
goto failed;
}
if (cmd.opcode == SCO_OP_STATUS) {
//.........这里部分代码省略.........
示例9: ufw_recv
static inline int ufw_recv(ufw_sk *sk){
struct cmsghdr* cmsg;
char ctlbuf[4096];
struct iovec iov;
struct msghdr msg = {NULL, 0, &iov, 1, ctlbuf, sizeof(ctlbuf), 0};
ssize_t s;
struct node *cur;
struct hookinfo *hki;
struct timeval time;
struct iphdr *ip;
if(!sk){
errno = EBADF;
return -1;
}
iov.iov_base = sk->recvbuf;
iov.iov_len = sizeof(sk->recvbuf);
ip = (struct iphdr *)sk->recvbuf;
for(;;){
next:
s = recvmsg(sk->fd, &msg, MSG_WAITALL);
if(s < 0){
if(errno == EAGAIN)
return 0;
else {
if(sk->opts & FATAL)die("recvmsg");
return -1;
}
}
/* filter */
if((sk->opts & FILTER_SADDR) && ip->daddr != sk->saddr)
continue;
if((sk->opts & FILTER_DADDR) && ip->saddr != sk->daddr)
continue;
if((sk->opts & FILTER_SPORT)
&& *(u_int16_t *)(sk->recvbuf + (ip->ihl << 2) + 2) != sk->sport)
continue;
if((sk->opts & FILTER_DPORT)
&& *(u_int16_t *)(sk->recvbuf + (ip->ihl << 2)) != sk->dport)
continue;
sk->received = 1;
/* get timestamp */
for(cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
if(cmsg->cmsg_level == SOL_SOCKET
&& cmsg->cmsg_type == SO_TIMESTAMP
&& cmsg->cmsg_len >= CMSG_LEN(sizeof(struct timeval))){
time = *(struct timeval *)CMSG_DATA(cmsg);
break;
}
if(!sk->first_packet.tv_sec)
sk->first_packet = time;
for(cur = sk->recvhook; cur; cur = cur->next){
hki = cur->data;
if(!hki->func(sk->recvbuf, &time, 1, hki->user))
goto next;
}
if((sk->opts & DUMP_RECV) && sk->dump)
dump_write(sk->dump, sk->recvbuf, 0, &time);
time.tv_sec -= sk->first_packet.tv_sec;
time.tv_usec -= sk->first_packet.tv_usec;
if(time.tv_usec < 0)time.tv_usec += 1000000;
if(sk->opts & PRINT_RECV)
print_packet(sk->recvbuf, &time, 0);
}
return -1;//never
}
示例10: pa_iochannel_read_with_ancil_data
ssize_t pa_iochannel_read_with_ancil_data(pa_iochannel*io, void*data, size_t l, pa_cmsg_ancil_data *ancil_data) {
ssize_t r;
struct msghdr mh;
struct iovec iov;
union {
struct cmsghdr hdr;
uint8_t data[CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * MAX_ANCIL_DATA_FDS)];
} cmsg;
pa_assert(io);
pa_assert(data);
pa_assert(l);
pa_assert(io->ifd >= 0);
pa_assert(ancil_data);
if (io->ifd_type > 0) {
ancil_data->creds_valid = false;
ancil_data->nfd = 0;
return pa_iochannel_read(io, data, l);
}
iov.iov_base = data;
iov.iov_len = l;
pa_zero(mh);
mh.msg_iov = &iov;
mh.msg_iovlen = 1;
mh.msg_control = &cmsg;
mh.msg_controllen = sizeof(cmsg);
if ((r = recvmsg(io->ifd, &mh, 0)) >= 0) {
struct cmsghdr *cmh;
ancil_data->creds_valid = false;
ancil_data->nfd = 0;
for (cmh = CMSG_FIRSTHDR(&mh); cmh; cmh = CMSG_NXTHDR(&mh, cmh)) {
if (cmh->cmsg_level != SOL_SOCKET)
continue;
if (cmh->cmsg_type == SCM_CREDENTIALS) {
struct ucred u;
pa_assert(cmh->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
memcpy(&u, CMSG_DATA(cmh), sizeof(struct ucred));
ancil_data->creds.gid = u.gid;
ancil_data->creds.uid = u.uid;
ancil_data->creds_valid = true;
}
else if (cmh->cmsg_type == SCM_RIGHTS) {
int nfd = (cmh->cmsg_len - CMSG_LEN(0)) / sizeof(int);
if (nfd > MAX_ANCIL_DATA_FDS) {
int i;
pa_log("Trying to receive too many file descriptors!");
for (i = 0; i < nfd; i++)
pa_close(((int*) CMSG_DATA(cmh))[i]);
continue;
}
memcpy(ancil_data->fds, CMSG_DATA(cmh), nfd * sizeof(int));
ancil_data->nfd = nfd;
ancil_data->close_fds_on_cleanup = true;
}
}
io->readable = io->hungup = false;
enable_events(io);
}
if (r == -1 && errno == ENOTSOCK) {
io->ifd_type = 1;
return pa_iochannel_read_with_ancil_data(io, data, l, ancil_data);
}
return r;
}
示例11: mm_receive_fd
int
mm_receive_fd(int sock)
{
struct msghdr msg;
union {
struct cmsghdr hdr;
char buf[1024];
} cmsgbuf;
struct cmsghdr *cmsg;
struct iovec vec;
ssize_t n;
char ch;
int fd;
struct pollfd pfd;
if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) {
error("%s: %zu < %zu, recompile", __func__,
sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
return -1;
}
memset(&msg, 0, sizeof(msg));
memset(&cmsgbuf, 0, sizeof(cmsgbuf));
vec.iov_base = &ch;
vec.iov_len = 1;
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = CMSG_SPACE(sizeof(int));
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;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL) {
error("%s: no message header", __func__);
return -1;
}
if (cmsg->cmsg_type != SCM_RIGHTS) {
error("%s: expected type %d got %d", __func__,
SCM_RIGHTS, cmsg->cmsg_type);
return -1;
}
fd = (*(int *)CMSG_DATA(cmsg));
return fd;
}
示例12: recvfromto
ssize_t
recvfromto(int s, void *buf, size_t len, int flags, struct sockaddr *from,
socklen_t *fromlen, struct sockaddr *to, socklen_t *tolen)
{
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
struct in6_pktinfo *pkt6;
struct sockaddr_in *in;
struct sockaddr_in6 *in6;
ssize_t ret;
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(struct sockaddr_storage))];
} cmsgbuf;
#if !defined(IP_RECVDSTADDR) && defined(IP_PKTINFO)
struct in_pktinfo *pkt;
#endif
bzero(&msg, sizeof(msg));
bzero(&cmsgbuf.buf, sizeof(cmsgbuf.buf));
iov.iov_base = buf;
iov.iov_len = len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_name = from;
msg.msg_namelen = *fromlen;
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
if ((ret = recvmsg(s, &msg, 0)) == -1)
return (-1);
*fromlen = SA_LEN(from);
*tolen = 0;
if (getsockname(s, to, tolen) != 0)
*tolen = 0;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
switch (from->sa_family) {
case AF_INET:
#if defined(IP_RECVDSTADDR)
if (cmsg->cmsg_level == IPPROTO_IP &&
cmsg->cmsg_type == IP_RECVDSTADDR) {
in = (struct sockaddr_in *)to;
in->sin_family = AF_INET;
in->sin_len = *tolen = sizeof(*in);
memcpy(&in->sin_addr, CMSG_DATA(cmsg),
sizeof(struct in_addr));
}
#elif defined(IP_PKTINFO)
if (cmsg->cmsg_level == IPPROTO_IP &&
cmsg->cmsg_type == IP_PKTINFO) {
in = (struct sockaddr_in *)to;
in->sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
in->sin_len = *tolen = sizeof(*in);
#else
*tolen = sizeof(*in);
#endif
pkt = (struct in_pktinfo *)CMSG_DATA(cmsg);
memcpy(&in->sin_addr, &pkt->ipi_addr,
sizeof(struct in_addr));
}
#endif
break;
case AF_INET6:
if (cmsg->cmsg_level == IPPROTO_IPV6 &&
cmsg->cmsg_type == IPV6_PKTINFO) {
in6 = (struct sockaddr_in6 *)to;
in6->sin6_family = AF_INET6;
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
in6->sin6_len = *tolen = sizeof(*in6);
#else
*tolen = sizeof(*in6);
#endif
pkt6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
memcpy(&in6->sin6_addr, &pkt6->ipi6_addr,
sizeof(struct in6_addr));
if (IN6_IS_ADDR_LINKLOCAL(&in6->sin6_addr))
in6->sin6_scope_id =
pkt6->ipi6_ifindex;
}
break;
}
}
return (ret);
}
示例13: netl_listen
int netl_listen(struct netl_handle *h, void *args)
{
int len, buflen, err;
ssize_t status;
struct nlmsghdr *hdr;
struct sockaddr_nl nladdr;
struct iovec iov;
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
char buf[8192];
struct pollfd fds[1];
if (h == NULL)
return -1;
iov.iov_base = buf;
if (h->cb.init != NULL) {
err = h->cb.init(args);
if (err != 0)
return err;
}
fds[0].events = POLLIN;
fds[0].fd = h->fd;
while (h->closing != 1) {
int res = poll(fds, 1, NETL_POLL_TIMEOUT);
if (res < 0 && errno != EINTR) {
perror("error during cmdline_run poll");
return 0;
}
if (fds[0].revents & POLLIN) {
iov.iov_len = sizeof(buf);
status = recvmsg(h->fd, &msg, 0);
if (status < 0) {
// TODO: EINT / EAGAIN / ENOBUF should continue
return -1;
}
if (status == 0) {
// EOF
return -1;
}
if (msg.msg_namelen != sizeof(nladdr)) {
// Invalid length
return -1;
}
for (hdr = (struct nlmsghdr *) buf;
(size_t) status >= sizeof(*hdr);) {
len = hdr->nlmsg_len;
buflen = len - sizeof(*hdr);
if (buflen < 0 || buflen > status) {
// truncated
return -1;
}
err = netl_handler(h, &nladdr, hdr, args);
if (err < 0)
return err;
status -= NLMSG_ALIGN(len);
hdr =
(struct nlmsghdr *) ((char *) hdr + NLMSG_ALIGN(len));
}
if (status) {
// content not read
return -1;
}
}
}
return 1;
}
static inline __u32 nl_mgrp(__u32 group)
{
return group ? (1 << (group - 1)) : 0;
}
struct netl_handle *netl_create(void)
{
struct netl_handle *netl_handle;
int rcvbuf = 1024 * 1024 * 1024;
socklen_t addr_len;
unsigned subscriptions = 0;
// get notified whenever interface change (new vlans / ...)
subscriptions |= nl_mgrp(RTNLGRP_LINK);
// get notified whenever ip changes
subscriptions |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
//.........这里部分代码省略.........
示例14: rtm_dispatch
/*
* Dispatch kernel routing socket messages.
*/
static int
rtm_dispatch(void)
{
struct msghdr mh;
struct iovec iov[1];
struct rt_msghdr *rtm;
struct rtm_dispinfo *di;
ssize_t len;
int retval;
di = malloc(sizeof(*di));
if (di == NULL) {
daemon_log(LOG_ERR, "malloc(%d): %s", sizeof(*di),
strerror(errno));
return (-1);
}
di->di_buflen = MAX_RTMSG_SIZE;
di->di_buf = calloc(MAX_RTMSG_SIZE, 1);
if (di->di_buf == NULL) {
free(di);
daemon_log(LOG_ERR, "calloc(%d): %s", MAX_RTMSG_SIZE,
strerror(errno));
return (-1);
}
memset(&mh, 0, sizeof(mh));
iov[0].iov_base = di->di_buf;
iov[0].iov_len = di->di_buflen;
mh.msg_iov = iov;
mh.msg_iovlen = 1;
retval = 0;
for (;;) {
len = recvmsg(fd, &mh, MSG_DONTWAIT);
if (len == -1) {
if (errno == EWOULDBLOCK)
break;
else {
daemon_log(LOG_ERR, "recvmsg(): %s",
strerror(errno));
retval = -1;
break;
}
}
rtm = (void *)di->di_buf;
if (rtm->rtm_version != RTM_VERSION) {
daemon_log(LOG_ERR,
"unknown routing socket message (version %d)\n",
rtm->rtm_version);
/* this is non-fatal; just ignore it for now. */
continue;
}
switch (rtm->rtm_type) {
case RTM_NEWADDR:
case RTM_DELADDR:
retval = rtm_dispatch_newdeladdr(di);
break;
case RTM_IFANNOUNCE:
retval = rtm_dispatch_ifannounce(di);
break;
default:
daemon_log(LOG_DEBUG, "%s: rtm_type %d ignored", __func__, rtm->rtm_type);
break;
}
/*
* If we got an error; assume our position on the call
* stack is enclosed by a level-triggered event loop,
* and signal the error condition.
*/
if (retval != 0)
break;
}
free(di->di_buf);
free(di);
return (retval);
}
示例15: loop_udp
void
loop_udp(int sockfd)
{
int maxfdp1, nread, ntowrite, stdineof,
clilen, servlen, flags;
fd_set rset;
struct sockaddr_in cliaddr; /* for UDP server */
struct sockaddr_in servaddr; /* for UDP client */
#ifdef HAVE_MSGHDR_MSG_CONTROL
struct iovec iov[1];
struct msghdr msg;
#ifdef IP_RECVDSTADDR /* 4.3BSD Reno and later */
static struct cmsghdr *cmptr = NULL; /* malloc'ed */
struct in_addr dstinaddr; /* for UDP server */
#define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(struct in_addr))
#endif /* IP_RECVDSTADDR */
#endif /* MSG_TRUNC */
if (pauseinit)
sleep_us(pauseinit*1000); /* intended for server */
flags = 0;
stdineof = 0;
FD_ZERO(&rset);
maxfdp1 = sockfd + 1; /* check descriptors [0..sockfd] */
/* If UDP client issues connect(), recv() and write() are used.
Server is harder since cannot issue connect(). We use recvfrom()
or recvmsg(), depending on OS. */
for ( ; ; ) {
if (stdineof == 0)
FD_SET(STDIN_FILENO, &rset);
FD_SET(sockfd, &rset);
if (select(maxfdp1, &rset, NULL, NULL, NULL) < 0)
err_sys("select error");
if (FD_ISSET(STDIN_FILENO, &rset)) { /* data to read on stdin */
if ( (nread = read(STDIN_FILENO, rbuf, readlen)) < 0)
err_sys("read error from stdin");
else if (nread == 0) { /* EOF on stdin */
if (halfclose) {
if (shutdown(sockfd, SHUT_WR) < 0)
err_sys("shutdown() error");
FD_CLR(STDIN_FILENO, &rset);
stdineof = 1; /* don't read stdin anymore */
continue; /* back to select() */
}
break; /* default: stdin EOF -> done */
}
if (crlf) {
ntowrite = crlf_add(wbuf, writelen, rbuf, nread);
if (connectudp) {
if (write(sockfd, wbuf, ntowrite) != ntowrite)
err_sys("write error");
} else {
if (sendto(sockfd, wbuf, ntowrite, 0,
(struct sockaddr *) &servaddr, sizeof(servaddr))
!= ntowrite)
err_sys("sendto error");
}
} else {
if (connectudp) {
if (write(sockfd, rbuf, nread) != nread)
err_sys("write error");
} else {
if (sendto(sockfd, rbuf, nread, 0,
(struct sockaddr *) &servaddr, sizeof(servaddr))
!= nread)
err_sys("sendto error");
}
}
}
if (FD_ISSET(sockfd, &rset)) { /* data to read from socket */
if (server) {
clilen = sizeof(cliaddr);
#ifndef MSG_TRUNC /* vanilla BSD sockets */
nread = recvfrom(sockfd, rbuf, readlen, 0,
(struct sockaddr *) &cliaddr, &clilen);
#else /* 4.3BSD Reno and later; use recvmsg() to get at MSG_TRUNC flag */
/* Also lets us get at control information (destination address) */
iov[0].iov_base = rbuf;
iov[0].iov_len = readlen;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = (caddr_t) &cliaddr;
msg.msg_namelen = clilen;
#ifdef IP_RECVDSTADDR
if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL)
//.........这里部分代码省略.........