Windows fixes
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Sat, 12 Nov 2016 17:57:19 +0000 (18:57 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Sat, 12 Nov 2016 18:04:09 +0000 (19:04 +0100)
ethsock.c
nmrp.c
tftp.c

index 1d6c173864d66e283ee7494270fc9be185218a9d..75e166867047e8d9026fb06a688b05a655f45221 100644 (file)
--- a/ethsock.c
+++ b/ethsock.c
@@ -152,7 +152,7 @@ static bool get_intf_info(const char *intf, uint8_t *hwaddr, DWORD *index)
 
        if ((ret = GetAdaptersInfo(adapters, &bufLen) == NO_ERROR)) {
                for (adapter = adapters; adapter; adapter = adapter->Next) {
-                       if (adapter->Type != MIB_IF_TYPE_ETHERNET) {
+                       if (adapter->Type != MIB_IF_TYPE_ETHERNET && adapter->Type != IF_TYPE_IEEE80211) {
                                continue;
                        }
 
@@ -751,8 +751,10 @@ out:
 #else // NMRPFLASH_WINDOWS
        ULONG instance;
 
+       (*undo)->context = 0;
+
        DWORD ret = AddIPAddress(ipaddr, ipmask, sock->index, &(*undo)->context, &instance);
-       if (ret != NO_ERROR) {
+       if (ret != NO_ERROR && ret != ERROR_DUP_DOMAINNAME && ret != ERROR_OBJECT_ALREADY_EXISTS) {
                win_perror2("AddIPAddress", ret);
                return -1;
        }
@@ -776,13 +778,8 @@ int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
                ret = 0;
        }
 #else
-       DWORD err = DeleteIPAddress((*undo)->context);
-       if (err != NO_ERROR) {
-               win_perror2("DeleteIPAddress", ret);
-               ret = -1;
-       } else {
-               ret = 0;
-       }
+       DeleteIPAddress((*undo)->context);
+       ret = 0;
 #endif
 
        free(*undo);
diff --git a/nmrp.c b/nmrp.c
index 3cdba476f83f6d92e0adeda0c973865c0b7f2217..c2e634aa7241cbac4e745911c97fc62c4ccdcfbf 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -583,6 +583,12 @@ int nmrp_do(struct nmrpd_args *args)
                                memcpy(arpmac, rx.eh.ether_shost, 6);
                                memcpy(&arpip, &ipconf.addr, sizeof(ipconf.addr));
 
+                               if (autoip) {
+                                       if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &gundo) != 0) {
+                                               goto out;
+                                       }
+                               }
+
                                if (ethsock_arp_add(sock, arpmac, &arpip) != 0) {
                                        goto out;
                                }
@@ -622,12 +628,6 @@ int nmrp_do(struct nmrpd_args *args)
 
                                status = 0;
 
-                               if (autoip) {
-                                       if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &gundo) != 0) {
-                                               goto out;
-                                       }
-                               }
-
                                if (args->tftpcmd) {
                                        printf("Executing '%s' ... ", args->tftpcmd);
                                        fflush(stdout);
@@ -662,10 +662,6 @@ int nmrp_do(struct nmrpd_args *args)
                                        status = tftp_put(args);
                                }
 
-                               if (ethsock_ip_del(sock, &gundo) != 0) {
-                                       goto out;
-                               }
-
                                if (!status) {
                                        printf("OK\nWaiting for remote to respond.\n");
                                        upload_ok = 1;
@@ -739,6 +735,7 @@ out:
        signal(SIGINT, sigh_orig);
        gsock = NULL;
        ethsock_arp_del(sock, arpmac, &arpip);
+       ethsock_ip_del(sock, &gundo);
        ethsock_close(sock);
        return status;
 }
diff --git a/tftp.c b/tftp.c
index 7da210b70b25a5cf59ae40bc5a83665fde7dde34..ad02be72f48e49a736738f754d3c172e03501842 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -262,12 +262,40 @@ int tftp_put(struct nmrpd_args *args)
                goto cleanup;
        }
 
+       memset(&addr, 0, sizeof(addr));
+
+       addr.sin_family = AF_INET;
+
+       if (args->ipaddr_intf) {
+               if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) {
+                       perror("inet_addr");
+                       goto cleanup;
+               }
+
+               int tries = 100;
+
+               while (1) {
+                       if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
+#ifdef NMRPFLASH_WINDOWS
+                               // Wait for AddIPAddress to update the IP tables
+                               if (WSAGetLastError() == WSAEADDRNOTAVAIL) {
+                                       if (--tries) {
+                                               continue;
+                                       }
+                               }
+#endif
+                               sock_perror("bind");
+                               goto cleanup;
+                       }
+
+                       break;
+               }
+       }
+
        if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
                perror("inet_addr");
                goto cleanup;
        }
-
-       addr.sin_family = AF_INET;
        addr.sin_port = htons(args->port);
 
        block = 0;