Niclas Antti c447e5cf15 Uncrustify maxscale
See script directory for method. The script to run in the top level
MaxScale directory is called maxscale-uncrustify.sh, which uses
another script, list-src, from the same directory (so you need to set
your PATH). The uncrustify version was 0.66.
2018-09-09 22:26:19 +03:00

60 lines
1.3 KiB
C++

/*
* Find local ip used as source ip in ip packets.
* Use getsockname and a udp connection
*/
#include <stdio.h> // printf
#include <string.h> // memset
#include <errno.h> // errno
#include <sys/socket.h> // socket
#include <netinet/in.h> // sockaddr_in
#include <arpa/inet.h> // getsockname
#include <unistd.h> // close
#include "get_my_ip.h"
int get_my_ip(char* remote_ip, char* my_ip)
{
int dns_port = 53;
struct sockaddr_in serv;
int sock = socket (AF_INET, SOCK_DGRAM, 0);
// Socket could not be created
if (sock < 0)
{
return 1;
}
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(remote_ip);
serv.sin_port = htons(dns_port);
connect(sock, (const struct sockaddr*) &serv, sizeof(serv));
struct sockaddr_in name;
socklen_t namelen = sizeof(name);
getsockname(sock, (struct sockaddr*) &name, &namelen);
char buffer[100];
const char* p = inet_ntop(AF_INET, &name.sin_addr, buffer, 100);
if (p != NULL)
{
// printf("Local ip is : %s \n" , buffer);
strcpy(my_ip, buffer);
close(sock);
return 0;
}
else
{
// Some error
printf ("Error number : %d . Error message : %s \n", errno, strerror(errno));
close(sock);
return 2;
}
}