forked from cultcode/msgmgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitNodeStatus.c
152 lines (114 loc) · 3.77 KB
/
InitNodeStatus.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <curl/curl.h>
#include <assert.h>
#include "InitNodeStatus.h"
#include "cJSON.h"
#include "Security.h"
int svrversion=DEFAULT_SVRVERSION;
int svrtype=DEFAULT_SVRTYPE;
static int transfered;
__attribute__((weak)) int servertimezone=DEFAULT_SERVERTIMEZONE;
size_t readfunction( void *ptr, size_t size, size_t nmemb, void *userdata)
{
int length = -1;
char * cipher=NULL, content[CONTENT_LEN]={0};
char *out=NULL;
char EpochTime[16]={0};
cJSON *root=NULL;
struct NodeStatus * ns = (struct NodeStatus *)userdata;
if(transfered) return 0;
root=cJSON_CreateObject();
sprintf(EpochTime,"%lx",ns->EpochTime);
cJSON_AddStringToObject(root,"EpochTime",EpochTime);
cJSON_AddStringToObject(root,"NodeName",ns->NodeName);
if(svrversion) {
cJSON_AddStringToObject(root,"Version",ns->Version);
}
if(svrtype) {
cJSON_AddNumberToObject(root,"SvrType",ns->SvrType);
}
strcpy(content, out=cJSON_PrintUnformatted(root));
cJSON_Delete(root);
free(out);
log4c_cdn(mycat, info, "POST", "%s",content);
length = ContentEncode(NODE_3DES_KEY, NODE_3DES_IV, content, &cipher, strlen(content));
if(length<0){
log4c_cdn(mycat, error, "ENDEC", "ContentEncode() failed");
}
if(length<0) exit(1);
memcpy(ptr,cipher,length);
free(cipher);
transfered=1;
return length;
}
size_t writefunction( void *ptr, size_t size, size_t nmemb, void *userdata)
{
int length = -1;
char * plain = NULL;
cJSON *root=NULL, *item=NULL;
struct NodeStatus * ns = (struct NodeStatus *)userdata;
if(size*nmemb == 0) return 0;
length = ContentDecode(NODE_3DES_KEY, NODE_3DES_IV, ptr, &plain, StripNewLine(ptr, size*nmemb));
log4c_cdn(mycat, info, "POST", "%s",plain);
if(length<0) {
log4c_cdn(mycat, error, "ENDEC", "ContentDecode() failed");
exit(1);
}
if((root = cJSON_Parse(plain)) == NULL) {
log4c_cdn(mycat, error, "JSON", "cJSON_Parse() failed: %s",cJSON_GetErrorPtr());
exit(1);
}
item = cJSON_GetObjectItem(root,"Status");
ns->Status = item->valueint;
item = cJSON_GetObjectItem(root,"StatusDesc");
strcpy(ns->StatusDesc, item->valuestring);
if(ns->Status == SUCCESS) {
item = cJSON_GetObjectItem(root,"NodeId");
ns->NodeId = item->valueint;
}
cJSON_Delete(root);
if(ns->Status == FAIL) {
log4c_cdn(mycat, error, "POST", "FAIL received");
exit(1);
}
free(plain);
return size*nmemb;
}
void InitNodeStatus()
{
CURL *curl=NULL;
CURLcode res;
struct NodeStatus ns={0};
memset(&ns,0,sizeof(struct NodeStatus));
/*structure http request
{"EpochTime":" 97d76a","NodeName":"Imgo-1","Version":"3.0"}
*/
ns.EpochTime = GetLocaltimeSeconds(servertimezone);
gethostname(ns.NodeName,NODENAME_LEN);
sprintf(ns.Version,"%s",VERSION);
ns.SvrType = svrtype;
char posturl[URL_LEN]={0};
sprintf(posturl,"http://%s:%hd%s",server,port[REMOTE][TCP],url[0]);
struct curl_slist *header = NULL;
header = curl_slist_append(header, "Transfer-Encoding: chunked");
header = curl_slist_append(header, "Connection: Close");
transfered=0;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, posturl);
curl_easy_setopt(curl, CURLOPT_READDATA, &ns);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readfunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ns);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(header);
assert(res == CURLE_OK);
}