Guest User

Untitled

a guest
Oct 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. /*
  2. i get it from internet
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <curl/curl.h>
  7. #include <time.h>
  8. #include <libxml/parser.h>
  9. #include <libxml/xmlmemory.h>
  10. #include <libxml/xpath.h>
  11. #include <libxml/xpathInternals.h>
  12. static void set_prop(xmlXPathContextPtr context, const xmlChar *xpath, const xmlChar *name, const xmlChar *value){
  13. xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context);
  14. if (result) {
  15. xmlSetProp(result->nodesetval->nodeTab[0], (const xmlChar *) name, (const xmlChar *) value);
  16. xmlXPathFreeObject (result);
  17. }
  18. }
  19. static void set_value(xmlXPathContextPtr context, const xmlChar *xpath, const xmlChar *value){
  20. xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context);
  21. if ( result ) {
  22. xmlNodeSetPtr nodeset = result->nodesetval;
  23. xmlNodeSetContent(nodeset->nodeTab[0], value);
  24. xmlXPathFreeObject(result);
  25. }
  26. }
  27. static void get_request(
  28. char *buffer,
  29. int *len,
  30. const char *orderno,
  31. const char *username,
  32. const char *password,
  33. const char *imsi,
  34. const char *action,
  35. const char *serivcename) {
  36. xmlChar *buff;
  37. xmlDocPtr doc;
  38. if ( strcmp(action, "A") == 0 ) {
  39. doc = xmlParseFile("mobb_activate.xml");
  40. }
  41. else if ( strcmp(action, "D") == 0 ) {
  42. doc = xmlParseFile("mobb_cancel.xml");
  43. }
  44. else {
  45. doc = xmlParseFile("mobb_status.xml");
  46. }
  47. if ( doc == NULL ) {
  48. printf("xmlParseFile failed/n");
  49. return;
  50. }
  51. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  52. if ( context == NULL ) {
  53. printf("xmlXPathNewContext failed/n");
  54. return;
  55. }
  56. time_t now;
  57. struct tm ts;
  58. char timestamp[80];
  59. time(&now);
  60. ts = *localtime(&now);
  61. strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S+08:00", &ts);
  62. set_prop(context, (const xmlChar *)"//ProvisioningRequest", (const xmlChar *) "TransactionId", (const xmlChar *) orderno);
  63. set_value(context, (const xmlChar *)"//Login", (const xmlChar *) username);
  64. set_value(context, (const xmlChar *)"//Password", (const xmlChar *) password);
  65. set_value(context, (const xmlChar *)"//TimeStamp", (const xmlChar *) timestamp);
  66. set_value(context, (const xmlChar *)"//ProvisioningDataItem[@name='IMSI']", (const xmlChar *) imsi);
  67. if ( strcmp(action, "A") == 0 ) {
  68. set_value(context, (const xmlChar *)"//ProvisioningDataItem[@name='ServiceName']", (const xmlChar *) serivcename);
  69. }
  70. xmlDocDumpMemory(doc, &buff, len);
  71. strcpy(buffer, (char *) buff);
  72. printf("%s/n", buffer);
  73. xmlFree(buff);
  74. xmlXPathFreeContext(context);
  75. xmlFreeDoc(doc);
  76. xmlCleanupParser();
  77. }
  78. static size_t get_response(void *ptr, size_t size, size_t nmemb, void *stream) {
  79. char resp[2048];
  80. strcpy(resp, (char *) ptr);
  81. if ( strncmp(resp, "<?xml", 5) == 0 ) {
  82. printf("%s/n", resp);
  83. xmlDocPtr doc = xmlParseMemory(resp, strlen(resp));
  84. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  85. xmlXPathObjectPtr result = xmlXPathEvalExpression((const xmlChar *) "//ErrorCode", context);
  86. if ( result ) {
  87. xmlNodeSetPtr nodeset = result->nodesetval;
  88. printf("MD_RT_CODE: %s/n", xmlNodeGetContent(nodeset->nodeTab[0]));
  89. xmlXPathFreeObject(result);
  90. }
  91. result = xmlXPathEvalExpression((const xmlChar *) "//ErrorDescription", context);
  92. if ( result ) {
  93. xmlNodeSetPtr nodeset = result->nodesetval;
  94. printf("MD_RT_MESSAGE: %s/n", xmlNodeGetContent(nodeset->nodeTab[0]));
  95. xmlXPathFreeObject(result);
  96. }
  97. xmlXPathFreeContext(context);
  98. xmlFreeDoc(doc);
  99. }
  100. return strlen(resp);
  101. }
  102. int main(int argc, char **argv) {
  103. if ( argc < 7 || argc > 8 ) {
  104. printf("Usage: BlackBerryProv endpoint username password orderno imsi action [servicename]/n");
  105. exit(-1);
  106. }
  107. const char *action = argv[6];
  108. if ( strcmp(action, "A") != 0 && strcmp(action, "D") != 0 && strcmp(action, "S") != 0 ) {
  109. printf("action must be A (Activation), D (De-activation) or S (Status)/n");
  110. exit(-1);
  111. }
  112. if ( strcmp(action, "A") == 0 && argc == 7 ) {
  113. printf("service name is needed in activation/n");
  114. exit(-1);
  115. }
  116. const char *endpoint = argv[1];
  117. const char *username = argv[2];
  118. const char *password = argv[3];
  119. const char *orderno = argv[4];
  120. const char *imsi = argv[5];
  121. const char *servicename = argv[7];
  122. char buffer[2048];
  123. int len = 0;
  124. get_request(buffer, &len, orderno, username, password, imsi, action, servicename);
  125. struct curl_slist *headers = NULL;
  126. CURL *curl = curl_easy_init();
  127. if ( curl ) {
  128. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  129. //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  130. curl_easy_setopt(curl, CURLOPT_URL, endpoint);
  131. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  132. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer);
  133. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(buffer));
  134. curl_easy_setopt(curl, CURLOPT_POST, 1);
  135. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  136. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  137. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  138. if ( strcmp(action, "S") != 0 ) {
  139. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_response);
  140. }
  141. curl_easy_perform(curl);
  142. /* always cleanup */
  143. curl_easy_cleanup(curl);
  144. curl_slist_free_all(headers);
  145. }
  146. return 0;
  147. }
Add Comment
Please, Sign In to add comment