add: helpers
unstable: discovering
This commit is contained in:
		
							parent
							
								
									d17500a3b0
								
							
						
					
					
						commit
						2d24339421
					
				
					 16 changed files with 205 additions and 81 deletions
				
			
		
							
								
								
									
										43
									
								
								helpers/bind_tcp_server.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								helpers/bind_tcp_server.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
#include <netdb.h>
 | 
			
		||||
#include <trantor/utils/Logger.h>
 | 
			
		||||
#include <helpers.h>
 | 
			
		||||
 | 
			
		||||
#include "config.h"
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
helpers::bind_tcp_server(const char *addr, const char *port, int max_client_backlog)
 | 
			
		||||
{
 | 
			
		||||
    struct addrinfo hints, *res;
 | 
			
		||||
    int fd;
 | 
			
		||||
    int status;
 | 
			
		||||
 | 
			
		||||
    memset(&hints, 0, sizeof hints);
 | 
			
		||||
    hints.ai_family = AF_UNSPEC;
 | 
			
		||||
    hints.ai_socktype = SOCK_STREAM;
 | 
			
		||||
    hints.ai_flags = AI_PASSIVE;
 | 
			
		||||
 | 
			
		||||
    if ((status = getaddrinfo(addr, port, &hints, &res)) != 0)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error getting address info: " << gai_strerror(status);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 | 
			
		||||
 | 
			
		||||
    if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error binding socket. " << status;
 | 
			
		||||
        freeaddrinfo(res);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((status = listen(fd, max_client_backlog)) == -1)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error setting up listener. " << status;
 | 
			
		||||
        freeaddrinfo(res);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    freeaddrinfo(res);
 | 
			
		||||
 | 
			
		||||
    return fd;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								helpers/get_server_port.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								helpers/get_server_port.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
#include <netdb.h>
 | 
			
		||||
#include <helpers.h>
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
helpers::get_server_port(int fd)
 | 
			
		||||
{
 | 
			
		||||
    if(fd == -1)
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    struct sockaddr_in sin;
 | 
			
		||||
    socklen_t addrlen = sizeof(sin);
 | 
			
		||||
    if(getsockname(fd, (struct sockaddr *)&sin, &addrlen) == 0)
 | 
			
		||||
    {
 | 
			
		||||
        return sin.sin_port;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										40
									
								
								helpers/send_udp_broadcast.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								helpers/send_udp_broadcast.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
#include <netdb.h>
 | 
			
		||||
#include <arpa/inet.h>
 | 
			
		||||
#include <trantor/utils/Logger.h>
 | 
			
		||||
#include "config.h"
 | 
			
		||||
#include <helpers.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
helpers::send_udp_broadcast(const char *addr, int port, const char* message)
 | 
			
		||||
{
 | 
			
		||||
    struct sockaddr_in their_addr;
 | 
			
		||||
    int fd;
 | 
			
		||||
 | 
			
		||||
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error creating socket";
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int broadcast = 1;
 | 
			
		||||
    if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) < 0)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error setting broadcast";
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    memset(&their_addr, 0, sizeof(their_addr));
 | 
			
		||||
    their_addr.sin_family = AF_INET;
 | 
			
		||||
    their_addr.sin_port = htons(port);
 | 
			
		||||
    their_addr.sin_addr.s_addr = inet_addr(addr);
 | 
			
		||||
 | 
			
		||||
    if(sendto(fd, message, strlen(message), 0, (struct sockaddr *)&their_addr, sizeof(their_addr)) < 0)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR << "Error sending broadcast " << errno << " " << strerror(errno);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    close(fd);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue