#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char hex2dec(unsigned char hex)
{
	hex = toupper(hex);
	hex -= (hex - 48 > 9) ? 55 : 48;
	return hex;
}

int hexstr_to_int(char *hex)
{
	int i, j;

	union {
		int l;
		unsigned char ch[4];
	} num;

	j = 0;
	for (i = 0; i < 8; i += 2)
		num.ch[j++] = hex2dec(hex[i]) * 16 + hex2dec(hex[i + 1]);

	return htonl(num.l);
}

static char *get_gateway_iface(void)
{
	FILE *fp;
	char *rt_file = "/proc/net/route";
	char str[256];

	char dst[9];
	char *dst_bgn;

	static char iface_str[9];

	fp = fopen(rt_file, "r");
	if (fp == NULL) {
		printf("Cannot open file: %s\n", rt_file);
		perror("fopen()");
		exit(EXIT_FAILURE);
	}

	while (fgets(str, 255, fp) != NULL) {
		dst_bgn = strchr(str, '\11') + 1;
		memcpy(dst, dst_bgn, 8);
		dst[8] = '\0';
		if (strcmp(dst, "00000000") == 0) {
			memcpy(iface_str, str, dst_bgn - str - 1);
			iface_str[dst_bgn - str - 1] = '\0';
		}
	}

	return iface_str;
}

int main()
{
	printf("%s", get_gateway_iface());
	return 0;
}