nmrpd.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #define NMRP_ETH_TIMEOUT_S 60
  79. #define NMRP_MAX_UL_REQS 3
  80. struct eth_hdr {
  81. uint8_t ether_dhost[6];
  82. uint8_t ether_shost[6];
  83. uint16_t ether_type;
  84. } PACKED;
  85. enum nmrp_op {
  86. NMRP_UPLOAD_FW = 0,
  87. NMRP_UPLOAD_ST = 1,
  88. NMRP_SET_REGION = 2,
  89. };
  90. struct ethsock;
  91. struct nmrpd_args {
  92. unsigned rx_timeout;
  93. unsigned ul_timeout;
  94. const char *tftpcmd;
  95. const char *file_local;
  96. const char *file_remote;
  97. const char *ipaddr_intf;
  98. const char *ipaddr;
  99. const char *ipmask;
  100. const char *intf;
  101. const char *mac;
  102. enum nmrp_op op;
  103. bool blind;
  104. uint16_t port;
  105. const char *region;
  106. off_t offset;
  107. bool maybe_invalid_firmware_file;
  108. struct ethsock *sock;
  109. };
  110. const char *leafname(const char *path);
  111. ssize_t tftp_put(struct nmrpd_args *args);
  112. bool tftp_is_valid_filename(const char *filename);
  113. int nmrp_do(struct nmrpd_args *args);
  114. bool nmrp_discard(struct ethsock *sock);
  115. int select_fd(int fd, unsigned timeout);
  116. const char *mac_to_str(uint8_t *mac);
  117. #ifdef NMRPFLASH_WINDOWS
  118. void win_perror2(const char *msg, DWORD err);
  119. void sock_perror(const char *msg);
  120. #else
  121. #define sock_perror(x) xperror(x)
  122. #endif
  123. extern int verbosity;
  124. struct ethsock_arp_undo;
  125. struct ethsock_ip_undo;
  126. struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
  127. bool ethsock_is_unplugged(struct ethsock *sock);
  128. bool ethsock_is_wifi(struct ethsock *sock);
  129. int ethsock_close(struct ethsock *sock);
  130. int ethsock_send(struct ethsock *sock, void *buf, size_t len);
  131. ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
  132. int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
  133. unsigned ethsock_get_timeout(struct ethsock *sock);
  134. uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
  135. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
  136. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
  137. int ethsock_list_all(void);
  138. struct ethsock_ip_callback_args
  139. {
  140. struct in_addr *ipaddr;
  141. struct in_addr *ipmask;
  142. void *arg;
  143. };
  144. typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
  145. int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
  146. void *arg);
  147. int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
  148. int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
  149. time_t time_monotonic();
  150. long long millis();
  151. char *lltostr(long long ll, int base);
  152. uint32_t bitcount(uint32_t n);
  153. uint32_t netmask(uint32_t count);
  154. void xperror(const char *msg);
  155. extern volatile sig_atomic_t g_interrupted;
  156. #endif