utils.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. void get_path_from_pid(char* dest, int pid)
  28. {
  29. char ppath[50];
  30. sprintf(ppath, "/proc/%d/exe", pid);
  31. if (readlink (ppath, dest, 50) != -1){
  32. }
  33. }
  34. void print_ip(int ip)
  35. {
  36. unsigned char bytes[4];
  37. bytes[0] = ip & 0xFF;
  38. bytes[1] = (ip >> 8) & 0xFF;
  39. bytes[2] = (ip >> 16) & 0xFF;
  40. bytes[3] = (ip >> 24) & 0xFF;
  41. printf("%d.%d.%d.%d\n", bytes[0], bytes[1], bytes[2], bytes[3]);
  42. //return buf;
  43. }
  44. /* --- */
  45. #ifdef NETCON_SERVICE
  46. ip_addr_t convert_ip(struct sockaddr_in * addr)
  47. {
  48. ip_addr_t conn_addr;
  49. struct sockaddr_in *ipv4 = addr;
  50. short a = ip4_addr1(&(ipv4->sin_addr));
  51. short b = ip4_addr2(&(ipv4->sin_addr));
  52. short c = ip4_addr3(&(ipv4->sin_addr));
  53. short d = ip4_addr4(&(ipv4->sin_addr));
  54. IP4_ADDR(&conn_addr, a,b,c,d);
  55. return conn_addr;
  56. }
  57. ip_addr_t ip_addr_sin(register struct sockaddr_in *sin) {
  58. ip_addr_t ip;
  59. *((struct sockaddr_in*) &ip) = *sin;
  60. return ip;
  61. }
  62. #endif
  63. /* --- */
  64. #ifdef NETCON_INTERCEPT
  65. typedef unsigned char u8_t;
  66. #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
  67. #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
  68. #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
  69. #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
  70. int is_local(struct sockaddr_in* addr)
  71. {
  72. struct sockaddr_in *ipv4 = addr;
  73. short a = ip4_addr1(&(ipv4->sin_addr));
  74. short b = ip4_addr2(&(ipv4->sin_addr));
  75. short c = ip4_addr3(&(ipv4->sin_addr));
  76. short d = ip4_addr4(&(ipv4->sin_addr));
  77. return (a==127 && b == 0 && c == 0 && d == 1);
  78. }
  79. #endif