summaryrefslogtreecommitdiff
path: root/decoders/stratum/stratum_decoder.cpp
blob: 3267817f3b881490302068f9f8d7999afb37e93c (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "cJSON.h"
#include "stellar/stellar.h"
#include "stellar/session.h"
#include "stellar/stellar_mq.h"
#include "stellar/stellar_exdata.h"
#include "stellar/stratum_decoder.h"
#include "stellar/log.h"

#define unused(x) ((void)(x))

#define STRATUM_LOG_MOUDLE            "STRATUM_DECODER"

//字段内容宏
#define METHOD_STR				"method"
#define WORKER_STR				"worker"
#define PARAMS_STR				"params"
#define AGENT_STR				"agent"

enum stratum_field_type
{
	CRYPTOCURRENCY_FLAG = 1,
	MINING_POOL_FLAG,
	MINING_PROGRAM_FLAG,
	MINING_SUBSCRIBE_FLAG,
};

struct stratum_decoder_info
{
	int plugin_id;
	struct stellar *st;
	struct logger *log_handle;
	int tcp_topic_id;
    int stratum_decoder_topic_id;
};

void free_stratum_filed(struct stratum_field *stratum_field)
{
	if(stratum_field!=NULL){
		if(stratum_field->mining_program.iov_base != NULL){
			free(stratum_field->mining_program.iov_base);
			stratum_field->mining_program.iov_len = 0;
		}
		if(stratum_field->mining_pools.iov_base != NULL){
			free(stratum_field->mining_pools.iov_base);
			stratum_field->mining_pools.iov_len = 0;
		}
		if(stratum_field->mining_subscribe.iov_base != NULL){
			free(stratum_field->mining_subscribe.iov_base);
			stratum_field->mining_subscribe.iov_len = 0;
		}
		free(stratum_field);
	}
	return;
}

void stratum_decoder_session_msg_free_cb(void *msg, void *msg_free_arg)
{
	unused(msg_free_arg);

    struct stratum_field *stratum_field = (struct stratum_field *)msg;

    free_stratum_filed(stratum_field);

    return;
}

int stratum_field_assign(char *content, struct stratum_field* stratum_field,int field_range)
{
	switch (field_range)
	{
		case MINING_POOL_FLAG:
			stratum_field->mining_pools.iov_len=strlen(content);
			stratum_field->mining_pools.iov_base=(char*)malloc(stratum_field->mining_pools.iov_len+1);
			memcpy(stratum_field->mining_pools.iov_base, content, stratum_field->mining_pools.iov_len);
			((char*)stratum_field->mining_pools.iov_base)[stratum_field->mining_pools.iov_len]='\0';
			break;
		case MINING_PROGRAM_FLAG:
			stratum_field->mining_program.iov_len=strlen(content);
			stratum_field->mining_program.iov_base=(char*)malloc(stratum_field->mining_program.iov_len+1);
			memcpy(stratum_field->mining_program.iov_base, content, stratum_field->mining_program.iov_len);
			((char*)stratum_field->mining_program.iov_base)[stratum_field->mining_program.iov_len]='\0';
			break;
		case MINING_SUBSCRIBE_FLAG:
			stratum_field->mining_subscribe.iov_len=strlen(content);
			stratum_field->mining_subscribe.iov_base=(char*)malloc(stratum_field->mining_subscribe.iov_len+1);
			memcpy(stratum_field->mining_subscribe.iov_base, content,stratum_field->mining_subscribe.iov_len);
			((char*)stratum_field->mining_subscribe.iov_base)[stratum_field->mining_subscribe.iov_len]='\0';
			break;
		default:
			return -1;
	}
	
	return 0;
}

struct stratum_field *stratum_json_decode(struct stratum_decoder_info *stratum_decoder_info, char *raw_json_str)
{
	
    if(raw_json_str==NULL){
		STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode error!:raw_json_str is null! ");
		return NULL;
    }
    
    cJSON *root = cJSON_Parse(raw_json_str);
    if (root == NULL){
		STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode error!:root_json is null,error: %s ",cJSON_GetErrorPtr());
        return NULL;
    }

   cJSON *method_item = cJSON_GetObjectItem(root, METHOD_STR);
    if(method_item==NULL){
		STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode error!:not_found '%s' in raw_json_str ", METHOD_STR);
		cJSON_Delete(root);
    	return NULL;
    }
	
    struct stratum_field *stratum_field = (struct stratum_field *)calloc(1, sizeof(struct stratum_field));
	int field_range = 0;
	if(cJSON_IsString(method_item)){
		int assign_ret = 0;
		STELLAR_LOG_DEBUG(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode,find '%s':%s\n", METHOD_STR, method_item->valuestring);

		if((strncasecmp("mining.subscribe", method_item->valuestring,strlen("mining.subscribe")) == 0)
			||(strncasecmp("login", method_item->valuestring,strlen("login")) == 0)){
			field_range = MINING_SUBSCRIBE_FLAG;
			assign_ret = stratum_field_assign(raw_json_str, stratum_field, field_range);
			
			cJSON *paras_item = cJSON_GetObjectItem(root, PARAMS_STR);
			if(paras_item == NULL){
				STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!not_found '%s' in raw_json_str ", PARAMS_STR);
				goto err_ret;
			}
			
			if(cJSON_IsArray(paras_item)){
				int asize = cJSON_GetArraySize(paras_item);
				stratum_field->type = OTHER;
				cJSON *array_item = cJSON_GetArrayItem(paras_item, 0);
				if(cJSON_IsString(array_item)){
					field_range = MINING_PROGRAM_FLAG;
					assign_ret = stratum_field_assign(array_item->valuestring, stratum_field, field_range);
				}
				if(asize>2){
					array_item = cJSON_GetArrayItem(paras_item, 2);
					if(cJSON_IsString(array_item)){
						field_range = MINING_POOL_FLAG;
						assign_ret = stratum_field_assign(array_item->valuestring, stratum_field, field_range);
					}
				}
			}else if(cJSON_IsObject(paras_item) || cJSON_IsRaw(paras_item)){
				cJSON *agent_item = cJSON_GetObjectItem(paras_item, AGENT_STR);
				if(agent_item == NULL){
					STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!not_found '%s' in raw_json_str ", AGENT_STR);
					goto err_ret;
				}
				if(cJSON_IsString(agent_item)){
					field_range = MINING_PROGRAM_FLAG;
					stratum_field->type = OTHER;
					assign_ret = stratum_field_assign(agent_item->valuestring, stratum_field, field_range);
				}
			}else{
				STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!found '%s',json_value_type:%d\n ", PARAMS_STR, paras_item->type);
				goto err_ret;
			}
		}else if(strncasecmp("eth_submitLogin", method_item->valuestring, strlen("eth_submitLogin")) == 0){
			field_range = MINING_SUBSCRIBE_FLAG;
			assign_ret = stratum_field_assign(raw_json_str, stratum_field, field_range);
			
			cJSON *worker_item = cJSON_GetObjectItem(root, WORKER_STR);
			if(worker_item == NULL){
				STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!not_found '%s' in raw_json_str ", WORKER_STR);
				goto err_ret;
			}
			if(cJSON_IsString(worker_item)){
				field_range = MINING_PROGRAM_FLAG;
				stratum_field->type = ETH;
				assign_ret = stratum_field_assign(worker_item->valuestring, stratum_field, field_range);
			}else{
				STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!found '%s',json_value_type:%d\n ", WORKER_STR, worker_item->type);
				goto err_ret;
			}
		}else{
			STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!found '%s',json_value_type:%d\n ", METHOD_STR, method_item->type);
			goto err_ret;
		}
		
		if(assign_ret<0){
			STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "field_assign_error:raw_json_str:%s\n ", raw_json_str);
			goto err_ret;
		}
		cJSON_Delete(root);	
    	return stratum_field;
	}else{
		STELLAR_LOG_INFO(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "json_decode:error!found '%s',json_value_type:%d\n ", METHOD_STR, method_item->type);
		goto err_ret;
	}
	
err_ret:
    cJSON_Delete(root);
	free_stratum_filed(stratum_field);
    return NULL;
}

struct stratum_field *stratum_data_process(struct stratum_decoder_info *stratum_decoder_info, const char *tcpdata, size_t datalen)
{
	if(tcpdata == NULL || datalen == 0)
	{
		return NULL;
	}
	
	char *tcp_json=(char*)malloc(datalen+1);
	memcpy(tcp_json, tcpdata, datalen);
	tcp_json[datalen]='\0';

	struct stratum_field *stratum_field=stratum_json_decode(stratum_decoder_info, tcp_json);
	if(stratum_field==NULL)
	{
		STELLAR_LOG_DEBUG(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "payload_decode failed!stratum_field is null");
	}

	free(tcp_json);
	tcp_json=NULL;
	
	return stratum_field;
	
}

void stratum_decoder_tcp_on_msg_cb(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
{
	unused(topic_id);
	unused(per_session_ctx);
	unused(msg);

    struct stratum_decoder_info *stratum_decoder_info = (struct stratum_decoder_info *)plugin_env;

	const struct packet *pkt = session_get0_current_packet(sess);
	const char *payload = packet_get_payload(pkt);
	size_t payload_len = packet_get_payload_len(pkt);
    
	struct stratum_field *stratum_field = stratum_data_process(stratum_decoder_info, payload, payload_len);
	if (stratum_field != NULL)
	{
		if (session_mq_publish_message(sess, stratum_decoder_info->stratum_decoder_topic_id, stratum_field) < 0)
		{
			STELLAR_LOG_FATAL(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "stratum_decoder_tcp_on_msg_cb:session_mq_publish_message failed");
			free_stratum_filed(stratum_field);
		}
		stellar_session_plugin_dettach_current_session(sess);
	}
	
	return;
}

extern "C" void *stratum_decoder_init(struct stellar *st)
{
    struct stratum_decoder_info *stratum_decoder_info = (struct stratum_decoder_info *)malloc(sizeof(struct stratum_decoder_info));

    stratum_decoder_info->st = st;
	stratum_decoder_info->log_handle = stellar_get_logger(st);

    stratum_decoder_info->plugin_id = stellar_session_plugin_register(st, NULL, NULL, stratum_decoder_info);
    if (stratum_decoder_info->plugin_id < 0)
    {
		STELLAR_LOG_FATAL(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "stellar_session_plugin_register failed");
        goto ERROR;
    }

    stratum_decoder_info->stratum_decoder_topic_id = stellar_mq_create_topic(st, STRATUM_MESSAGE_TOPIC, stratum_decoder_session_msg_free_cb, NULL);
    if (stratum_decoder_info->stratum_decoder_topic_id < 0)
    {
		STELLAR_LOG_FATAL(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "stellar_session_mq_create_topic failed");
        goto ERROR;
    }

    stratum_decoder_info->tcp_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP_STREAM);
    if (stratum_decoder_info->tcp_topic_id < 0)
    {
		STELLAR_LOG_FATAL(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "stellar_session_mq_get_topic_id failed");
        goto ERROR;
    }

    if (stellar_session_mq_subscribe(st, stratum_decoder_info->tcp_topic_id, stratum_decoder_tcp_on_msg_cb, stratum_decoder_info->plugin_id) < 0)
    {
		STELLAR_LOG_FATAL(stratum_decoder_info->log_handle, STRATUM_LOG_MOUDLE, "stellar_session_mq_subscribe failed");
        goto ERROR;
    }

    return stratum_decoder_info;

ERROR:
    if (stratum_decoder_info)
    {
        free(stratum_decoder_info);
    }
    return NULL;
}

extern "C" void stratum_decoder_exit(void *plugin_env)
{
    struct stratum_decoder_info *stratum_decoder_info = (struct stratum_decoder_info *)plugin_env;

    if (stratum_decoder_info != NULL)
    {
        free(stratum_decoder_info);
    }

    return;
}