cstringaircrack-ng

Preceding zeros are ignored


I am modifying airodump-ng to build a custom application.

I need the output in this format

{AP Mac 1, Station Mac 1},{AP Mac 2, Station Mac 2},...............

To do this I traverse through struct ST_INFO and using multiple strcat calls I generate an array in the above format.

The problem arises when the MAC address contains preceding zeros and this results in data corruption

eg: 0A1B23443311 is saved as A1B23443311
eg: 001B3311ff22 is saved as   1B3311ff22 ( The 0s have been ignored)

What should I do so that data is saved properly when MAC address contains preceding zeros?

The final array is written to a file.

Update: Printing leading 0's in C?

When I tried to print the MAC address the results were the same as given in the above examples but when I used %02x (I learned about it from above link) the problem was solved when I want to print.

Since, I want to save the contents to an array, is there any trick like the %02x for printf.

The struct ST_INFO contains unsigned char st_mac[6] (MAC address is stored in hex format) and my final array is also unsigned char array.


Solution

  • There are multiple ways to do, but if you're using snprintf() or one of its relatives, the %02x (or maybe %02X, or %.2x or %.2X) formats will be useful. For example:

    const unsigned char *st_mac = st_info_struct.st_mac;
    unsigned char  buffer[13];
    
    for (int i = 0; i < 6; i++)
        sprintf(&buffer[2*i], "%.2X", st_mac[i]);
    

    (Usually, using snprintf() is a good idea; here, it is unnecessary overkill, though it would not be wrong.)

    You should not be using multiple strcat() calls to build up the string. That leads to a quadratic algorithm. If the strings are long (say kilobytes or more), this begins to matter. You also shouldn't be using strcat() because you need to know how long everything is (the string you've created so far, and the string you're adding to it) so that you can ensure you don't overflow your storage space.