/*
 * lwip_hook_ip4_input.c
 *
 *  Created on: 20.05.2019
 *      Author: balbekova
 */



#include "lwip_hook_ip4_input.h"

#include "ip4.h"

#include "settings_api.h"
#include "parameters.h"

#ifdef PRINTF_STDLIB
#include <stdio.h>
#endif
#ifdef PRINTF_CUSTOM
#include "tinystdio.h"
#endif
#include <string.h>
#include <stdlib.h>

int ip_input_hook(struct pbuf *p, struct netif *inp)
{

	bool flag = true;
	struct ip_hdr *iphdr;
	uint32_t mask_white_list;
	uint32_t ip_white_list;
	char str[20];
	uint8_t len = 0;

	iphdr = (struct ip_hdr *)p->payload;

	for(uint8_t i = 0; i < MAX_WHITE_LIST; i ++){
		memset(str, 0, 20);
		GetWhiteListSTR(str, &len, i);
		GetWhiteListMask(&mask_white_list, i);
		GetWhiteListIP(&ip_white_list, i);
		if(strlen(str) != 0){

			if((iphdr->src.addr & mask_white_list) == (ip_white_list & mask_white_list)){
				flag = true;
				break;
			}
			else{
				flag = false;
			}
		}
	}
	if(flag){
		return 0;
	}
	else{
		pbuf_free(p);
		return 1;
	}

}
