void test_longest_prefix_in_empty_window () { PRINT_TEST_NAME("longest prefix in empty window"); char *buffer_data = "ABC"; memcpy(buffer, buffer_data, 3); window_length = 0; buffer_length = 3; int longest = longest_prefix(); ASSERT_INT_EQUAL(longest, -1); }
void test_longest_prefix_found_amongst_many_smaller () { PRINT_TEST_NAME("longest prefix found amongst many smaller"); char *window_data = "0123ABCD4567"; char *buffer_data = "ABCDEF"; memcpy(window, window_data, 12); memcpy(buffer, buffer_data, 6); window_length = 12; buffer_length = 6; int longest = longest_prefix(); ASSERT_INT_EQUAL(longest, 4); ASSERT_INT_EQUAL(last_offset, 4); }
static std::string find_longest_prefix(const std::vector<std::string>& lst) { if (lst.empty()) { return ""; } else { std::string prefix = lst.front(); for(std::vector<std::string>::const_iterator i = lst.begin() + 1; i != lst.end(); ++i) { prefix = longest_prefix(prefix, *i); } return prefix; } }
int main(){ char c; L = -1; N = 0; freopen("prefix.in","r",stdin); do{ scanf("%s",primitive[++L]); }while(primitive[L][0] != '.'); while(scanf("%c",&c) != EOF){ if(c != '\n') S[N++] = c; } int m = longest_prefix(); printf("%d\n",m); return 0; }
int make_and_send(struct sr_instance* sr, char * interface, uint32_t dstIP, uint8_t* payld, unsigned int len, uint8_t proto) { assert(payld); assert(sr); assert(interface); #if 0 printf("********* at %s Payload is 0x ", __func__); int i ; for(i = 0; i < len; i++) { printf("%hhx ", payld[i]); } printf("\n"); #endif /* doing some testing */ printf("\n*************************\n\n"); print_ip(dstIP); printf("\n\n**************************\n\n"); char* test; test = longest_prefix(sr, ntohl(dstIP)); printf("\n\n**************************\n"); struct sr_vns_if* myIntf = sr_get_interface(sr, interface); struct ip* ipHdr = (struct ip*) malloc_or_die(20); int rtn; struct in_addr src; struct in_addr dst; src.s_addr = myIntf->ip; dst.s_addr = dstIP; //fill in ip hdr //How do we set version and hl? ipHdr->ip_hl = 5; ipHdr->ip_v = 4; ipHdr->ip_tos = 0x0; ipHdr->ip_len = htons(len + 20); ipHdr->ip_id = htons(1638); ipHdr->ip_off = 0x0; ipHdr->ip_ttl = 64; ipHdr->ip_p = proto; // probably always IPPROTO_ICMP for the router ipHdr->ip_sum = 0x0; ipHdr->ip_src = src; ipHdr->ip_dst = dst; uint16_t check = cksum((uint8_t*)ipHdr, 20); ipHdr->ip_sum = check; //Then we make an ethernet payload // Tracking back from sr_arp.c:298 // we don't have the valid ICMP here even so go back further uint8_t* packet = (uint8_t*) malloc_or_die(len + 20); packet = array_join(ipHdr, payld, 20, len); #if 0 printf("\n"); for(i = 0; i < len + 20; i++) { printf("%hhx ", packet[i]); } //printf("\n************* %s ******************\n", __func__); #endif rtn = arp_lookup(sr, interface, packet, dstIP, (len + 20)); return rtn; }
/** * Inserts a value into the tree * @arg t The tree to insert into * @arg key The key of the value * @arg value Initially points to the value to insert, replaced * by the value that was updated if any * @return 0 if the value was inserted, 1 if the value was updated. */ int radix_insert(radix_tree *t, char *key, void **value) { radix_node *child, *parent=NULL, *n = &t->root; radix_leaf *leaf; char *search = key; int common_prefix; do { // Check if we've exhausted the key if (!search || *search == 0) { leaf = n->edges[0]; if (leaf) { // Return the old value void *old = leaf->value; leaf->value = *value; *value = old; return 1; } else { // Add a new node leaf = malloc(sizeof(radix_leaf)); n->edges[0] = leaf; leaf->key = key ? strdup(key) : NULL; leaf->value = *value; return 0; } } // Get the edge parent = n; n = n->edges[(int)*search]; if (!n) { child = parent->edges[(int)*search] = calloc(1, sizeof(radix_node)); leaf = child->edges[0] = malloc(sizeof(radix_leaf)); leaf->key = strdup(key); leaf->value = *value; // The key of the node is some child->key = leaf->key + (search - key); child->key_len = strlen(search); return 0; } // Determine longest prefix of the search key on match common_prefix = longest_prefix(search, n->key, n->key_len); if (common_prefix == n->key_len) { search += n->key_len; // If we share a sub-set, we need to split the nodes } else { // Split the node with the shared prefix child = calloc(1, sizeof(radix_node)); parent->edges[(int)*search] = child; child->key = n->key; child->key_len = common_prefix; // Restore pointer to the existing node n->key += common_prefix; n->key_len -= common_prefix; child->edges[(int)*(n->key)] = n; // Create a new leaf leaf = malloc(sizeof(radix_leaf)); leaf->key = strdup(key); leaf->value = *value; // If the new key is a subset, add to to this node search += common_prefix; if (*search == 0) { child->edges[0] = leaf; return 0; } // Create a new node for the new key n = calloc(1, sizeof(radix_node)); child->edges[(int)*search] = n; n->edges[0] = leaf; // The key of the node is some n->key = leaf->key + (search - key); n->key_len = strlen(search); return 0; } } while (1); return 0; }