-
-
Save netskink/4f554ed6657954b17ab255ad5bc6d1f0 to your computer and use it in GitHub Desktop.
can get mac address but how to get ip address?
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
/* ipchange.c | |
* | |
* Netlink based IP monitor on specific interface. | |
* Executes command on new IPv4 address. | |
* | |
* Sebastian Kricner | |
* tuxwave.net | |
* June 2014 | |
* | |
*/ | |
#include <libnl3/netlink/netlink.h> | |
//#include <libnl3/netlink/socket.h> | |
// #include <libnl3/netlink/msg.h> | |
// #include <libnl3/netlink/errno.h> | |
#include <libnl3/netlink/route/link.h> | |
#include <libnl3/netlink/route/addr.h> | |
// #include <libnl3/netlink/route/addr.h> | |
// #include <libnl3/netlink/route/qdisc.h> | |
// | |
// #include <net/if.h> | |
// | |
#include <errno.h> | |
// #include <unistd.h> | |
// #include <stdlib.h> | |
// #include <syslog.h> | |
// #include <sys/types.h> | |
// #include <string.h> | |
/* | |
gcc ipchange.c -o ipchange $(pkg-config --cflags --libs libnl-3.0 libnl-route-3.0 libnl-cli-3.0) | |
*/ | |
int main(int argc, char **argv, char **envp) { | |
int err; | |
struct nl_sock *sock; | |
struct nl_cache *link_cache; | |
struct nl_cache *addr_cache; | |
struct rtnl_addr *addr; | |
struct nl_addr *p_nl_addr; | |
struct rtnl_link *p_rtnl_link; | |
struct addrinfo *result; | |
char *pchLinkName; | |
char *pchLinkAddr; | |
char *pchIPAddr; | |
char *interface; | |
interface = "enp6s0"; | |
pchLinkAddr = malloc(40); | |
pchIPAddr = malloc(40); | |
strcpy(pchLinkAddr,"11:22:33:44:55:66"); | |
strcpy(pchIPAddr,"123.456.789.abc"); | |
sock = nl_socket_alloc(); | |
if (!sock) { | |
fprintf(stderr, "Could not allocate netlink socket.\n"); | |
exit(ENOMEM); | |
} | |
// Connect to socket | |
if(err = nl_connect(sock, NETLINK_ROUTE)) { | |
fprintf(stderr, "netlink error: %s\n", nl_geterror(err)); | |
sock = NULL; | |
exit(err); | |
} | |
// Either choice, the result below is a mac address | |
//err = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache); | |
err = rtnl_link_alloc_cache(sock, AF_INET, &link_cache); | |
if (0 != err) { | |
/* error */ | |
printf("rtnl_link_alloc_cache failed: %s\n", nl_geterror(err)); | |
return(EXIT_FAILURE); | |
} | |
err = rtnl_addr_alloc_cache(sock, &addr_cache); | |
if (0 != err) { | |
/* error */ | |
printf("rtnl_addr_alloc_cache failed: %s\n", nl_geterror(err)); | |
return(EXIT_FAILURE); | |
} | |
p_rtnl_link = rtnl_link_get_by_name(link_cache, "enp6s0"); | |
if (NULL == p_rtnl_link) { | |
/* error */ | |
printf("rtnl_link_get_by_name failed\n"); | |
return(EXIT_FAILURE); | |
} | |
pchLinkName = rtnl_link_get_name(p_rtnl_link); | |
if (NULL == pchLinkName) { | |
/* error */ | |
printf("rtnl_link_get_name failed\n"); | |
return(EXIT_FAILURE); | |
} | |
printf("Returned link name is %s\n",pchLinkName); | |
////////////////////////////////// mac address | |
p_nl_addr = rtnl_link_get_addr(p_rtnl_link); | |
if (NULL == p_nl_addr) { | |
/* error */ | |
printf("rtnl_link_get_addr failed\n"); | |
return(EXIT_FAILURE); | |
} | |
pchLinkAddr = nl_addr2str(p_nl_addr, pchLinkAddr, 40); | |
if (NULL == pchLinkAddr) { | |
/* error */ | |
printf("rtnl_link_get_name failed\n"); | |
return(EXIT_FAILURE); | |
} | |
printf("Returned link addr is %s\n",pchLinkAddr); | |
////////////////////////////////// ip address | |
// How to get ip address for a specified interface? | |
return(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment