Last active
October 7, 2015 08:17
-
-
Save grigorescu/3133526 to your computer and use it in GitHub Desktop.
ElasticSearch Autodiscovery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
int main(void) | |
{ | |
struct sockaddr_in socket_addr; | |
int socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0); | |
if (socketDescriptor < 0) { perror("Error creating socket - "); exit(1); } | |
socklen_t socketLen = sizeof(socket_addr); | |
memset(&socket_addr, 0, sizeof(struct sockaddr_in)); | |
socket_addr.sin_family = AF_INET; | |
socket_addr.sin_port = htons(54328); | |
socket_addr.sin_addr.s_addr = inet_addr("224.2.2.4"); | |
struct ip_mreq imr; | |
imr.imr_multiaddr.s_addr = inet_addr("224.2.2.4"); | |
imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
if (setsockopt(socketDescriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(struct ip_mreq)) < 0) { perror("Error in setsockopt (IP_ADD_MEMBERSHIP) - "); exit(1); } | |
unsigned char loop = 0; | |
setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); | |
if (bind(socketDescriptor, (struct sockaddr *) &socket_addr, sizeof(socket_addr)) < 0) { perror("Error binding - "); exit(1); } | |
int cnt = sendto(socketDescriptor, "{\"request\": {\"cluster_name\": \"elasticsearch\"} }\n", 48, 0, (struct sockaddr *) &socket_addr, socketLen); | |
if (cnt < 0) { perror("Error in sendto - "); exit(1); } | |
char buf[256]; | |
memset(&buf, 0, sizeof(buf)); | |
cnt = recvfrom(socketDescriptor, buf, sizeof(buf), 0, (struct sockaddr *) &socket_addr, &socketLen); | |
if (cnt < 0) { perror("Error in recvfrom - "); exit(1); } | |
printf("%s\n", buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment