nmrpd.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <iphlpapi.h>
  54. #include <ws2tcpip.h>
  55. #include <windows.h>
  56. #include <conio.h>
  57. #endif
  58. #ifndef MIN
  59. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  60. #endif
  61. #ifndef MAX
  62. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  63. #endif
  64. #ifndef PACKED
  65. #define PACKED __attribute__((packed))
  66. #endif
  67. #define NMRPFLASH_SET_REGION
  68. struct eth_hdr {
  69. uint8_t ether_dhost[6];
  70. uint8_t ether_shost[6];
  71. uint16_t ether_type;
  72. } PACKED;
  73. enum nmrp_op {
  74. NMRP_UPLOAD_FW = 0,
  75. NMRP_UPLOAD_ST = 1,
  76. NMRP_SET_REGION = 2,
  77. };
  78. struct nmrpd_args {
  79. unsigned rx_timeout;
  80. unsigned ul_timeout;
  81. const char *tftpcmd;
  82. const char *file_local;
  83. const char *file_remote;
  84. const char *ipaddr_intf;
  85. const char *ipaddr;
  86. const char *ipmask;
  87. const char *intf;
  88. const char *mac;
  89. enum nmrp_op op;
  90. bool blind;
  91. uint16_t port;
  92. const char *region;
  93. off_t offset;
  94. };
  95. const char *leafname(const char *path);
  96. int tftp_put(struct nmrpd_args *args);
  97. bool tftp_is_valid_filename(const char *filename);
  98. int nmrp_do(struct nmrpd_args *args);
  99. int select_fd(int fd, unsigned timeout);
  100. const char *mac_to_str(uint8_t *mac);
  101. #ifdef NMRPFLASH_WINDOWS
  102. void win_perror2(const char *msg, DWORD err);
  103. void sock_perror(const char *msg);
  104. #else
  105. #define sock_perror(x) xperror(x)
  106. #endif
  107. extern int verbosity;
  108. struct ethsock;
  109. struct ethsock_arp_undo;
  110. struct ethsock_ip_undo;
  111. struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
  112. bool ethsock_is_unplugged(struct ethsock *sock);
  113. int ethsock_close(struct ethsock *sock);
  114. int ethsock_send(struct ethsock *sock, void *buf, size_t len);
  115. ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
  116. int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
  117. uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
  118. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
  119. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
  120. int ethsock_list_all(void);
  121. struct ethsock_ip_callback_args
  122. {
  123. struct in_addr *ipaddr;
  124. struct in_addr *ipmask;
  125. void *arg;
  126. };
  127. typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
  128. int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
  129. void *arg);
  130. int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
  131. int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
  132. time_t time_monotonic();
  133. char *lltostr(long long ll, int base);
  134. uint32_t bitcount(uint32_t n);
  135. uint32_t netmask(uint32_t count);
  136. void xperror(const char *msg);
  137. extern volatile sig_atomic_t g_interrupted;
  138. #endif