Commit 0c771f35 by michaelpastushkov

notification tests

parent 7dfbe0ad
...@@ -58,8 +58,7 @@ struct struct_rc { ...@@ -58,8 +58,7 @@ struct struct_rc {
struct sockaddr_in my_addr; struct sockaddr_in my_addr;
struct hostent *remote_host; struct hostent *remote_host;
int notify_socket; int notify_sockets[2];
struct sockaddr_un notify_addr;
}; };
struct client_info { struct client_info {
......
#!/bin/sh #!/bin/sh
gcc -o bytevia bytevia.c && ./bytevia --local-port=1948 --remote-host=localhost --remote-port=1949 --secret=52341 --encrypt=1 --log=2 gcc -o bytevia bytevia.c && ./bytevia --local-port=1948 --remote-host=localhost --remote-port=1984 --secret=52341 --encrypt=1 --log=2
#!/bin/sh #!/bin/sh
gcc -o bytevia bytevia.c && ./bytevia --local-port=1949 --remote-host=p4pn.net --remote-port=1948 --mode=server --secret=52341 --fork --encrypt=1 --log=2 gcc -o bytevia bytevia.c && ./bytevia --local-port=1984 --remote-host=p4pn.net --remote-port=1948 --mode=server --secret=52341 --fork --encrypt=1 --log=2
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h> #include <signal.h>
#include <sys/socket.h>
#include <sys/select.h>
#define LOCAL_PORT 1984
#define REMOTE_IP "52.204.74.175"
#define REMOTE_PORT 3389
#define BUFFER_SIZE 4096
void error_exit(const char *message) {
perror(message);
exit(EXIT_FAILURE);
}
int main() {
int local_socket, remote_socket;
struct sockaddr_in local_addr, remote_addr, client_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);
char buffer[BUFFER_SIZE];
fd_set read_fds;
// Create local socket
if ((local_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
error_exit("Local socket creation failed");
}
memset(&local_addr, 0, sizeof(local_addr)); // Global pipe file descriptors
local_addr.sin_family = AF_INET; int pipefd[2];
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); volatile sig_atomic_t signal_received = 0; // Signal flag for notification
local_addr.sin_port = htons(LOCAL_PORT);
// Bind the local socket to 127.0.0.1:1984 // Signal handler that sets the flag when a signal is received
if (bind(local_socket, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) { void handle_signal(int sig)
error_exit("Binding local socket failed"); {
} signal_received = 1;
}
// Create remote socket int main()
if ((remote_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { {
error_exit("Remote socket creation failed"); // Create a pipe for parent-child communication
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(1);
} }
memset(&remote_addr, 0, sizeof(remote_addr)); pid_t pid = fork();
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr(REMOTE_IP);
remote_addr.sin_port = htons(REMOTE_PORT);
printf("UDP Proxy running on 127.0.0.1:%d, forwarding to %s:%d\n", LOCAL_PORT, REMOTE_IP, REMOTE_PORT); if (pid == -1)
{
perror("fork");
exit(1);
}
while (1) { if (pid == 0)
FD_ZERO(&read_fds); {
FD_SET(local_socket, &read_fds); // Child process
FD_SET(remote_socket, &read_fds); struct sigaction sa;
sa.sa_handler = handle_signal;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
// Set up signal handler for SIGUSR1
if (sigaction(SIGUSR1, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}
int max_fd = (local_socket > remote_socket) ? local_socket : remote_socket; printf("Child waiting for signal and data...\n");
int activity = select(max_fd + 1, &read_fds, NULL, NULL, NULL); // Wait for the signal from parent
if (activity < 0) { while (!signal_received)
error_exit("select() error"); {
pause(); // Wait for signal
} }
// Traffic from local (127.0.0.1:1984) to remote (52.204.74.175:3389) // Once signal is received, read the integer data from the pipe
if (FD_ISSET(local_socket, &read_fds)) { close(pipefd[1]); // Close unused write end of the pipe
int received = recvfrom(local_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len); int received_data;
if (received < 0) { read(pipefd[0], &received_data, sizeof(received_data));
error_exit("recvfrom() failed");
}
buffer[received] = '\0'; // Null-terminate for safety printf("Child received data: %d\n", received_data);
close(pipefd[0]);
if (sendto(remote_socket, buffer, received, 0, (struct sockaddr *)&remote_addr, addr_len) < 0) { exit(0);
error_exit("sendto() to remote failed"); }
} else
{
// Parent process
close(pipefd[0]); // Close unused read end of the pipe
printf("Forwarded %d bytes from local to remote\n", received); sleep(1); // Give the child time to set up
}
// Traffic from remote (52.204.74.175:3389) back to local (127.0.0.1:1984) // The integer to send to the child
if (FD_ISSET(remote_socket, &read_fds)) { int data_to_send = 42;
int received = recvfrom(remote_socket, buffer, BUFFER_SIZE, 0, NULL, NULL);
if (received < 0) {
error_exit("recvfrom() from remote failed");
}
buffer[received] = '\0'; // Null-terminate for safety // Send the signal to the child to notify it
printf("Parent sending signal to child and writing data to pipe.\n");
kill(pid, SIGUSR1);
if (sendto(local_socket, buffer, received, 0, (struct sockaddr *)&client_addr, addr_len) < 0) { // Write the integer data to the pipe
error_exit("sendto() to local failed"); write(pipefd[1], &data_to_send, sizeof(data_to_send));
}
printf("Forwarded %d bytes from remote to local\n", received); close(pipefd[1]); // Close the write end of the pipe
}
}
close(local_socket); // Wait for the child to finish
close(remote_socket); wait(NULL);
printf("Parent finished.\n");
}
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment