Files
MaxScale/maxscale-system-test/get_my_ip.cpp
Timofey Turenko cd732ac14f MXS-2243 System tests brings VMs by themselves (#193)
maxscale-system-test changed in order to control test environment by itself.
Every test checks which machines are running, compare with list of needed machines
and start new VMs is they are missing in the running machines list.
Tests are executiong MDBCI commands, MDBCI executable should be in the PATH
2019-04-02 13:27:34 +03:00

61 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;
}
}