Ady Wicaksono Daily Activities

Archive for November 8th, 2007

Detecting MAC Address using C application

with 9 comments

I got this code long time ago from somewhere (off course from internet), using this C code it’s now simpler for you to create application that needs to detect MAC address.

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef Linux
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if.h>
#endif

#ifdef HPUX
#include <netio.h>
#endif

#ifdef AIX
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
#endif

long mac_addr_sys ( u_char *addr)
{
/* implementation for Linux */
#ifdef Linux
    struct ifreq ifr;
    struct ifreq *IFR;
    struct ifconf ifc;
    char buf[1024];
    int s, i;
    int ok = 0;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s==-1) {
        return -1;
    }

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    ioctl(s, SIOCGIFCONF, &ifc);

    IFR = ifc.ifc_req;
    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {

        strcpy(ifr.ifr_name, IFR->ifr_name);
        if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
            if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
                if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
                    ok = 1;
                    break;
                }
            }
        }
    }

    close(s);
    if (ok) {
        bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);
    }
    else {
        return -1;
    }
    return 0;
#endif

/* implementation for HP-UX */
#ifdef HPUX

#define LAN_DEV0 "/dev/lan0"

    intfd;
    struct fisiocnt_block;
    inti;
    charnet_buf[sizeof(LAN_DEV0)+1];
    char*p;

    (void)sprintf(net_buf, "%s", LAN_DEV0);
    p = net_buf + strlen(net_buf) - 1;

    /*
     * Get 802.3 address from card by opening the driver and interrogating it.
     */
    for (i = 0; i < 10; i++, (*p)++) {
        if ((fd = open (net_buf, O_RDONLY)) != -1) {
iocnt_block.reqtype = LOCAL_ADDRESS;
ioctl (fd, NETSTAT, &iocnt_block);
close (fd);

            if (iocnt_block.vtype == 6)
                break;
        }
    }

    if (fd == -1 || iocnt_block.vtype != 6) {
        return -1;
    }

bcopy( &iocnt_block.value.s[0], addr, 6);
return 0;

#endif /* HPUX */

/* implementation for AIX */
#ifdef AIX

    int size;
    struct kinfo_ndd *nddp;

    size = getkerninfo(KINFO_NDD, 0, 0, 0);
    if (size <= 0) {
        return -1;
    }
    nddp = (struct kinfo_ndd *)malloc(size);

    if (!nddp) {
        return -1;
    }
    if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
        free(nddp);
        return -1;
    }
    bcopy(nddp->ndd_addr, addr, 6);
    free(nddp);
    return 0;
#endif

/* Not implemented platforms */
return -1;
}

/***********************************************************************/
/*
 * Main (only for testing)
 */
#ifdef MAIN
int main( int argc, char **argv)
{
    long stat;
    int i;
    u_char addr[6];

    stat = mac_addr_sys( addr);
    if (0 == stat) {
        printf( "MAC address = ");
        for (i=0; i<6; ++i) {
            printf("%2.2x", addr[i]);
        }
        printf( "\n");
    }
    else {
        fprintf( stderr, "can't get MAC address\n");
        exit( 1);
    }
    return 0;
}
#endif

E.g you want to use on Linux, save it as file.c simply compile like this:

   gcc -O2 -DMAIN -DLinux file.c -o file

And run it

./file
MAC address = 0008c7e9e386

Compare with /sbin/ifconfig result:

/sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:08:C7:E9:E3:86
          inet addr:***.***.***.***  Bcast:***.***.***.***  Mask:255.255.255.0
          inet6 addr: ********************* Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:265555947 errors:0 dropped:0 overruns:0 frame:0
          TX packets:50507373 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4090310752 (3.8 GiB)  TX bytes:1085018636 (1.0 GiB)

It’s the same :)

Written by adywicaksono

November 8, 2007 at 9:29 am

Posted in C/C++, Linux

How much memory space we could allocate for single process in linux?

without comments

Try this C code

#include <stdio.h>
#include <stdlib.h>

int main(){
	size_t siz = 100 * 1024 * 1024 ;
	size_t idx = 1 ;
	void *ptr;

	for (;;){
		ptr = malloc ( siz * idx );
		if(!ptr)
			break ;
		free(ptr);
		idx++;
	}
	printf ("Max malloc %d * 100 MB \n", idx - 1 );
	return (0);
}

On my linux 2.4 the limitation is 2000 MB ~ 2 G
On my linux 2.6 the limitation is 2800 MB ~ 2.8 G
No matter how big your RAM is, 1 process could only occupied 2.8 G on kernel 2.6 (in my case I have 4 Gbytes), next I will share you how to reconfigure the kernel to allow us use bigger memory. This is very important, especially if you run a database like MySQL that handle huge transaction and data, but your mysqld process itself is limited to use 2.8 Gbytes memory, you buy more memory but impact nothing :)

$ free
             total       used       free     shared    buffers     cached
Mem:       4149288    4026472     122816          0     108004    1277016
-/+ buffers/cache:    2641452    1507836
Swap:      4192924        836    4192088

$ uname -a
Linux x 2.6.11-1.1369_FC4smp #1 SMP Thu Jun 2 23:08:39 EDT 2005 i686 i686 i386 GNU/Linux

$ ./a.out
Max malloc 28 * 100 MB

Written by adywicaksono

November 8, 2007 at 5:25 am

Posted in Linux