Browse Source

Work on p2p socket communication, Stylistic changes

master
Apostolos Fanakis 6 years ago
parent
commit
1a33ea4c39
  1. 169
      lib/helpers.c
  2. 31
      lib/helpers.h
  3. 145
      src/zaqar.c
  4. 6
      src/zaqar.h

169
lib/helpers.c

@ -1,56 +1,179 @@
#include "helpers.h" #include "helpers.h"
int create_socket(uint16_t port) { void set_timer_and_handler(void (*handler)(int), long int timer_interval) {
int sock; struct itimerval interval_timer;
struct sockaddr_in socket_name; struct sigaction signal_action;
// Installs handler as the signal handler for SIGALRM
memset(&signal_action, 0, sizeof(signal_action));
signal_action.sa_handler = handler;
if (sigaction(SIGALRM, &signal_action, NULL)) {
perror("Couldn't install function handler for SIGALRM signals.");
exit(EXIT_FAILURE);
}
// Sets an interval timer to deliver a SIGALRM signal every timer_interval seconds
interval_timer.it_interval.tv_usec = 0;
interval_timer.it_interval.tv_sec = timer_interval;
interval_timer.it_value.tv_usec = 0;
interval_timer.it_value.tv_sec = timer_interval;
if (setitimer(ITIMER_REAL, &interval_timer, NULL) == -1) {
perror("Couldn't set timer.");
exit(EXIT_FAILURE);
}
}
void enable_echo_broadcast(void) {
if (system("echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts") < 0) {
perror("Couldn't allow echo broadcasts.");
exit(EXIT_FAILURE);
}
}
/*
* Function based on this snippet:
* https://codereview.stackexchange.com/a/58107
*/
void search_for_neighbors(void) {
// Broadcasts ping
if (system("ping -b 10.255.255.255 -c 3 > /dev/null") < 0) {
perror("Couldn't broadcast echo.");
exit(EXIT_FAILURE);
}
// Reads the ARP file checking for connected devices
FILE *arpCache = fopen(ARP_CACHE, "r");
if (!arpCache) {
perror("ARP Cache: Failed to open file \"" ARP_CACHE "\"");
exit(EXIT_FAILURE);
}
// Ignores the first line, which contains the header
char header[ARP_BUFFER_LEN];
if (!fgets(header, sizeof(header), arpCache)) {
perror("Couldn't read ARP file header.");
exit(EXIT_FAILURE);
}
// Extracts IP addresses found in the file
char ipAddr[ARP_BUFFER_LEN];
int count = 0;
while (1 == fscanf(arpCache, ARP_LINE_FORMAT, ipAddr)) {
printf("%03d: [%s]\n", ++count, ipAddr);
}
fclose(arpCache);
}
/*
* Function based on this example:
* https://www.gnu.org/software/libc/manual/html_node/Inet-Example.html#Inet-Example
*/
int create_socket_and_listen(uint16_t port, uint8_t backlog_size) {
int in_sock;
struct sockaddr_in own_name;
// Creates the socket // Creates the socket
sock = socket (PF_INET, SOCK_STREAM, 0); //TODO: check if PF_INET is the right one in_sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0) { if (in_sock < 0) {
perror("Couldn't create the socket."); perror("Couldn't create the socket.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Gives the socket a name // Gives the socket a name
socket_name.sin_family = AF_INET; own_name.sin_family = AF_INET;
socket_name.sin_port = htons(port); own_name.sin_port = htons(port);
socket_name.sin_addr.s_addr = htonl(INADDR_ANY); own_name.sin_addr.s_addr = htonl(INADDR_ANY);
// Binds own address structure to socket // Binds own address structure to socket
if (bind(sock, (struct sockaddr *) &socket_name, sizeof(socket_name)) < 0) { if (bind(in_sock, (struct sockaddr *) &own_name, sizeof(own_name)) < 0) {
perror("Couldn't bind the address structure to the socket."); perror("Couldn't bind the address structure to the socket.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return sock; if (listen(in_sock, backlog_size) < 0) {
perror("Couldn't listen for connections on the socket.");
exit(EXIT_FAILURE);
}
return in_sock;
}
void send_message(const char *ipv4, uint16_t port, const char *message) {
int out_sock;
struct sockaddr_in peer_name;
// Creates the socket
out_sock = socket(PF_INET, SOCK_STREAM, 0);
if (out_sock < 0) {
perror("Couldn't create the socket.");
exit(EXIT_FAILURE);
}
// Connects to the peer
init_sockaddr(&peer_name, ipv4, port);
if (connect(out_sock, (struct sockaddr *) &peer_name, sizeof(peer_name))) {
printf("Couldn't connect to the peer.\n");
} else {
// Sends data to the peer
write_to_peer(out_sock, message);
}
close(out_sock);
}
void accept_connection(int sock, struct sockaddr_in *peer_name, fd_set *active_fd_set) {
size_t peer_name_size = sizeof((*peer_name));
int comm_socket = accept(sock, (struct sockaddr *) peer_name, &peer_name_size);
if (comm_socket < 0) {
perror("Couldn't accept the connection.");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Connected to host %s, port %hd.\n",
inet_ntoa((*peer_name).sin_addr), ntohs((*peer_name).sin_port));
FD_SET(comm_socket, active_fd_set);
}
void write_to_peer(int file_desc, const char *message) {
int num_bytes = write(file_desc, message, strlen(message) + 1);
if (num_bytes < 0) {
perror("Couldn't write to peer.");
exit(EXIT_FAILURE);
}
} }
int read_from_client(int file_des, uint16_t max_line) { int read_from_peer(int file_des, uint16_t max_line) {
char buffer[max_line]; char buffer[max_line];
int nbytes; int num_bytes;
nbytes = read(file_des, buffer, sizeof(buffer)); num_bytes = read(file_des, buffer, sizeof(buffer));
if (nbytes < 0) { if (num_bytes < 0) {
perror("Couldn't read from client."); perror("Couldn't read from peer.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (nbytes == 0) } else if (num_bytes == 0)
// End-of-file // End-of-file
return -1; return -1;
else { else {
fprintf(stderr, "Server: got message: `%s'\n", buffer); fprintf(stderr, "Got message: `%s'\n", buffer);
return 0; return 0;
} }
} }
void init_sockaddr(struct sockaddr_in *name, const char *hostname, uint16_t port) { /*
* Function based on this example:
* https://www.gnu.org/software/libc/manual/html_node/Inet-Example.html#Inet-Example
*/
void init_sockaddr(struct sockaddr_in *peer_name, const char *ipv4, uint16_t port) {
struct hostent *hostinfo; struct hostent *hostinfo;
name->sin_family = AF_INET; peer_name->sin_family = AF_INET;
name->sin_port = htons(port); peer_name->sin_port = htons(port);
hostinfo = gethostbyname(hostname); hostinfo = gethostbyname(ipv4);
if (hostinfo == NULL) { if (hostinfo == NULL) {
fprintf(stderr, "Unknown host %s.\n", hostname); fprintf(stderr, "Unknown host %s.\n", ipv4);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
name->sin_addr = *(struct in_addr *) hostinfo->h_addr_list[0]; peer_name->sin_addr = *(struct in_addr *) hostinfo->h_addr_list[0];
} }

31
lib/helpers.h

@ -11,10 +11,37 @@
#include <netdb.h> #include <netdb.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
int create_socket(uint16_t port); // Macros to turn a numeric macro into a string literal
#define xstr(s) str(s)
#define str(s) #s
int read_from_client(int file_des, uint16_t max_line); // Neighbor discovery related definitions
#define ARP_CACHE "/proc/net/arp"
#define ARP_STRING_LEN 1023
#define ARP_BUFFER_LEN (ARP_STRING_LEN + 1)
// Format for fscanf() to read the 1st field of ARP
#define ARP_LINE_FORMAT "%" xstr(ARP_STRING_LEN) "s %*s %*s %*s %*s %*s"
void set_timer_and_handler(void (*handler)(int), long int timer_interval);
void enable_echo_broadcast(void);
void search_for_neighbors(void);
int create_socket_and_listen(uint16_t port, uint8_t backlog_size);
void send_message(const char *ipv4, uint16_t port, const char *message);
void accept_connection(int sock, struct sockaddr_in *client_name, fd_set *active_fd_set);
void write_to_peer(int filedes, const char *message);
int read_from_peer(int file_des, uint16_t max_line);
void init_sockaddr(struct sockaddr_in *name, const char *hostname, uint16_t port); void init_sockaddr(struct sockaddr_in *name, const char *hostname, uint16_t port);

145
src/zaqar.c

@ -2,154 +2,75 @@
#define MESSAGE "You got mail!!! xo xo xo" #define MESSAGE "You got mail!!! xo xo xo"
volatile sig_atomic_t out_flag = false; volatile sig_atomic_t sigalrm_flag = false;
void write_to_server(int filedes) {
int nbytes;
nbytes = write(filedes, MESSAGE, strlen(MESSAGE) + 1);
if (nbytes < 0) {
perror("write");
exit(EXIT_FAILURE);
}
}
void handle_alarm(int sig) {
if (sig == SIGALRM) {
out_flag = true;
int sock;
struct sockaddr_in servername;
printf("GOT_CL1\n");
/* Create the socket. */
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket (client)");
exit(EXIT_FAILURE);
}
/* Connect to the server. */
init_sockaddr (&servername, "10.0.88.79", PORT);
if (connect(sock, (struct sockaddr *) &servername, sizeof (servername))) {
printf("GOT_CL2\n");
//perror("connect (client)");
//exit(EXIT_FAILURE);
} else {
printf("GOT_CL3\n");
/* Send data to the server. */
write_to_server(sock);
}
//close (sock);
}
}
int main(void) { int main(void) {
int sock; int in_sock;
fd_set active_fd_set, read_fd_set; fd_set active_fd_set, read_fd_set;
struct sockaddr_in client_name; struct sockaddr_in peer_name;
size_t size;
alarm(0);
//signal(SIGALRM, handle_alarm);
struct itimerval new;
new.it_interval.tv_usec = 0;
new.it_interval.tv_sec = 8;
new.it_value.tv_usec = 0;
new.it_value.tv_sec = 8;
if (setitimer (ITIMER_REAL, &new, NULL) == -1) { // Sets a timer and handler to produce interrupts for sending messages
perror("Couldn't set timer."); set_timer_and_handler(handle_alarm, TIMER_INTERVAL);
exit(EXIT_FAILURE);
}
sigset_t mask;
int sfd;
struct signalfd_siginfo fdsi;
sigemptyset(&mask); // Searches network for neighbors
sigaddset(&mask, SIGALRM); enable_echo_broadcast();
/*if (sigprocmask(SIGALRM, &mask, NULL) == -1) search_for_neighbors();
perror("sigprocmask");*/
sfd = signalfd(-1, &mask, 0); // Creates a socket and sets it up to accept connections
if (sfd == -1) in_sock = create_socket_and_listen(PORT, BACKLOG_SIZE);
perror("signalfd");
// Creates the socket and sets it up to accept connections
sock = create_socket(PORT);
if (listen(sock, BACKLOG_SIZE) < 0) {
perror("Couldn't listen for connections on the socket.");
exit(EXIT_FAILURE);
}
printf("GOT_SV1\n");
// Initializes the set of active sockets // Initializes the set of active sockets
// Clears the descriptor set // Clears the descriptor set
FD_ZERO(&active_fd_set); FD_ZERO(&active_fd_set);
// Sets socket in active readset // Sets socket in active readset
FD_SET(sock, &active_fd_set); FD_SET(in_sock, &active_fd_set);
FD_SET(sfd, &active_fd_set);
while (1) { while (1) {
printf("GOT_SV2\n"); if (sigalrm_flag) {
// It's time to send a message!
send_message("10.0.82.61", PORT, MESSAGE);
sigalrm_flag = false;
}
// Shallow copies the readset // Shallow copies the readset
read_fd_set = active_fd_set; read_fd_set = active_fd_set;
// Blocks until input arrives on one or more active sockets // Blocks until input arrives on one or more active sockets
if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) { if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
if (out_flag) { // Handles the wake-up from alarm signal interrupts
printf("GOT_SV2.1\n"); if (sigalrm_flag) {
out_flag = false;
continue; continue;
} }
printf("GOT_SV2.2\n");
perror("Couldn't initiate synchronous I/O multiplexing."); perror("Couldn't initiate synchronous I/O multiplexing.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Services all the sockets with input pending // Services all the sockets with input pending
for (int i = 0; i < FD_SETSIZE; ++i) { //TODO: is this needed? for (int i = 0; i < FD_SETSIZE; ++i) {
if (FD_ISSET(i, &read_fd_set)) { if (FD_ISSET(i, &read_fd_set)) {
printf("GOT_SV3\n"); if (i == in_sock) {
if (i == sock) {
// Connection request on original socket // Connection request on original socket
size = sizeof(client_name); accept_connection(in_sock, &peer_name, &active_fd_set);
int new = accept(sock, (struct sockaddr *) &client_name, &size);
if (new < 0) {
perror("Couldn't accept the connection.");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Server: connect from host %s, port %hd.\n",
inet_ntoa(client_name.sin_addr), ntohs(client_name.sin_port));
FD_SET(new, &active_fd_set);
} else if (i == sfd) {
ssize_t s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo))
perror("read");
if (fdsi.ssi_signo == SIGALRM) {
printf("Got SIGALRM\n");
handle_alarm(SIGALRM);
} else {
printf("Read unexpected signal\n");
}
} else { } else {
// Data arriving on an already-connected socket // Data arriving on an already-connected socket
if (read_from_client(i, MAXLINE) < 0) { if (read_from_peer(i, MAXLINE) < 0) {
close(i); close(i);
FD_CLR(i, &active_fd_set); FD_CLR(i, &active_fd_set);
} }
} }
} }
} }
printf("GOT_SV4\n");
} }
return 0; return 0;
} }
void handle_alarm(int sig) {
if (sig != SIGALRM) {
return;
} else {
sigalrm_flag = true;
}
}

6
src/zaqar.h

@ -2,8 +2,6 @@
#define ZAQAR_H_ #define ZAQAR_H_
#include <stdbool.h> #include <stdbool.h>
#include <signal.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@ -14,14 +12,16 @@
#include <netdb.h> #include <netdb.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/time.h>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include "helpers.h" #include "helpers.h"
#define TIMER_INTERVAL 10
#define PORT 5000 #define PORT 5000
#define MAXLINE 1024 #define MAXLINE 1024
#define BACKLOG_SIZE 2 #define BACKLOG_SIZE 2
void handle_alarm(int sig);
#endif //ZAQAR_H_ #endif //ZAQAR_H_

Loading…
Cancel
Save