util.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "log.h"
  2. #include "util.h"
  3. #include <arpa/inet.h>
  4. #include <errno.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <tox/tox.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. void writechecksum(uint8_t *address)
  12. {
  13. uint8_t *checksum = address + 36;
  14. uint32_t i;
  15. for (i = 0; i < 36; ++i)
  16. checksum[i % 2] ^= address[i];
  17. }
  18. /* From utox/util.c */
  19. void to_hex(char_t *a, const char_t *p, int size)
  20. {
  21. char_t b, c;
  22. const char_t *end = p + size;
  23. while(p != end) {
  24. b = *p++;
  25. c = (b & 0xF);
  26. b = (b >> 4);
  27. if(b < 10) {
  28. *a++ = b + '0';
  29. } else {
  30. *a++ = b - 10 + 'A';
  31. }
  32. if(c < 10) {
  33. *a++ = c + '0';
  34. } else {
  35. *a++ = c - 10 + 'A';
  36. }
  37. }
  38. *a = '\0';
  39. }
  40. /* From utox/util.c */
  41. void id_to_string(char_t *dest, const char_t *src)
  42. {
  43. to_hex(dest, src, TOX_ADDRESS_SIZE);
  44. }
  45. /* From utox/util.c */
  46. int string_to_id(char_t *w, char_t *a)
  47. {
  48. char_t *end = w + TOX_ADDRESS_SIZE;
  49. while(w != end) {
  50. char_t c, v;
  51. c = *a++;
  52. if(c >= '0' && c <= '9') {
  53. v = (c - '0') << 4;
  54. } else if(c >= 'A' && c <= 'F') {
  55. v = (c - 'A' + 10) << 4;
  56. } else if(c >= 'a' && c <= 'f') {
  57. v = (c - 'a' + 10) << 4;
  58. } else {
  59. return 0;
  60. }
  61. c = *a++;
  62. if(c >= '0' && c <= '9') {
  63. v |= (c - '0');
  64. } else if(c >= 'A' && c <= 'F') {
  65. v |= (c - 'A' + 10);
  66. } else if(c >= 'a' && c <= 'f') {
  67. v |= (c - 'a' + 10);
  68. } else {
  69. return 0;
  70. }
  71. *w++ = v;
  72. }
  73. return 1;
  74. }
  75. /* Parse the -L parameter */
  76. /* 0 = success */
  77. int parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port)
  78. {
  79. char *lport;
  80. char *host;
  81. char *rport;
  82. /* First replace all @ with :, ':' is forbidden in some environments */
  83. char *p = string;
  84. while(*p)
  85. {
  86. if(*p == '@') *p = ':';
  87. p++;
  88. }
  89. lport = strtok(string, ":");
  90. host = strtok(NULL, ":");
  91. rport = strtok(NULL, ":");
  92. if(!lport || !host || !rport)
  93. {
  94. return -1;
  95. }
  96. *local_port = atoi(lport);
  97. *hostname = host;
  98. *remote_port = atoi(rport);
  99. return 0;
  100. }
  101. /* Parse the -W parameter */
  102. /* 0 = success */
  103. int parse_pipe_port_forward(char *string, char **hostname, int *remote_port)
  104. {
  105. char *host;
  106. char *rport;
  107. /* First replace all @ with :, ':' is forbidden in some environments */
  108. char *p = string;
  109. while(*p)
  110. {
  111. if(*p == '@') *p = ':';
  112. p++;
  113. }
  114. host = strtok(string, ":");
  115. rport = strtok(NULL, ":");
  116. if(!host || !rport)
  117. {
  118. return -1;
  119. }
  120. *hostname = host;
  121. *remote_port = atoi(rport);
  122. return 0;
  123. }
  124. void* file_raw(char *path, uint32_t *size)
  125. {
  126. FILE *file;
  127. char *data;
  128. int len;
  129. file = fopen(path, "rb");
  130. if(!file) {
  131. log_printf(L_WARNING, "File not found (%s)\n", path);
  132. return NULL;
  133. }
  134. fseek(file, 0, SEEK_END);
  135. len = ftell(file);
  136. if(len <= 0)
  137. {
  138. fclose(file);
  139. return NULL;
  140. }
  141. data = malloc(len);
  142. if(!data) {
  143. fclose(file);
  144. return NULL;
  145. }
  146. fseek(file, 0, SEEK_SET);
  147. if(fread(data, len, 1, file) != 1) {
  148. log_printf(L_WARNING, "Read error (%s)\n", path);
  149. fclose(file);
  150. free(data);
  151. return NULL;
  152. }
  153. fclose(file);
  154. log_printf(L_DEBUG, "Read %u bytes (%s)\n", len, path);
  155. if(size) {
  156. *size = len;
  157. }
  158. return data;
  159. }
  160. const char *readable_connection_status(TOX_CONNECTION status)
  161. {
  162. switch(status)
  163. {
  164. case TOX_CONNECTION_NONE:
  165. return "There is no connection";
  166. case TOX_CONNECTION_TCP:
  167. return "A TCP connection has been established (via TCP relay)";
  168. case TOX_CONNECTION_UDP:
  169. return "An UDP connection has been established";
  170. default:
  171. log_printf(L_WARNING, "Received unknown connection status %d\n", (int)status);
  172. return "Unknown connection status";
  173. }
  174. }
  175. /* From https://github.com/TokTok/c-toxcore/blob/master/other/fun/cracker.c */
  176. size_t hex_string_to_bin(const char *hex_string, size_t hex_len, uint8_t *bytes)
  177. {
  178. size_t i;
  179. const char *pos = hex_string;
  180. // make even
  181. for (i = 0; i < hex_len / 2; ++i, pos += 2) {
  182. uint8_t val;
  183. if (sscanf(pos, "%02hhx", &val) != 1) {
  184. return 0;
  185. }
  186. bytes[i] = val;
  187. }
  188. if (i * 2 < hex_len) {
  189. uint8_t val;
  190. if (sscanf(pos, "%hhx", &val) != 1) {
  191. return 0;
  192. }
  193. bytes[i] = (uint8_t)(val << 4);
  194. ++i;
  195. }
  196. return i;
  197. }
  198. /* Very stupid test to filter out hostnames */
  199. bool is_valid_ipv4(const char *ip_address)
  200. {
  201. unsigned int a,b,c,d;
  202. return sscanf(ip_address,"%u.%u.%u.%u", &a, &b, &c, &d) == 4;
  203. }
  204. bool is_valid_ipv6(const char *ip_address)
  205. {
  206. struct in6_addr result;
  207. return (inet_pton(AF_INET6, ip_address, &result) == 1);
  208. }
  209. void save_printable_tox_id(const unsigned char *tox_printable_id, const char *path)
  210. {
  211. FILE *f = fopen(path, "w");
  212. if(!f)
  213. {
  214. log_printf(L_ERROR, "Could not write to %s: %d %s", path, errno, strerror(errno));
  215. }
  216. log_printf(L_DEBUG, "Writing Tox ID to %s", path);
  217. fputs((char*)tox_printable_id, f);
  218. fclose(f);
  219. }