nmrpd.h 4.8 KB

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