nmrpd.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <stdbool.h>
  23. #if defined(_WIN32) || defined(_WIN64)
  24. #define NMRPFLASH_WINDOWS
  25. #elif defined(__linux__)
  26. #define NMRPFLASH_LINUX
  27. #elif defined(__APPLE__) && defined(__MACH__)
  28. #define NMRPFLASH_OSX
  29. #elif defined(__unix__)
  30. #define NMRPFLASH_UNIX
  31. #warning "nmrpflash is not fully supported on your operating system"
  32. #endif
  33. #ifndef NMRPFLASH_WINDOWS
  34. #include <arpa/inet.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #include <net/if.h>
  39. #ifndef NMRPFLASH_LINUX
  40. #include <net/if_dl.h>
  41. #endif
  42. #else
  43. #include <winsock2.h>
  44. #include <iphlpapi.h>
  45. #include <ws2tcpip.h>
  46. #include <windows.h>
  47. #endif
  48. #ifndef MIN
  49. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  50. #endif
  51. #ifndef PACKED
  52. #define PACKED __attribute__((packed))
  53. #endif
  54. struct eth_hdr {
  55. uint8_t ether_dhost[6];
  56. uint8_t ether_shost[6];
  57. uint16_t ether_type;
  58. } PACKED;
  59. enum nmrp_op {
  60. NMRP_UPLOAD_FW = 0,
  61. NMRP_UPLOAD_ST = 1,
  62. NMRP_SET_REGION = 2,
  63. };
  64. struct nmrpd_args {
  65. unsigned rx_timeout;
  66. unsigned ul_timeout;
  67. const char *tftpcmd;
  68. const char *file_local;
  69. const char *file_remote;
  70. const char *ipaddr_intf;
  71. const char *ipaddr;
  72. const char *ipmask;
  73. const char *intf;
  74. const char *mac;
  75. enum nmrp_op op;
  76. uint16_t port;
  77. const char *region;
  78. };
  79. const char *leafname(const char *path);
  80. int tftp_put(struct nmrpd_args *args);
  81. bool tftp_is_valid_filename(const char *filename);
  82. int nmrp_do(struct nmrpd_args *args);
  83. int select_fd(int fd, unsigned timeout);
  84. const char *mac_to_str(uint8_t *mac);
  85. #ifdef NMRPFLASH_WINDOWS
  86. void win_perror2(const char *msg, DWORD err);
  87. void sock_perror(const char *msg);
  88. #else
  89. #define sock_perror(x) perror(x)
  90. #endif
  91. extern int verbosity;
  92. struct ethsock;
  93. struct ethsock_arp_undo;
  94. struct ethsock_ip_undo;
  95. struct ethsock *ethsock_create(const char *intf, uint16_t protocol);
  96. int ethsock_close(struct ethsock *sock);
  97. int ethsock_send(struct ethsock *sock, void *buf, size_t len);
  98. ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
  99. int ethsock_set_timeout(struct ethsock *sock, unsigned msec);
  100. uint8_t *ethsock_get_hwaddr(struct ethsock *sock);
  101. int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo);
  102. int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo);
  103. int ethsock_list_all(void);
  104. struct ethsock_ip_callback_args
  105. {
  106. struct in_addr *ipaddr;
  107. struct in_addr *ipmask;
  108. void *arg;
  109. };
  110. typedef int (*ethsock_ip_callback_t)(struct ethsock_ip_callback_args *args);
  111. int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
  112. void *arg);
  113. int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo);
  114. int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo);
  115. time_t time_monotonic();
  116. char *lltostr(long long ll, int base);
  117. uint32_t bitcount(uint32_t n);
  118. uint32_t netmask(uint32_t count);
  119. #endif