Scanning the subnet by ip range

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












By applying the get_name and get_mac functions to the specified IP address, it is possible to estimate the network availability of a segment:



#include <Winsock2.h>
#include <iphlpapi.h>
#include <cstdio>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

bool get_name(unsigned char* name, char dest[32])

struct in_addr destip;
struct hostent* info;

destip.s_addr = inet_addr(dest);

info = gethostbyaddr((char*)&destip, 4, AF_INET);

if (info != NULL)

strcpy((char*)name, info->h_name);

else

return false;


return true;


bool get_mac(unsigned char* mac, char dest[32])

struct in_addr destip;
ULONG mac_address[2];
ULONG mac_address_len = 6;

destip.s_addr = inet_addr(dest);

SendARP((IPAddr)destip.S_un.S_addr, 0, mac_address, &mac_address_len);

if (mac_address_len)

BYTE* mac_address_buffer = (BYTE*)&mac_address;
for (int i = 0; i < (int)mac_address_len; i++)

mac[i] = (char)mac_address_buffer[i];


else

return false;


return true;


int main()

char address[32] = "192.168.14.101" , "192.168.14.102" ,
"192.168.14.103" , "192.168.14.106 " ;
WSADATA sock;

if (WSAStartup(MAKEWORD(2, 2), &sock) != 0)

printf("Failed to initialise winsock. (%d)n", WSAGetLastError());
return 1;


for (int i = 0; i < (int)sizeof(address) / 32; i++)

unsigned char mac[6] = '' ;
unsigned char name[100] = '' ;

if (get_mac(mac, address[i]))

printf("%s : %s : %.2X-%.2X-%.2X-%.2X-%.2X-%.2Xn", address[i],
(get_name(name, address[i])) ? (char*)name : "-",
mac[0],mac[1],mac[2],mac[3], mac[4], mac[5]);
fflush(stdout);



printf("nDone.n");
fflush(stdout);
system("PAUSE");

return 0;



An attempt to implement a program that scans the subnet and displays IP address, host name, and MAC of machines on the network.







