本文整理汇总了C++中sendmsg函数的典型用法代码示例。如果您正苦于以下问题:C++ sendmsg函数的具体用法?C++ sendmsg怎么用?C++ sendmsg使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sendmsg函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: tcp_flush
static grpc_endpoint_op_status tcp_flush(grpc_tcp *tcp) {
struct msghdr msg;
struct iovec iov[MAX_WRITE_IOVEC];
int iov_size;
ssize_t sent_length;
ssize_t sending_length;
ssize_t trailing;
ssize_t unwind_slice_idx;
ssize_t unwind_byte_idx;
for (;;) {
sending_length = 0;
unwind_slice_idx = tcp->outgoing_slice_idx;
unwind_byte_idx = tcp->outgoing_byte_idx;
for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count &&
iov_size != MAX_WRITE_IOVEC;
iov_size++) {
iov[iov_size].iov_base =
GPR_SLICE_START_PTR(
tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) +
tcp->outgoing_byte_idx;
iov[iov_size].iov_len =
GPR_SLICE_LENGTH(
tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) -
tcp->outgoing_byte_idx;
sending_length += iov[iov_size].iov_len;
tcp->outgoing_slice_idx++;
tcp->outgoing_byte_idx = 0;
}
GPR_ASSERT(iov_size > 0);
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = iov_size;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
GRPC_TIMER_BEGIN(GRPC_PTAG_SENDMSG, 0);
do {
/* TODO(klempner): Cork if this is a partial write */
sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS);
} while (sent_length < 0 && errno == EINTR);
GRPC_TIMER_END(GRPC_PTAG_SENDMSG, 0);
if (sent_length < 0) {
if (errno == EAGAIN) {
tcp->outgoing_slice_idx = unwind_slice_idx;
tcp->outgoing_byte_idx = unwind_byte_idx;
return GRPC_ENDPOINT_PENDING;
} else {
/* TODO(klempner): Log some of these */
return GRPC_ENDPOINT_ERROR;
}
}
GPR_ASSERT(tcp->outgoing_byte_idx == 0);
trailing = sending_length - sent_length;
while (trailing > 0) {
ssize_t slice_length;
tcp->outgoing_slice_idx--;
slice_length = GPR_SLICE_LENGTH(
tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]);
if (slice_length > trailing) {
tcp->outgoing_byte_idx = slice_length - trailing;
break;
} else {
trailing -= slice_length;
}
}
if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) {
return GRPC_ENDPOINT_DONE;
}
};
}
示例2: conn_read_netlink
/* Returns zero on success, less than zero on socket error and greater than
* zero on other errors. */
static int conn_read_netlink (void)
{
#if HAVE_STRUCT_LINUX_INET_DIAG_REQ
int fd;
struct inet_diag_msg *r;
char buf[8192];
/* If this fails, it's likely a permission problem. We'll fall back to
* reading this information from files below. */
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
if (fd < 0)
{
ERROR ("tcpconns plugin: conn_read_netlink: socket(AF_NETLINK, SOCK_RAW, "
"NETLINK_INET_DIAG) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
return (-1);
}
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK
};
struct nlreq req = {
.nlh.nlmsg_len = sizeof(req),
.nlh.nlmsg_type = TCPDIAG_GETSOCK,
/* NLM_F_ROOT: return the complete table instead of a single entry.
* NLM_F_MATCH: return all entries matching criteria (not implemented)
* NLM_F_REQUEST: must be set on all request messages */
.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
.nlh.nlmsg_pid = 0,
/* The sequence_number is used to track our messages. Since netlink is not
* reliable, we don't want to end up with a corrupt or incomplete old
* message in case the system is/was out of memory. */
.nlh.nlmsg_seq = ++sequence_number,
.r.idiag_family = AF_INET,
.r.idiag_states = 0xfff,
.r.idiag_ext = 0
};
struct iovec iov = {
.iov_base = &req,
.iov_len = sizeof(req)
};
struct msghdr msg = {
.msg_name = (void*)&nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1
};
if (sendmsg (fd, &msg, 0) < 0)
{
ERROR ("tcpconns plugin: conn_read_netlink: sendmsg(2) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
close (fd);
return (-1);
}
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
while (1)
{
int status;
struct nlmsghdr *h;
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void*)&nladdr;
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
status = recvmsg(fd, (void *) &msg, /* flags = */ 0);
if (status < 0)
{
if ((errno == EINTR) || (errno == EAGAIN))
continue;
ERROR ("tcpconns plugin: conn_read_netlink: recvmsg(2) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
close (fd);
return (-1);
}
else if (status == 0)
{
close (fd);
DEBUG ("tcpconns plugin: conn_read_netlink: Unexpected zero-sized "
"reply from netlink socket.");
return (0);
}
h = (struct nlmsghdr*)buf;
while (NLMSG_OK(h, status))
{
if (h->nlmsg_seq != sequence_number)
{
h = NLMSG_NEXT(h, status);
//.........这里部分代码省略.........
示例3: emf_cfg_request_send
int
emf_cfg_request_send(emf_cfg_request_t *buffer, int length)
{
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct msghdr msg;
struct iovec iov;
int sock_fd, ret;
if ((buffer == NULL) || (length > MAX_DATA_SIZE))
{
fprintf(stderr, "Invalid parameters %p %d\n", buffer, length);
return (FAILURE);
}
/* Create a netlink socket */
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_EMFC);
if (sock_fd < 0)
{
fprintf(stderr, "Netlink socket create failed\n");
return (FAILURE);
}
/* Associate a local address with the opened socket */
memset(&src_addr, 0, sizeof(struct sockaddr_nl));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = 0;
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
/* Fill the destination address, pid of 0 indicates kernel */
memset(&dest_addr, 0, sizeof(struct sockaddr_nl));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0;
dest_addr.nl_groups = 0;
/* Allocate memory for sending configuration request */
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_DATA_SIZE));
if (nlh == NULL)
{
fprintf(stderr, "Out of memory allocating cfg buffer\n");
return (FAILURE);
}
/* Fill the netlink message header. The configuration request
* contains netlink header followed by data.
*/
nlh->nlmsg_len = NLMSG_SPACE(MAX_DATA_SIZE);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
/* Fill the data part */
memcpy(NLMSG_DATA(nlh), buffer, length);
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
memset(&msg, 0, sizeof(struct msghdr));
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* Send request to kernel module */
ret = sendmsg(sock_fd, &msg, 0);
if (ret < 0)
{
perror("sendmsg:");
free(nlh);
return (ret);
}
/* Wait for the response */
memset(nlh, 0, NLMSG_SPACE(MAX_DATA_SIZE));
ret = recvmsg(sock_fd, &msg, 0);
if (ret < 0)
{
perror("recvmsg:");
free(nlh);
return (ret);
}
/* Copy data to user buffer */
memcpy(buffer, NLMSG_DATA(nlh), length);
free(nlh);
close(sock_fd);
return (ret);
}
示例4: thread_loop
void thread_loop(void *userdata)
{
struct state *state = userdata;
pthread_spin_init(&state->lock, PTHREAD_PROCESS_PRIVATE);
pthread_spin_lock(&state->lock);
stddev_init(&state->stddev);
stddev_init(&state->stddev_packet);
pthread_spin_unlock(&state->lock);
char send_buf[MTU_SIZE], recv_buf[MTU_SIZE];
struct msghdr *msg = calloc(2, sizeof(struct msghdr));
struct iovec *iovec = calloc(2, sizeof(struct iovec));
int i;
for (i = 0; i < 2; i++) {
msg[i].msg_iov = &iovec[i];
msg[i].msg_iovlen = 1;
iovec[i].iov_len = sizeof(send_buf);
iovec[i].iov_len = state->packet_size;
}
iovec[0].iov_base = send_buf;
iovec[1].iov_base = recv_buf;
char pktinfo[4096];
msg[1].msg_control = pktinfo;
msg[1].msg_controllen = sizeof(pktinfo);
uint64_t packet_no = 0;
while (1) {
int fd = net_connect_udp(state->target_addr, state->src_port,
state->busy_poll);
struct timeval tv = {1, 0}; // 1 second
int r = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
if (r < 0) {
PFATAL("setsockopt(SO_RCVTIMEO)");
}
for (;; packet_no++) {
memset(send_buf, 0, sizeof(send_buf));
snprintf(send_buf, state->packet_size, "%i-%li-%lu",
getpid(), gettid(), packet_no);
uint64_t t0 = realtime_now(), t1 = 0, tp = 0;
int r = sendmsg(fd, &msg[0], 0);
if (r <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK ||
errno == ECONNREFUSED) {
break;
}
if (errno == EINTR) {
continue;
}
PFATAL("sendmmsg()");
}
msg[1].msg_controllen = sizeof(pktinfo);
while (1) {
int flags = state->polling ? MSG_DONTWAIT : 0;
r = recvmsg(fd, &msg[1], flags);
t1 = realtime_now();
if (t1 - t0 >= 1000000000) {
/* No msg for 1s */
errno = ECONNREFUSED;
break;
}
if (r <= 0 &&
(errno == EINTR || errno == EAGAIN ||
errno == EWOULDBLOCK)) {
continue;
}
if (r == 0 && state->polling) {
continue;
}
break;
}
if (r <= 0 && errno == ECONNREFUSED) {
sleep(1);
break;
}
if (r <= 0) {
PFATAL("recvmmsg()");
}
if (memcmp(send_buf, recv_buf, sizeof(recv_buf)) != 0) {
fprintf(stderr, "[!] bad message\n");
sleep(1);
break;
}
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(&msg[1]); cmsg;
//.........这里部分代码省略.........
示例5: _ribified_sendmsg
ssize_t _ribified_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
int res;
while ((res = sendmsg(sockfd, msg, flags)) < 0 &&
yield_if_eagain(sockfd));
return res;
}
示例6: test_sendmsg_server
void test_sendmsg_server(cyg_addrword_t pnetdata)
{
int s, i, sendmsglen;
struct sockaddr_in sa, r_sa;
struct hostent *hp;
struct msghdr msghdr_msg;
struct iovec *piov;
int j;
int perIov_len;
char tmp[TEST_SENDMSG_MSG_LEN];
int threadid;
int port = ((TEST_SENDMSG_DATA_T*)pnetdata)->iport;
char *pbuf = ((TEST_SENDMSG_DATA_T*)pnetdata)->pbuf;
threadid = port;
if(inet_aton(TEST_SENDMSG_SERVER_ADDR, &sa.sin_addr, pbuf, RNT_BUFFER_LEN) == 0)
{
test_printf_error("test_sendmsg_server");
cyg_thread_exit();
}
sa.sin_family = AF_INET;
sa.sin_port = htons(IPPORT_USERRESERVED + port);
if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
{
test_printf_error("test_sendmsg_server");
cyg_thread_exit();
}
memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
r_sa.sin_family = AF_INET;
r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
{
test_printf_error("test_sendmsg_server");
cyg_thread_exit();
}
if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
{
test_printf_error("test_sendmsg_server");
netclose(s, pbuf, RNT_BUFFER_LEN);
cyg_thread_exit();
}
if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
{
test_printf_error("test_sendmsg_server");
netclose(s, pbuf, RNT_BUFFER_LEN);
cyg_thread_exit();
}
if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1)
{
test_printf_error("test_sendmsg_server");
cyg_thread_exit();
}
for(i = 0; i < TEST_SENDMSG_WRITE_TIMES; i++)
{
strcpy(tmp, TEST_SENDMSG_MSG);
perIov_len = TEST_SENDMSG_MSG_PART_LEN;
piov = malloc(TEST_SENDMSG_MSG_PARTS*sizeof(struct iovec));
if(piov == NULL)
{
test_printf_error("test_sendmsg_server");
goto fail;
}
memset(piov, 0, TEST_SENDMSG_MSG_PARTS*sizeof(struct iovec));
for(j = 0; j < TEST_SENDMSG_MSG_PARTS; j++)
{
piov[j].iov_base = malloc(perIov_len);
if(piov[j].iov_base == NULL)
{
test_printf_error("test_sendmsg_server");
for(i = 0; i < j; i++) free(piov[j].iov_base);
if(piov != NULL) free(piov);
goto fail;
}
piov[j].iov_len = perIov_len;
memcpy(piov[j].iov_base, tmp + j * TEST_SENDMSG_MSG_PART_LEN, TEST_SENDMSG_MSG_PART_LEN);
}
msghdr_msg.msg_name = NULL;
msghdr_msg.msg_namelen = 0;
msghdr_msg.msg_iov = piov;
msghdr_msg.msg_iovlen = TEST_SENDMSG_MSG_PARTS;
msghdr_msg.msg_control = NULL;
msghdr_msg.msg_controllen = 0;
msghdr_msg.msg_flags = 0;
sendmsglen = sendmsg(s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN);
if(sendmsglen < 0)
{
//.........这里部分代码省略.........
示例7: dhcp_network_icmp6_send_router_solicitation
int dhcp_network_icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr)
{
struct sockaddr_in6 dst = {
.sin6_family = AF_INET6,
.sin6_addr = IN6ADDR_ALL_ROUTERS_MULTICAST_INIT,
};
struct {
struct nd_router_solicit rs;
struct nd_opt_hdr rs_opt;
struct ether_addr rs_opt_mac;
} _packed_ rs = {
.rs.nd_rs_type = ND_ROUTER_SOLICIT,
};
struct iovec iov[1] = {
{ &rs, },
};
struct msghdr msg = {
.msg_name = &dst,
.msg_namelen = sizeof(dst),
.msg_iov = iov,
.msg_iovlen = 1,
};
int r;
if (ether_addr) {
memcpy(&rs.rs_opt_mac, ether_addr, ETH_ALEN);
rs.rs_opt.nd_opt_type = ND_OPT_SOURCE_LINKADDR;
rs.rs_opt.nd_opt_len = 1;
iov[0].iov_len = sizeof(rs);
} else
iov[0].iov_len = sizeof(rs.rs);
r = sendmsg(s, &msg, 0);
if (r < 0)
return -errno;
return 0;
}
int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
struct in6_pktinfo pktinfo = {
.ipi6_ifindex = index,
};
union sockaddr_union src = {
.in6.sin6_family = AF_INET6,
.in6.sin6_port = htobe16(DHCP6_PORT_CLIENT),
.in6.sin6_addr = IN6ADDR_ANY_INIT,
};
_cleanup_close_ int s = -1;
int r, off = 0, on = 1;
if (local_address)
memcpy(&src.in6.sin6_addr, local_address,
sizeof(src.in6.sin6_addr));
s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
IPPROTO_UDP);
if (s < 0)
return -errno;
r = setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &pktinfo,
sizeof(pktinfo));
if (r < 0)
return -errno;
r = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
if (r < 0)
return -errno;
r = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off));
if (r < 0)
return -errno;
r = bind(s, &src.sa, sizeof(src.in6));
if (r < 0)
return -errno;
r = s;
s = -1;
return r;
}
int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
const void *packet, size_t len) {
union sockaddr_union dest = {
.in6.sin6_family = AF_INET6,
.in6.sin6_port = htobe16(DHCP6_PORT_SERVER),
};
int r;
assert(server_address);
memcpy(&dest.in6.sin6_addr, server_address, sizeof(dest.in6.sin6_addr));
r = sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in6));
if (r < 0)
return -errno;
return 0;
}
示例8: sctp_sendv
//.........这里部分代码省略.........
}
if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) {
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_PRINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo));
msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
}
if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_AUTHINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo));
msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
}
break;
default:
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
addr = addrs;
msg.msg_name = NULL;
msg.msg_namelen = 0;
for (i = 0; i < addrcnt; i++) {
switch (addr->sa_family) {
case AF_INET:
addr_len = (socklen_t) sizeof(struct sockaddr_in);
addr_in = (struct sockaddr_in *)addr;
if (addr_in->sin_len != addr_len) {
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
if (i == 0) {
port = addr_in->sin_port;
} else {
if (port == addr_in->sin_port) {
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_DSTADDRV4;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr));
msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr));
cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr)));
} else {
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
}
break;
case AF_INET6:
addr_len = (socklen_t) sizeof(struct sockaddr_in6);
addr_in6 = (struct sockaddr_in6 *)addr;
if (addr_in6->sin6_len != addr_len) {
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
if (i == 0) {
port = addr_in6->sin6_port;
} else {
if (port == addr_in6->sin6_port) {
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_DSTADDRV6;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr));
memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr));
msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr));
cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr)));
} else {
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
}
break;
default:
free(cmsgbuf);
errno = EINVAL;
return (-1);
}
if (i == 0) {
msg.msg_name = addr;
msg.msg_namelen = addr_len;
}
addr = (struct sockaddr *)((caddr_t)addr + addr_len);
}
if (msg.msg_controllen == 0) {
msg.msg_control = NULL;
}
msg.msg_iov = (struct iovec *)iov;
msg.msg_iovlen = iovcnt;
msg.msg_flags = 0;
ret = sendmsg(sd, &msg, flags);
free(cmsgbuf);
return (ret);
}
示例9: sctp_sendmsg
ssize_t
sctp_sendmsg(int s,
const void *data,
size_t len,
const struct sockaddr *to,
socklen_t tolen,
uint32_t ppid,
uint32_t flags,
uint16_t stream_no,
uint32_t timetolive,
uint32_t context)
{
#ifdef SYS_sctp_generic_sendmsg
struct sctp_sndrcvinfo sinfo;
memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
sinfo.sinfo_ppid = ppid;
sinfo.sinfo_flags = flags;
sinfo.sinfo_stream = stream_no;
sinfo.sinfo_timetolive = timetolive;
sinfo.sinfo_context = context;
sinfo.sinfo_assoc_id = 0;
return (syscall(SYS_sctp_generic_sendmsg, s,
data, len, to, tolen, &sinfo, 0));
#else
ssize_t sz;
struct msghdr msg;
struct sctp_sndrcvinfo *s_info;
struct iovec iov;
char controlVector[SCTP_CONTROL_VEC_SIZE_RCV];
struct cmsghdr *cmsg;
struct sockaddr *who = NULL;
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
} addr;
if ((tolen > 0) &&
((to == NULL) || (tolen < sizeof(struct sockaddr)))) {
errno = EINVAL;
return (-1);
}
if (to && (tolen > 0)) {
if (to->sa_family == AF_INET) {
if (tolen != sizeof(struct sockaddr_in)) {
errno = EINVAL;
return (-1);
}
if ((to->sa_len > 0) &&
(to->sa_len != sizeof(struct sockaddr_in))) {
errno = EINVAL;
return (-1);
}
memcpy(&addr, to, sizeof(struct sockaddr_in));
addr.in.sin_len = sizeof(struct sockaddr_in);
} else if (to->sa_family == AF_INET6) {
if (tolen != sizeof(struct sockaddr_in6)) {
errno = EINVAL;
return (-1);
}
if ((to->sa_len > 0) &&
(to->sa_len != sizeof(struct sockaddr_in6))) {
errno = EINVAL;
return (-1);
}
memcpy(&addr, to, sizeof(struct sockaddr_in6));
addr.in6.sin6_len = sizeof(struct sockaddr_in6);
} else {
errno = EAFNOSUPPORT;
return (-1);
}
who = (struct sockaddr *)&addr;
}
iov.iov_base = (char *)data;
iov.iov_len = len;
if (who) {
msg.msg_name = (caddr_t)who;
msg.msg_namelen = who->sa_len;
} else {
msg.msg_name = (caddr_t)NULL;
msg.msg_namelen = 0;
}
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (caddr_t)controlVector;
cmsg = (struct cmsghdr *)controlVector;
cmsg->cmsg_level = IPPROTO_SCTP;
cmsg->cmsg_type = SCTP_SNDRCV;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
s_info = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
s_info->sinfo_stream = stream_no;
s_info->sinfo_ssn = 0;
s_info->sinfo_flags = flags;
s_info->sinfo_ppid = ppid;
s_info->sinfo_context = context;
s_info->sinfo_assoc_id = 0;
//.........这里部分代码省略.........
示例10: pdcp_fifo_flush_sdus
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int
pdcp_fifo_flush_sdus ()
{
//-----------------------------------------------------------------------------
mem_block_t *sdu = list_get_head (&pdcp_sdu_list);
int bytes_wrote = 0;
int pdcp_nb_sdu_sent = 0;
uint8_t cont = 1;
int ret;
while ((sdu) && (cont)) {
#ifdef USER_MODE
// asjust the instance id when passing sdu to IP
((pdcp_data_ind_header_t *)(sdu->data))->inst = (((pdcp_data_ind_header_t *)(sdu->data))->inst >= NB_eNB_INST) ?
((pdcp_data_ind_header_t *)(sdu->data))->inst - NB_eNB_INST +oai_emulation.info.nb_enb_local - oai_emulation.info.first_ue_local :// UE
((pdcp_data_ind_header_t *)(sdu->data))->inst - oai_emulation.info.first_ue_local; // ENB
#else
((pdcp_data_ind_header_t *)(sdu->data))->inst = 0;
#endif
#ifdef PDCP_DEBUG
msg("[PDCP][INFO] PDCP->IP TTI %d INST %d: Preparing %d Bytes of data from rab %d to Nas_mesh\n",
Mac_rlc_xface->frame,
((pdcp_data_ind_header_t *)(sdu->data))->inst,
((pdcp_data_ind_header_t *)(sdu->data))->data_size,
((pdcp_data_ind_header_t *)(sdu->data))->rb_id);
#endif //PDCP_DEBUG
cont = 0;
if (!pdcp_output_sdu_bytes_to_write) {
if (!pdcp_output_header_bytes_to_write) {
pdcp_output_header_bytes_to_write = sizeof (pdcp_data_ind_header_t);
}
#ifndef USER_MODE
bytes_wrote = rtf_put (PDCP2NAS_FIFO,
&(((uint8_t *) sdu->data)[sizeof (pdcp_data_ind_header_t) - pdcp_output_header_bytes_to_write]),
pdcp_output_header_bytes_to_write);
#else
#ifdef NAS_NETLINK
#ifdef LINUX
memcpy(NLMSG_DATA(nas_nlh), &(((uint8_t *) sdu->data)[sizeof (pdcp_data_ind_header_t) - pdcp_output_header_bytes_to_write]),
pdcp_output_header_bytes_to_write);
nas_nlh->nlmsg_len = pdcp_output_header_bytes_to_write;
#endif //LINUX
#endif //NAS_NETLINK
bytes_wrote = pdcp_output_header_bytes_to_write;
#endif //USER_MODE
#ifdef PDCP_DEBUG
msg("[PDCP][INFO] TTI %d Sent %d Bytes of header to Nas_mesh\n",
Mac_rlc_xface->frame,
bytes_wrote);
#endif //PDCP_DEBUG
if (bytes_wrote > 0) {
pdcp_output_header_bytes_to_write = pdcp_output_header_bytes_to_write - bytes_wrote;
if (!pdcp_output_header_bytes_to_write) { // continue with sdu
pdcp_output_sdu_bytes_to_write = ((pdcp_data_ind_header_t *) sdu->data)->data_size;
#ifndef USER_MODE
bytes_wrote = rtf_put (PDCP2NAS_FIFO, &(sdu->data[sizeof (pdcp_data_ind_header_t)]), pdcp_output_sdu_bytes_to_write);
#else
#ifdef NAS_NETLINK
#ifdef LINUX
memcpy(NLMSG_DATA(nas_nlh)+sizeof(pdcp_data_ind_header_t), &(sdu->data[sizeof (pdcp_data_ind_header_t)]), pdcp_output_sdu_bytes_to_write);
nas_nlh->nlmsg_len += pdcp_output_sdu_bytes_to_write;
ret = sendmsg(nas_sock_fd,&nas_msg,0);
if (ret<0) {
msg("[PDCP_FIFOS] sendmsg returns %d\n",ret);
perror("error code:");
mac_xface->macphy_exit("");
break;
}
#endif // LINUX
#endif //NAS_NETLINK
bytes_wrote= pdcp_output_sdu_bytes_to_write;
#endif // USER_MODE
#ifdef PDCP_DEBUG
msg("[PDCP][INFO] PDCP->IP TTI %d INST %d: Sent %d Bytes of data from rab %d to Nas_mesh\n",
Mac_rlc_xface->frame,
((pdcp_data_ind_header_t *)(sdu->data))->inst,
bytes_wrote,
((pdcp_data_ind_header_t *)(sdu->data))->rb_id);
#endif //PDCP_DEBUG
if (bytes_wrote > 0) {
pdcp_output_sdu_bytes_to_write -= bytes_wrote;
//.........这里部分代码省略.........
示例11: send_to_from
ssize_t
send_to_from(int fd, void *buf, size_t len, int flags,
const struct sockaddr *to,
const struct sockaddr *from,
socklen_t tolen)
{
#ifndef IP_PKTINFO
return sendto(fd, buf, len, flags, to, tolen);
#else
struct iovec iov[1];
struct msghdr msg;
char cbuf[sizeof(struct in_pktinfo)
#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
| sizeof(struct in6_pktinfo) /* (a|b) is poor man's max(a,b) */
#endif
];
struct cmsghdr* cmsgptr;
if (from->sa_family != AF_INET
#if ENABLE_FEATURE_IPV6
&& from->sa_family != AF_INET6
#endif
) {
/* ANY local address */
return sendto(fd, buf, len, flags, to, tolen);
}
/* man recvmsg and man cmsg is needed to make sense of code below */
iov[0].iov_base = buf;
iov[0].iov_len = len;
memset(cbuf, 0, sizeof(cbuf));
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */
msg.msg_namelen = tolen;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
msg.msg_flags = flags;
cmsgptr = CMSG_FIRSTHDR(&msg);
if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
struct in_pktinfo *pktptr;
cmsgptr->cmsg_level = IPPROTO_IP;
cmsgptr->cmsg_type = IP_PKTINFO;
cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
/* pktptr->ipi_ifindex = 0; -- already done by memset(cbuf...) */
pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
}
#if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
struct in6_pktinfo *pktptr;
cmsgptr->cmsg_level = IPPROTO_IPV6;
cmsgptr->cmsg_type = IPV6_PKTINFO;
cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
/* pktptr->ipi6_ifindex = 0; -- already done by memset(cbuf...) */
pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
}
#endif
return sendmsg(fd, &msg, flags);
#endif
}
示例12: conn_read_netlink
/* Returns zero on success, less than zero on socket error and greater than
* zero on other errors. */
static int conn_read_netlink (void)
{
int fd;
struct sockaddr_nl nladdr;
struct nlreq req;
struct msghdr msg;
struct iovec iov;
struct inet_diag_msg *r;
char buf[8192];
/* If this fails, it's likely a permission problem. We'll fall back to
* reading this information from files below. */
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG);
if (fd < 0)
{
ERROR ("tcpconns plugin: conn_read_netlink: socket(AF_NETLINK, SOCK_RAW, "
"NETLINK_INET_DIAG) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
return (-1);
}
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
memset(&req, 0, sizeof(req));
req.nlh.nlmsg_len = sizeof(req);
req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
/* NLM_F_ROOT: return the complete table instead of a single entry.
* NLM_F_MATCH: return all entries matching criteria (not implemented)
* NLM_F_REQUEST: must be set on all request messages */
req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.nlh.nlmsg_pid = 0;
/* The sequence_number is used to track our messages. Since netlink is not
* reliable, we don't want to end up with a corrupt or incomplete old
* message in case the system is/was out of memory. */
req.nlh.nlmsg_seq = ++sequence_number;
req.r.idiag_family = AF_INET;
req.r.idiag_states = 0xfff;
req.r.idiag_ext = 0;
memset(&iov, 0, sizeof(iov));
iov.iov_base = &req;
iov.iov_len = sizeof(req);
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void*)&nladdr;
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (sendmsg (fd, &msg, 0) < 0)
{
ERROR ("tcpconns plugin: conn_read_netlink: sendmsg(2) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
close (fd);
return (-1);
}
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
while (1)
{
int status;
struct nlmsghdr *h;
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void*)&nladdr;
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
status = recvmsg(fd, (void *) &msg, /* flags = */ 0);
if (status < 0)
{
if ((errno == EINTR) || (errno == EAGAIN))
continue;
ERROR ("tcpconns plugin: conn_read_netlink: recvmsg(2) failed: %s",
sstrerror (errno, buf, sizeof (buf)));
close (fd);
return (-1);
}
else if (status == 0)
{
close (fd);
DEBUG ("tcpconns plugin: conn_read_netlink: Unexpected zero-sized "
"reply from netlink socket.");
return (0);
}
h = (struct nlmsghdr*)buf;
while (NLMSG_OK(h, status))
{
if (h->nlmsg_seq != sequence_number)
{
h = NLMSG_NEXT(h, status);
continue;
//.........这里部分代码省略.........
示例13: netlink_rcv
extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer)
{
int ret;
struct sockaddr_nl nladdr;
struct iovec iov = {
.iov_base = answer->nlmsghdr,
.iov_len = answer->nlmsghdr->nlmsg_len,
};
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0;
nladdr.nl_groups = 0;
again:
ret = recvmsg(handler->fd, &msg, 0);
if (ret < 0) {
if (errno == EINTR)
goto again;
return -errno;
}
if (!ret)
return 0;
if (msg.msg_flags & MSG_TRUNC &&
ret == answer->nlmsghdr->nlmsg_len)
return -EMSGSIZE;
return ret;
}
extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg)
{
struct sockaddr_nl nladdr;
struct iovec iov = {
.iov_base = nlmsg->nlmsghdr,
.iov_len = nlmsg->nlmsghdr->nlmsg_len,
};
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
int ret;
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0;
nladdr.nl_groups = 0;
ret = sendmsg(handler->fd, &msg, 0);
if (ret < 0)
return -errno;
return ret;
}
#ifndef NLMSG_ERROR
#define NLMSG_ERROR 0x2
#endif
extern int netlink_transaction(struct nl_handler *handler,
struct nlmsg *request, struct nlmsg *answer)
{
int ret;
ret = netlink_send(handler, request);
if (ret < 0)
return ret;
ret = netlink_rcv(handler, answer);
if (ret < 0)
return ret;
if (answer->nlmsghdr->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer->nlmsghdr);
return err->error;
}
return 0;
}
extern int netlink_open(struct nl_handler *handler, int protocol)
{
socklen_t socklen;
int sndbuf = 32768;
int rcvbuf = 32768;
memset(handler, 0, sizeof(*handler));
handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
if (handler->fd < 0)
//.........这里部分代码省略.........
示例14: main
int main(int argc, char **argv)
{
struct sockaddr_un sun;
char buf[1024];
int s, sunlen, ret, buflen;
struct msghdr msg = { 0 };
struct iovec iov;
struct cmsghdr *cmsg;
int myfd = 0;
char cmsgbuf[CMSG_SPACE(sizeof myfd)];
int *fdptr;
if (argc != 3) {
fprintf(stderr, "usage: %s testfile address\n", argv[0]);
exit(-1);
}
myfd = open(argv[1], O_RDWR);
if (myfd < 0) {
perror(argv[1]);
exit(-1);
}
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
exit(-1);
}
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path, argv[2]);
sunlen = strlen(sun.sun_path) + 1 + sizeof(short);
ret = connect(s, (struct sockaddr *)&sun, sunlen);
if (ret < 0) {
perror("connect");
exit(-1);
}
printf("client: Connected to server via %s\n", sun.sun_path);
strcpy(buf, "hello world");
buflen = strlen(buf)+1;
iov.iov_base = buf;
iov.iov_len = buflen;
msg.msg_name = 0;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof cmsgbuf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
fdptr = (int *)CMSG_DATA(cmsg);
memcpy(fdptr, &myfd, sizeof(int));
msg.msg_controllen = cmsg->cmsg_len;
ret = sendmsg(s, &msg, 0);
if (ret < 0) {
perror("sendmsg");
exit(-1);
}
printf("client: Sent descriptor, waiting for reply\n");
buf[0] = 0;
ret = recv(s, buf, sizeof(buf), 0);
if (ret < 0) {
perror("recv");
exit(-1);
}
printf("client: Received reply, code=%d\n", buf[0]);
if (buf[0])
printf("client: ...This implies the descriptor was not received\n");
else
printf("client: ...This implies the descriptor was received\n");
exit(buf[0]);
}
示例15: rauth
void
rauth(Job *job)
{
sendmsg(job, "dns: authentication not required");
}