main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "../common/common.hpp"
  2. void Usage()
  3. {
  4. printf(
  5. "Usage: ForceDeviceIP <cmd> <MAC> <newIP> <newNetmask> <newGateway>\n"
  6. " - <cmd> should be -force/-static/-dynamic\n"
  7. " Example: ForceDeviceIP -force 12:34:56:78:90:ab 192.168.1.100 255.255.255.0 192.168.1.1\n"
  8. "\n"
  9. " 1. Force the device IP temporarily.\n"
  10. " - The newIP/newNetmask will be valid only before reboot.\n"
  11. " Command: ForceDeviceIP -force <MAC> <newIP> <newNetmask> <newGateway>\n"
  12. "\n"
  13. " 2. Force the device IP persistently.\n"
  14. " - The newIP/newNetmask will be fixed and always vaild.\n"
  15. " Command: ForceDeviceIP -static <MAC> <newIP> <newNetmask> <newGateway>\n"
  16. "\n"
  17. " 3. Set the device IP to dynamic.\n"
  18. " - The simplest way, which is more convenient than usage 4.\n"
  19. " - Not supported by some old devices. Try usage 4 in this case.\n"
  20. " - Take effect immediately. The device will begin to get new IP from DHCP server or Link-local.\n"
  21. " Command: ForceDeviceIP -dynamic <MAC>\n"
  22. "\n"
  23. " 4. Set the device IP to dynamic, for legacy devices.\n"
  24. " - Force the newIP to the device first.\n"
  25. " - Make sure the tool can access the device properly after newIP is forced.\n"
  26. " - Take effect after reboot.\n"
  27. " Command: ForceDeviceIP -dynamic <MAC> <newIP> <newNetmask> <newGateway>\n"
  28. );
  29. }
  30. int main(int argc, char* argv[])
  31. {
  32. char * cmd = NULL;
  33. char * mac = NULL;
  34. const char * newIP = "0.0.0.0";
  35. const char * newNetmask = "0.0.0.0";
  36. const char * newGateway = "0.0.0.0";
  37. if (argc < 3) {
  38. Usage();
  39. return -1;
  40. }
  41. cmd = argv[1];
  42. mac = argv[2];
  43. if (strcmp(cmd, "-force") != 0
  44. && strcmp(cmd, "-static") != 0
  45. && strcmp(cmd, "-dynamic") != 0) {
  46. Usage();
  47. return -1;
  48. }
  49. if (argc >= 4) {
  50. newIP = argv[3];
  51. }
  52. if (argc >= 5) {
  53. newNetmask = argv[4];
  54. }
  55. if (argc >= 6) {
  56. newGateway = argv[5];
  57. }
  58. LOGD("Init lib");
  59. ASSERT_OK( TYInitLib() );
  60. TY_VERSION_INFO ver;
  61. ASSERT_OK( TYLibVersion(&ver) );
  62. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  63. LOGD("Update interface list");
  64. ASSERT_OK( TYUpdateInterfaceList() );
  65. uint32_t n = 0;
  66. ASSERT_OK( TYGetInterfaceNumber(&n) );
  67. LOGD("Got %u interface list", n);
  68. if(n == 0){
  69. LOGE("interface number incorrect");
  70. return TY_STATUS_ERROR;
  71. }
  72. std::vector<TY_INTERFACE_INFO> ifaces(n);
  73. ASSERT_OK( TYGetInterfaceList(&ifaces[0], n, &n) );
  74. ASSERT( n == ifaces.size() );
  75. for(uint32_t i = 0; i < n; i++){
  76. LOGI("Found interface %u:", i);
  77. LOGI(" name: %s", ifaces[i].name);
  78. LOGI(" id: %s", ifaces[i].id);
  79. LOGI(" type: 0x%x", ifaces[i].type);
  80. if(TYIsNetworkInterface(ifaces[i].type)){
  81. LOGI(" MAC: %s", ifaces[i].netInfo.mac);
  82. LOGI(" ip: %s", ifaces[i].netInfo.ip);
  83. LOGI(" netmask: %s", ifaces[i].netInfo.netmask);
  84. LOGI(" gateway: %s", ifaces[i].netInfo.gateway);
  85. LOGI(" broadcast: %s", ifaces[i].netInfo.broadcast);
  86. TY_INTERFACE_HANDLE hIface;
  87. ASSERT_OK( TYOpenInterface(ifaces[i].id, &hIface) );
  88. if (TYForceDeviceIP(hIface, mac, newIP, newNetmask, newGateway) == TY_STATUS_OK) {
  89. LOGD("**** Set Temporary IP/Netmask/Gateway ...Done! ****");
  90. bool open_needed = false;
  91. const char * ip_save = newIP;
  92. const char * netmask_save = newNetmask;
  93. const char * gateway_save = newGateway;
  94. if(strcmp(cmd, "-static") == 0) {
  95. open_needed = true;
  96. }
  97. if(strcmp(cmd, "-dynamic") == 0 && strcmp(newIP, "0.0.0.0") != 0){
  98. open_needed = true;
  99. ip_save = "0.0.0.0";
  100. netmask_save = "0.0.0.0";
  101. gateway_save = "0.0.0.0";
  102. }
  103. if(open_needed){
  104. TYUpdateDeviceList(hIface);
  105. TY_DEV_HANDLE hDev;
  106. if(TYOpenDeviceWithIP(hIface, newIP, &hDev) == TY_STATUS_OK){
  107. int32_t ip_i[4];
  108. uint8_t ip_b[4];
  109. int32_t ip32;
  110. sscanf(ip_save, "%d.%d.%d.%d", &ip_i[0], &ip_i[1], &ip_i[2], &ip_i[3]);
  111. ip_b[0] = ip_i[0];ip_b[1] = ip_i[1];ip_b[2] = ip_i[2];ip_b[3] = ip_i[3];
  112. ip32 = TYIPv4ToInt(ip_b);
  113. LOGI("Set persistent IP 0x%x(%d.%d.%d.%d)", ip32, ip_b[0], ip_b[1], ip_b[2], ip_b[3]);
  114. ASSERT_OK( TYSetInt(hDev, TY_COMPONENT_DEVICE, TY_INT_PERSISTENT_IP, ip32) );
  115. sscanf(netmask_save, "%d.%d.%d.%d", &ip_i[0], &ip_i[1], &ip_i[2], &ip_i[3]);
  116. ip_b[0] = ip_i[0];ip_b[1] = ip_i[1];ip_b[2] = ip_i[2];ip_b[3] = ip_i[3];
  117. ip32 = TYIPv4ToInt(ip_b);
  118. LOGI("Set persistent Netmask 0x%x(%d.%d.%d.%d)", ip32, ip_b[0], ip_b[1], ip_b[2], ip_b[3]);
  119. ASSERT_OK( TYSetInt(hDev, TY_COMPONENT_DEVICE, TY_INT_PERSISTENT_SUBMASK, ip32) );
  120. sscanf(gateway_save, "%d.%d.%d.%d", &ip_i[0], &ip_i[1], &ip_i[2], &ip_i[3]);
  121. ip_b[0] = ip_i[0];ip_b[1] = ip_i[1];ip_b[2] = ip_i[2];ip_b[3] = ip_i[3];
  122. ip32 = TYIPv4ToInt(ip_b);
  123. LOGI("Set persistent Gateway 0x%x(%d.%d.%d.%d)", ip32, ip_b[0], ip_b[1], ip_b[2], ip_b[3]);
  124. ASSERT_OK( TYSetInt(hDev, TY_COMPONENT_DEVICE, TY_INT_PERSISTENT_GATEWAY, ip32) );
  125. LOGD("**** Set Persistent IP/Netmask/Gateway ...Done! ****");
  126. }
  127. }
  128. } else {
  129. LOGW("Force ip failed on interface %s", ifaces[i].id);
  130. }
  131. ASSERT_OK( TYCloseInterface(hIface));
  132. }
  133. }
  134. ASSERT_OK(TYDeinitLib());
  135. return 0;
  136. }