Help with program C get Mac Addres

0

Hi, I would like to know if you could help me with the following error that the program throws at me, it consists of three codes which are:

Lic2.h - > which is the header

#ifndef _LIC2_H_
#define _LIC2_H_

#define LIC_OK                       0
#define LIC_NO_ADAPT_NAME_PROVIDED  -1
#define LIC_INVALID_ADAPTER         -2

#ifdef __cplusplus
extern "C" {
#endif

int licGetMacAddress(char * adapter, unsigned char mAddress[6]);

#ifdef __cplusplus
}
#endif

#endif /* LIC2_H_ */

Lic2.c- > which contains the functions to use

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "Lic2.h"

int licGetMacAddress(char * adapter, unsigned char mAddress[6]) {
int x, i;
int fd;
struct ifreq ifr;
char direcMac[20];

/* Initialices output variable */
memset(mAddress, 0, mAddress);

/* Validates adapter name */
if (!adapter)
  return LIC_NO_ADAPT_NAME_PROVIDED;
if (!strlen(adapter))
  return LIC_NO_ADAPT_NAME_PROVIDED;

 /* Gets adapter's macaddress */
 fd = socket(AF_INET, SOCK_DGRAM, 0);
 ifr.ifr_addr.sa_family = AF_INET;
 strncpy(ifr.ifr_name , adapter , IFNAMSIZ-1);
 ioctl(fd, SIOCGIFHWADDR, &ifr);
 close(fd);
 memcpy(mAddress, ifr.ifr_hwaddr.sa_data, 6);

 /* Verifies if adapter doesn't exist */
 x = 0;
 for (i = 0; i < 6; i++)
  if (!mAddress[i]) x++;
  if (x == 6)
  return LIC_INVALID_ADAPTER;

  return LIC_OK; 
   }

example.c - > that contains the main method to execute the functions

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Lic2.h"

int main () {
int status, i;
unsigned char mAddress[6];

status=licGetMacAddress("eth0", mAddress);
if (status!=LIC_OK) {
  printf("Error al obtener el Mac Address: %d \n", status);
} else {
  printf("Mac Address: ");
  for (i=0; i<6; i++)
    printf("%02X", mAddress[i]);
  printf("\n");
}
return status;
}

When executing it, of course, it is in linux with

 gcc lic2.c ejemplo.c -o getId

I get the following error:

Lic2.c: In function ‘licGetMacAddress’:
Lic2.c:17:24: warning: passing argument 3 of ‘memset’ makes integer from pointer without a cast
memset(mAddress, 0, mAddress);
                    ^
In file included from Lic2.c:3:0:
/usr/include/string.h:66:14: note: expected ‘size_t’ but argument is of type   ‘unsigned char *’
extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
    
asked by Edú Arias 16.11.2016 в 17:12
source

2 answers

2

The signature of memset is as follows:

void * memset ( void * ptr, int value, size_t num );

And you're making the next call:

memset(mAddress, 0, mAddress);

That is, the third parameter, which is supposed to indicate the number of elements to initialize, is actually receiving a pointer ...

The correct call should be:

memset(mAddress, 0, 6);

Taking advantage of the occasion, note that this number (the 6 ) must be exactly the same as the number that appears in the creation of the variable:

unsigned char mAddress[6];

These types of values should not be included directly in the code because they are a problem:

  • In some parts of the code it will not be obvious to guess why a literal is used instead of a different one
  • If the literal changes in a part of the code you will have to manually modify that literal in the whole code, which can be a complex task.

To avoid this problem, get used to using #define to assign an alias to the literal:

#define MAX_ELEMS 6

int main()
{
  unsigned char mAddress[MAX_ELEMS];

  // opción 1 (válida para cualquier tipo de arreglos)
  memset(mAddress, 0, MAX_ELEMS * sizeof(unsigned char));

  // opción 2 (válida unicamente para arreglos en la pila)
  memset(mAddress, 0, sizeof(mAddress));

If it turns out that you have to modify the literal, with a single change you will get all the code readapted when recompiling.

    
answered by 16.11.2016 / 17:19
source
1

memset expects the size of the buffer as the third parameter.

you should use:

memset(mAddress, 0, sizeof(mAddress));

This will leave all the bytes of the array at 0.

    
answered by 16.11.2016 в 17:21