Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <unistd.h>
  6. #include <sys/socket.h>
  7. #include <arpa/inet.h>
  8. #include <stdbool.h>
  9. #include <assert.h>
  10.  
  11. #define MAX_LEN 1024
  12.  
  13. char *ip;
  14. char *perem;
  15.  
  16. unsigned short port = 23;
  17.  
  18. void usage(const char *progname) {
  19. printf("Usage: %s IP\n", progname);
  20. exit(0);
  21. }
  22.  
  23. void fail(const char *reason) {
  24. printf("%s\n", reason);
  25. exit(0);
  26. }
  27.  
  28. typedef struct _meteodata {
  29. char *varName;
  30. double *varValue;
  31. } meteodata;
  32.  
  33. void printMeteoData(meteodata *md) {
  34. if(!md) {
  35. printf("%s", "(null)");
  36. return;
  37. }
  38. printf("varName = \"%s\"; varValue = %lf\n", md->varName, *md->varValue);
  39. }
  40.  
  41. void pSkipGarbage(char **istream) {
  42. assert(istream && *istream);
  43. char *cur = *istream;
  44.  
  45. while(*cur && !isalpha(*cur)) ++cur;
  46.  
  47. *istream = cur;
  48. }
  49.  
  50. bool pChar(char **istream, char c) {
  51. assert(istream && *istream);
  52. char *cur = *istream;
  53.  
  54. if(*cur != c) return false;
  55. ++cur;
  56.  
  57. *istream = cur;
  58. return true;
  59. }
  60.  
  61. char* pName(char **istream) {
  62. assert(istream && *istream);
  63. char *cur = *istream,
  64. *name = NULL;
  65. unsigned nameLen = 0;
  66.  
  67. while(*cur && isalpha(*cur)) ++cur;
  68. nameLen = cur - *istream;
  69. if(!nameLen) return NULL;
  70. name = malloc(nameLen + 1);
  71. memset(name, 0, nameLen + 1);
  72. strncpy(name, *istream, nameLen);
  73.  
  74. *istream = cur;
  75. return name;
  76. }
  77.  
  78. unsigned* pPositiveInteger(char **istream) {
  79. assert(istream && *istream);
  80. char *cur = *istream,
  81. *numStr = NULL;
  82. unsigned numLen = 0,
  83. *num = malloc(sizeof(unsigned));
  84.  
  85. while(*cur && isdigit(*cur)) ++cur;
  86. numLen = cur - *istream;
  87. if(!numLen) {
  88. free(num);
  89. return NULL;
  90. }
  91. numStr = malloc(numLen + 1);
  92. memset(numStr, 0, numLen + 1);
  93. strncpy(numStr, *istream, numLen);
  94. sscanf(numStr, "%u", num);
  95. free(numStr);
  96.  
  97. *istream = cur;
  98. return num;
  99. }
  100.  
  101. unsigned* pInteger(char **istream) {
  102. assert(istream && *istream);
  103. char *oldStream = *istream;
  104. unsigned *num = NULL;
  105. bool neg = false;
  106. int *intNum = malloc(sizeof(int));
  107.  
  108. neg = pChar(istream, '-');
  109. num = pPositiveInteger(istream);
  110. if(!num) {
  111. *istream = oldStream;
  112. free(intNum);
  113. return NULL;
  114. }
  115. if(neg) *intNum = 0 - *num;
  116. else *intNum = *num;
  117.  
  118. return intNum;
  119. }
  120.  
  121. double* pDouble(char **istream) {
  122. assert(istream && *istream);
  123. char *oldStream = *istream,
  124. *doubleStr = malloc(MAX_LEN);
  125. memset(doubleStr, 0, MAX_LEN);
  126. int *numBeforeDecimal = NULL;
  127. unsigned *numAfterDecimal = NULL;
  128. double *doubleNum = malloc(sizeof(double));
  129.  
  130. numBeforeDecimal = pInteger(istream);
  131. if(!numBeforeDecimal) {
  132. *istream = oldStream;
  133. free(doubleStr);
  134. free(doubleNum);
  135. return NULL;
  136. }
  137. if(pChar(istream, '.')) {
  138. numAfterDecimal = pPositiveInteger(istream);
  139. if(!numAfterDecimal) {
  140. *istream = oldStream;
  141. free(doubleStr);
  142. free(doubleNum);
  143. return NULL;
  144. }
  145. snprintf(doubleStr, MAX_LEN, "%d.%u\n", *numBeforeDecimal, *numAfterDecimal);
  146. sscanf(doubleStr, "%lf", doubleNum);
  147. } else {
  148. *doubleNum = (double) *numBeforeDecimal;
  149. }
  150.  
  151. free(doubleStr);
  152. return doubleNum;
  153. }
  154.  
  155. meteodata* pMeteoData(char **istream) {
  156. assert(istream && *istream);
  157. char *oldStream = *istream;
  158. meteodata *result = malloc(sizeof(meteodata));
  159.  
  160. pSkipGarbage(istream);
  161. if(!(result->varName = pName(istream))) {
  162. *istream = oldStream;
  163. return NULL;
  164. }
  165. if(!pChar(istream, '=')) {
  166. *istream = oldStream;
  167. return NULL;
  168. }
  169. if(!(result->varValue = pDouble(istream))) {
  170. *istream = oldStream;
  171. return NULL;
  172. }
  173.  
  174. return result;
  175. }
  176.  
  177. meteodata** pMeteoDataStar(char **istream) {
  178. assert(istream && *istream);
  179. meteodata **allData = malloc(sizeof(*allData) * MAX_LEN),
  180. *cur = NULL;
  181. memset(allData, 0, sizeof(*allData) * MAX_LEN);
  182. unsigned i = 0;
  183.  
  184. for(;;) {
  185. cur = pMeteoData(istream);
  186. if(!cur) break;
  187. allData[i++] = cur;
  188. }
  189. allData[i] = 0;
  190.  
  191. return allData;
  192. }
  193.  
  194. void findVariableValue(char *noi, meteodata **mds, meteodata **poi) {
  195. assert(noi && mds && poi);
  196. unsigned i = 0;
  197.  
  198. *poi = NULL;
  199. while(*(mds + i)) {
  200. if(!strncmp(noi, mds[i]->varName, MAX_LEN))
  201. *poi = mds[i];
  202. ++i;
  203. }
  204. }
  205.  
  206. int main(int argc, char **argv) {
  207. int s = -1;
  208. struct sockaddr_in meteo_addr;
  209.  
  210. if(argc != 3) usage(argv[0]);
  211. ip = argv[1];
  212. perem = argv[2];
  213.  
  214. if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) fail("can't create socket");
  215. memset(&meteo_addr, 0, sizeof(meteo_addr));
  216. meteo_addr.sin_family = AF_INET;
  217. meteo_addr.sin_addr.s_addr = inet_addr(ip);
  218. meteo_addr.sin_port = htons(port);
  219. if(connect(s, (struct sockaddr*)&meteo_addr, sizeof(meteo_addr)) < 0) fail("connect failed");
  220.  
  221. char rq[128];
  222. memset(rq, 0, sizeof(rq));
  223. strcat(rq, "\xff\xfc\x18\xff\xfc\x20\xff\xfc\x23\xff\xfc\x27\xff\xfe\x03\xff\xfc\x01\xff\xfc\x1f\xff\xfe\x05\xff\xfc\x21\r\n0R0\r\n");
  224. if(send(s, rq, strlen(rq), 0) < strlen(rq)) fail("socket send failed");
  225. sleep(5);
  226.  
  227. char buf[256];
  228. memset(buf, 0, sizeof(buf));
  229. if(read(s, buf, sizeof(buf) - 1) < 0) fail("socket read failed");
  230.  
  231. char *realInput = buf
  232. , *input = realInput;
  233. memset(input, 0, MAX_LEN);
  234. meteodata **mds = NULL, *pointOfInterest = NULL;
  235. unsigned i = 0;
  236.  
  237. fgets(input, MAX_LEN, stdin);
  238. mds = pMeteoDataStar(&input);
  239.  
  240. findVariableValue(perem, mds, &pointOfInterest);
  241. if(!pointOfInterest) {
  242. free(mds);
  243. free(realInput);
  244. return 1;
  245. }
  246. printf("%lf\n", *(pointOfInterest->varValue));
  247.  
  248. free(mds);
  249. free(realInput);
  250. close(s);
  251. exit(0);
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement