Format Android protector C code

This commit is contained in:
Andy Wang 2020-10-17 11:11:15 +01:00
parent 36bf0c919f
commit c6d6f40021
1 changed files with 42 additions and 45 deletions

View File

@ -13,56 +13,53 @@ package main
#include <sys/un.h> #include <sys/un.h>
#include <sys/uio.h> #include <sys/uio.h>
#define ANCIL_FD_BUFFER(n) \ #define ANCIL_FD_BUFFER(n) \
struct { \ struct { \
struct cmsghdr h; \ struct cmsghdr h; \
int fd[n]; \ int fd[n]; \
} }
int int ancil_send_fds_with_buffer(int sock, const int *fds, unsigned n_fds,
ancil_send_fds_with_buffer(int sock, const int *fds, unsigned n_fds, void *buffer) void *buffer) {
{ struct msghdr msghdr;
struct msghdr msghdr; char nothing = '!';
char nothing = '!'; struct iovec nothing_ptr;
struct iovec nothing_ptr; struct cmsghdr *cmsg;
struct cmsghdr *cmsg; int i;
int i;
nothing_ptr.iov_base = &nothing; nothing_ptr.iov_base = &nothing;
nothing_ptr.iov_len = 1; nothing_ptr.iov_len = 1;
msghdr.msg_name = NULL; msghdr.msg_name = NULL;
msghdr.msg_namelen = 0; msghdr.msg_namelen = 0;
msghdr.msg_iov = &nothing_ptr; msghdr.msg_iov = &nothing_ptr;
msghdr.msg_iovlen = 1; msghdr.msg_iovlen = 1;
msghdr.msg_flags = 0; msghdr.msg_flags = 0;
msghdr.msg_control = buffer; msghdr.msg_control = buffer;
msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * n_fds; msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * n_fds;
cmsg = CMSG_FIRSTHDR(&msghdr); cmsg = CMSG_FIRSTHDR(&msghdr);
cmsg->cmsg_len = msghdr.msg_controllen; cmsg->cmsg_len = msghdr.msg_controllen;
cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_type = SCM_RIGHTS;
for(i = 0; i < n_fds; i++) for (i = 0; i < n_fds; i++)
((int *)CMSG_DATA(cmsg))[i] = fds[i]; ((int *)CMSG_DATA(cmsg))[i] = fds[i];
return(sendmsg(sock, &msghdr, 0) >= 0 ? 0 : -1); return (sendmsg(sock, &msghdr, 0) >= 0 ? 0 : -1);
} }
int int ancil_send_fd(int sock, int fd) {
ancil_send_fd(int sock, int fd) ANCIL_FD_BUFFER(1) buffer;
{
ANCIL_FD_BUFFER(1) buffer;
return(ancil_send_fds_with_buffer(sock, &fd, 1, &buffer)); return (ancil_send_fds_with_buffer(sock, &fd, 1, &buffer));
} }
void void set_timeout(int sock) {
set_timeout(int sock) struct timeval tv;
{ tv.tv_sec = 3;
struct timeval tv; tv.tv_usec = 0;
tv.tv_sec = 3; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
tv.tv_usec = 0; sizeof(struct timeval));
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)); sizeof(struct timeval));
} }
*/ */
import "C" import "C"