summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_nackinfo.c
blob: fb7b58c6e94022bdfe9bc2770f0c77303adad139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tcomp_nackinfo.c
 * @brief  RFC 4077 - A Negative Acknowledgement Mechanism for Signaling Compression (NACK).
 */
#include "tcomp_nackinfo.h"
#include "tcomp_message.h"
#include "tcomp_nack_codes.h"

#include "tsk_sha1.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

/* "OPCODE of failed instruction" is a one-byte field that includes
the opcode to which the PC was pointing when the failure occurred.
If failure occurred before the UDVM began executing any code, this
field is set to 0.
*/
#define OPCODE_UNKNOWN	0
/* "PC of failed instruction" is a two-byte field containing the
      value of the program counter when failure occurred (i.e., the
      memory address of the failed UDVM instruction).  The field is
      encoded with the most significant byte of the PC first (i.e., in
      network or big endian order).  If failure occurred before the UDVM
      began executing any code, this field is set to 0.
*/
#define PC_UNKNOWN		0

/** Creates new NACK object */
tcomp_nackinfo_t* tcomp_nackinfo_create()
{
	tcomp_nackinfo_t *nackinfo;
	if((nackinfo = tsk_object_new(tcomp_nackinfo_def_t))){
		nackinfo->version = NACK_VERSION;
		nackinfo->details = tcomp_buffer_create_null();
	}
	return nackinfo;
}

