Esempio n. 1
0
int
isns_req_receive(int s, struct isns_req *req)
{
	struct isns_hdr *hdr;
	ssize_t res, len;

	req->ir_usedlen = 0;
	isns_req_getspace(req, sizeof(*hdr));
	res = read(s, req->ir_buf, sizeof(*hdr));
	if (res < (ssize_t)sizeof(*hdr))
		return (-1);
	req->ir_usedlen = sizeof(*hdr);
	hdr = (struct isns_hdr *)req->ir_buf;
	if (be16dec(hdr->ih_version) != ISNS_VERSION)
		return (-1);
	if ((be16dec(hdr->ih_flags) & (ISNS_FLAG_LAST | ISNS_FLAG_FIRST)) !=
	    (ISNS_FLAG_LAST | ISNS_FLAG_FIRST))
		return (-1);
	len = be16dec(hdr->ih_length);
	isns_req_getspace(req, len);
	res = read(s, &req->ir_buf[req->ir_usedlen], len);
	if (res < len)
		return (-1);
	req->ir_usedlen += len;
	return (0);
}
Esempio n. 2
0
/*
 * Decode the relevant fields of a sun disk label, and return zero if the
 * magic and checksum works out OK.
 */
int
sunlabel_dec(void const *pp, struct sun_disklabel *sl)
{
	const uint8_t *p;
	size_t i;
	u_int u;

	p = pp;
	for (i = 0; i < sizeof(sl->sl_text); i++)
		sl->sl_text[i] = p[SL_TEXT + i];
	sl->sl_rpm = be16dec(p + SL_RPM);
	sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS);
	sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL);
	sl->sl_interleave = be16dec(p + SL_INTERLEAVE);
	sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS);
	sl->sl_acylinders = be16dec(p + SL_ACYLINDERS);
	sl->sl_ntracks = be16dec(p + SL_NTRACKS);
	sl->sl_nsectors = be16dec(p + SL_NSECTORS);
	for (i = 0; i < SUN_NPART; i++) {
		sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART +
		    (i * SDKP_SIZEOF) + SDKP_CYLOFFSET);
		sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART +
		    (i * SDKP_SIZEOF) + SDKP_NSECTORS);
	}
	sl->sl_magic = be16dec(p + SL_MAGIC);
	for (i = u = 0; i < SUN_SIZE; i += 2)
		u ^= be16dec(p + i);
	if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
		return (0);
	else
		return (EINVAL);
}
Esempio n. 3
0
void
uuid_dec_be(void const *buf, struct uuid *uuid)
{
	const uint8_t *p = buf;
	int i;

	uuid->time_low = be32dec(p);
	uuid->time_mid = be16dec(p + 4);
	uuid->time_hi_and_version = be16dec(p + 6);
	uuid->clock_seq_hi_and_reserved = p[8];
	uuid->clock_seq_low = p[9];
	for (i = 0; i < _UUID_NODE_LEN; i++)
		uuid->node[i] = p[10 + i];
}
Esempio n. 4
0
/*
 * Encode the relevant fields into a sun disklabel, compute new checksum.
 */
void
sunlabel_enc(void *pp, struct sun_disklabel *sl)
{
	uint8_t *p;
	size_t i;
	u_int u;

	p = pp;
	for (i = 0; i < SL_TEXT_SIZEOF; i++)
		p[SL_TEXT + i] = sl->sl_text[i];
	be16enc(p + SL_RPM, sl->sl_rpm);
	be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
	be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
	be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
	be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
	be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
	be16enc(p + SL_NTRACKS, sl->sl_ntracks);
	be16enc(p + SL_NSECTORS, sl->sl_nsectors);
	for (i = 0; i < SUN_NPART; i++) {
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
		    sl->sl_part[i].sdkp_cyloffset);
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
		    sl->sl_part[i].sdkp_nsectors);
	}
	be16enc(p + SL_MAGIC, sl->sl_magic);
	for (i = u = 0; i < SUN_SIZE; i += 2)
		u ^= be16dec(p + i);
	be16enc(p + SL_CKSUM, u);
}
Esempio n. 5
0
static uint64_t tdecode(int t, ...)
{
	uint8_t buf[16];
	bool be = t > 0;
	va_list ap;
	uint64_t val = 777;
	int i;

	if (t < 0) t = -t;

	va_start(ap, t);
	memset(buf, 0xC1, sizeof(buf));
	for (i = 0; i < t; i++)
		buf[i] = va_arg(ap, int);
	va_end(ap);

	if (be) {
		switch (t) {
		case 2: val = be16dec(buf); break;
		case 4: val = be32dec(buf); break;
		case 8: val = be64dec(buf); break;
		}
	} else {
		switch (t) {
		case 2: val = le16dec(buf); break;
		case 4: val = le32dec(buf); break;
		case 8: val = le64dec(buf); break;
		}
	}
	return val;
}
Esempio n. 6
0
static void
g_dec_drvrdesc(u_char *ptr, struct g_apple_softc *sc)
{
	sc->dd_bsiz = be16dec(ptr + 2);
	sc->dd_blkcnt = be32dec(ptr + 4);
	sc->dd_drvrcnt = be32dec(ptr + 16);
}
Esempio n. 7
0
static struct servent *
_servent_getbyport(struct servent_data *sd, struct servent *sp, int port,
    const char *proto)
{

	if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0)
		return NULL;

	if (sd->flags & _SV_CDB) {
		uint8_t buf[255 + 4];
		size_t protolen;
		const uint8_t *data;
		const void *data_ptr;
		size_t datalen;

		port = be16toh(port);

		if (proto != NULL && *proto == '\0')
			return NULL;
		if (proto != NULL)
			protolen = strlen(proto);
		else
			protolen = 0;
		if (port < 0 || port > 65536)
			return NULL;

		buf[0] = 0;
		buf[1] = protolen;
		be16enc(buf + 2, port);
		memcpy(buf + 4, proto, protolen);

		if (cdbr_find(sd->cdb, buf, 4 + protolen,
		    &data_ptr, &datalen))
			return NULL;

		if (datalen < protolen + 4)
			return NULL;

		data = data_ptr;
		if (be16dec(data) != port)
			return NULL;
		if (protolen) {
			if (data[2] != protolen)
				return NULL;
			if (memcmp(data + 3, proto, protolen + 1))
				return NULL;
		}
		return _servent_parsedb(sd, sp, data, datalen);
	} else {
		while (_servent_getline(sd) != -1) {
			if (_servent_parseline(sd, sp) == NULL)
				continue;
			if (sp->s_port != port)
				continue;
			if (proto == NULL || strcmp(sp->s_proto, proto) == 0)
				return sp;
		}
		return NULL;
	}
}
Esempio n. 8
0
unsigned int
peak_tcp_prepare(struct peak_packet *packet)
{
	struct tcphdr *th = packet->flow.th;
	unsigned int flow_hlen, flags;

	flow_hlen = th->th_off << 2;
	if (unlikely(flow_hlen < sizeof(*th) ||
	    flow_hlen > TCP_MAXHLEN)) {
		return (1);
	}

	if (unlikely(flow_hlen > packet->flow_len)) {
		return (1);
	}

	if (unlikely(!th->th_sport || !th->th_dport)) {
		return (1);
	}

	/* XXX verify checksum */

	flags = th->th_flags;
	if (flags & TH_SYN) {
		if (flags & TH_RST) {
			return (1);
		}
	} else {
		if (!(flags & (TH_ACK|TH_RST))) {
			return (1);
		}
	}

	if (!(flags & TH_ACK)) {
		if (flags & (TH_FIN|TH_PUSH|TH_URG)) {
			return (1);
		}
	}

	packet->app.raw = packet->flow.raw + flow_hlen;
	packet->app_len = packet->flow_len - flow_hlen;
	packet->flow_sport = be16dec(&th->th_sport);
	packet->flow_dport = be16dec(&th->th_dport);
	packet->flow_hlen = flow_hlen;

	return (0);
}
Esempio n. 9
0
static u_int64_t
elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member)
{
    u_int64_t val;

    val = 0;
    switch (e->e_ident[EI_CLASS]) {
    case ELFCLASS32:
        base = (char *)base + elf32_offsets[member];
        switch (e->e_ident[EI_DATA]) {
        case ELFDATA2MSB:
            val = be16dec(base);
            break;
        case ELFDATA2LSB:
            val = le16dec(base);
            break;
        case ELFDATANONE:
            errx(1, "invalid data format");
        }
        break;
    case ELFCLASS64:
        base = (char *)base + elf64_offsets[member];
        switch (e->e_ident[EI_DATA]) {
        case ELFDATA2MSB:
            val = be16dec(base);
            break;
        case ELFDATA2LSB:
            val = le16dec(base);
            break;
        case ELFDATANONE:
            errx(1, "invalid data format");
        }
        break;
    case ELFCLASSNONE:
        errx(1, "invalid class");
    }

    return val;
}
Esempio n. 10
0
static void
le_lebuffer_copytodesc(struct lance_softc *sc, void *fromv, int off, int len)
{
	struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc;
	caddr_t from = fromv;

	for (; len >= 8; len -= 8, off += 8, from += 8)
		bus_write_8(lesc->sc_bres, off, be64dec(from));
	for (; len >= 4; len -= 4, off += 4, from += 4)
		bus_write_4(lesc->sc_bres, off, be32dec(from));
	for (; len >= 2; len -= 2, off += 2, from += 2)
		bus_write_2(lesc->sc_bres, off, be16dec(from));
	if (len == 1)
		bus_write_1(lesc->sc_bres, off, *from);
}
/*
 * Encode the relevant fields into a sun disklabel, compute new checksum.
 */
void
sunlabel_enc(void *pp, struct sun_disklabel *sl)
{
	uint8_t *p;
	size_t i;
	u_int u;

	p = pp;
	for (i = 0; i < SL_TEXT_SIZEOF; i++)
		p[SL_TEXT + i] = sl->sl_text[i];
	be16enc(p + SL_RPM, sl->sl_rpm);
	be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders);
	be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl);
	be16enc(p + SL_INTERLEAVE, sl->sl_interleave);
	be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders);
	be16enc(p + SL_ACYLINDERS, sl->sl_acylinders);
	be16enc(p + SL_NTRACKS, sl->sl_ntracks);
	be16enc(p + SL_NSECTORS, sl->sl_nsectors);
	for (i = 0; i < SUN_NPART; i++) {
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET,
		    sl->sl_part[i].sdkp_cyloffset);
		be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS,
		    sl->sl_part[i].sdkp_nsectors);
	}
	be16enc(p + SL_MAGIC, sl->sl_magic);
	if (sl->sl_vtoc_sane == SUN_VTOC_SANE
	    && sl->sl_vtoc_nparts == SUN_NPART) {
		/*
		 * Write SVR4-compatible VTOC elements.
		 */
		be32enc(p + SL_VTOC_VERS, sl->sl_vtoc_vers);
		be32enc(p + SL_VTOC_SANITY, SUN_VTOC_SANE);
		memcpy(p + SL_VTOC_VOLNAME, sl->sl_vtoc_volname,
		    SUN_VOLNAME_LEN);
		be16enc(p + SL_VTOC_NPART, SUN_NPART);
		for (i = 0; i < SUN_NPART; i++) {
			be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
				+ SVTOC_TAG,
				sl->sl_vtoc_map[i].svtoc_tag);
			be16enc(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF)
				+ SVTOC_FLAG,
				sl->sl_vtoc_map[i].svtoc_flag);
		}
	}
	for (i = u = 0; i < SUN_SIZE; i += 2)
		u ^= be16dec(p + i);
	be16enc(p + SL_CKSUM, u);
}
Esempio n. 12
0
int
isns_req_send(int s, struct isns_req *req)
{
	struct isns_hdr *hdr;
	int res;

	hdr = (struct isns_hdr *)req->ir_buf;
	be16enc(hdr->ih_length, req->ir_usedlen - sizeof(*hdr));
	be16enc(hdr->ih_flags, be16dec(hdr->ih_flags) |
	    ISNS_FLAG_LAST | ISNS_FLAG_FIRST);
	be16enc(hdr->ih_transaction, 0);
	be16enc(hdr->ih_sequence, 0);

	res = write(s, req->ir_buf, req->ir_usedlen);
	return ((res < 0) ? -1 : 0);
}
Esempio n. 13
0
static void
test_load(const char *file, const unsigned int *len, const size_t count)
{
	struct peak_load *trace = peak_load_init(file);
	struct ether_header *eth;
	size_t i;

	assert(trace);

	eth = (struct ether_header *)trace->buf;

	for (i = 0; i < count; ++i) {
		assert(peak_load_next(trace) == len[i]);
		assert(be16dec(&eth->ether_type) == ETHERTYPE_IP);
	}

	assert(!peak_load_next(trace));
	assert(!peak_load_next(trace));

	peak_load_exit(trace);
}
Esempio n. 14
0
static int
vend_rfc1048(u_char *cp, u_int len)
{
	u_char *ep;
	int size;
	u_char tag;
	const char *val;

#ifdef BOOTP_DEBUG
	if (debug)
		printf("vend_rfc1048 bootp info. len=%d\n", len);
#endif
	ep = cp + len;

	/* Step over magic cookie */
	cp += sizeof(int);

	setenv_(cp, ep, NULL);

	while (cp < ep) {
		tag = *cp++;
		size = *cp++;
		if (tag == TAG_END)
			break;

		if (tag == TAG_SUBNET_MASK) {
			bcopy(cp, &netmask, sizeof(netmask));
		}
		if (tag == TAG_GATEWAY) {
			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
		}
		if (tag == TAG_SWAPSERVER) {
			/* let it override bp_siaddr */
			bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
		}
		if (tag == TAG_ROOTPATH) {
			if ((val = getenv("dhcp.root-path")) == NULL)
				val = (const char *)cp;
			strlcpy(rootpath, val, sizeof(rootpath));
		}
		if (tag == TAG_HOSTNAME) {
			if ((val = getenv("dhcp.host-name")) == NULL)
				val = (const char *)cp;
			strlcpy(hostname, val, sizeof(hostname));
		}
		if (tag == TAG_INTF_MTU) {
			intf_mtu = 0;
			if ((val = getenv("dhcp.interface-mtu")) != NULL) {
				unsigned long tmp;
				char *end;

				errno = 0;
				/*
				 * Do not allow MTU to exceed max IPv4 packet
				 * size, max value of 16-bit word.
				 */
				tmp = strtoul(val, &end, 0);
				if (errno != 0 ||
				    *val == '\0' || *end != '\0' ||
				    tmp > USHRT_MAX) {
					printf("%s: bad value: \"%s\", "
					    "ignoring\n",
					    "dhcp.interface-mtu", val);
				} else {
					intf_mtu = (u_int)tmp;
				}
			}
			if (intf_mtu == 0)
				intf_mtu = be16dec(cp);
		}
#ifdef SUPPORT_DHCP
		if (tag == TAG_DHCP_MSGTYPE) {
			if(*cp != expected_dhcpmsgtype)
			    return(-1);
			dhcp_ok = 1;
		}
		if (tag == TAG_SERVERID) {
			bcopy(cp, &dhcp_serverip.s_addr,
			      sizeof(dhcp_serverip.s_addr));
		}
#endif
		cp += size;
	}
	return(0);
}
/*
 * Decode the relevant fields of a sun disk label, and return zero if the
 * magic and checksum works out OK.
 */
int
sunlabel_dec(void const *pp, struct sun_disklabel *sl)
{
	const uint8_t *p;
	size_t i;
	u_int u;
	uint32_t vtocsane;
	uint16_t npart;

	p = pp;
	for (i = 0; i < sizeof(sl->sl_text); i++)
		sl->sl_text[i] = p[SL_TEXT + i];
	sl->sl_rpm = be16dec(p + SL_RPM);
	sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS);
	sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL);
	sl->sl_interleave = be16dec(p + SL_INTERLEAVE);
	sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS);
	sl->sl_acylinders = be16dec(p + SL_ACYLINDERS);
	sl->sl_ntracks = be16dec(p + SL_NTRACKS);
	sl->sl_nsectors = be16dec(p + SL_NSECTORS);
	for (i = 0; i < SUN_NPART; i++) {
		sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART +
		    (i * SDKP_SIZEOF) + SDKP_CYLOFFSET);
		sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART +
		    (i * SDKP_SIZEOF) + SDKP_NSECTORS);
	}
	sl->sl_magic = be16dec(p + SL_MAGIC);
	vtocsane = be32dec(p + SL_VTOC_SANITY);
	npart = be16dec(p + SL_VTOC_NPART);
	if (vtocsane == SUN_VTOC_SANE && npart == SUN_NPART) {
		/*
		 * Seems we've got SVR4-compatible VTOC information
		 * as well, decode it.
		 */
		sl->sl_vtoc_sane = vtocsane;
		sl->sl_vtoc_vers = be32dec(p + SL_VTOC_VERS);
		memcpy(sl->sl_vtoc_volname, p + SL_VTOC_VOLNAME,
		    SUN_VOLNAME_LEN);
		sl->sl_vtoc_nparts = SUN_NPART;
		for (i = 0; i < SUN_NPART; i++) {
			sl->sl_vtoc_map[i].svtoc_tag = be16dec(p +
				SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_TAG);
			sl->sl_vtoc_map[i].svtoc_flag = be16dec(p +
				SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_FLAG);
		}
	}
	for (i = u = 0; i < SUN_SIZE; i += 2)
		u ^= be16dec(p + i);
	if (u == 0 && sl->sl_magic == SUN_DKMAGIC)
		return (0);
	else
		return (EINVAL);
}
Esempio n. 16
0
struct servent *
_servent_parsedb(struct servent_data *sd, struct servent *sp,
    const uint8_t *data, size_t len)
{
	char **q;
	size_t i;
	int oerrno;

	if ((sd->flags & _SV_STAYOPEN) == 0) {
		if (len > sd->cdb_buf_len) {
			void *tmp = realloc(sd->cdb_buf, len);
			if (tmp == NULL)
				goto fail;
			sd->cdb_buf = tmp;
			sd->cdb_buf_len = len;
		}
		memcpy(sd->cdb_buf, data, len);
		data = sd->cdb_buf;
	}

	if (len < 2)
		goto fail;
	sp->s_port = htobe16(be16dec(data));
	data += 2;
	len -= 2;

	if (len == 0 || len < (size_t)data[0] + 2)
		goto fail;
	sp->s_proto = __UNCONST(data + 1);

	if (sp->s_proto[data[0]] != '\0')
		goto fail;

	len -= 2 + data[0];
	data += 2 + data[0];

	if (len == 0)
		goto fail;
	if (len < (size_t)data[0] + 2)
		goto fail;

	sp->s_name = __UNCONST(data + 1);
	len -= 2 + data[0];
	data += 2 + data[0];

	if (sd->aliases == NULL) {
		sd->maxaliases = 10;
		sd->aliases = malloc(sd->maxaliases * sizeof(char *));
		if (sd->aliases == NULL)
			goto fail;
	}
	sp->s_aliases = sd->aliases;
	i = 0;
	while (len) {
		if (len < (size_t)data[0] + 2)
			goto fail;
		if (i == sd->maxaliases - 2) {
			sd->maxaliases *= 2;
			q = realloc(sd->aliases, sd->maxaliases * sizeof(*q));
			if (q == NULL)
				goto fail;
			sp->s_aliases = sd->aliases = q;
		}
		sp->s_aliases[i++] = __UNCONST(data + 1);
		len -= 2 + data[0];
		data += 2 + data[0];
	}
	sp->s_aliases[i] = NULL;
	return sp;

fail:
	oerrno = errno;
	endservent_r(sd);
	errno = oerrno;
	return NULL;
}