Example #1
0
File: packet.c Project: NICMx/Jool
static verdict handle_icmp6(struct xlation *state, struct pkt_metadata const *meta)
{
	union {
		struct icmp6hdr icmp;
		struct frag_hdr frag;
	} buffer;
	union {
		struct icmp6hdr *icmp;
		struct frag_hdr *frag;
	} ptr;
	verdict result;

	ptr.icmp = skb_hdr_ptr(state->in.skb, meta->l4_offset, buffer.icmp);
	if (!ptr.icmp)
		return truncated(state, "ICMPv6 header");

	if (has_inner_pkt6(ptr.icmp->icmp6_type)) {
		result = validate_inner6(state, meta);
		if (result != VERDICT_CONTINUE)
			return result;
	}

	if (xlat_is_siit() && meta->has_frag_hdr && is_icmp6_info(ptr.icmp->icmp6_type)) {
		ptr.frag = skb_hdr_ptr(state->in.skb, meta->frag_offset, buffer.frag);
		if (!ptr.frag)
			return truncated(state, "fragment header");
		if (is_fragmented_ipv6(ptr.frag)) {
			log_debug("Packet is a fragmented ping; its checksum cannot be translated.");
			return drop(state, JSTAT_FRAGMENTED_PING);
		}
	}

	return VERDICT_CONTINUE;
}
static IntPoint whirled(const Point& p, const Point& c, coord r,
  const Angle& whirl)
{
  const Angle angle = whirl * sq(1.0 - r);
  coord sina = sin(angle);
  coord cosa = cos(angle);
  return IntPoint(truncated(cosa * p.x - sina * p.y + c.x),
    truncated(sina * p.x + cosa * p.y + c.y));
}
Example #3
0
File: packet.c Project: NICMx/Jool
static verdict validate_inner6(struct xlation *state,
		struct pkt_metadata const *outer_meta)
{
	union {
		struct ipv6hdr ip6;
		struct frag_hdr frag;
		struct icmp6hdr icmp;
	} buffer;
	union {
		struct ipv6hdr *ip6;
		struct frag_hdr *frag;
		struct icmp6hdr *icmp;
	} ptr;

	struct pkt_metadata meta;
	verdict result;

	ptr.ip6 = skb_hdr_ptr(state->in.skb, outer_meta->payload_offset,
			buffer.ip6);
	if (!ptr.ip6)
		return truncated(state, "inner IPv6 header");
	if (unlikely(ptr.ip6->version != 6))
		return inhdr6(state, "Version is not 6.");

	result = summarize_skb6(state, outer_meta->payload_offset, &meta);
	if (result != VERDICT_CONTINUE)
		return result;

	if (meta.has_frag_hdr) {
		ptr.frag = skb_hdr_ptr(state->in.skb, meta.frag_offset,
				buffer.frag);
		if (!ptr.frag)
			return truncated(state, "inner fragment header");
		if (!is_first_frag6(ptr.frag))
			return inhdr6(state, "Inner packet is not a first fragment.");
	}

	if (meta.l4_proto == L4PROTO_ICMP) {
		ptr.icmp = skb_hdr_ptr(state->in.skb, meta.l4_offset,
				buffer.icmp);
		if (!ptr.icmp)
			return truncated(state, "inner ICMPv6 header");
		if (has_inner_pkt6(ptr.icmp->icmp6_type))
			return inhdr6(state, "Packet inside packet inside packet.");
	}

	if (!pskb_may_pull(state->in.skb, meta.payload_offset)) {
		log_debug("Could not 'pull' the headers out of the skb.");
		return truncated(state, "inner headers");
	}

