Last active
June 8, 2017 03:10
-
-
Save pbfy0/90fe671af0a1f046da20592f7932df88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <dlfcn.h> | |
#include <stdio.h> | |
//#include <poll.h> | |
//struct pollfd pfd = {-1}; | |
int csock = -1; | |
__attribute__((constructor)) void constructor() { | |
int sock; | |
if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) return; | |
struct sockaddr_in addr = {0}; | |
addr.sin_port = htons(5810); | |
int enable = 1; | |
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); | |
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { | |
close(sock); | |
return; | |
} | |
csock = sock; | |
} | |
__attribute__((destructor)) void destructor() { | |
if(csock != -1) close(csock); | |
} | |
ssize_t (*r_recvfrom)(int sockfd, void *buf, size_t len, int flags, | |
struct sockaddr *src_addr, socklen_t *addrlen) = NULL; | |
int (*r_bind)(int sockfd, const struct sockaddr *addr, | |
socklen_t addrlen) = NULL; | |
int important_fd = -1; | |
struct ctrl_packet { | |
uint8_t takeover; | |
uint8_t value; | |
}; | |
struct ctrl_packet ctp = {0}; | |
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, | |
struct sockaddr *src_addr, socklen_t *addrlen) { | |
if(r_recvfrom == NULL) r_recvfrom = dlsym(RTLD_NEXT, "recvfrom"); | |
ssize_t ret = r_recvfrom(sockfd, buf, len, flags, src_addr, addrlen); | |
if(sockfd == important_fd && ret >= 6) { | |
if(csock != -1) while(recv(csock, &ctp, sizeof(ctp), MSG_DONTWAIT) == sizeof(ctp)); | |
uint8_t *u8b = (uint8_t *)buf; | |
u8b[3] = (u8b[3] & ~ctp.takeover) | (ctp.value & ctp.takeover); | |
} | |
return ret; | |
} | |
int bind(int sockfd, const struct sockaddr *addr, | |
socklen_t addrlen) { | |
struct sockaddr_in *sai = (struct sockaddr_in *)addr; | |
if(sai->sin_port == htons(1110)){ | |
important_fd = sockfd; | |
printf("frc_remote ready\n"); | |
//printf("important_fd %d\n", important_fd); | |
} | |
if(r_bind == NULL) r_bind = dlsym(RTLD_NEXT, "bind"); | |
return r_bind(sockfd, addr, addrlen); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment