Esempio n. 1
0
int main(void)
{
	struct netflow_header *pkt_head;

	srand(time(NULL));
	for (;;)
	{
		pkt_head = set_header();

		set_data_template();
		pkt_head->count++;

		set_data(rand());
		pkt_head->count++;

		pkt_head->count = htobe16(pkt_head->count);
		write(1, accum, pos);
		sleep(1);
	}


	return 0;



	
}
Esempio n. 2
0
Address::Address( uint32 ip, uint16 port )
{
	::memset( &m_addr, 0x00, sizeof(m_addr) );
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.s_addr = htobe32( ip );
	m_addr.sin_port = htobe16( port );
}
Esempio n. 3
0
static void
usie_if_init(void *arg)
{
    struct usie_softc *sc = arg;
    struct ifnet *ifp = sc->sc_ifp;
    uint8_t i;

    mtx_lock(&sc->sc_mtx);

    /* write tx descriptor */
    sc->sc_txd.hip.id = USIE_HIP_CTX;
    sc->sc_txd.hip.param = 0;	/* init value */
    sc->sc_txd.desc_type = htobe16(USIE_IP_TX);

    for (i = 0; i != USIE_IF_N_XFER; i++)
        usbd_xfer_set_stall(sc->sc_if_xfer[i]);

    usbd_transfer_start(sc->sc_uc_xfer[USIE_HIP_IF][USIE_UC_RX]);
    usbd_transfer_start(sc->sc_if_xfer[USIE_IF_STATUS]);
    usbd_transfer_start(sc->sc_if_xfer[USIE_IF_RX]);

    /* if not running, initiate the modem */
    if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
        usie_cns_req(sc, USIE_CNS_ID_INIT, USIE_CNS_OB_LINK_UPDATE);

    mtx_unlock(&sc->sc_mtx);

    DPRINTF("ifnet initialized\n");
}
Esempio n. 4
0
LMD LinkC_lEth(PollMgr pm, char* ifname) {
	int mtu;
	
	struct sockaddr_ll addr = {0};
	addr.sll_family = AF_INET;
	addr.sll_protocol = htobe16(LinkC_eth_proto);
	if (!LinkC_getIfInfo(ifname, &(addr.sll_ifindex), &mtu)) return NULL;

	int bpf = CapsH_createBPF(ifname);
	if (bpf == -1) return NULL;
	
	NBS nbs = NBS_ctor(bpf, bpf, SockType_BPF);

	NBS_pollAttach(nbs, pm);
	
	/* get size of BPF buffer */
	if( ioctl( bpf, BIOCGBLEN, &nbs->bpf_len ) == -1 ) {
	    perror("error getting size of bpf device\n");
 	    return NULL;	
	}

	SockAddr lAddr = SockAddr_create(&addr, sizeof(struct sockaddr_ll));
	((struct sockaddr_ll*)(SockAddr_addr(lAddr)))->sll_family = AF_PACKET;
	LMD lmd = LMD_ctor(nbs, lAddr, mtu);
	return lmd;
}
Esempio n. 5
0
inline size_t
writeVarNumber(std::ostream& os, uint64_t varNumber)
{
  if (varNumber < 253) {
    os.put(static_cast<char>(varNumber));
    return 1;
  }
  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
    os.put(static_cast<char>(253));
    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
    os.write(reinterpret_cast<const char*>(&value), 2);
    return 3;
  }
  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
    os.put(static_cast<char>(254));
    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
    os.write(reinterpret_cast<const char*>(&value), 4);
    return 5;
  }
  else {
    os.put(static_cast<char>(255));
    uint64_t value = htobe64(varNumber);
    os.write(reinterpret_cast<const char*>(&value), 8);
    return 9;
  }
}
Esempio n. 6
0
static int build_isgl(__be64 *queue_start, __be64 *queue_end,
		      struct fw_ri_isgl *isglp, struct ibv_sge *sg_list,
		      int num_sge, u32 *plenp)
{
	int i;
	u32 plen = 0;
	__be64 *flitp;

	if ((__be64 *)isglp == queue_end)
		isglp = (struct fw_ri_isgl *)queue_start;

	flitp = (__be64 *)isglp->sge;
	for (i = 0; i < num_sge; i++) {
		if ((plen + sg_list[i].length) < plen)
			return -EMSGSIZE;
		plen += sg_list[i].length;
		*flitp = htobe64(((u64)sg_list[i].lkey << 32) |
				 sg_list[i].length);
		if (++flitp == queue_end)
			flitp = queue_start;
		*flitp = htobe64(sg_list[i].addr);
		if (++flitp == queue_end)
			flitp = queue_start;
	}
	*flitp = 0;
	isglp->op = FW_RI_DATA_ISGL;
	isglp->r1 = 0;
	isglp->nsge = htobe16(num_sge);
	isglp->r2 = 0;
	if (plenp)
		*plenp = plen;
	return 0;
}
int main (int argc, char** argv) {
	uint16_t var16bit;
	uint32_t var32bit;
	uint64_t var64bit;
	uint64_t tmp;
	
	uint16_t var16bitResult;
	uint32_t var32bitResult;
	uint64_t var64bitResult;
	
	var16bit = 0x3210;
	var32bit = 0x76543210L;
	var64bit = 0x76543210L;
	
	tmp = 0xFEDCBA98L;
	var64bit = var64bit | (tmp << 32);
	
	var16bitResult = le16toh(var16bit);
	var16bitResult = htobe16(var16bit);
	
	var32bitResult = le32toh(var32bit);
	var32bitResult = htobe32(var32bit);
	
	var64bitResult = le64toh(var64bit);
	var64bitResult = htobe64(var64bit);

	/* for preventing unused variable warnings */
	printf("Argc = %i\nArgv[0] = %s\nResult 16bit = %"PRIu16"\nResult 32bit = %"PRIu32"\nResult 64bit = %"PRIu64"\n", argc, argv[0], var16bitResult, var32bitResult, var64bitResult);

	return 0;
}
Esempio n. 8
0
int xbee_api_send_frame(int fd, uint8_t type, const uint8_t * data, uint16_t len)
{
	fprintf(stderr, "xbee_api_send_frame\n");
	uint16_t totallen = sizeof(struct xbee_api_head) + len + sizeof(struct xbee_api_checksum);
	uint8_t * buf = (uint8_t *)malloc(totallen);
	uint8_t * end = buf + totallen;

	struct xbee_api_head * api_head = (struct xbee_api_head *)buf;
	uint8_t * api_payload = (uint8_t *)(buf + sizeof(struct xbee_api_head));	
	struct xbee_api_checksum * api_checksum = (struct xbee_api_checksum *)(buf + sizeof(struct xbee_api_head) + len);

	assert(api_payload <= end);
	assert((uint8_t *)api_checksum <= end);

	api_head->start_delim = 0x7e;
	api_head->length = htobe16(len+1);
	api_head->type = type;
	
	memcpy(api_payload, data, len);

	api_checksum->checksum = xbee_api_calc_checksum(&(api_head->type), len + 1);

	xbee_api_print_frame(fd, buf, totallen);
	xbee_api_uart_send_frame(fd, buf, totallen);

	return 0;
}
Esempio n. 9
0
static int handle_msg_attn(struct opal_prd_ctx *ctx, struct opal_prd_msg *msg)
{
	uint64_t proc, ipoll_mask, ipoll_status;
	int rc;

	proc = be64toh(msg->attn.proc);
	ipoll_status = be64toh(msg->attn.ipoll_status);
	ipoll_mask = be64toh(msg->attn.ipoll_mask);

	if (!hservice_runtime->handle_attns) {
		pr_log_nocall("handle_attns");
		return -1;
	}

	rc = call_handle_attns(proc, ipoll_status, ipoll_mask);
	if (rc) {
		pr_log(LOG_ERR, "HBRT: handle_attns(%lx,%lx,%lx) failed, rc %d",
				proc, ipoll_status, ipoll_mask, rc);
		return -1;
	}

	/* send the response */
	msg->hdr.type = OPAL_PRD_MSG_TYPE_ATTN_ACK;
	msg->hdr.size = htobe16(sizeof(*msg));
	msg->attn_ack.proc = htobe64(proc);
	msg->attn_ack.ipoll_ack = htobe64(ipoll_status);
	rc = write(ctx->fd, msg, sizeof(*msg));

	if (rc != sizeof(*msg)) {
		pr_log(LOG_WARNING, "FW: Failed to send ATTN_ACK message: %m");
		return -1;
	}

	return 0;
}
Esempio n. 10
0
static int run_attn_loop(struct opal_prd_ctx *ctx)
{
	struct pollfd pollfds[2];
	struct opal_prd_msg msg;
	int rc, fd;

	if (hservice_runtime->enable_attns) {
		pr_debug("HBRT: calling enable_attns");
		rc = call_enable_attns();
		if (rc) {
			pr_log(LOG_ERR, "HBRT: enable_attns() failed, "
					"aborting");
			return -1;
		}
	}

	/* send init message, to unmask interrupts */
	msg.hdr.type = OPAL_PRD_MSG_TYPE_INIT;
	msg.hdr.size = htobe16(sizeof(msg));
	msg.init.version = htobe64(opal_prd_version);
	msg.init.ipoll = htobe64(opal_prd_ipoll);

	pr_debug("FW: writing init message");
	rc = write(ctx->fd, &msg, sizeof(msg));
	if (rc != sizeof(msg)) {
		pr_log(LOG_ERR, "FW: Init message failed: %m. Aborting.");
		return -1;
	}

	pollfds[0].fd = ctx->fd;
	pollfds[0].events = POLLIN | POLLERR;
	pollfds[1].fd = ctx->socket;
	pollfds[1].events = POLLIN | POLLERR;

	for (;;) {
		rc = poll(pollfds, 2, -1);
		if (rc < 0) {
			pr_log(LOG_ERR, "FW: event poll failed: %m");
			exit(EXIT_FAILURE);
		}

		if (!rc)
			continue;

		if (pollfds[0].revents & POLLIN)
			handle_prd_msg(ctx);

		if (pollfds[1].revents & POLLIN) {
			fd = accept(ctx->socket, NULL, NULL);
			if (fd < 0) {
				pr_log(LOG_NOTICE, "CTRL: accept failed: %m");
				continue;
			}
			handle_prd_control(ctx, fd);
			close(fd);
		}
	}

	return 0;
}
Esempio n. 11
0
int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
{
        int s;
        union sockaddr_union src = {
                .in.sin_family = AF_INET,
                .in.sin_port = htobe16(port),
                .in.sin_addr.s_addr = address,
        };

        s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
        if (s < 0)
                return -errno;

        if (bind(s, &src.sa, sizeof(src.in)) < 0) {
                close_nointr_nofail(s);
                return -errno;
        }

        return s;
}

