Querying for an ethernet address in the arp cache

Just a quick note for myself, and for anybody looking to do the
same. If you have an IP address and you want the corresponding MAC,
using the ARP cache, here is the function:

#include <stdio.h>
#include <sys/types.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>

void print_eth_addr(const char* ip)
     int s;
     struct sockaddr_in *sin;
     struct arpreq a;
     struct in_addr addr;

     s = socket(AF_INET, SOCK_DGRAM,0);

     memset(&a, 0, sizeof(a));
     strcpy(a.arp_dev, "wlan0");
     sin = (struct sockaddr_in*)&(a.arp_pa);
     sin->sin_family = AF_INET;
     a.arp_flags = ATF_PUBL;

     inet_aton(ip, &addr);

     memcpy(&sin->sin_addr, &addr, sizeof(struct in_addr));

     if (ioctl(s,SIOCGARP,&a)) {
     unsigned char* hw_addr = (unsigned char *) a.arp_ha.sa_data;
     printf("HWAddr found : %x:%x:%x:%x:%x:%x\n", hw_addr[0],
        hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);


Just change “wlan0″ for “eth0″ or your favorite network interface.

Yasnippet for C style comments

Are you using yasnippet right?
No? Well, in a nutshell is an awesome template system for Emacs, I
really recommend it. The template language is very easy to learn and
there are templates for a lot of programming languages.

Yasnippet stores your snippets in the filesystem, a single snippet per
file and inside a directory structure that mirrors the major mode
hierarchy. For example, the snippet for the using namespace std C++
idiom is stored in a file named using, and it’s located in the
directory snippets/text-mode/cc-mode/c++-mode. The file name (in
this example using) is used as the key for the expansion. You know,
convention over configuration .

Because yasnippets uses filenames as keys, you face filesystem’s
naming restrictions, and this is a problem if you want to define a
snippet for C comments, you can’t name a file /* for obvious
reasons. What do you do then? Well, yasnippet allows you to define
your snippets in elisp code directly, so if you want to create your C
sytle comments template, you can do something like this

  ;; $0 defines where the cursor will be left after the expansion
  (yas/define 'cc-mode "/*" "/* $0 */") 
  (yas/define 'cc-mode "/**" "/**\n * $0\n **/")

The first argument is the major mode where this snippet will be available, the second is the key you will use to expand the template, and the last argument is the template itself.

For more about writting snippets for this system, go here