2012-02-01 13:00:49 +03:00
|
|
|
#ifndef UTIL_NET_H_
|
|
|
|
#define UTIL_NET_H_
|
|
|
|
|
2012-03-28 17:36:00 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2012-02-01 13:00:49 +03:00
|
|
|
#define UNIX_PATH_MAX (sizeof(struct sockaddr_un) - \
|
|
|
|
(size_t)((struct sockaddr_un *) 0)->sun_path)
|
|
|
|
|
2012-03-02 14:01:57 +04:00
|
|
|
#ifndef SO_PEEK_OFF
|
|
|
|
#define SO_PEEK_OFF 42
|
|
|
|
#endif
|
|
|
|
|
2012-03-28 17:36:00 +04:00
|
|
|
/*
|
|
|
|
* Because of kernel doing kmalloc for user data passed
|
|
|
|
* in SCM messages, and there is SCM_MAX_FD as a limit
|
|
|
|
* for descriptors passed at once we're trying to reduce
|
|
|
|
* the pressue on kernel memory manager and use predefined
|
|
|
|
* known to work well size of the message buffer.
|
|
|
|
*/
|
|
|
|
#define CR_SCM_MSG_SIZE (1024)
|
|
|
|
#define CR_SCM_MAX_FD (252)
|
|
|
|
|
|
|
|
struct scm_fdset {
|
|
|
|
struct msghdr hdr;
|
|
|
|
struct iovec iov;
|
|
|
|
char msg_buf[CR_SCM_MSG_SIZE];
|
2012-04-10 18:36:13 +04:00
|
|
|
char msg[CR_SCM_MAX_FD];
|
2012-03-28 17:36:00 +04:00
|
|
|
};
|
|
|
|
|
2012-04-10 18:36:13 +04:00
|
|
|
extern int send_fds(int sock, struct sockaddr_un *saddr, int saddr_len,
|
|
|
|
int *fds, int nr_fds, bool with_flags);
|
|
|
|
extern int recv_fds(int sock, int *fds, int nr_fds, char *flags);
|
2012-03-28 23:44:00 +04:00
|
|
|
|
|
|
|
static inline int send_fd(int sock, struct sockaddr_un *saddr, int saddr_len, int fd)
|
|
|
|
{
|
2012-04-10 18:36:13 +04:00
|
|
|
return send_fds(sock, saddr, saddr_len, &fd, 1, false);
|
2012-03-28 23:44:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int recv_fd(int sock)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
2012-04-10 18:36:13 +04:00
|
|
|
ret = recv_fds(sock, &fd, 1, NULL);
|
2012-03-28 23:44:00 +04:00
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-02-01 13:00:49 +03:00
|
|
|
#endif
|