Last active
August 29, 2015 13:58
-
-
Save Hugne/9936175 to your computer and use it in GitHub Desktop.
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 <sys/types.h> | |
#include <sys/socket.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#include <string.h> | |
#include <sys/param.h> | |
#include <sys/poll.h> | |
#include <netdb.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <linux/tipc.h> | |
int main(int argc, char *argv[], char *dummy[]) | |
{ | |
struct sockaddr_tipc topsrv; | |
struct tipc_subscr subscr; | |
struct tipc_event event; | |
int sd; | |
printf("TIPC Topology subscriber started\n"); | |
/* Connect to topology server */ | |
memset(&topsrv, 0, sizeof(topsrv)); | |
topsrv.family = AF_TIPC; | |
topsrv.addrtype = TIPC_ADDR_NAME; | |
topsrv.addr.name.name.type = TIPC_TOP_SRV; | |
topsrv.addr.name.name.instance = TIPC_TOP_SRV; | |
sd = socket (AF_TIPC, SOCK_SEQPACKET, 0); | |
if (0 > connect(sd, (struct sockaddr *)&topsrv, sizeof(topsrv))) { | |
perror("Client: failed to connect to topology server"); | |
exit(1); | |
} | |
printf("Client: connected to topology server\n"); | |
/* Subscribe to link state events */ | |
subscr.seq.type = htonl(TIPC_LINK_STATE); | |
subscr.seq.lower = htonl(0); | |
subscr.seq.upper = htonl(~0); | |
subscr.timeout = htonl(TIPC_WAIT_FOREVER); | |
subscr.filter = htonl(TIPC_SUB_PORTS); | |
subscr.usr_handle[0] = (char)4; | |
if (send(sd, &subscr, sizeof(subscr), 0) != sizeof(subscr)) { | |
perror("Client: failed to subscribe to link state events on plane A"); | |
exit(1); | |
} | |
printf("Client: subscriptions remain active until client is killed\n"); | |
/* Now wait for the subscriptions to fire */ | |
while (recv(sd, &event, sizeof(event), 0) == sizeof(event)) { | |
unsigned int type = ntohl(event.s.seq.type); | |
unsigned int peer = ntohl(event.found_lower); | |
unsigned int bearer_id = ntohl(event.port.ref); | |
if (type == TIPC_LINK_STATE) { | |
struct tipc_sioc_ln_req lnr; | |
memset(&lnr,0,sizeof(lnr)); | |
lnr.peer = peer; | |
lnr.bearer_id = bearer_id; | |
if (ioctl(sd, SIOCGETLINKNAME, &lnr) < 0) { | |
perror("ioctl"); | |
exit(1); | |
} | |
if (event.event == htonl(TIPC_PUBLISHED)) | |
printf("Link: <%s> is UP\n", lnr.linkname); | |
else if(event.event == htonl(TIPC_WITHDRAWN)) | |
printf("Link: <%s> is DOWN\n", lnr.linkname); | |
} | |
} | |
perror("Client: failed to receive event"); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment