11#include < sys/types.h>
22#include < sys/socket.h>
3+ #include < errno.h>
34#include < netdb.h>
45#include < unistd.h>
56#include < thread>
67#include < chrono>
8+ #include < cstring>
9+ #include < netinet/in.h>
10+ #include < arpa/inet.h>
711
812#include " Router.h"
913#include " Manager.h"
@@ -19,7 +23,7 @@ Router::Router(int qsize, std::string port) {
1923 this ->logger = Logger (" Router" );
2024 this ->port = port;
2125 // initialize the socket
22- this ->init_socket ();
26+ this -> listening_socket_fd = this ->init_socket ();
2327}
2428
2529Router::~Router () {
@@ -29,9 +33,17 @@ Router::~Router() {
2933 freeaddrinfo (addr);
3034}
3135
36+ void *get_in_addr (struct sockaddr *sa) {
37+ if (sa->sa_family == AF_INET) {
38+ return &(((struct sockaddr_in *)sa)->sin_addr );
39+ }
40+
41+ return &(((struct sockaddr_in6 *)sa)->sin6_addr );
42+ }
43+
3244void Router::watch () {
3345 socklen_t addr_size;
34- struct sockaddr_storage incoming_connection_info ;
46+ struct sockaddr_storage client ;
3547 int handling_socket;
3648
3749 this ->logger .debug (" watching port: " + this ->port );
@@ -44,15 +56,20 @@ void Router::watch() {
4456 // listen for new connections
4557 listen (this ->listening_socket_fd , this ->queue_size );
4658
47- addr_size = sizeof incoming_connection_info;
48- handling_socket = accept (listening_socket_fd, (struct sockaddr *) &incoming_connection_info, &addr_size);
49- this ->logger .info (" Handling incoming connection from: " );
59+ addr_size = sizeof client;
60+ handling_socket = accept (this ->listening_socket_fd , (struct sockaddr *) &client, &addr_size);
61+ if (handling_socket < 0 ) {
62+ char * err = std::strerror (errno);
63+ throw RouterException (" Error when accepting connection: " + std::string (err ? err : " unknown error" ));
64+ }
65+
66+ struct sockaddr_in *sin = (struct sockaddr_in *)&client;
67+ char client_addr[INET6_ADDRSTRLEN];
68+ inet_ntop (AF_INET, &sin->sin_addr , client_addr, sizeof client_addr);
69+ this ->logger .info (" Handling incoming connection from: " + std::string (client_addr));
5070 // this will create a new worker to work with
51- // TODO: handle too many workers exception
71+ // TODO: add try/catch and handle too many workers exception
5272 manager.handle_request (handling_socket);
53-
54- // sleep a little
55- std::this_thread::sleep_for (std::chrono::seconds (1 ));
5673 }
5774}
5875
@@ -70,9 +87,18 @@ int Router::init_socket() {
7087
7188 // TODO error handling (what if we cannot open the socket)
7289 int listening_socket = socket (this ->addr ->ai_family , this ->addr ->ai_socktype , this ->addr ->ai_protocol );
90+ if (listening_socket < 0 ) {
91+ char * err = std::strerror (errno);
92+ throw RouterException (" Error opening socket: " + std::string (err ? err : " unknown error" ));
93+ }
7394
7495 // TODO error handling (what if we can't bind)
75- bind (listening_socket, this ->addr ->ai_addr , this ->addr ->ai_addrlen );
96+ int check = bind (listening_socket, this ->addr ->ai_addr , this ->addr ->ai_addrlen );
97+ if (check < 0 ) {
98+ char * err = std::strerror (errno);
99+ throw RouterException (" Error binding socket: " + std::string (err ? err : " unknown error" ));
100+ }
76101
102+ this ->logger .debug (" Got socket: " + std::to_string (listening_socket));
77103 return listening_socket;
78104}
0 commit comments