Commit 0c771f35 by michaelpastushkov

notification tests

parent 7dfbe0ad
......@@ -58,8 +58,7 @@ struct struct_rc {
struct sockaddr_in my_addr;
struct hostent *remote_host;
int notify_socket;
struct sockaddr_un notify_addr;
int notify_sockets[2];
};
struct client_info {
......
#!/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
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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.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");
}
#include <signal.h>
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
local_addr.sin_port = htons(LOCAL_PORT);
// Global pipe file descriptors
int pipefd[2];
volatile sig_atomic_t signal_received = 0; // Signal flag for notification
// Bind the local socket to 127.0.0.1:1984
if (bind(local_socket, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
error_exit("Binding local socket failed");
}
// Signal handler that sets the flag when a signal is received
void handle_signal(int sig)
{
signal_received = 1;
}
// Create remote socket
if ((remote_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
error_exit("Remote socket creation failed");
int main()
{
// Create a pipe for parent-child communication
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(1);
}
memset(&remote_addr, 0, sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr(REMOTE_IP);
remote_addr.sin_port = htons(REMOTE_PORT);
pid_t pid = fork();
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) {
FD_ZERO(&read_fds);
FD_SET(local_socket, &read_fds);
FD_SET(remote_socket, &read_fds);
if (pid == 0)
{
// Child process
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);
if (activity < 0) {
error_exit("select() error");
// Wait for the signal from parent
while (!signal_received)
{
pause(); // Wait for signal
}
// Traffic from local (127.0.0.1:1984) to remote (52.204.74.175:3389)
if (FD_ISSET(local_socket, &read_fds)) {
int received = recvfrom(local_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (received < 0) {
error_exit("recvfrom() failed");
}
// Once signal is received, read the integer data from the pipe
close(pipefd[1]); // Close unused write end of the pipe
int received_data;
read(pipefd[0], &received_data, sizeof(received_data));
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) {
error_exit("sendto() to remote failed");
}
exit(0);
}
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)
if (FD_ISSET(remote_socket, &read_fds)) {
int received = recvfrom(remote_socket, buffer, BUFFER_SIZE, 0, NULL, NULL);
if (received < 0) {
error_exit("recvfrom() from remote failed");
}
// The integer to send to the child
int data_to_send = 42;
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) {
error_exit("sendto() to local failed");
}
// Write the integer data to the pipe
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);
close(remote_socket);
// Wait for the child to finish
wait(NULL);
printf("Parent finished.\n");
}
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