libmnl  1.0.3
rtnl-route-add.c
00001 /* This example is placed in the public domain. */
00002 #include <netinet/in.h>
00003 #include <arpa/inet.h>
00004 #include <time.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <strings.h>
00009 #include <net/if.h>
00010 
00011 #include <libmnl/libmnl.h>
00012 #include <linux/if_link.h>
00013 #include <linux/rtnetlink.h>
00014 
00015 int main(int argc, char *argv[])
00016 {
00017         if (argc <= 3) {
00018                 printf("Usage: %s iface destination cidr [gateway]\n", argv[0]);
00019                 printf("Example: %s eth0 10.0.1.12 32 10.0.1.11\n", argv[0]);
00020                 exit(EXIT_FAILURE);
00021         }
00022 
00023         int iface;
00024         iface = if_nametoindex(argv[1]);
00025         if (iface == 0) {
00026                 printf("Bad interface name\n");
00027                 exit(EXIT_FAILURE);
00028         }
00029 
00030         in_addr_t dst;
00031         if (!inet_pton(AF_INET, argv[2], &dst)) {
00032                 printf("Bad destination\n");
00033                 exit(EXIT_FAILURE);
00034         }
00035 
00036         uint32_t mask;
00037         if (sscanf(argv[3], "%u", &mask) == 0) {
00038                 printf("Bad CIDR\n");
00039                 exit(EXIT_FAILURE);
00040         }
00041 
00042         in_addr_t gw;
00043         if (argc >= 5 && !inet_pton(AF_INET, argv[4], &gw)) {
00044                 printf("Bad gateway\n");
00045                 exit(EXIT_FAILURE);
00046         }
00047 
00048         struct mnl_socket *nl;
00049         char buf[MNL_SOCKET_BUFFER_SIZE];
00050         struct nlmsghdr *nlh;
00051         struct rtmsg *rtm;
00052 
00053         nlh = mnl_nlmsg_put_header(buf);
00054         nlh->nlmsg_type = RTM_NEWROUTE;
00055         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
00056         nlh->nlmsg_seq = time(NULL);
00057 
00058         rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
00059         rtm->rtm_family = AF_INET;
00060         rtm->rtm_dst_len = mask;
00061         rtm->rtm_src_len = 0;
00062         rtm->rtm_tos = 0;
00063         rtm->rtm_protocol = RTPROT_BOOT;
00064         rtm->rtm_table = RT_TABLE_MAIN;
00065         rtm->rtm_type = RTN_UNICAST;
00066         /* is there any gateway? */
00067         rtm->rtm_scope = (argc == 4) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
00068         rtm->rtm_flags = 0;
00069 
00070         mnl_attr_put_u32(nlh, RTA_DST, dst);
00071         mnl_attr_put_u32(nlh, RTA_OIF, iface);
00072         if (argc >= 5)
00073                 mnl_attr_put_u32(nlh, RTA_GATEWAY, gw);
00074 
00075         nl = mnl_socket_open(NETLINK_ROUTE);
00076         if (nl == NULL) {
00077                 perror("mnl_socket_open");
00078                 exit(EXIT_FAILURE);
00079         }
00080 
00081         if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00082                 perror("mnl_socket_bind");
00083                 exit(EXIT_FAILURE);
00084         }
00085 
00086         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00087                 perror("mnl_socket_send");
00088                 exit(EXIT_FAILURE);
00089         }
00090 
00091         mnl_socket_close(nl);
00092 
00093         return 0;
00094 }