int tcomp_nackinfo_write(tcomp_buffer_handle_t* buffer, 
						 uint8_t reasonCode, 
						 uint8_t opCode, 
						 int16_t memory_address_of_instruction, 
						 const void* sigCompMessagePtr, tsk_size_t sigCompMessageSize, 
						 tcomp_buffer_handle_t* lpDetails,
						 uint16_t udvm_size,
						 uint8_t cpbValue)
{
	tsk_sha1context_t sha;
	uint8_t *nackbuffer_ptr;

	if(!buffer || !sigCompMessagePtr || !sigCompMessageSize){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	tcomp_buffer_allocBuff(buffer, INDEX_NACK_SHA1 + TSK_SHA1_DIGEST_SIZE);
	if(!(nackbuffer_ptr = tcomp_buffer_getBuffer(buffer))){
		TSK_DEBUG_ERROR("NACK buffer is null");
		return -2;
	}
	
	nackbuffer_ptr[INDEX_NACK_HEADER] = 0xf8;
	nackbuffer_ptr[INDEX_NACK_VERSION] = NACK_VERSION;
	nackbuffer_ptr[INDEX_NACK_REASON_CODE] = reasonCode;
	nackbuffer_ptr[INDEX_NACK_OPCODE] = opCode;
	nackbuffer_ptr[INDEX_NACK_PC] = (memory_address_of_instruction >> 8);
	nackbuffer_ptr[INDEX_NACK_PC + 1] = (memory_address_of_instruction & 0x00ff);
	
	// SHA-1(message) computation
	tsk_sha1reset(&sha);
	tsk_sha1input(&sha, sigCompMessagePtr, sigCompMessageSize);
	tsk_sha1result(&sha, &nackbuffer_ptr[INDEX_NACK_SHA1]);
	
#if 0
	{
		int i;
		TSK_DEBUG_INFO("Create NACK with id:");
		for(i = 0; i < TSK_SHA1_DIGEST_SIZE; ++i){
			printf("%x ", nackbuffer_ptr[INDEX_NACK_SHA1 + i]);
		}
		printf("\n");
	}
#endif

	// Details
	if(lpDetails && tcomp_buffer_getSize(lpDetails)){
		tcomp_buffer_appendBuff(buffer, tcomp_buffer_getBuffer(lpDetails), tcomp_buffer_getSize(lpDetails));
	}
	else if(reasonCode == NACK_BYTECODES_TOO_LARGE){
		tcomp_buffer_appendBuff(buffer, &udvm_size, 2);
	}
	else if(reasonCode == NACK_CYCLES_EXHAUSTED){
		tcomp_buffer_appendBuff(buffer, &cpbValue, 1);
	}

	return 0;

}

int tcomp_nackinfo_write_2(tcomp_buffer_handle_t* buffer, 
						 uint8_t reasonCode, 
						 uint8_t opCode, 
						 int16_t memory_address_of_instruction, 
						 const tcomp_message_t* sigCompMessage, 
						 tcomp_buffer_handle_t* lpDetails,
						 uint16_t udvm_size,
						 uint8_t cpbValue)
{
	return tcomp_nackinfo_write(buffer, 
						 reasonCode, 
						 opCode, 
						 memory_address_of_instruction, 
						 sigCompMessage->startPtr, sigCompMessage->totalSize, 
						 lpDetails,
						 udvm_size,
						 cpbValue);
}

int tcomp_nackinfo_write_3(tcomp_buffer_handle_t* buffer, 
						 uint8_t reasonCode, 
						 const void* sigCompMessagePtr, tsk_size_t sigCompMessageSize)
{
	return tcomp_nackinfo_write(buffer,
								reasonCode,
								OPCODE_UNKNOWN,
								PC_UNKNOWN,
								sigCompMessagePtr, sigCompMessageSize,
								tsk_null,
								0,
								0);
}

int tcomp_nackinfo_write_4(tcomp_buffer_handle_t* buffer, 
						 uint8_t reasonCode, 
						 const tcomp_message_t* sigCompMessage)
{
	return tcomp_nackinfo_write_2(buffer,
								reasonCode,
								OPCODE_UNKNOWN,
								PC_UNKNOWN,
								sigCompMessage,
								tsk_null,
								0,
								0);
}

const char* tcomp_nackinfo_get_description(const tcomp_buffer_handle_t* buffer)
{
	uint8_t reasonCode;
	if(!buffer){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
	if(tcomp_buffer_getSize(buffer) < 3){
		TSK_DEBUG_ERROR("Too short");
		return tsk_null;
	}
	reasonCode = *((const uint8_t*)tcomp_buffer_getBufferAtPos(buffer, 3));
	if(reasonCode >= (sizeof(TCOMP_NACK_DESCRIPTIONS)/sizeof(TCOMP_NACK_DESCRIPTIONS[0]))){
		TSK_DEBUG_ERROR("%d not valid as reasonCode", (int32_t)reasonCode);
		return tsk_null;
	}
	return TCOMP_NACK_DESCRIPTIONS[reasonCode].desc;
}

//========================================================
//	NackInfo object definition
//

/*
* Creates a nack info message. You MUST use @ref tcomp_nackinfo_destroy to free the nackinfo.
* @retval The NACK info message.
* @sa @ref tcomp_nackinfo_destroy.
*/
static tsk_object_t* tcomp_nackinfo_ctor(tsk_object_t *self, va_list* app)
{
	tcomp_nackinfo_t *nackinfo = self;
	if(nackinfo){
	}
	return self;
}

/*
* Destroy a nackinfo message previously created using @ref tcomp_nackinfo_create.
* @param nackinfo The NACK info message to free.
* @sa @ref tcomp_nackinfo_create.
*/
static tsk_object_t* tcomp_nackinfo_dtor(tsk_object_t* self)
{
	tcomp_nackinfo_t *nackinfo = self;
	if(nackinfo){
		TSK_OBJECT_SAFE_FREE(nackinfo->details);
	}
	return self;
}


static const tsk_object_def_t tcomp_nackinfo_def_s = 
{
	sizeof(tcomp_nackinfo_t),
	tcomp_nackinfo_ctor, 
	tcomp_nackinfo_dtor,
	tsk_null
};
const tsk_object_def_t* tcomp_nackinfo_def_t = &tcomp_nackinfo_def_s;

OpenPOWER on IntegriCloud