int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
                                 const void *packet, size_t len)
{
        assert(link);
        assert(packet);
        assert(len);

        if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
                return -errno;

        return 0;
}
Esempio n. 12
0
    void get_status(uint8_t *data, size_t rank)
    {
        auto hdr = header(data);

        hdr->rank = htobe16(rank);
        m_coder->write_feedback(hdr->status);
    }
Esempio n. 13
0
	size_t TunnelGatewayBuffer::CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len)
	{
		size_t ret = 1;
		buf[0] = block->deliveryType << 5; // flag
		if (block->deliveryType == eDeliveryTypeTunnel)
		{
			*(uint32_t *)(buf + ret) = htobe32 (block->tunnelID);
			ret += 4;
		}
		if (block->deliveryType == eDeliveryTypeTunnel || block->deliveryType == eDeliveryTypeRouter)
		{
			memcpy (buf + ret, block->hash, 32);
			ret += 32;
		}	
		size_t size = block->data->GetLength ();
		if (block->totalLen > len) // entire message doesn't fit
		{	
			buf[0] |= 0x08; // set fragmented bit
			m_NextMsgID = block->data->GetHeader ()->msgID;
			*(uint32_t *)(buf + ret) = m_NextMsgID;
			ret += 4; // msgID
			m_NextSeqn = 1;
			size = len - ret - 2; // 2 bytes for size field
			m_NextOffset = size;
		}	
		*(uint16_t *)(buf + ret) = htobe16 (size); // size
		ret += 2;
		memcpy (buf + ret, block->data->GetBuffer (), size);
		ret += size;
		return ret;	
	}	
Esempio n. 14
0
static bool is_keyfile_valid(const char *keyfile, char *keydata)
{
	int fd;
	int len;
	uint16_t size;

	fd = open(keyfile, O_RDONLY);
	if (fd == -1) {
		/* errno is set appropriately by the call to open() */
		perror("Keyfile error");
		return false;
	}

	/* First two bytes of data are used for storing the length.
	 * +1 read for validation.
	 */
	len = read(fd, keydata + 2, UAK_KEY_LENGTH + 1);
	close(fd);

	if (len != UAK_KEY_LENGTH) {
		warnx("Keyfile of incorrect length");
		return false;
	}

	/* Terminating '\0' is implied as buffer has been set to 0
	 * and exactly UAK_KEY_LENGTH bytes have been copied
	 */

	/* Fill first 2 bytes with the UAK length + 1 (+1 for ending '\0') */
	size = htobe16(UAK_KEY_LENGTH + 1);
	memcpy(keydata, &size, sizeof(size));

	return true;
}
Esempio n. 15
0
	I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::RouterInfo * router)
	{
		if (!router) // we send own RouterInfo
			router = &context.GetRouterInfo ();

		I2NPMessage * m = NewI2NPShortMessage ();
		I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload ();		

		memcpy (msg->key, router->GetIdentHash (), 32);
		msg->type = 0;
		msg->replyToken = 0;
		
		CryptoPP::Gzip compressor;
		compressor.Put (router->GetBuffer (), router->GetBufferLen ());
		compressor.MessageEnd();
		auto size = compressor.MaxRetrievable ();
		uint8_t * buf = m->GetPayload () + sizeof (I2NPDatabaseStoreMsg);
		*(uint16_t *)buf = htobe16 (size); // size
		buf += 2;
		// TODO: check if size doesn't exceed buffer
		compressor.Get (buf, size); 
		m->len += sizeof (I2NPDatabaseStoreMsg) + 2 + size; // payload size
		FillI2NPMessageHeader (m, eI2NPDatabaseStore);
		
		return m;
	}	
Esempio n. 16
0
static void writeInteger(std::vector<char> &buff, uint64_t value, int type)
{
    assert(type + 24 < 256);

    if( value < 24 )
    {
        uint8_t byte = type + value;

        buff.push_back(byte);
    }
    else if( value < 256 )
    {
        uint8_t bytes[2] = {static_cast<uint8_t>(type + 24), static_cast<uint8_t>(value)};

        buff.insert(buff.end(), bytes, bytes + sizeof(bytes));
    }
    else if( value < 65536 )
    {
        uint16_t ui16 = htobe16(value);
        uint8_t bytes[3] = {
            static_cast<uint8_t>(type + 25),
            static_cast<uint8_t>(ui16 & 0xff),
            static_cast<uint8_t>((ui16 & 0xff00) >> 8)
        };

        buff.insert(buff.end(), bytes, bytes + sizeof(bytes));
    }
size_t
Encoder::appendVarNumber(uint64_t varNumber)
{
  if (varNumber < 253) {
    appendByte(static_cast<uint8_t>(varNumber));
    return 1;
  }
  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
    appendByte(253);
    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
    return 3;
  }
  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
    appendByte(254);
    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
    return 5;
  }
  else {
    appendByte(255);
    uint64_t value = htobe64(varNumber);
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
    return 9;
  }
}
Esempio n. 18
0
/*
 * Encode data stream into TLV.
 */
uint8_t *
tlv_encode (const uint8_t type, const uint8_t *istream, uint16_t isize, size_t *osize)
{
    uint8_t *res;
    off_t n = 0;

    if (osize)
        *osize = 0;

    if (isize == 0xffff) /* RFU */
        return NULL;

    if ((res = malloc (1 + ((isize > 254) ? 3 : 1) + isize + 1))) {
        /* type + size + payload + terminator */
        res[n++] = type;

        if (isize > 254) {
            res[n++] = 0xff;
            uint16_t size_be = htobe16 (isize);
            memcpy (res + n, &size_be, sizeof (uint16_t));
            n += 2;
        } else {
            res[n++] = (uint8_t)isize;
        }

        memcpy (res + n, istream, isize);

        n += isize;
        res[n++] = TLV_TERMINATOR;

        if (osize)
            *osize = n;
    }
    return res;
}
Esempio n. 19
0
static int
utx_log_add(const struct futx *fu)
{
	struct iovec vec[2];
	int error, fd;
	uint16_t l;

	/*
	 * Append an entry to the log file.  We only need to append
	 * records to this file, so to conserve space, trim any trailing
	 * zero-bytes.  Prepend a length field, indicating the length of
	 * the record, excluding the length field itself.
	 */
	for (l = sizeof(*fu); l > 0 && ((const char *)fu)[l - 1] == '\0'; l--) ;
	vec[0].iov_base = &l;
	vec[0].iov_len = sizeof(l);
	vec[1].iov_base = __DECONST(void *, fu);
	vec[1].iov_len = l;
	l = htobe16(l);

	fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC, 0644);
	if (fd < 0)
		return (-1);
	if (_writev(fd, vec, 2) == -1)
		error = errno;
	else
		error = 0;
	_close(fd);
	if (error != 0)
		errno = error;
	return (error == 0 ? 0 : 1);
}
Esempio n. 20
0
	size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, std::shared_ptr<const I2NPMessage> msg)
	{
		size_t blockSize = 0;
		bool createNewTags = m_Owner && m_NumTags && ((int)m_SessionTags.size () <= m_NumTags*2/3);
		UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr;
		htobuf16 (buf, newTags ? htobe16 (newTags->numTags) : 0); // tag count
		blockSize += 2;
		if (newTags) // session tags recreated
		{
			for (int i = 0; i < newTags->numTags; i++)
			{
				memcpy (buf + blockSize, newTags->sessionTags[i], 32); // tags
				blockSize += 32;
			}
		}
		uint32_t * payloadSize = (uint32_t *)(buf + blockSize);
		blockSize += 4;
		uint8_t * payloadHash = buf + blockSize;
		blockSize += 32;
		buf[blockSize] = 0; // flag
		blockSize++;
		size_t len = CreateGarlicPayload (buf + blockSize, msg, newTags);
		htobe32buf (payloadSize, len);
		SHA256(buf + blockSize, len, payloadHash);
		blockSize += len;
		size_t rem = blockSize % 16;
		if (rem)
			blockSize += (16-rem); //padding
		m_Encryption.Encrypt(buf, blockSize, buf);
		return blockSize;
	}
Esempio n. 21
0
/**
 * send a generic command to the device
 */
static int
sickldmrs_send_command(struct sickldmrs_device *dev, 
    void *data, size_t len, int timeo)
{
	struct sickldmrs_header *h;
	struct sickldmrs_cmd *cmd;
	uint8_t *buf;
	size_t pktlen;
	int ret;

	pktlen = sizeof(*h) + len;
	buf = malloc(pktlen);
	if (buf == NULL) {
		warn("%s: malloc error", __func__);
		return -1;
	}
	/* initialize header */
	memset(buf, 0, sizeof(*h));
	h = (struct sickldmrs_header *)buf;
	h->magic_word = htobe32(SLDMRS_MAGIC_WORD);
	h->msg_size = htobe32(len);
	h->data_type = htobe16(SLDMRS_COMMAND);
	memcpy(buf + sizeof(*h), data, len);
	cmd = (struct sickldmrs_cmd *)(buf + sizeof(*h));
	ret =  send(dev->priv->fd, buf, pktlen, 0);
	free(buf);
	if (timeo == 0 || ret < 0)
		return ret;
	return sickldmrs_wait_reply(dev, cmd->command_id, timeo);
}
static void pppoe_tag_append(struct pppoe_hdr *packet, size_t packet_size, be16_t tag_type, const void *tag_data, uint16_t tag_len) {
        struct pppoe_tag *tag;

        assert(packet);
        assert(sizeof(struct pppoe_hdr) + PPPOE_PACKET_LENGTH(packet) + sizeof(struct pppoe_tag) + tag_len <= packet_size);
        assert(!(!tag_data ^ !tag_len));

        tag = PPPOE_PACKET_TAIL(packet);

        tag->tag_len = htobe16(tag_len);
        tag->tag_type = tag_type;
        if (tag_data)
                memcpy(tag->tag_data, tag_data, tag_len);

        packet->length = htobe16(PPPOE_PACKET_LENGTH(packet) + sizeof(struct pppoe_tag) + tag_len);
}
Esempio n. 23
0
static int dns_transaction_make_packet(DnsTransaction *t) {
        _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
        int r;

        assert(t);

        if (t->sent)
                return 0;

        r = dns_packet_new_query(&p, t->scope->protocol, 0);
        if (r < 0)
                return r;

        r = dns_scope_good_key(t->scope, t->key);
        if (r < 0)
                return r;
        if (r == 0)
                return -EDOM;

        r = dns_packet_append_key(p, t->key, NULL);
        if (r < 0)
                return r;

        DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
        DNS_PACKET_HEADER(p)->id = t->id;

        t->sent = p;
        p = NULL;

        return 0;
}
Esempio n. 24
0
static int
mpcsa_leds_pin_read(void *arg, int pin)
{
	struct mpcsa_leds_softc *sc = arg;
	pin %= MPCSA_LEDS_NPINS;
	return (sc->sc_pinstate & htobe16(1U << pin)) ? 1 : 0;
}
Esempio n. 25
0
int mlx5_alloc_srq_buf(struct ibv_context *context, struct mlx5_srq *srq)
{
	struct mlx5_wqe_srq_next_seg *next;
	int size;
	int buf_size;
	int i;
	struct mlx5_context	   *ctx;

	ctx = to_mctx(context);

	if (srq->max_gs < 0) {
		errno = EINVAL;
		return -1;
	}

	srq->wrid = malloc(srq->max * sizeof *srq->wrid);
	if (!srq->wrid)
		return -1;

	size = sizeof(struct mlx5_wqe_srq_next_seg) +
		srq->max_gs * sizeof(struct mlx5_wqe_data_seg);
	size = max(32, size);

	size = mlx5_round_up_power_of_two(size);

	if (size > ctx->max_recv_wr) {
		errno = EINVAL;
		return -1;
	}
	srq->max_gs = (size - sizeof(struct mlx5_wqe_srq_next_seg)) /
		sizeof(struct mlx5_wqe_data_seg);

	srq->wqe_shift = mlx5_ilog2(size);

	buf_size = srq->max * size;

	if (mlx5_alloc_buf(&srq->buf, buf_size,
			   to_mdev(context->device)->page_size)) {
		free(srq->wrid);
		return -1;
	}

	memset(srq->buf.buf, 0, buf_size);

	/*
	 * Now initialize the SRQ buffer so that all of the WQEs are
	 * linked into the list of free WQEs.
	 */

	for (i = 0; i < srq->max; ++i) {
		next = get_wqe(srq, i);
		next->next_wqe_index = htobe16((i + 1) & (srq->max - 1));
	}

	srq->head = 0;
	srq->tail = srq->max - 1;

	return 0;
}
int dhcp_network_bind_udp_socket(be32_t address, uint16_t port) {
        union sockaddr_union src = {
                .in.sin_family = AF_INET,
                .in.sin_port = htobe16(port),
                .in.sin_addr.s_addr = address,
        };
        _cleanup_close_ int s = -1;
        int r, on = 1, tos = IPTOS_CLASS_CS6;

        s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
        if (s < 0)
                return -errno;

        r = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
        if (r < 0)
                return -errno;

        r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        if (r < 0)
                return -errno;

        if (address == INADDR_ANY) {
                r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on));
                if (r < 0)
                        return -errno;

                r = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
                if (r < 0)
                        return -errno;
        } else {
                r = setsockopt(s, IPPROTO_IP, IP_FREEBIND, &on, sizeof(on));
                if (r < 0)
                        return -errno;
        }

        r = bind(s, &src.sa, sizeof(src.in));
        if (r < 0)
                return -errno;

        r = s;
        s = -1;

        return r;
}

int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
                                 const void *packet, size_t len) {
        int r;

        assert(link);
        assert(packet);
        assert(len);

        r = sendto(s, packet, len, 0, &link->sa, sizeof(link->ll));
        if (r < 0)
                return -errno;

        return 0;
}
Esempio n. 27
0
int
safte_bio_blink(struct safte_softc *sc, struct bioc_blink *blink)
{
	struct safte_writebuf_cmd	cmd;
	struct safte_slotop		*op;
	int				slot;
	int				flags;
	int				wantblink;

	switch (blink->bb_status) {
	case BIOC_SBBLINK:
		wantblink = 1;
		break;
	case BIOC_SBUNBLINK:
		wantblink = 0;
		break;
	default:
		return (EINVAL);
	}

	rw_enter_read(&sc->sc_lock);
	for (slot = 0; slot < sc->sc_nslots; slot++) {
		if (sc->sc_slots[slot] == blink->bb_target)
			break;
	}
	rw_exit_read(&sc->sc_lock);

	if (slot >= sc->sc_nslots)
		return (ENODEV);

	op = malloc(sizeof(struct safte_slotop), M_TEMP, 0);

	memset(op, 0, sizeof(struct safte_slotop));
	op->opcode = SAFTE_WRITE_SLOTOP;
	op->slot = slot;
	op->flags |= wantblink ? SAFTE_SLOTOP_IDENTIFY : 0;

	memset(&cmd, 0, sizeof(cmd));
	cmd.opcode = WRITE_BUFFER;
	cmd.flags |= SAFTE_WR_MODE;
	cmd.length = htobe16(sizeof(struct safte_slotop));
	flags = SCSI_DATA_OUT;
#ifndef SCSIDEBUG
	flags |= SCSI_SILENT;
#endif
	if (cold)
		flags |= SCSI_AUTOCONF;

	if (scsi_scsi_cmd(sc->sc_link, (struct scsi_generic *)&cmd,
	    sizeof(cmd), (u_char *)op, sizeof(struct safte_slotop),
	    2, 30000, NULL, flags) != 0) {
		free(op, M_TEMP);
		return (EIO);
	}

	free(op, M_TEMP);

	return (0);
}
Esempio n. 28
0
static inline u_int16_t
qlw_swap16(struct qlw_softc *sc, u_int16_t value)
{
	if (sc->sc_isp_gen == QLW_GEN_ISP1000)
		return htobe16(value);
	else
		return htole16(value);
}
Esempio n. 29
0
static uint16_t tlv_hton16_ptr(uint8_t * in, uint8_t * out)
{
    uint16_t temp;
    temp = WICED_READ_16(in);
    temp = htobe16(temp);
    WICED_WRITE_16(out, temp);
    return temp;
}
Esempio n. 30
0
static int
send_structured_reply_read (struct connection *conn,
                            uint64_t handle, uint16_t cmd,
                            const char *buf, uint32_t count, uint64_t offset)
{
  /* Once we are really using structured replies and sending data back
   * in chunks, we'll be able to grab the write lock for each chunk,
   * allowing other threads to interleave replies.  As we're not doing
   * that yet we acquire the lock for the whole function.
   */
  ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&conn->write_lock);
  struct structured_reply reply;
  struct structured_reply_offset_data offset_data;
  int r;

  assert (cmd == NBD_CMD_READ);

  reply.magic = htobe32 (NBD_STRUCTURED_REPLY_MAGIC);
  reply.handle = handle;
  reply.flags = htobe16 (NBD_REPLY_FLAG_DONE);
  reply.type = htobe16 (NBD_REPLY_TYPE_OFFSET_DATA);
  reply.length = htobe32 (count + sizeof offset_data);

  r = conn->send (conn, &reply, sizeof reply);
  if (r == -1) {
    nbdkit_error ("write reply: %s: %m", name_of_nbd_cmd (cmd));
    return connection_set_status (conn, -1);
  }

  /* Send the offset + read data buffer. */
  offset_data.offset = htobe64 (offset);
  r = conn->send (conn, &offset_data, sizeof offset_data);
  if (r == -1) {
    nbdkit_error ("write data: %s: %m", name_of_nbd_cmd (cmd));
    return connection_set_status (conn, -1);
  }

  r = conn->send (conn, buf, count);
  if (r == -1) {
    nbdkit_error ("write data: %s: %m", name_of_nbd_cmd (cmd));
    return connection_set_status (conn, -1);
  }

  return 1;                     /* command processed ok */
}