Example #1
0
File: text.c Project: EQ4/rsynth
void
guess_word(void *arg, out_p out, unsigned char *word)
{
 int index;                       /* Current position in word */
 index = 1;                       /* Skip the initial blank */
 do 
  {
   index = find_rule(arg, out, word, index, lang->rules(word[index]));
  }
 while (word[index] != '\0');
}
Example #2
0
// return NULL if error (such as path limit reached)
NsRule *find_or_create_rule(void) {
	struct task_struct *real_parent;

	// look for an exiting active namespace entry in the list
	NsRule *ptr = find_rule(current->nsproxy);
	if (ptr)
		return ptr;
	
	// find the sandbox task
	real_parent = get_sandbox();

	// reuse an inactive entry
	ptr = head.next;
	while (ptr) {
		if (ptr->active == 0)
			break;
		ptr = ptr->next;
	}
	if (ptr) { // found an inactive entry
		int i;
		
		// clean unix path
		for (i = 0; i < MAX_UNIX_PATH; i++) {
			ptr->unix_path_len[i] = 0;
			if (ptr->unix_path[i]) {
				char *tmp = ptr->unix_path[i];
				ptr->unix_path[i] = NULL;
				kfree(tmp);
			}
		}
		
		// set the rule
		ptr->nsproxy = current->nsproxy;
		ptr->sandbox_pid = real_parent->pid;
		ptr->active = 1;
		printk(KERN_INFO "firejail[%u]: setup sandbox.\n", ptr->sandbox_pid);
		return ptr;
	}
	
	// allocate a new rule
	ptr = kmalloc(sizeof(NsRule), GFP_KERNEL);
	memset(ptr, 0, sizeof(NsRule));
	ptr->nsproxy = current->nsproxy;
	ptr->sandbox_pid = real_parent->pid;

	// insert the new rule in the rule list
	ptr->next = head.next;
	head.next = ptr;
	ptr->active = 1;
	printk(KERN_INFO "firejail[%u]: setup sandbox.\n", ptr->sandbox_pid);
	return ptr;
}
Example #3
0
void
dmk_word(char *word)
{
  int index;			/* Current position in word */
  int type;			/* First letter of match part */

  index = 1;			/* Skip the initial blank */
  do
    {
      if (isupper((int)word[index]))
	type = word[index] - 'A' + 1;
      else
	type = 0;

      index = find_rule(word, index, Rules[type]);
    }
  while (word[index] != '\0');
}
void G_parser::find_rule(vector<int>& seq_t){
    
    //cout << "reached 1" << endl;
    
    //cout << "seq_t: " << seq_t[2] << " & " << seq_t[3] << endl;
    // if (previous (in left_str) is rootpitch) { search for its terminal equivalents; }
    //if IV in previous context (instead of iv) then lookup corresponding chords (IV = iv*, iv6*, iv7*) OR (IV = iv*)
    //what about IV instead of iv later? (what if already translated as iv from IV?) --> e.g. "IV I_3" ->...
    //      if context not found, look for equivalent rootpitch
    //      perhaps categorise IV as iv|iv6|iv7
    
    bool rule_found = false;
    bool opt;
    rule r;
    
    int cont_size;
    vector<int> leftmost_t;
    
    //find (if) timed_rule
    //search existing_times
    for (int i=0; i<existing_times.size(); i++){//replace with while()
        
        //cout << "reached 2" << endl;
        if (seq_t[2]==existing_times[i][0] && seq_t[3]==existing_times[i][1]){//seq_t[2,3] for beat, bar..
            
            //cout << "reached 3" << endl;
            //check context (match rule with current context)
            for (int j=0; j<timed_rules[{seq_t[2], seq_t[3]}].size(); j++){
                
                //cout << "reached 4" << endl;
                //check_context(seq_t);
                
                //i6 could map to I etc. if context not found ?
                cont_size = timed_rules[{seq_t[2], seq_t[3]}][j].left_str.size();
                leftmost_t = timed_rules[{seq_t[2], seq_t[3]}][j].leftmost_time;
                opt = timed_rules[{seq_t[2], seq_t[3]}][j].is_optional;
                
                vector<string> curr_cont = get_context(leftmost_t, cont_size);
                
                if (timed_rules[{seq_t[2], seq_t[3]}][j].is_optional){//timed and optional
                    
                    rule r = timed_rules[{seq_t[2], seq_t[3]}][j];
                    rule_found = check_optional(r, seq_t);
                    
                    if (rule_found) break;
                    
                }
                else {//timed, but not optional
                    
                    //cout << "reached 11" << endl;
                    if (curr_cont == timed_rules[{seq_t[2], seq_t[3]}][j].left_str){
                        
                        //cout << "reached 12" << endl;
                        rule_found = true;
                        
                        //expand to support optional too
                        r = timed_rules[{seq_t[2], seq_t[3]}][j];
                        
                        the_rule = r;
                        //debug
                        cout << "the_rule: ";
                        for (int j=0; j<the_rule.left_str.size(); j++) cout << the_rule.left_str[j] << ", ";
                        cout << endl;
                        
                        rewrite(r, seq_t);
                        cout << "case 2" << endl;//timed, but not optional
                        
                        break;
                    }
                }
                
            }
            
            break;
        }
    }
    
    //else find general_rule
    if(!rule_found) {//replace with while()
        
        //search general_rules (context again)
        for (int i=0; i<general_rules.size(); i++){
            
            //check if context exists before or after current chord (elem)
            int c_off = - general_rules[i].left_str.size();//context offset (back n forth)
            cont_size = general_rules[i].left_str.size();
            //leftmost_t = general_rules[i].leftmost_time;//is {0, 0}
            opt = general_rules[i].is_optional; //need to access specific general_rule
            
            for (int z = c_off; z < 0; z++){
                
                general_rules[i].leftmost_time[1] = seq_t[3] + (z+1) / harm_rh;
                
                vector<string> curr_cont = get_context(general_rules[i].leftmost_time, cont_size);
                
                //check context if(general_rule[i].is_optional)
                if (opt){
                    
                    r = general_rules[i];
                    rule_found = check_optional(r, seq_t);
                    if (rule_found) break;
                    
                }
                
                //check context if (!general_rule[i].is_optional)
                else if(get_context(general_rules[i].leftmost_time, cont_size) == general_rules[i].left_str){
                    
                    rule_found = true;
                    
                    r = general_rules[i];
                    
                    the_rule = r;
                    //debug
                    cout << "the_rule: ";
                    for (int j=0; j<the_rule.left_str.size(); j++) cout << the_rule.left_str[j] << ", ";
                    cout << endl;
                    
                    rewrite(r, seq_t);
                    cout << "case 5" << endl;//general and not optional
                    
                    break;
                }
            }
            
            if (rule_found) break;
        }
    }
    
    cout << "is_terminal?: " << curr_cycle[seq_t[3]].name << endl << endl;
    //stop if terminal
    if (!is_terminal(seq_t)) find_rule(seq_t);
    //else return_terminal(curr_cycle[seq_t[3]].name);
    else translate_to_midi();//translate / spit out (return_terminal())
}
Example #5
0
File: state.c Project: UraKn0x/gbdk
int main()
{
   FILE *source;
   int state;
   char line[100], current_text[100];
   char *current, *store;
   pmstate rule;
   mtoken first_token;
   pmtoken current_token;
   mmacro first_macro;
   pmmacro current_macro;
	pmdefine current_define;

   int tokenclass;

   source = fopen( "test.S", "r" );
   if (source) {
      state = RST;

      current_text[0] = '\0';
      store = current_text;
      current_token = &first_token;
      current_token->next = NULL;
      current_macro = &first_macro;
      current_macro->next = NULL;

      while (!feof(source)) {
         fgets( line, 100, source );
         current = line;
         while (*current != '\0') {
            tokenclass = get_token_class( *current );
            rule = find_rule( state, tokenclass );
            if (rule!=NULL) {
               if ((rule->current != rule->next)) {
						if (!((rule->next ==SOPCODE_ARGS_ENDSTRING)&&(rule->current==SOPCODE_ARGS_STRING))) {
                  /* Print what was scanned and what rule it falls under */
                  *store = '\0';
                  if (remotely_interesting[state]) {
/*                     printf("[%s] %s\n", state_names[state], current_text );*/
                     /* Add it on to the list of tokens */
                     current_token->next = malloc( sizeof( mtoken ));
                     current_token->next->previous = current_token;
                     current_token = current_token->next;
                     current_token->next = NULL;

                     current_token->state = state;
                     strcpy( current_token->text, current_text );
						}
                  store = current_text;
                  *store = '\0';
						}
                  state = rule->next;
               }
/*               else
                  printf("%s -> %s %c\n", state_names[rule->current], state_names[rule->next], *current );*/
               *store = *current;
               store++;
            }
            else 
               printf("%s -> ??? \'%c\' %s\n", state_names[state], *current, line );
            current++;
         }
      }

      add_macros( first_token.next, &first_macro );

      /* Print out the token list */
      current_token = first_token.next;
      print_token_chain( first_token.next, &first_macro, NULL );

      current_macro = first_macro.next;
      printf("\nDefined macros:\n");
      while (current_macro) {
         printf("  [%s]", current_macro->name );
			current_define = current_macro->args;
			while (current_define) {
				printf(" %s", current_define->find);
				current_define = current_define->next;
			}
			printf("\n");
         current_macro = current_macro->next;
      }
      return 0;

   }
   return -1;
};
Example #6
0
int main(int argc, char **argv)
{
    int overwrite;
    int interactive;
    int remove;
    int have_colors;
    struct Colors colors, colors_tmp;
    struct Cell_stats statf;
    int have_stats = 0;
    struct FPRange range;
    DCELL min, max;
    char *name, *mapset;
    char *style, *cmap, *cmapset;
    char *rules;
    int fp;
    struct GModule *module;
    struct
    {
	struct Flag *r, *w, *l, *g, *a, *e, *i, *q, *n;
    } flag;
    struct
    {
	struct Option *map, *colr, *rast, *rules;
    } opt;


    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("raster, color table");
    module->description =
	_("Creates/modifies the color table associated with a raster map layer.");

    opt.map = G_define_standard_option(G_OPT_R_MAP);
    opt.map->required = NO;
    opt.map->guisection = _("Required");

    scan_rules();

    opt.colr = G_define_option();
    opt.colr->key = "color";
    opt.colr->key_desc = "style";
    opt.colr->type = TYPE_STRING;
    opt.colr->required = NO;
    opt.colr->options = rules_list();
    opt.colr->description = _("Type of color table");
    opt.colr->descriptions = rules_descriptions();
    opt.colr->guisection = _("Colors");

    opt.rast = G_define_option();
    opt.rast->key = "raster";
    opt.rast->type = TYPE_STRING;
    opt.rast->required = NO;
    opt.rast->gisprompt = "old,cell,raster";
    opt.rast->description =
	_("Raster map name from which to copy color table");

    opt.rules = G_define_standard_option(G_OPT_F_INPUT);
    opt.rules->key = "rules";
    opt.rules->required = NO;
    opt.rules->description = _("Path to rules file (\"-\" to read rules from stdin)");
    opt.rules->guisection = _("Colors");

    flag.r = G_define_flag();
    flag.r->key = 'r';
    flag.r->description = _("Remove existing color table");

    flag.w = G_define_flag();
    flag.w->key = 'w';
    flag.w->description =
	_("Only write new color table if one doesn't already exist");

    flag.l = G_define_flag();
    flag.l->key = 'l';
    flag.l->description = _("List available rules then exit");

    flag.n = G_define_flag();
    flag.n->key = 'n';
    flag.n->description = _("Invert colors");
    flag.n->guisection = _("Colors");

    flag.g = G_define_flag();
    flag.g->key = 'g';
    flag.g->description = _("Logarithmic scaling");
    flag.g->guisection = _("Colors");

    flag.a = G_define_flag();
    flag.a->key = 'a';
    flag.a->description = _("Logarithmic-absolute scaling");
    flag.a->guisection = _("Colors");

    flag.e = G_define_flag();
    flag.e->key = 'e';
    flag.e->description = _("Histogram equalization");
    flag.e->guisection = _("Colors");

    flag.i = G_define_flag();
    flag.i->key = 'i';
    flag.i->description = _("Enter rules interactively");

    /* please, remove before GRASS 7 released */
    flag.q = G_define_flag();
    flag.q->key = 'q';
    flag.q->description = _("Run quietly");


    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    /* please, remove before GRASS 7 released */
    if (flag.q->answer) {
	G_putenv("GRASS_VERBOSE", "0");
	G_warning(_("The '-q' flag is superseded and will be removed "
		    "in future. Please use '--quiet' instead."));
    }

    if (flag.l->answer) {
	list_rules();
	return EXIT_SUCCESS;
    }

    overwrite = !flag.w->answer;
    interactive = flag.i->answer;
    remove = flag.r->answer;

    name = opt.map->answer;

    style = opt.colr->answer;
    cmap = opt.rast->answer;
    rules = opt.rules->answer;

    if (!name)
	G_fatal_error(_("No raster map specified"));

    if (!cmap && !style && !rules && !interactive && !remove)
	G_fatal_error(_("One of \"-i\" or \"-r\" or options \"color\", \"rast\" or \"rules\" must be specified!"));

    if (interactive && (style || rules || cmap))
	G_fatal_error(_("Interactive mode is incompatible with \"color\", \"rules\", and \"raster\" options"));

    if ((style && (cmap || rules)) || (cmap && rules)) {
	if ((style && rules && !cmap) && strcmp(style, "rules") == 0)
	    style = NULL;
	else
	    G_fatal_error(
		_("\"color\", \"rules\", and \"raster\" options are mutually exclusive"));
    }

    /* handle rules="-" (from stdin) by translating that to colors=rules */
    /* this method should not be ported to GRASS 7 verbatim, as color=rules DNE */
    if (rules && strcmp(rules, "-") == 0) {
	style = G_store("rules");
	rules = NULL;
    }

    if (flag.g->answer && flag.a->answer)
	G_fatal_error(_("-g and -a flags are mutually exclusive"));

    mapset = G_find_cell2(name, "");
    if (mapset == NULL)
	G_fatal_error(_("Raster map <%s> not found"), name);

    if (remove) {
	int stat = G_remove_colors(name, mapset);

	if (stat < 0)
	    G_fatal_error(_("Unable to remove color table of raster map <%s>"), name);
	if (stat == 0)
	    G_warning(_("Color table of raster map <%s> not found"), name);
	return EXIT_SUCCESS;
    }

    G_suppress_warnings(1);
    have_colors = G_read_colors(name, mapset, &colors);
    /*if (have_colors >= 0)
       G_free_colors(&colors); */

    if (have_colors > 0 && !overwrite) {
	G_warning(_("Color table exists. Exiting."));
	exit(EXIT_FAILURE);
    }

    G_suppress_warnings(0);

    fp = G_raster_map_is_fp(name, mapset);
    G_read_fp_range(name, mapset, &range);
    G_get_fp_range_min_max(&range, &min, &max);

    if (interactive) {
	if (!read_color_rules(stdin, &colors, min, max, fp))
	    exit(EXIT_FAILURE);
    }
    else if (style) {
	/* 
	 * here the predefined color-table color-styles are created by GRASS library calls. 
	 */
	if (strcmp(style, "random") == 0) {
	    if (fp)
		G_fatal_error(_("Color table 'random' is not supported for floating point raster map"));
	    G_make_random_colors(&colors, (CELL) min, (CELL) max);
	}
	else if (strcmp(style, "grey.eq") == 0) {
	    if (fp)
		G_fatal_error(_("Color table 'grey.eq' is not supported for floating point raster map"));
	    if (!have_stats)
		have_stats = get_stats(name, mapset, &statf);
	    G_make_histogram_eq_colors(&colors, &statf);
	}
	else if (strcmp(style, "grey.log") == 0) {
	    if (fp)
		G_fatal_error(_("Color table 'grey.log' is not supported for floating point raster map"));
	    if (!have_stats)
		have_stats = get_stats(name, mapset, &statf);
	    G_make_histogram_log_colors(&colors, &statf, (CELL) min,
					(CELL) max);
	}
	else if (strcmp(style, "rules") == 0) {
	    if (!read_color_rules(stdin, &colors, min, max, fp))
		exit(EXIT_FAILURE);
	}
	else if (find_rule(style))
	    G_make_fp_colors(&colors, style, min, max);
	else
	    G_fatal_error(_("Unknown color request '%s'"), style);
    }
    else if (rules) {
	if (!G_load_fp_colors(&colors, rules, min, max)) {
	    /* for backwards compatibility try as std name; remove for GRASS 7 */
	    char path[GPATH_MAX];

	    /* don't bother with native dirsep as not needed for backwards compatibility */
	    sprintf(path, "%s/etc/colors/%s", G_gisbase(), rules);

	    if (!G_load_fp_colors(&colors, path, min, max))
		G_fatal_error(_("Unable to load rules file <%s>"), rules);
	}
    }
    else {
	/* use color from another map (cmap) */
	cmapset = G_find_cell2(cmap, "");
	if (cmapset == NULL)
	    G_fatal_error(_("Raster map <%s> not found"), cmap);

	if (G_read_colors(cmap, cmapset, &colors) < 0)
	    G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap);
    }

    if (fp)
	G_mark_colors_as_fp(&colors);

    if (flag.n->answer)
	G_invert_colors(&colors);

    if (flag.e->answer) {
	if (fp) {
	    struct FP_stats fpstats;
	    get_fp_stats(name, mapset, &fpstats, min, max, flag.g->answer, flag.a->answer);
	    G_histogram_eq_colors_fp(&colors_tmp, &colors, &fpstats);
	}
	else {
	    if (!have_stats) 
		have_stats = get_stats(name, mapset, &statf);
	    G_histogram_eq_colors(&colors_tmp, &colors, &statf);
	}
	colors = colors_tmp;
    }

    if (flag.g->answer) {
	G_log_colors(&colors_tmp, &colors, 100);
	colors = colors_tmp;
    }

    if (flag.a->answer) {
	G_abs_log_colors(&colors_tmp, &colors, 100);
	colors = colors_tmp;
    }

    if (fp)
	G_mark_colors_as_fp(&colors);

    if (G_write_colors(name, mapset, &colors) >= 0)
	G_message(_("Color table for raster map <%s> set to '%s'"), name,
		  interactive ? "rules" : style ? style : rules ? rules :
		  cmap);

    exit(EXIT_SUCCESS);
}
void callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char* packet){
    //static int count = 1;
    //printf("\nep1s: Packet number [%d], length of this packet is: %d\n", count++, pkthdr->len);

    struct ether_hdr *ethernet;
    ethernet = (struct ether_hdr *)packet;

    if(ntohs(ethernet->ethertype) == ETHERTYPE_ARP){
        //printf("Ethertype = %u\n", (unsigned int)ntohs(ethernet->ethertype));
        struct arp_hdr *arp;
        arp = (struct arp_hdr *)ethernet->data;
        if(memcmp(ep1_mac_addr, arp->sender_mac, ETHER_ADDR_LEN) == 0){

            memcpy(ethernet->dst_mac, ethernet->src_mac, ETHER_ADDR_LEN);
            memcpy(ethernet->src_mac, ep1s_mac_addr, ETHER_ADDR_LEN);

            memcpy(arp->receiver_mac, arp->sender_mac, ETHER_ADDR_LEN);
            memcpy(arp->sender_mac, ep1s_mac_addr, ETHER_ADDR_LEN);

            uint32_t temp;
            memcpy(&temp, arp->srcip, sizeof(uint32_t));
            memcpy(arp->srcip, arp->dstip, sizeof(uint32_t));
            memcpy(arp->dstip, &temp, sizeof(uint32_t));

            uint16_t temp_oper = htons(2);
            memcpy(arp->oper, &temp_oper, sizeof(uint16_t));

            if(pcap_sendpacket(ep1s_descr, (u_char *)ethernet, pkthdr->len) != 0)
                printf("Could't send ARP response\n");
            else
                printf("Sent ARP response\n");
        }
    }
	else if(ntohs(ethernet->ethertype) == ETHERTYPE_IP){
		enum PROTOCOL protocol;
		enum ACTION result = 0;
		struct ip_hdr *ip;
		ip = (struct ip_hdr *)ethernet->data;
		if(ip->protocol == IPPROTO_ICMP){
			protocol = ICMP;

			//No need to modify the icmp packet

			/*Modifying the ipv4 header
			Change srcip to ipaddress of wlan0
			Recalculate checksum
			*/
			uint16_t ipchecksum = 0x0000;
			struct in_addr tmpip;
			inet_aton(wlan_ipaddr, &tmpip);
			ip->srcip = tmpip;
			memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));
			ipchecksum = ip_checksum(ip, sizeof(ip_hdr_t));
			memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));

			/*Modify the ethernet header
			src_mac is the mac address of wlan0
			dst_mac is the mac address of the gateway
			*/
			memcpy(ethernet->src_mac, wlan_mac_addr, ETHER_ADDR_LEN);
			memcpy(ethernet->dst_mac, gateway_mac_addr, ETHER_ADDR_LEN);

			struct findrule_hdr *find = (struct findrule_hdr *)malloc(sizeof(struct findrule_hdr));
	        find->protocol = protocol;
			find->srcip = ntohl(ip->srcip.s_addr);
	        find->dstip = ntohl(ip->dstip.s_addr);
	        find->srcport = 0;
    	    find->dstport = 0;

			result = find_rule(find);
			free(find);				
			if(result == BLOCK){
				printf("Blocked packet.\n");
					return;
			}

			if(pcap_sendpacket(wlan_descr, (u_char *)ethernet, pkthdr->len) != 0)
				printf("Couldn't send ICMP packet to physical interface\n");
			else
				printf("Sent ICMP packet to physical interface\n");
		}
		else if(ip->protocol == IPPROTO_TCP){
			protocol = TCP;
			struct tcp_hdr *tcp;
			tcp = (struct tcp_hdr *)ip->options_and_data;
			
			/*Perform NAT on the port numbers
			Get a random number in the range 50000 to 65535 and if it is already mapped to a port, reroll.
			This is the outgoing portnumber on the physical interface
			*/
			uint16_t natport = ntohs(tcp->srcport);
			tcpnat[natport].used = 1;
			
			/*Modifying the TCP header
			Set checksum to 0.
			Change sourceport to the natport.
			Recompute the checksum.
			*/
			uint16_t tcpchecksum = 0x0000;
			uint16_t blankify = 0x0000;
			memcpy(&tcpchecksum, tcp->checksum, 16);
			memcpy(tcp->checksum, &blankify, sizeof(uint16_t));

			//Modifying the IP header
			uint16_t ipchecksum = 0x0000;
            struct in_addr tmpip;
            inet_aton(wlan_ipaddr, &tmpip);
            ip->srcip = tmpip;
            memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));
            ipchecksum = ip_checksum(ip, sizeof(ip_hdr_t));
            memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));
		
			/*uint16_t totlen;
			memcpy(&totlen, ip->total_len, sizeof(uint16_t));
			totlen = ntohs(totlen);
	
			uint16_t tcpoptlen = tcp->offset_rsvd&0x00f0;
			tcpoptlen = tcpoptlen>>4;
			tcpoptlen = tcpoptlen * 4 - 20;
			printf("Offset = %u\n", tcpoptlen);

			uint16_t iphl = ip->version_ihl&0x000f;
            iphl *= 4;

			uint16_t tcpdatalen = totlen - (tcpoptlen + 20) - iphl;

			uint16_t pseudotcplen = sizeof(struct tcp_hdr) + sizeof(struct pseudo_hdr) + tcpdatalen;*/

			/*Create pseudoheader
			Compute TCP checksum*/
			uint16_t iphl = ip->version_ihl&0x000f;
			iphl *= 4;
			uint16_t totlen;
			memcpy(&totlen, ip->total_len, sizeof(uint16_t));
			totlen = ntohs(totlen);
			uint16_t tcplen = totlen - iphl;
			if(tcplen % 2 == 1)
				tcplen++;
			//tcplen = htons(tcplen);
			
			struct pseudo_hdr *pseudo = (struct pseudo_hdr *)calloc(1, sizeof(struct pseudo_hdr) + (tcplen - sizeof(struct tcp_hdr)));
			tcplen = htons(tcplen);
			//struct pseudo_hdr *pseudo = (struct pseudo_hdr *)malloc(pseudotcplen);
			if(pseudo == NULL){
				printf("Couldn't malloc\n");
				exit(1);
			}

			//Copying data into the pseudo header			
			pseudo->srcip = ip->srcip;
			pseudo->dstip = ip->dstip;
			pseudo->zeroes = 0x00;
			pseudo->protocol = IPPROTO_TCP;
			//pseudo->tcplen = htons(sizeof(struct tcp_hdr) + tcpoptlen + tcpdatalen);
			pseudo->tcplen = tcplen;
			memcpy(&(pseudo->srcport), &(tcp->srcport), ntohs(pseudo->tcplen));
			
			//Calculate TCP Checksum
			tcpchecksum = 0x0000;
			tcpchecksum = ip_checksum(pseudo, 4*3 + ntohs(tcplen));
			//tcpchecksum = ip_checksum(pseudo, pseudotcplen);
			memcpy(tcp->checksum, &tcpchecksum, sizeof(uint16_t));
			free(pseudo);

			//Modifying the Ethernet header
			memcpy(ethernet->src_mac, wlan_mac_addr, ETHER_ADDR_LEN);
            memcpy(ethernet->dst_mac, gateway_mac_addr, ETHER_ADDR_LEN);
		
			struct findrule_hdr *find = (struct findrule_hdr *)malloc(sizeof(struct findrule_hdr));
            find->protocol = protocol;
            find->srcip = ntohl(ip->srcip.s_addr);
            find->dstip = ntohl(ip->dstip.s_addr);
            find->srcport = ntohs(tcp->srcport);
            find->dstport = ntohs(tcp->dstport);

			result = find_rule(find);
            free(find);
            if(result == BLOCK){
                printf("Blocked packet.\n");
                    return;
            }

            if(pcap_sendpacket(wlan_descr, (u_char *)ethernet, pkthdr->len) != 0)
                printf("Couldn't send TCP packet to physical interface\n");
            else
                printf("Sent TCP packet to physical interface\n");
		}
		else if(ip->protocol == IPPROTO_UDP){
			protocol = UDP;
			struct udp_hdr *udp;
			udp = (struct udp_hdr *)ip->options_and_data;
			uint16_t srcport, dstport;
			memcpy(&srcport, udp->srcport, sizeof(uint16_t));
			memcpy(&dstport, udp->dstport, sizeof(uint16_t));
			printf("Source port = %u\nDestination port = %u\n", ntohs(srcport), ntohs(dstport));

			/*Perform NAT on the port numbers
            Get a random number in the range 50000 to 65535 and if it is already mapped to a port, reroll.
            This is the outgoing portnumber on the physical interface
            */
            uint16_t natport = ntohs(srcport);
            tcpnat[natport].used = 1;
			
			uint16_t udpchecksump = 0x0000;
			memcpy(udp->checksum, &udpchecksump, sizeof(uint16_t));
			
			//Modifying the IP header
            uint16_t ipchecksum = 0x0000;
            struct in_addr tmpip;
            inet_aton(wlan_ipaddr, &tmpip);
            ip->srcip = tmpip;
            memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));
            ipchecksum = ip_checksum(ip, sizeof(ip_hdr_t));
            memcpy(ip->checksum, &ipchecksum, sizeof(uint16_t));	

			/*Modify the ethernet header
            src_mac is the mac address of wlan0
            dst_mac is the mac address of the gateway
            */
            memcpy(ethernet->src_mac, wlan_mac_addr, ETHER_ADDR_LEN);
            memcpy(ethernet->dst_mac, gateway_mac_addr, ETHER_ADDR_LEN);

            struct findrule_hdr *find = (struct findrule_hdr *)malloc(sizeof(struct findrule_hdr));
            find->protocol = protocol;
            find->srcip = ntohl(ip->srcip.s_addr);
            find->dstip = ntohl(ip->dstip.s_addr);
            find->srcport = ntohs(srcport);
            find->dstport = ntohs(dstport);

            result = find_rule(find);
            free(find);
            if(result == BLOCK){
                printf("Blocked packet.\n");
                    return;
            }

            if(pcap_sendpacket(wlan_descr, (u_char *)ethernet, pkthdr->len) != 0)
                printf("Couldn't send UDP packet to physical interface\n");
            else
                printf("Sent UDP packet to physical interface\n");
		}
	}
}