share|improve this question



























    up vote
    0
    down vote

    favorite












    By applying the get_name and get_mac functions to the specified IP address, it is possible to estimate the network availability of a segment:



    #include <Winsock2.h>
    #include <iphlpapi.h>
    #include <cstdio>

    #pragma comment(lib, "iphlpapi.lib")
    #pragma comment(lib, "ws2_32.lib")

    bool get_name(unsigned char* name, char dest[32])

    struct in_addr destip;
    struct hostent* info;

    destip.s_addr = inet_addr(dest);

    info = gethostbyaddr((char*)&destip, 4, AF_INET);

    if (info != NULL)

    strcpy((char*)name, info->h_name);

    else

    return false;


    return true;


    bool get_mac(unsigned char* mac, char dest[32])

    struct in_addr destip;
    ULONG mac_address[2];
    ULONG mac_address_len = 6;

    destip.s_addr = inet_addr(dest);

    SendARP((IPAddr)destip.S_un.S_addr, 0, mac_address, &mac_address_len);

    if (mac_address_len)

    BYTE* mac_address_buffer = (BYTE*)&mac_address;
    for (int i = 0; i < (int)mac_address_len; i++)

    mac[i] = (char)mac_address_buffer[i];


    else

    return false;


    return true;


    int main()

    char address[32] = "192.168.14.101" , "192.168.14.102" ,
    "192.168.14.103" , "192.168.14.106 " ;
    WSADATA sock;

    if (WSAStartup(MAKEWORD(2, 2), &sock) != 0)

    printf("Failed to initialise winsock. (%d)n", WSAGetLastError());
    return 1;


    for (int i = 0; i < (int)sizeof(address) / 32; i++)

    unsigned char mac[6] = '' ;
    unsigned char name[100] = '' ;

    if (get_mac(mac, address[i]))

    printf("%s : %s : %.2X-%.2X-%.2X-%.2X-%.2X-%.2Xn", address[i],
    (get_name(name, address[i])) ? (char*)name : "-",
    mac[0],mac[1],mac[2],mac[3], mac[4], mac[5]);
    fflush(stdout);



    printf("nDone.n");
    fflush(stdout);
    system("PAUSE");

    return 0;



    An attempt to implement a program that scans the subnet and displays IP address, host name, and MAC of machines on the network.







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      By applying the get_name and get_mac functions to the specified IP address, it is possible to estimate the network availability of a segment:



      #include <Winsock2.h>
      #include <iphlpapi.h>
      #include <cstdio>

      #pragma comment(lib, "iphlpapi.lib")
      #pragma comment(lib, "ws2_32.lib")

      bool get_name(unsigned char* name, char dest[32])

      struct in_addr destip;
      struct hostent* info;

      destip.s_addr = inet_addr(dest);

      info = gethostbyaddr((char*)&destip, 4, AF_INET);

      if (info != NULL)

      strcpy((char*)name, info->h_name);

      else

      return false;


      return true;


      bool get_mac(unsigned char* mac, char dest[32])

      struct in_addr destip;
      ULONG mac_address[2];
      ULONG mac_address_len = 6;

      destip.s_addr = inet_addr(dest);

      SendARP((IPAddr)destip.S_un.S_addr, 0, mac_address, &mac_address_len);

      if (mac_address_len)

      BYTE* mac_address_buffer = (BYTE*)&mac_address;
      for (int i = 0; i < (int)mac_address_len; i++)

      mac[i] = (char)mac_address_buffer[i];


      else

      return false;


      return true;


      int main()

      char address[32] = "192.168.14.101" , "192.168.14.102" ,
      "192.168.14.103" , "192.168.14.106 " ;
      WSADATA sock;

      if (WSAStartup(MAKEWORD(2, 2), &sock) != 0)

      printf("Failed to initialise winsock. (%d)n", WSAGetLastError());
      return 1;


      for (int i = 0; i < (int)sizeof(address) / 32; i++)

      unsigned char mac[6] = '' ;
      unsigned char name[100] = '' ;

      if (get_mac(mac, address[i]))

      printf("%s : %s : %.2X-%.2X-%.2X-%.2X-%.2X-%.2Xn", address[i],
      (get_name(name, address[i])) ? (char*)name : "-",
      mac[0],mac[1],mac[2],mac[3], mac[4], mac[5]);
      fflush(stdout);



      printf("nDone.n");
      fflush(stdout);
      system("PAUSE");

      return 0;



      An attempt to implement a program that scans the subnet and displays IP address, host name, and MAC of machines on the network.







      share|improve this question













      By applying the get_name and get_mac functions to the specified IP address, it is possible to estimate the network availability of a segment:



      #include <Winsock2.h>
      #include <iphlpapi.h>
      #include <cstdio>

      #pragma comment(lib, "iphlpapi.lib")
      #pragma comment(lib, "ws2_32.lib")

      bool get_name(unsigned char* name, char dest[32])

      struct in_addr destip;
      struct hostent* info;

      destip.s_addr = inet_addr(dest);

      info = gethostbyaddr((char*)&destip, 4, AF_INET);

      if (info != NULL)

      strcpy((char*)name, info->h_name);

      else

      return false;


      return true;


      bool get_mac(unsigned char* mac, char dest[32])

      struct in_addr destip;
      ULONG mac_address[2];
      ULONG mac_address_len = 6;

      destip.s_addr = inet_addr(dest);

      SendARP((IPAddr)destip.S_un.S_addr, 0, mac_address, &mac_address_len);

      if (mac_address_len)

      BYTE* mac_address_buffer = (BYTE*)&mac_address;
      for (int i = 0; i < (int)mac_address_len; i++)

      mac[i] = (char)mac_address_buffer[i];


      else

      return false;


      return true;


      int main()

      char address[32] = "192.168.14.101" , "192.168.14.102" ,
      "192.168.14.103" , "192.168.14.106 " ;
      WSADATA sock;

      if (WSAStartup(MAKEWORD(2, 2), &sock) != 0)

      printf("Failed to initialise winsock. (%d)n", WSAGetLastError());
      return 1;


      for (int i = 0; i < (int)sizeof(address) / 32; i++)

      unsigned char mac[6] = '' ;
      unsigned char name[100] = '' ;

      if (get_mac(mac, address[i]))

      printf("%s : %s : %.2X-%.2X-%.2X-%.2X-%.2X-%.2Xn", address[i],
      (get_name(name, address[i])) ? (char*)name : "-",
      mac[0],mac[1],mac[2],mac[3], mac[4], mac[5]);
      fflush(stdout);



      printf("nDone.n");
      fflush(stdout);
      system("PAUSE");

      return 0;



      An attempt to implement a program that scans the subnet and displays IP address, host name, and MAC of machines on the network.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Aug 3 at 8:45









      Toby Speight

      17.1k13485




      17.1k13485









      asked Aug 3 at 8:16









      Eugeny Tarasov

      42




      42




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          I won't cover any bugs in your implementation, I'll only have a look at your coding style. It looks like you were writing plain c but tagged it as c++. I think I didn't even found a single c++-only feature you made usage of.



          Don't use a string as data storage unless it's necessary



          And of course, don't use plain c-style strings, because you'll do it wrong.



          char address[32] = "192.168.14.101" , "192.168.14.102" , 
          "192.168.14.103" , "192.168.14.106 " ;


          You reserve 32 chars for your ip-address storage, but that isn't ever necessary. If you really want to store them as a string, do it as std::string, or if it's a constant string, as std::string_view(C++17). An ip-address has a upper maximum of 4 * 3 signs + 3 dot signs and additionally the zero at the end (that accumulates to a max of 16 signs). So, that means you reserved twice as much memory as you really ever need.



          Use better data type



          Why don't you simply store the ip-address as an 32bit uint; or as an std::array<std::unit8_t, 4>. You could easily create a typedef for that:
          using ipv4 = std::array<std::unit8_t, 4>
          With that typedef you'll be able to use that type everywhere you need the ipv4 address. That makes the code more readable and less error prone.



          rethink your return statements



          That looks really horrible.



          bool get_name(unsigned char* name, char dest[32])

          // ...
          if (info != NULL)

          // ...

          else

          return false;

          return true;



          You could write that in an easier way:



          bool get_name(unsigned char* name, char dest[32])

          // ...
          if (info != NULL)

          // ...
          return true;

          return false;



          use std::optional if you expect your function return a invalid return value



          In C++17 we got a new helper template class called std::optional. It's very helpfull in returning values with an invalid state. You could adjust your get_name function like this:



          std::optional<std::string> get_name(ipv4 ip)

          // ...
          if (info != NULL)

          return info.name; // pseudo-code; don't know the real interface of this info object, but hopefully you get what I try to explain

          return std::nullopt;



          That will make your code more reable and self-descriptive. All of the above is also true for your get_mac function.



          c-style cast are bad



          You used them alot; don't do that. In c++ there are plenty of cast you should use instead. For example (int)mac_address_len should be written as static_cast<int>(mac_address_len)






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200876%2fscanning-the-subnet-by-ip-range%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            I won't cover any bugs in your implementation, I'll only have a look at your coding style. It looks like you were writing plain c but tagged it as c++. I think I didn't even found a single c++-only feature you made usage of.



            Don't use a string as data storage unless it's necessary



            And of course, don't use plain c-style strings, because you'll do it wrong.



            char address[32] = "192.168.14.101" , "192.168.14.102" , 
            "192.168.14.103" , "192.168.14.106 " ;


            You reserve 32 chars for your ip-address storage, but that isn't ever necessary. If you really want to store them as a string, do it as std::string, or if it's a constant string, as std::string_view(C++17). An ip-address has a upper maximum of 4 * 3 signs + 3 dot signs and additionally the zero at the end (that accumulates to a max of 16 signs). So, that means you reserved twice as much memory as you really ever need.



            Use better data type



            Why don't you simply store the ip-address as an 32bit uint; or as an std::array<std::unit8_t, 4>. You could easily create a typedef for that:
            using ipv4 = std::array<std::unit8_t, 4>
            With that typedef you'll be able to use that type everywhere you need the ipv4 address. That makes the code more readable and less error prone.



            rethink your return statements



            That looks really horrible.



            bool get_name(unsigned char* name, char dest[32])

            // ...
            if (info != NULL)

            // ...

            else

            return false;

            return true;



            You could write that in an easier way:



            bool get_name(unsigned char* name, char dest[32])

            // ...
            if (info != NULL)

            // ...
            return true;

            return false;



            use std::optional if you expect your function return a invalid return value



            In C++17 we got a new helper template class called std::optional. It's very helpfull in returning values with an invalid state. You could adjust your get_name function like this:



            std::optional<std::string> get_name(ipv4 ip)

            // ...
            if (info != NULL)

            return info.name; // pseudo-code; don't know the real interface of this info object, but hopefully you get what I try to explain

            return std::nullopt;



            That will make your code more reable and self-descriptive. All of the above is also true for your get_mac function.



            c-style cast are bad



            You used them alot; don't do that. In c++ there are plenty of cast you should use instead. For example (int)mac_address_len should be written as static_cast<int>(mac_address_len)






            share|improve this answer



























              up vote
              2
              down vote













              I won't cover any bugs in your implementation, I'll only have a look at your coding style. It looks like you were writing plain c but tagged it as c++. I think I didn't even found a single c++-only feature you made usage of.



              Don't use a string as data storage unless it's necessary



              And of course, don't use plain c-style strings, because you'll do it wrong.



              char address[32] = "192.168.14.101" , "192.168.14.102" , 
              "192.168.14.103" , "192.168.14.106 " ;


              You reserve 32 chars for your ip-address storage, but that isn't ever necessary. If you really want to store them as a string, do it as std::string, or if it's a constant string, as std::string_view(C++17). An ip-address has a upper maximum of 4 * 3 signs + 3 dot signs and additionally the zero at the end (that accumulates to a max of 16 signs). So, that means you reserved twice as much memory as you really ever need.



              Use better data type



              Why don't you simply store the ip-address as an 32bit uint; or as an std::array<std::unit8_t, 4>. You could easily create a typedef for that:
              using ipv4 = std::array<std::unit8_t, 4>
              With that typedef you'll be able to use that type everywhere you need the ipv4 address. That makes the code more readable and less error prone.



              rethink your return statements



              That looks really horrible.



              bool get_name(unsigned char* name, char dest[32])

              // ...
              if (info != NULL)

              // ...

              else

              return false;

              return true;



              You could write that in an easier way:



              bool get_name(unsigned char* name, char dest[32])

              // ...
              if (info != NULL)

              // ...
              return true;

              return false;



              use std::optional if you expect your function return a invalid return value



              In C++17 we got a new helper template class called std::optional. It's very helpfull in returning values with an invalid state. You could adjust your get_name function like this:



              std::optional<std::string> get_name(ipv4 ip)

              // ...
              if (info != NULL)

              return info.name; // pseudo-code; don't know the real interface of this info object, but hopefully you get what I try to explain

              return std::nullopt;



              That will make your code more reable and self-descriptive. All of the above is also true for your get_mac function.



              c-style cast are bad



              You used them alot; don't do that. In c++ there are plenty of cast you should use instead. For example (int)mac_address_len should be written as static_cast<int>(mac_address_len)






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                I won't cover any bugs in your implementation, I'll only have a look at your coding style. It looks like you were writing plain c but tagged it as c++. I think I didn't even found a single c++-only feature you made usage of.



                Don't use a string as data storage unless it's necessary



                And of course, don't use plain c-style strings, because you'll do it wrong.



                char address[32] = "192.168.14.101" , "192.168.14.102" , 
                "192.168.14.103" , "192.168.14.106 " ;


                You reserve 32 chars for your ip-address storage, but that isn't ever necessary. If you really want to store them as a string, do it as std::string, or if it's a constant string, as std::string_view(C++17). An ip-address has a upper maximum of 4 * 3 signs + 3 dot signs and additionally the zero at the end (that accumulates to a max of 16 signs). So, that means you reserved twice as much memory as you really ever need.



                Use better data type



                Why don't you simply store the ip-address as an 32bit uint; or as an std::array<std::unit8_t, 4>. You could easily create a typedef for that:
                using ipv4 = std::array<std::unit8_t, 4>
                With that typedef you'll be able to use that type everywhere you need the ipv4 address. That makes the code more readable and less error prone.



                rethink your return statements



                That looks really horrible.



                bool get_name(unsigned char* name, char dest[32])

                // ...
                if (info != NULL)

                // ...

                else

                return false;

                return true;



                You could write that in an easier way:



                bool get_name(unsigned char* name, char dest[32])

                // ...
                if (info != NULL)

                // ...
                return true;

                return false;



                use std::optional if you expect your function return a invalid return value



                In C++17 we got a new helper template class called std::optional. It's very helpfull in returning values with an invalid state. You could adjust your get_name function like this:



                std::optional<std::string> get_name(ipv4 ip)

                // ...
                if (info != NULL)

                return info.name; // pseudo-code; don't know the real interface of this info object, but hopefully you get what I try to explain

                return std::nullopt;



                That will make your code more reable and self-descriptive. All of the above is also true for your get_mac function.



                c-style cast are bad



                You used them alot; don't do that. In c++ there are plenty of cast you should use instead. For example (int)mac_address_len should be written as static_cast<int>(mac_address_len)






                share|improve this answer















                I won't cover any bugs in your implementation, I'll only have a look at your coding style. It looks like you were writing plain c but tagged it as c++. I think I didn't even found a single c++-only feature you made usage of.



                Don't use a string as data storage unless it's necessary



                And of course, don't use plain c-style strings, because you'll do it wrong.



                char address[32] = "192.168.14.101" , "192.168.14.102" , 
                "192.168.14.103" , "192.168.14.106 " ;


                You reserve 32 chars for your ip-address storage, but that isn't ever necessary. If you really want to store them as a string, do it as std::string, or if it's a constant string, as std::string_view(C++17). An ip-address has a upper maximum of 4 * 3 signs + 3 dot signs and additionally the zero at the end (that accumulates to a max of 16 signs). So, that means you reserved twice as much memory as you really ever need.



                Use better data type



                Why don't you simply store the ip-address as an 32bit uint; or as an std::array<std::unit8_t, 4>. You could easily create a typedef for that:
                using ipv4 = std::array<std::unit8_t, 4>
                With that typedef you'll be able to use that type everywhere you need the ipv4 address. That makes the code more readable and less error prone.



                rethink your return statements



                That looks really horrible.



                bool get_name(unsigned char* name, char dest[32])

                // ...
                if (info != NULL)

                // ...

                else

                return false;

                return true;



                You could write that in an easier way:



                bool get_name(unsigned char* name, char dest[32])

                // ...
                if (info != NULL)

                // ...
                return true;

                return false;



                use std::optional if you expect your function return a invalid return value



                In C++17 we got a new helper template class called std::optional. It's very helpfull in returning values with an invalid state. You could adjust your get_name function like this:



                std::optional<std::string> get_name(ipv4 ip)

                // ...
                if (info != NULL)

                return info.name; // pseudo-code; don't know the real interface of this info object, but hopefully you get what I try to explain

                return std::nullopt;



                That will make your code more reable and self-descriptive. All of the above is also true for your get_mac function.



                c-style cast are bad



                You used them alot; don't do that. In c++ there are plenty of cast you should use instead. For example (int)mac_address_len should be written as static_cast<int>(mac_address_len)







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited 2 days ago


























                answered Aug 3 at 9:41









                DNK

                55129




                55129






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200876%2fscanning-the-subnet-by-ip-range%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods