Implémentation de poll() terminé, ajouter les nouvelles fonctions et
leurs signtaure à faire
This commit is contained in:
parent
eefea551f7
commit
db3875205e
207
src/node.c
207
src/node.c
@ -7,6 +7,11 @@
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "node.h"
|
||||
#include "tlv.h"
|
||||
@ -551,35 +556,191 @@ void listen_for_packets(){
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
printf(">> Starting node\n");
|
||||
int cont = 1;
|
||||
int run_node(int sock_fd, struct sockaddr_in6 * peer){
|
||||
printf(">> Boostrapping node...\n");
|
||||
|
||||
while(cont){
|
||||
int ret;
|
||||
ssize_t bytes;
|
||||
char input_buffer[1024];
|
||||
char output_buffer[1024];
|
||||
struct pollfd fds[2];
|
||||
|
||||
// We create the neighbourhood table
|
||||
// neighbour_peer neighbour_list[NEIGHBOUR_MAX];
|
||||
// We create the message table
|
||||
/* Descriptor zero is stdin */
|
||||
fds[0].fd = 0;
|
||||
fds[1].fd = sock_fd;
|
||||
fds[0].events = POLLIN | POLLPRI;
|
||||
fds[1].events = POLLIN | POLLPRI;
|
||||
|
||||
// We create our own message.
|
||||
/* Normally we'd check an exit condition, but for this example
|
||||
* we loop endlessly.
|
||||
*/
|
||||
while (1) {
|
||||
/* Call poll() */
|
||||
ret = poll(fds, 2, -1);
|
||||
|
||||
// Listen for incoming packets
|
||||
listen_for_packets();
|
||||
if (ret < 0) {
|
||||
printf("Error - poll returned error: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
if (ret > 0) {
|
||||
|
||||
// This is in it's own fork.
|
||||
time_t delay = time(NULL) + 20;
|
||||
while(! (delay < time(NULL))) {
|
||||
// Theses functions are there for general book-keeping,and run in there own
|
||||
// thread, being run every 20 seconds.
|
||||
// Every 20 sec, if we have less than 5 neighbours, we ask for more peers
|
||||
// by sending out a TLV neighbour Request at a random peer.
|
||||
t_ask_for_more_peers();
|
||||
// Every 20 sec, we also check for a peer that didn't emit a new message for
|
||||
// the past 70 sec, if he's temporary, we delete him from the neighbourhood.
|
||||
t_update_neighbours();
|
||||
// We send out a TLV Network hash to get an ideal of the network state.
|
||||
t_get_network_state();
|
||||
/* Regardless of requested events, poll() can always return these */
|
||||
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
printf("Error - poll indicated stdin error\n");
|
||||
break;
|
||||
}
|
||||
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
printf("Error - poll indicated socket error\n");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check if data to read from stdin */
|
||||
printf(">> ");
|
||||
if (fds[0].revents & (POLLIN | POLLPRI)) {
|
||||
bytes = read(0, output_buffer, sizeof(output_buffer));
|
||||
if (bytes < 0) {
|
||||
printf("Error - stdin error: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
// // Reset buffer
|
||||
// // memset(&output_buffer, '\0', 1024);
|
||||
//
|
||||
// // Build packet
|
||||
// struct packet message;
|
||||
// message.magic = 95;
|
||||
// message.version = 1;
|
||||
// message.body = malloc(20);
|
||||
//
|
||||
// // Build TLV
|
||||
// struct neighbour_req *neigh_req = (neighbour_req*) malloc(sizeof(neighbour_req));
|
||||
//
|
||||
// neigh_req->type = 2;
|
||||
// neigh_req->length = 0;
|
||||
//
|
||||
// message.length = 2 * sizeof(unsigned char);
|
||||
// printf("(%i)\n", message.length );
|
||||
// // message.body = (char *) neigh_req;
|
||||
//
|
||||
// memcpy(message.body, &neigh_req, sizeof(struct neighbour_req));
|
||||
// memcpy(&output_buffer, &message, sizeof(struct packet));
|
||||
|
||||
struct iovec vec_buff_reply = {.iov_len = sizeof(output_buffer), .iov_base = output_buffer};
|
||||
|
||||
struct sockaddr_in6 ntp_server_adress;
|
||||
memset(&ntp_server_adress, 0, sizeof(ntp_server_adress));
|
||||
|
||||
int inet_p = inet_pton(AF_INET6, "2a00:1080:800::6:1", &ntp_server_adress.sin6_addr);
|
||||
if(inet_p < 1){
|
||||
perror(">> Failed inet_pton.");
|
||||
exit(1);
|
||||
}
|
||||
ntp_server_adress.sin6_family = AF_INET6;
|
||||
ntp_server_adress.sin6_port = htons(1212); // NTP port number.
|
||||
|
||||
const struct msghdr reply = {
|
||||
.msg_name = &ntp_server_adress,
|
||||
.msg_namelen = sizeof(ntp_server_adress),
|
||||
.msg_iov = &vec_buff_reply,
|
||||
.msg_iovlen = 1
|
||||
};
|
||||
|
||||
printf("Sending: %li bytes as : %s\n", sizeof(output_buffer), output_buffer);
|
||||
// bytes = sendto(sock_fd, output_buffer, sizeof(output_buffer), 0, (struct sockaddr *)peer, sizeof(struct sockaddr_in));
|
||||
bytes = sendmsg((int) sock_fd, &reply, 0);
|
||||
if (bytes < 0) {
|
||||
perror("deded");
|
||||
printf("Error - sendto error: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if data to read from socket */
|
||||
if (fds[1].revents & (POLLIN | POLLPRI)) {
|
||||
bytes = recvfrom(sock_fd, input_buffer, sizeof(input_buffer),
|
||||
0, NULL, NULL);
|
||||
if (bytes < 0) {
|
||||
printf("Error - recvfrom error: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
if (bytes > 0) {
|
||||
printf("Received: %.*s\r", (int)bytes, input_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
printf(">> Starting node\n");
|
||||
int cont = 1;
|
||||
|
||||
unsigned long local_port = 1212;
|
||||
unsigned long remote_port = 1212;
|
||||
|
||||
int sock_fd;
|
||||
|
||||
struct sockaddr_in6 server_addr;
|
||||
struct sockaddr_in6 peer_addr;
|
||||
|
||||
memset(&peer_addr, 0, sizeof(peer_addr));
|
||||
|
||||
peer_addr.sin6_family = AF_INET6;
|
||||
peer_addr.sin6_port = htons(remote_port);
|
||||
if (inet_pton(AF_INET6, "2001:660:3301:9200::51c2:1b9b", &peer_addr.sin6_addr) < 0) {
|
||||
printf("Error - invalid remote address '%s'\n", "2001:660:3301:9200::51c2:1b9b");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Create UDP socket */
|
||||
sock_fd = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if (sock_fd < 0) {
|
||||
printf("Error - failed to open socket: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Bind socket */
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin6_family = AF_INET6;
|
||||
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
|
||||
server_addr.sin6_port = htons(local_port);
|
||||
if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
||||
printf("Error - failed to bind socket: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
run_node(sock_fd, &peer_addr);
|
||||
|
||||
|
||||
close(sock_fd);
|
||||
//
|
||||
// while(cont){
|
||||
//
|
||||
// // We create the neighbourhood table
|
||||
// // neighbour_peer neighbour_list[NEIGHBOUR_MAX];
|
||||
// // We create the message table
|
||||
//
|
||||
// // We create our own message.
|
||||
//
|
||||
// // Listen for incoming packets
|
||||
// // listen_for_packets();
|
||||
// //
|
||||
// // // This is in it's own fork.
|
||||
// // time_t delay = time(NULL) + 20;
|
||||
// // while(! (delay < time(NULL))) {
|
||||
// // // Theses functions are there for general book-keeping,and run in there own
|
||||
// // // thread, being run every 20 seconds.
|
||||
// // // Every 20 sec, if we have less than 5 neighbours, we ask for more peers
|
||||
// // // by sending out a TLV neighbour Request at a random peer.
|
||||
// // t_ask_for_more_peers();
|
||||
// // // Every 20 sec, we also check for a peer that didn't emit a new message for
|
||||
// // // the past 70 sec, if he's temporary, we delete him from the neighbourhood.
|
||||
// // t_update_neighbours();
|
||||
// // // We send out a TLV Network hash to get an ideal of the network state.
|
||||
// // t_get_network_state();
|
||||
// }
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user