Example #1
0
File: packet.c Project: estock/spot
int packet_write (SESSION * session, unsigned char cmd,
		  unsigned char *payload, unsigned short len)
{
	unsigned char nonce[4];
	unsigned char *buf, *ptr;
	PHEADER *h;
	int ret;

	*(unsigned int *) nonce = htonl (session->key_send_IV);
	shn_nonce (&session->shn_send, nonce, 4);

	buf = (unsigned char *) malloc (3 + len + 4);

	h = (PHEADER *) buf;
	h->cmd = cmd;
	h->len = htons (len);

	ptr = buf + 3;
	if (payload != NULL)
	{
 		memcpy (ptr, payload, len);
	}
	
#ifdef DEBUG_PACKETS
	DSFYDEBUG
		("packet_write(): Sending packet with command 0x%02x, length %d\n",
		 h->cmd, ntohs (h->len));
	logdata ("send-hdr", session->key_send_IV, (unsigned char *) h, 3);
	if (payload != NULL) logdata ("send-payload", session->key_send_IV, payload, len);
#endif

	shn_encrypt (&session->shn_send, buf, 3 + len);
	ptr += len;

	shn_finish (&session->shn_send, ptr, 4);

	ret = block_write (session->ap_sock, buf, 3 + len + 4);

	free(buf);

	session->key_send_IV++;


	if(ret != 3 + len + 4) {
#ifdef DEBUG_PACKETS
		DSFYDEBUG ("packet_write(): only wrote %d of %d bytes\n", ret,
			   3 + len + 4);
#endif
		return -1;
	}

	return 0;
}
Example #2
0
int packet_write (sp_session * session, unsigned char cmd,
		  unsigned char *payload, unsigned short len)
{
	unsigned char nonce[4];
	unsigned char *buf, *ptr;
	PHEADER *h;
	int ret;

	*(unsigned int *) nonce = htonl (session->key_send_IV);
	shn_nonce (&session->shn_send, nonce, 4);

	buf = (unsigned char *) malloc (3 + len + 4);

	h = (PHEADER *) buf;
	h->cmd = cmd;
	h->len = htons (len);

	ptr = buf + 3;
	if (payload != NULL)
	{
 		memcpy (ptr, payload, len);
	}
	
	DSFYDEBUG("Sending packet with command 0x%02x, length %d, IV=%d\n",
		 h->cmd, ntohs (h->len), session->key_send_IV);

	shn_encrypt (&session->shn_send, buf, 3 + len);
	ptr += len;

	shn_finish (&session->shn_send, ptr, 4);

	ret = block_write (session->sock, buf, 3 + len + 4);

	free(buf);

	session->key_send_IV++;


	if(ret != 3 + len + 4) {
		DSFYDEBUG ("block_write() only wrote %d of %d bytes\n", ret,
			   3 + len + 4);
		return -1;
	}

	return 0;
}