nmrpd.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash 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. * nmrpflash 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 nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #ifndef NMRPD_H
  20. #define NMRPD_H
  21. #include <stdint.h>
  22. #include <signal.h>
  23. #include <stdbool.h>
  24. #if defined(_WIN32) || defined(_WIN64)
  25. # define NMRPFLASH_WINDOWS
  26. #elif defined(__APPLE__) && defined(__MACH__)
  27. # define NMRPFLASH_UNIX
  28. # define NMRPFLASH_OSX
  29. # define NMRPFLASH_BSD
  30. #elif defined (__unix__)
  31. # define NMRPFLASH_UNIX
  32. # if defined(__linux__)
  33. # define NMRPFLASH_LINUX
  34. # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
  35. # define NMRPFLASH_BSD
  36. # else
  37. # warning "nmrpflash is not fully supported on this platform"
  38. # endif
  39. #else
  40. # warning "nmrpflash is not supported on this platform"
  41. #endif
  42. #ifndef NMRPFLASH_WINDOWS
  43. #include <arpa/inet.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <net/if.h>
  48. #ifndef NMRPFLASH_LINUX
  49. #include <net/if_dl.h>
  50. #endif
  51. #else
  52. #include <winsock2.h>
  53. #include <ws2tcpip.h>
  54. #include <windows.h>
  55. #include <conio.h>
  56. #endif
  57. #ifndef MIN
  58. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  59. #endif
  60. #ifndef MAX
  61. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  62. #endif
  63. #ifndef PACKED
  64. #define PACKED __attribute__((packed))
  65. #endif
  66. #define NMRPFLASH_SET_REGION
  67. #define NMRP_DEFAULT_UL_TIMEOUT_S (30 * 60)
  68. #define NMRP_DEFAULT_RX_TIMEOUT_MS (10000)
  69. /*
  70. * These addresses should not cause collisions on most networks,
  71. * and if they do, the user is probably "poweruser" enough to
  72. * be able to use the -a and -A options.
  73. */
  74. #define NMRP_DEFAULT_IP_LOCAL "10.164.183.252"
  75. #define NMRP_DEFAULT_IP_REMOTE "10.164.183.253"
  76. #define NMRP_DEFAULT_SUBNET "255.255.255.0"
  77. #define NMRP_DEFAULT_TFTP_PORT 69
  78. struct eth_hdr {
  79. uint8_t ether_dhost[6];
  80. uint8_t ether_shost[6];
  81. uint16_t ether_type;
  82. } PACKED;
  83. enum nmrp_op {
  84. NMRP_UPLOAD_FW = 0,
  85. NMRP_UPLOAD_ST = 1,
  86. NMRP_SET_REGION = 2,
  87. };
  88. struct nmrpd_args {
  89. unsigned rx_timeout;
  90. unsigned ul_timeout;
  91. const char *tftpcmd;
  92. const char *file_local;
  93. const char *file_remote;
  94. const char *ipaddr_intf;
  95. const char *ipaddr;
  96. const char *ipmask;
  97. const char *intf;
  98. const char *mac;
  99. enum nmrp_op op;
  100. bool blind;
  101. uint16_t port;
  102. const char *region;
  103. off_t offset;
  104. };
  105. const char *leafname(const char *path);
  106. ssize_t tftp_put(struct nmrpd_args *args);
  107. bool tftp_is_valid_filename(const char *filename);
  108. int nmrp_do(struct nmrpd_args *args);
  109. int select_fd(int fd, unsigned timeout);
  110. const char *mac_to_str(uint8_t *mac);
  111. #ifdef NMRPFLASH_WINDOWS
  112. void win_perror2(const char *msg, DWORD err);
  113. void sock_perror(const char *msg);
  114. #else
  115. #define sock_perror(x) xperror(x)
  116. #endif
  117. extern int verbosity;
  118. struct ethsock;
  119. struct ethsock_arp_undo;
  120. struct ethsock_ip_undo;
  121. struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
  122. bool ethsock_is_unplugged(struct ethsock *sock);
  123. bool ethsock_is_wifi(struct ethsock *sock);
  124. int ethsock_close(struct ethsock *sock);
  125. int ethsock_send(struct ethsock *sock, void *buf, size_t len);
  126. ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
  127. int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
  128. uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
  129. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
  130. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
  131. int ethsock_list_all(void);
  132. struct ethsock_ip_callback_args
  133. {
  134. struct in_addr *ipaddr;
  135. struct in_addr *ipmask;
  136. void *arg;
  137. };
  138. typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
  139. int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
  140. void *arg);
  141. int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
  142. int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
  143. time_t time_monotonic();
  144. char *lltostr(long long ll, int base);
  145. uint32_t bitcount(uint32_t n);
  146. uint32_t netmask(uint32_t count);
  147. void xperror(const char *msg);
  148. extern volatile sig_atomic_t g_interrupted;
  149. #endif