#include <netinet/in.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

/* man netlink(7) netlink(3) */
/* man rtnetlink(7) rtnetlink(3) */

#define BUFSIZE 8192
char gateway[255];

struct route_info {
	struct in_addr dstAddr;
	struct in_addr srcAddr;
	struct in_addr gateWay;
	char ifName[IF_NAMESIZE];
};

int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId)
{
	struct nlmsghdr *nlHdr;
	int readLen = 0, msgLen = 0;

	do {
		/* Recieve response from the kernel */
		if ((readLen = recv(sockFd, bufPtr, BUFSIZE - msgLen, 0)) < 0) {
			perror("SOCK READ: ");
			return -1;
		}

		nlHdr = (struct nlmsghdr *)bufPtr;

		/* Check if the header is valid */
		if ((NLMSG_OK(nlHdr, readLen) == 0)
		    || (nlHdr->nlmsg_type == NLMSG_ERROR)) {
			perror("Error in recieved packet");
			return -1;
		}

		/* Check if the its the last message */
		if (nlHdr->nlmsg_type == NLMSG_DONE) {
			break;
		} else {
			/* Else move the pointer to buffer appropriately */
			bufPtr += readLen;
			msgLen += readLen;
		}

		/* Check if its a multi part message */
		if ((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0) {
			/* return if its not */
			break;
		}
	} while ((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pId));
	return msgLen;
}

/* For printing the routes. */
void printRoute(struct route_info *rt_info)
{
	char tempBuf[512];

/* Print Destination address */
	if (rt_info->dstAddr.s_addr != 0)
		strcpy(tempBuf, inet_ntoa(rt_info->dstAddr));
	else
		sprintf(tempBuf, "*.*.*.*\t");
	fprintf(stdout, "%s\t", tempBuf);

/* Print Gateway address */
	if (rt_info->gateWay.s_addr != 0)
		strcpy(tempBuf, (char *)inet_ntoa(rt_info->gateWay));
	else
		sprintf(tempBuf, "*.*.*.*\t");
	fprintf(stdout, "%s\t", tempBuf);

	/* Print Interface Name */
	fprintf(stdout, "%s\t", rt_info->ifName);

	/* Print Source address */
	if (rt_info->srcAddr.s_addr != 0)
		strcpy(tempBuf, inet_ntoa(rt_info->srcAddr));
	else
		sprintf(tempBuf, "*.*.*.*\t");
	fprintf(stdout, "%s\n", tempBuf);
}

void printGateway()
{
	printf("%s\n", gateway);
}

/* For parsing the route info returned */
void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rt_info)
{
	struct rtmsg *rt_msg;
	struct rtattr *rtAttr;
	int rtLen;

	rt_msg = (struct rtmsg *)NLMSG_DATA(nlHdr);

/* If the route is not for AF_INET or does not belong to main routing table
then return. */
	if ((rt_msg->rtm_family != AF_INET)
	    || (rt_msg->rtm_table != RT_TABLE_MAIN))
		return;

/* get the rtattr field */
	rtAttr = (struct rtattr *)RTM_RTA(rt_msg);
	rtLen = RTM_PAYLOAD(nlHdr);
	for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
		switch (rtAttr->rta_type) {
		case RTA_OIF:
			if_indextoname(*(int *)RTA_DATA(rtAttr),
				       rt_info->ifName);
			break;
		case RTA_GATEWAY:
			rt_info->gateWay.s_addr = *(u_int *) RTA_DATA(rtAttr);
			break;
		case RTA_PREFSRC:
			rt_info->srcAddr.s_addr = *(u_int *) RTA_DATA(rtAttr);
			break;
		case RTA_DST:
			rt_info->dstAddr.s_addr = *(u_int *) RTA_DATA(rtAttr);
			break;
		}
	}
	//printf("%s\n", inet_ntoa(rt_info->dstAddr));

	if (rt_info->dstAddr.s_addr == 0)
		sprintf(gateway, (char *)inet_ntoa(rt_info->gateWay));
	printRoute(rt_info);

	return;
}

int main()
{
	struct nlmsghdr *nl_msg;
	struct rtmsg *rt_msg;
	struct route_info *rt_info;
	char msg_buf[BUFSIZE];
	struct sockaddr_nl my_addr;

	int sock, len, msg_seq = 0;

	/* Create a netlink socket */
	if ((sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
		perror("socket() ERROR");
		exit(EXIT_FAILURE);
	}

	/* Bind to socket */
	memset(&my_addr, 0, sizeof(my_addr));
	my_addr.nl_family = AF_NETLINK;
	my_addr.nl_pid = getpid();
	bind(sock, (struct sockaddr *)&my_addr, sizeof(my_addr));

	/* Preparing msg buffer */
	memset(msg_buf, 0, BUFSIZE);
	nl_msg = (struct nlmsghdr *)msg_buf;
	rt_msg = (struct rtmsg *)NLMSG_DATA(nl_msg);

	/* Fill netlink msg header: Request for Dump table */
	nl_msg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	nl_msg->nlmsg_type = RTM_GETROUTE;
	nl_msg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
	nl_msg->nlmsg_seq = msg_seq++;
	nl_msg->nlmsg_pid = getpid();

	/* Send the request */
	if (send(sock, nl_msg, nl_msg->nlmsg_len, 0) < 0) {
		printf("Write To Socket Failed...\n");
		return -1;
	}

/* Read the response */
	if ((len = readNlSock(sock, msg_buf, msg_seq, getpid())) < 0) {
		printf("Read From Socket Failed...\n");
		return -1;
	}
/* Parse and print the response */
	rt_info = (struct route_info *)malloc(sizeof(struct route_info));
//fprintf(stdout, "Destination\tGateway\tInterface\tSource\n");
	for (; NLMSG_OK(nl_msg, len); nl_msg = NLMSG_NEXT(nl_msg, len)) {
		memset(rt_info, 0, sizeof(struct route_info));
		parseRoutes(nl_msg, rt_info);
	}
	free(rt_info);
	close(sock);

	printGateway();
	return 0;
}