	return VERDICT_CONTINUE;
}
Example #4
0
File: packet.c Project: NICMx/Jool
static verdict validate_inner4(struct xlation *state, struct pkt_metadata *meta)
{
	union {
		struct iphdr ip4;
		struct tcphdr tcp;
	} buffer;
	union {
		struct iphdr *ip4;
		struct tcphdr *tcp;
	} ptr;
	unsigned int ihl;
	unsigned int offset = meta->payload_offset;

	ptr.ip4 = skb_hdr_ptr(state->in.skb, offset, buffer.ip4);
	if (!ptr.ip4)
		return truncated(state, "inner IPv4 header");

	ihl = ptr.ip4->ihl << 2;
	if (ptr.ip4->version != 4)
		return inhdr4(state, "Inner packet is not IPv4.");
	if (ihl < 20)
		return inhdr4(state, "Inner packet's IHL is bogus.");
	if (ntohs(ptr.ip4->tot_len) < ihl)
		return inhdr4(state, "Inner packet's total length is bogus.");
	if (!is_first_frag4(ptr.ip4))
		return inhdr4(state, "Inner packet is not first fragment.");

	offset += ihl;

	switch (ptr.ip4->protocol) {
	case IPPROTO_TCP:
		ptr.tcp = skb_hdr_ptr(state->in.skb, offset, buffer.tcp);
		if (!ptr.tcp)
			return truncated(state, "inner TCP header");
		offset += tcp_hdr_len(ptr.tcp);
		break;
	case IPPROTO_UDP:
		offset += sizeof(struct udphdr);
		break;
	case IPPROTO_ICMP:
		offset += sizeof(struct icmphdr);
		break;
	}

	if (!pskb_may_pull(state->in.skb, offset))
		return truncated(state, "inner headers");

	return VERDICT_CONTINUE;
}
Example #5
0
// Draw
void
PropertyItemView::Draw(BRect updateRect)
{
	const Property* property = GetProperty();
	if (property && fParent) {
		BRect b(Bounds());

		// just draw background and label
		rgb_color labelColor = LowColor();
		if (fEnabled)
			labelColor = tint_color(labelColor, B_DARKEN_MAX_TINT);
		else
			labelColor = tint_color(labelColor, B_DISABLED_LABEL_TINT);
		
		SetHighColor(labelColor);
		BFont font;
		GetFont(&font);
		
		BString truncated(name_for_id(property->Identifier()));
		font.TruncateString(&truncated, B_TRUNCATE_MIDDLE, fLabelWidth - 10.0);

		font_height fh;
		font.GetHeight(&fh);

		FillRect(BRect(b.left, b.top, b.left + fLabelWidth, b.bottom), B_SOLID_LOW);
		DrawString(truncated.String(), BPoint(b.left + 5.0,
											  floorf(b.top + b.Height() / 2.0
												  		   + fh.ascent / 2.0)));

		// draw a "separator" line behind the label
		SetHighColor(tint_color(LowColor(), B_DARKEN_1_TINT));
		StrokeLine(BPoint(b.left + fLabelWidth - 1.0, b.top),
				   BPoint(b.left + fLabelWidth - 1.0, b.bottom), B_SOLID_HIGH);
	}
}
Example #6
0
std::string DirectoryIterator::truncate(const std::string & path)
{
    std::string truncated(path);
    while (truncated.find_last_of(g_sep) == truncated.size() - 1)
        truncated = truncated.substr(0, truncated.find_last_of(g_sep));

    return truncated;
}
Example #7
0
File: packet.c Project: NICMx/Jool
verdict pkt_init_ipv6(struct xlation *state, struct sk_buff *skb)
{
	struct pkt_metadata meta;
	verdict result;

	state->in.skb = skb;

	/*
	 * DO NOT, UNDER ANY CIRCUMSTANCES, EXTRACT ANY BYTES FROM THE SKB'S
	 * DATA AREA DIRECTLY (ie. without using skb_hdr_ptr()) UNTIL YOU KNOW
	 * IT HAS ALREADY BEEN pskb_may_pull()ED. ASSUME THAT EVEN THE MAIN
	 * LAYER 3 HEADER CAN BE PAGED.
	 *
	 * Also, careful in this function and subfunctions. pskb_may_pull()
	 * might change pointers, so you generally don't want to store them.
	 */

	result = paranoid_validations(state, sizeof(struct ipv6hdr));
	if (result != VERDICT_CONTINUE)
		return result;

	log_debug("Packet addresses: %pI6c->%pI6c",
			&ipv6_hdr(skb)->saddr,
			&ipv6_hdr(skb)->daddr);

	if (skb->len != get_tot_len_ipv6(skb))
		return inhdr6(state, "Packet size doesn't match the IPv6 header's payload length field.");

	result = summarize_skb6(state, skb_network_offset(skb), &meta);
	if (result != VERDICT_CONTINUE)
		return result;

	if (meta.l4_proto == L4PROTO_ICMP) {
		/* Do not move this to summarize_skb6(), because it risks infinite recursion. */
		result = handle_icmp6(state, &meta);
		if (result != VERDICT_CONTINUE)
			return result;
	}

	if (!pskb_may_pull(skb, meta.payload_offset))
		return truncated(state, "headers");

	state->in.l3_proto = L3PROTO_IPV6;
	state->in.l4_proto = meta.l4_proto;
	state->in.is_inner = 0;
	state->in.is_hairpin = false;
	state->in.hdr_frag = meta.has_frag_hdr
			? skb_offset_to_ptr(skb, meta.frag_offset)
			: NULL;
	skb_set_transport_header(skb, meta.l4_offset);
	state->in.payload = skb_offset_to_ptr(skb, meta.payload_offset);
	state->in.original_pkt = &state->in;

	return VERDICT_CONTINUE;
}
nsIAtom*
nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
                                        nsresult *aError)
{
  nsIAtom *retVal;
  nsresult res = NS_OK;

  retVal = mLangToGroup.GetWeak(aLanguage);

  if (!retVal) {
    if (!mLangGroups) {
      if (NS_FAILED(InitLangGroupTable())) {
        if (aError) {
          *aError = NS_ERROR_FAILURE;
        }
        return nullptr;
      }
    }

    nsString langStr;
    aLanguage->ToString(langStr);

    nsXPIDLString langGroupStr;
    res = mLangGroups->GetStringFromName(langStr.get(),
                                         getter_Copies(langGroupStr));
    if (NS_FAILED(res)) {
      int32_t hyphen = langStr.FindChar('-');
      if (hyphen >= 0) {
        nsAutoString truncated(langStr);
        truncated.Truncate(hyphen);
        res = mLangGroups->GetStringFromName(truncated.get(),
                                             getter_Copies(langGroupStr));
        if (NS_FAILED(res)) {
          langGroupStr.AssignLiteral("x-unicode");
        }
      } else {
        langGroupStr.AssignLiteral("x-unicode");
      }
    }

    nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);

    // The hashtable will keep an owning reference to the atom
    mLangToGroup.Put(aLanguage, langGroup);
    retVal = langGroup.get();
  }

  if (aError) {
    *aError = res;
  }

  return retVal;
}
Example #9
0
// Draw
void
FontValueView::Draw(BRect updateRect)
{
	BRect b(Bounds());
	// focus indication
	if (IsFocus()) {
		SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
		StrokeRect(b);
		b.InsetBy(1.0, 1.0);
		BRegion clipping;
		clipping.Include(b);
		ConstrainClippingRegion(&clipping);
		b.left --;
	}
	// background
	FillRect(b, B_SOLID_LOW);

	rgb_color labelColor = LowColor();
	if (fEnabled)
		labelColor = tint_color(labelColor, B_DARKEN_MAX_TINT);
	else
		labelColor = tint_color(labelColor, B_DISABLED_LABEL_TINT);

	SetHighColor(labelColor);

	b.InsetBy(2.0, 1.0);

	float center = floorf(b.top + b.Height() / 2.0);

	BPoint arrow[3];
	arrow[0] = BPoint(b.left, center - 3.0);
	arrow[1] = BPoint(b.left, center + 3.0);
	arrow[2] = BPoint(b.left + 3.0, center);

	FillPolygon(arrow, 3);

	b.left += 6.0;

	BFont font;
	GetFont(&font);

	font_height fh;
	font.GetHeight(&fh);

	BString truncated(fCurrentFont);
	font.TruncateString(&truncated, B_TRUNCATE_END, b.Width());

	DrawString(truncated.String(),
			   BPoint(b.left, floorf(center + fh.ascent / 2.0)));
}
Example #10
0
File: packet.c Project: NICMx/Jool
static verdict paranoid_validations(struct xlation *state, size_t min_hdr_size)
{
	verdict result;

	result = fail_if_shared(state);
	if (result != VERDICT_CONTINUE)
		return result;
	result = fail_if_broken_offset(state);
	if (result != VERDICT_CONTINUE)
		return result;
	if (!pskb_may_pull(state->in.skb, min_hdr_size))
		return truncated(state, "basic IP header");

	return VERDICT_CONTINUE;
}
Example #11
0
File: packet.c Project: NICMx/Jool
verdict pkt_init_ipv4(struct xlation *state, struct sk_buff *skb)
{
	struct pkt_metadata meta;
	verdict result;

	state->in.skb = skb;

	/*
	 * DO NOT, UNDER ANY CIRCUMSTANCES, EXTRACT ANY BYTES FROM THE SKB'S
	 * DATA AREA DIRECTLY (ie. without using skb_hdr_ptr()) UNTIL YOU KNOW
	 * IT HAS ALREADY BEEN pskb_may_pull()ED. ASSUME THAT EVEN THE MAIN
	 * LAYER 3 HEADER CAN BE PAGED.
	 *
	 * Also, careful in this function and subfunctions. pskb_may_pull()
	 * might change pointers, so you generally don't want to store them.
	 */

	result = paranoid_validations(state, sizeof(struct iphdr));
	if (result != VERDICT_CONTINUE)
		return result;

	log_debug("Packet addresses: %pI4->%pI4",
			&ip_hdr(skb)->saddr,
			&ip_hdr(skb)->daddr);

	result = summarize_skb4(state, &meta);
	if (result != VERDICT_CONTINUE)
		return result;

	if (!pskb_may_pull(skb, meta.payload_offset)) {
		log_debug("Could not 'pull' the headers out of the skb.");
		return truncated(state, "headers");
	}

	state->in.l3_proto = L3PROTO_IPV4;
	state->in.l4_proto = meta.l4_proto;
	state->in.is_inner = false;
	state->in.is_hairpin = false;
	state->in.hdr_frag = NULL;
	skb_set_transport_header(skb, meta.l4_offset);
	state->in.payload = skb_offset_to_ptr(skb, meta.payload_offset);
	state->in.original_pkt = &state->in;

	return VERDICT_CONTINUE;
}
Example #12
0
File: packet.c Project: NICMx/Jool
static verdict summarize_skb4(struct xlation *state, struct pkt_metadata *meta)
{
	struct iphdr *hdr4 = ip_hdr(state->in.skb);
	unsigned int offset;

	hdr4 = ip_hdr(state->in.skb);
	offset = skb_network_offset(state->in.skb) + (hdr4->ihl << 2);

	meta->has_frag_hdr = false;
	meta->l4_offset = offset;
	meta->payload_offset = offset;

	switch (hdr4->protocol) {
	case IPPROTO_TCP:
		meta->l4_proto = L4PROTO_TCP;
		if (is_first_frag4(hdr4)) {
			struct tcphdr buffer, *ptr;
			ptr = skb_hdr_ptr(state->in.skb, offset, buffer);
			if (!ptr)
				return truncated(state, "TCP header");
			meta->payload_offset += tcp_hdr_len(ptr);
		}
		return VERDICT_CONTINUE;

	case IPPROTO_UDP:
		meta->l4_proto = L4PROTO_UDP;
		if (is_first_frag4(hdr4))
			meta->payload_offset += sizeof(struct udphdr);
		return VERDICT_CONTINUE;

	case IPPROTO_ICMP:
		meta->l4_proto = L4PROTO_ICMP;
		if (is_first_frag4(hdr4))
			meta->payload_offset += sizeof(struct icmphdr);
		return handle_icmp4(state, meta);
	}

	meta->l4_proto = L4PROTO_OTHER;
	return VERDICT_CONTINUE;
}
Example #13
0
File: packet.c Project: NICMx/Jool
static verdict handle_icmp4(struct xlation *state, struct pkt_metadata *meta)
{
	struct icmphdr buffer, *ptr;
	verdict result;

	ptr = skb_hdr_ptr(state->in.skb, meta->l4_offset, buffer);
	if (!ptr)
		return truncated(state, "ICMP header");

	if (has_inner_pkt4(ptr->type)) {
		result = validate_inner4(state, meta);
		if (result != VERDICT_CONTINUE)
			return result;
	}

	if (xlat_is_siit() && is_icmp4_info(ptr->type) && is_fragmented_ipv4(ip_hdr(state->in.skb))) {
		log_debug("Packet is a fragmented ping; its checksum cannot be translated.");
		return drop(state, JSTAT_FRAGMENTED_PING);
	}

	return VERDICT_CONTINUE;
}
Example #14
0
void
ShowImageStatusView::Draw(BRect updateRect)
{
	rgb_color darkShadow = tint_color(LowColor(), B_DARKEN_2_TINT);
	rgb_color shadow = tint_color(LowColor(), B_DARKEN_1_TINT);
	rgb_color light = tint_color(LowColor(), B_LIGHTEN_MAX_TINT);

	BRect b(Bounds());

	BeginLineArray(5);
		AddLine(BPoint(b.left, b.top),
				BPoint(b.right, b.top), darkShadow);
		b.top += 1.0;
		AddLine(BPoint(b.left, b.top),
				BPoint(b.right, b.top), light);
		AddLine(BPoint(b.right, b.top + 1.0),
				BPoint(b.right, b.bottom), shadow);
		AddLine(BPoint(b.right - 1.0, b.bottom),
				BPoint(b.left + 1.0, b.bottom), shadow);
		AddLine(BPoint(b.left, b.bottom),
				BPoint(b.left, b.top + 1.0), light);
	EndLineArray();

	b.InsetBy(1.0, 1.0);

	// Truncate and layout text
	BString truncated(fText);
	BFont font;
	GetFont(&font);
	font.TruncateString(&truncated, B_TRUNCATE_MIDDLE, b.Width() - 4.0);
	font_height fh;
	font.GetHeight(&fh);

	FillRect(b, B_SOLID_LOW);
	SetDrawingMode(B_OP_OVER);
	DrawString(truncated.String(), BPoint(b.left + 2.0,
		floorf(b.top + b.Height() / 2.0 + fh.ascent / 2.0)));
}
Example #15
0
static void
vitrunc(void)
{
truncated(vision_name);
}
Example #16
0
int QDeclarativeText::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QDeclarativeImplicitSizeItem::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        if (_id < 17)
            qt_static_metacall(this, _c, _id, _a);
        _id -= 17;
    }
#ifndef QT_NO_PROPERTIES
      else if (_c == QMetaObject::ReadProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0: *reinterpret_cast< QString*>(_v) = text(); break;
        case 1: *reinterpret_cast< QFont*>(_v) = font(); break;
        case 2: *reinterpret_cast< QColor*>(_v) = color(); break;
        case 3: *reinterpret_cast< TextStyle*>(_v) = style(); break;
        case 4: *reinterpret_cast< QColor*>(_v) = styleColor(); break;
        case 5: *reinterpret_cast< HAlignment*>(_v) = hAlign(); break;
        case 6: *reinterpret_cast< VAlignment*>(_v) = vAlign(); break;
        case 7: *reinterpret_cast< WrapMode*>(_v) = wrapMode(); break;
        case 8: *reinterpret_cast< int*>(_v) = lineCount(); break;
        case 9: *reinterpret_cast< bool*>(_v) = truncated(); break;
        case 10: *reinterpret_cast< int*>(_v) = maximumLineCount(); break;
        case 11: *reinterpret_cast< TextFormat*>(_v) = textFormat(); break;
        case 12: *reinterpret_cast< TextElideMode*>(_v) = elideMode(); break;
        case 13: *reinterpret_cast< qreal*>(_v) = paintedWidth(); break;
        case 14: *reinterpret_cast< qreal*>(_v) = paintedHeight(); break;
        case 15: *reinterpret_cast< qreal*>(_v) = lineHeight(); break;
        case 16: *reinterpret_cast< LineHeightMode*>(_v) = lineHeightMode(); break;
        }
        _id -= 17;
    } else if (_c == QMetaObject::WriteProperty) {
        void *_v = _a[0];
        switch (_id) {
        case 0: setText(*reinterpret_cast< QString*>(_v)); break;
        case 1: setFont(*reinterpret_cast< QFont*>(_v)); break;
        case 2: setColor(*reinterpret_cast< QColor*>(_v)); break;
        case 3: setStyle(*reinterpret_cast< TextStyle*>(_v)); break;
        case 4: setStyleColor(*reinterpret_cast< QColor*>(_v)); break;
        case 5: setHAlign(*reinterpret_cast< HAlignment*>(_v)); break;
        case 6: setVAlign(*reinterpret_cast< VAlignment*>(_v)); break;
        case 7: setWrapMode(*reinterpret_cast< WrapMode*>(_v)); break;
        case 10: setMaximumLineCount(*reinterpret_cast< int*>(_v)); break;
        case 11: setTextFormat(*reinterpret_cast< TextFormat*>(_v)); break;
        case 12: setElideMode(*reinterpret_cast< TextElideMode*>(_v)); break;
        case 15: setLineHeight(*reinterpret_cast< qreal*>(_v)); break;
        case 16: setLineHeightMode(*reinterpret_cast< LineHeightMode*>(_v)); break;
        }
        _id -= 17;
    } else if (_c == QMetaObject::ResetProperty) {
        switch (_id) {
        case 5: resetHAlign(); break;
        case 10: resetMaximumLineCount(); break;
        }
        _id -= 17;
    } else if (_c == QMetaObject::QueryPropertyDesignable) {
        _id -= 17;
    } else if (_c == QMetaObject::QueryPropertyScriptable) {
        _id -= 17;
    } else if (_c == QMetaObject::QueryPropertyStored) {
        _id -= 17;
    } else if (_c == QMetaObject::QueryPropertyEditable) {
        _id -= 17;
    } else if (_c == QMetaObject::QueryPropertyUser) {
        _id -= 17;
    }
#endif // QT_NO_PROPERTIES
    return _id;
}
Example #17
0
void *load_exe(char *filename, Exe_head *eh)
{
long retval;
char *alligned_buf;
char *alloc_buf = NULL;
long (*rfunc)();
unsigned long code_offset;
unsigned long init_size;
unsigned long bss_size;
unsigned long total_size;
void *v;
unsigned long fixup_offset;
UWORD fixup[2];
UWORD *segpt;
UWORD code_seg;
int f;
unsigned i;
int ok = 0;

if ((f = jopen(filename, JREADONLY)) == 0)
	{
	cant_find(filename);
	return(NULL);
	}
if (!verify_exe_head(f, eh))
	goto OUT;
code_offset = eh->head_size;
code_offset *= 16;	/* make it a paragraph */
init_size = eh->blocks;
init_size *= 512;
init_size += eh->mod512;
if (eh->mod512 != 0)
	init_size -= 512;
init_size -= code_offset;
bss_size = eh->min_data;
bss_size *= 16;
total_size = init_size + bss_size;
if ((alloc_buf = begmem((unsigned)total_size+16)) == NULL)
	goto OUT;
code_seg = ptr_seg(alloc_buf) + 1;
alligned_buf = make_ptr(0, code_seg);
zero_structure(alligned_buf, (unsigned)total_size);
jseek(f, code_offset, JSEEK_START);
if (jread(f, alligned_buf, init_size) < init_size)
	{
	truncated(filename);
	goto OUT;
	}
v = alligned_buf;
eh->entry_point = v;
if (eh->reloc_count > 0)
	{
	fixup_offset = eh->reloc_list;
	jseek(f, fixup_offset, JSEEK_START);
	for (i=0; i<eh->reloc_count; i++)
		{
		if (jread(f, fixup, sizeof(fixup)) != sizeof(fixup))
			{
			truncated(filename);
			goto OUT;
			}
		segpt = make_ptr(fixup[0], code_seg + fixup[1]);
		segpt[0] += code_seg;
		}
	}
ok = 1;
OUT:
if (!ok)
	{
	gentle_freemem(alloc_buf);
	alloc_buf = NULL;
	}
jclose(f);
return(alloc_buf);
}
Example #18
0
static
truncm()
{
truncated(mac_name);
}
Example #19
0
File: packet.c Project: NICMx/Jool
/**
 * Walks through @skb's headers, collecting data and adding it to @meta.
 *
 * @hdr6_offset number of bytes between skb->data and the IPv6 header.
 *
 * BTW: You might want to read summarize_skb4() first, since it's a lot simpler.
 */
static verdict summarize_skb6(struct xlation *state,
		unsigned int hdr6_offset,
		struct pkt_metadata *meta)
{
	union {
		struct ipv6_opt_hdr opt;
		struct frag_hdr frag;
		struct tcphdr tcp;
	} buffer;
	union {
		struct ipv6_opt_hdr *opt;
		struct frag_hdr *frag;
		struct tcphdr *tcp;
		u8 *nexthdr;
	} ptr;

	struct sk_buff *skb = state->in.skb;
	u8 nexthdr;
	unsigned int offset;
	bool is_first = true;

	ptr.nexthdr = skb_hdr_ptr(skb,
			hdr6_offset + offsetof(struct ipv6hdr, nexthdr),
			nexthdr);
	if (!ptr.nexthdr)
		return truncated(state, "IPv6 header");
	nexthdr = *ptr.nexthdr;
	offset = hdr6_offset + sizeof(struct ipv6hdr);

	meta->has_frag_hdr = false;

	do {
		switch (nexthdr) {
		case NEXTHDR_TCP:
			meta->l4_proto = L4PROTO_TCP;
			meta->l4_offset = offset;
			meta->payload_offset = offset;

			if (is_first) {
				ptr.tcp = skb_hdr_ptr(skb, offset, buffer.tcp);
				if (!ptr.tcp)
					return truncated(state, "TCP header");
				meta->payload_offset += tcp_hdr_len(ptr.tcp);
			}

			return VERDICT_CONTINUE;

		case NEXTHDR_UDP:
			meta->l4_proto = L4PROTO_UDP;
			meta->l4_offset = offset;
			meta->payload_offset = is_first
					? (offset + sizeof(struct udphdr))
					: offset;
			return VERDICT_CONTINUE;

		case NEXTHDR_ICMP:
			meta->l4_proto = L4PROTO_ICMP;
			meta->l4_offset = offset;
			meta->payload_offset = is_first
					? (offset + sizeof(struct icmp6hdr))
					: offset;
			return VERDICT_CONTINUE;

		case NEXTHDR_FRAGMENT:
			ptr.frag = skb_hdr_ptr(skb, offset, buffer.frag);
			if (!ptr.frag)
				return truncated(state, "fragment header");

			meta->has_frag_hdr = true;
			meta->frag_offset = offset;
			is_first = is_first_frag6(ptr.frag);

			offset += sizeof(struct frag_hdr);
			nexthdr = ptr.frag->nexthdr;
			break;

		case NEXTHDR_HOP:
		case NEXTHDR_ROUTING:
		case NEXTHDR_DEST:
			ptr.opt = skb_hdr_ptr(skb, offset, buffer.opt);
			if (!ptr.opt)
				return truncated(state, "extension header");

			offset += ipv6_optlen(ptr.opt);
			nexthdr = ptr.opt->nexthdr;
			break;

		default:
			meta->l4_proto = L4PROTO_OTHER;
			meta->l4_offset = offset;
			meta->payload_offset = offset;
			return VERDICT_CONTINUE;
		}
	} while (true);

	return VERDICT_CONTINUE; /* whatever. */
}
int main(int argc ,char* argv[])
{
	if(argc<3) {
		printf("hostname and recode type required");
		exit(0);
	}
	signal(SIGALRM,ha);
	struct sockaddr_in servaddr;
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family= AF_INET;
	servaddr.sin_port =htons( SERV_PORT);
	servaddr.sin_addr.s_addr = inet_addr(addr);

	int sock = socket(AF_INET,SOCK_DGRAM,0);
	if(sock<0) errExit("socket");
	Msg msg;
	bzero(&msg, sizeof(msg));
	msg.header.id = htons(1);
	msg.header.noques = htons(1);

	msg.qtype=htons(findType(argv[2]));
	
	msg.qclass=htons(1);
	
	char delim[2] = ".";
   	char *label;
   	int i=0;
    label = strtok(argv[1], delim);
    while( label!= NULL ) 
    {
   	  msg.question[i].length = strlen(label);
   	  memcpy(msg.question[i].label,label,strlen(label));
   	  i++;
      label = strtok(NULL, delim);
    }
    msg.question[i].length=0;
    

    char* udp_msg = (char*)malloc(512*sizeof(char));
	char* udp_temp = udp_msg;
	bzero(udp_msg, 512);
	memcpy(udp_temp,&msg.header.id,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.header.param,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.header.noques,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.header.noans,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.header.noauth,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.header.noadd,2);
	udp_temp += 2;
	int m = 0;
	while(msg.question[m].length != 0){
		memcpy(udp_temp,(char*)&msg.question[m].length,1);
		udp_temp += 1;
		memcpy(udp_temp,msg.question[m].label,strlen(msg.question[m].label));
		udp_temp += strlen(msg.question[m].label);
		m++;
	}
	*udp_temp = 0;
	udp_temp += 1;
	memcpy(udp_temp,(char*)&msg.qtype,2);
	udp_temp += 2;
	memcpy(udp_temp,(char*)&msg.qclass,2);
	udp_temp += 2;
	*udp_temp = '\0';

	int answer_offset=udp_temp-udp_msg;
	char recvline[513];
	while(1){
		printf("MSG TO BE SENT:\n");
		printQuestionSection(udp_msg);
		printf("\nSending to server \n");
		printf("Trying %s.....\n",inet_ntoa(servaddr.sin_addr));
		alarm(5);
		if(sendto(sock,udp_msg,512,0,(struct sockaddr*) &servaddr,sizeof(servaddr)) < 0)
			errExit("sendto");
		alarm(0);
		int n;
		alarm(5);
		if((n = recvfrom(sock,recvline,512,0,NULL,NULL)) < 0)
			errExit("recvfrom");
		alarm(0);
		recvline[n] = '\0';

		printf("MSG RECIEVED:\n");
		printQuestionSection(recvline);
		char* newAddr= printAnswerSection(recvline,answer_offset);
		
		printf("\n******************************************************************************************\n");

		char* temp= recvline+2;
		if((*temp) & (uint8_t)0x04)
		{
			printf("Got authoritative answer\n");
			exit(0);
		}
		else
		{
			
			
			if(truncated(recvline))
			{
				printf("Truncated msg, exiting\n");
				exit(0);
			}
			if(error(recvline) || newAddr==NULL)
			{
				setjmp(env);
				canjmp=1;
				if(cur<0)
					exit(0);
				else 
				{
					newAddr = ipaddresses[cur];
					cur--;
					num--;
				}
			}
			servaddr.sin_addr.s_addr= *((int32_t*) newAddr);
		}
	}
return 0;
}