Welcome

Wheresoever you go, go with all your heart. (Confucius)

5/14/2013

Getting MAC address on the linux by using C++


After getting a list of network interfaces,
This code will print MAC address per each interface except loopback.

#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#define MAC_STRING_LENGTH 13

int main()
{
    char mac_address[MAC_STRING_LENGTH];

    struct ifreq ifr;
    struct ifconf ifc;
    char buf[1024];
    int success = 0;

    int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock == -1) { return -1;}

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
    {
        std::cout << "error" << std::endl;
        return 0;
    }

    struct ifreq* it = ifc.ifc_req;
    const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));

    for (; it != end; ++it) {
        std::cout << it->ifr_name << std::endl;

        strcpy(ifr.ifr_name, it->ifr_name);
        if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
            if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
                if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {

                    for (int i = 0; i < 6; ++i)
                        snprintf(mac_address+i*2,MAC_STRING_LENGTH-i*2,"%02x",(unsigned char) ifr.ifr_addr.sa_data[i]);
                        std::cout << mac_address << std::endl;

                }
            }
        }
        else {
            std::cout << "error" << std::endl;
            return 0;
        }
    }
}

2/09/2013

vncserver on Debian

vncserver allows you to access the remote desktop where the operating system is Windows or Linux. This article will describe how to set up vncserver on linux and use vncviewer. Especially I have tested on Debian Squeeze and used tightvncserver which is one of vnc servers.

[Server Side]

First of all, you need to set up vncserver on linux.
1. installation of vncserver
   # apt-get install tightvncserver
   
   After installing vncserver, you need to configure several options.

   # tightvncserver :1
   # tightvncserver -kill :11
   ( This command will initialize the default options and kill the vncserver process)

   By editing the following file, set up options.
   # vi ~/.vnc/xstartup
       xsetroot -solid grey
       x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
       gnome-session &

   "gnome-session &" option supports the desktop based on GNOME.
   Finally, you can start vncserver like this.

   # vncserver :1 -geometry 1920x1200 -depth 24 -dpi 95

   ":1" is the display number, "-geometry 1920x1200" is the display resolution, "-depth 24" is the color depth, and "-dpi 95" is the font size. Usually the desktop accessed by vncviewer has the small font size. So, you need to reset the font size on the server by "-dpi" option.


[Client Side]


To access the vnc server, you can use TightVNC client which is a free remote control software package.



Input a remote host in the edit box "Remote Host:" like "xxx.xxx.xxx:1".
After an IP information, you should attach a display number which has already been configured when starting vncserver.



[Share the desktop]

Here is another tip.
If you want to share the same desktop view between the linux server and the client,you have to use "x11vnc" command.

On the server side, type the following command.

$ x11vnc -display :0

After that, type a remote host in TightVNC client.
Only enter IP or host name.