Pages

Thursday, December 4, 2014

Get next Permutation - C program

Rule:

1. Find largest index i such that array[i − 1] < array[i].
2. Find largest index j such that j ≥ i and array[j] > array[i − 1].
3. Swap array[j] and array[i − 1].
4. Reverse the suffix starting at array[i].

Code:
#include "stdio.h"

#define MAX 100

void ins_sort(int *a, int n) {
    int i, j, tmp;

    for (i = 1; i < n; i++) {
        for (j = i; j > 0; j--) {
            if (a[j] < a[j-1]) {
                tmp = a[j];
                a[j] = a[j-1];
                a[j-1] = tmp;
            }
        }
    }
}

void reverse(int *begin, int *end) {
    int tmp;
    while (begin < end) {
        tmp = *begin;
        *begin = *end;
        *end = tmp;
        begin++;
        end--;
    }
}

int next_permutation(int *begin, int *end)
{
    int tmp;
    int *i, *j, *k;

    if (begin == end)
        return 0;

    /* Single element */
    i = begin;
    ++i;
    if (i == end)
        return 0;

    i = end;
    --i;

    while (1)
    {
        j = i;
        --i;

        if (*i < *j)
        {
            k = end;

            while (!(*i < *--k));

            //swap(i, k);
            tmp = *i;
            *i = *k;
            *k = tmp;

            reverse(j, --end);
            return 1;
        }

        if (i == begin)
        {
            reverse(begin, --end);
            return 0;
        }
    }
}

int main() {
    int n, a[MAX], i;

    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    ins_sort(a, n);

    while (next_permutation(a, a+n)) {
        for (i = 0; i < n; i++)
            printf("%d ", a[i]);
        printf("\n");
    }
    return 0;
}


Thursday, September 18, 2014

Breakfast Drive in BMW

It was a Wednesday morning. I saw an advertisement in my Facebook wall from zoomcar.in saying that they were renting BMW 320D discounted on weekdays. It costs 780 rupees for four hours Monday-Thursday. Only 4 hours is the minimum we can book. Immediately I asked my friends in whatsapp “BMW service at your office on just 200 rupees”. Everybody replied with a hand accepting the deal.

I booked the BMW for Monday morning 6 AM to 10 AM. So we can go to office after the drive. The rent has to be paid immediately and security deposit Rs.5000 could be delayed till the day before of the trip. Saturday morning once again I confirmed my friends and paid the security deposit.

Sunday evening we decided the meet-up points and pick-up plans. I woke-up at 4.30 and called Abi. Went to Abi’s PG at 5.30 to pick-up her. We reached Brigade Metropolis at ITPL at 5.45 where the BMW is ready. As I have made the registration, I have to be there in person to do formalities and receive the car. The guy in there was a nice guy. He marked all the scratches, showed the documents and gave the car to us.

It was the moment I lived for. I’m inside a BMW at the wheel. My Adrenaline rushed to my head. I pressed the start button. With small cough the Horse opened its eyes. Abi was next to me in the front seat. Now I realized that it was an automatic transmission and I have never drove an automatic car. I lowered the window and called guy to show me how to put gear! The guy was shocked but anyway he showed the controls.

No clutch so No rev-up. I released the break, and BMW started glide. I was two inches above the seat literally flying in between clouds. The steering was so smooth. I never felt driving a car. It was BMW! I couldn’t believe its happening for some moment.

We parked the car at the entrance of Brigade Metropolis and took some photos. Photos was the only purpose my friends accepted this deal. I called Gyan. They were at Marathahalli at the moment. After 10 minutes they came. In the mean time we took some more photos.

Abi was at the front. Smriti, Dinesh and Gyan were at back seat, Dinesh was in the centre. Fortunately none of them knew driving. So I’m the one to drive the entire 40 Kilometres (Only 40 KM were allowed for four hours). We want some good photos. So I took the car to some rural place where traffic will be less and we could stop the car for pictures. I have chosen the road in the following map.


That was around 16 Kilometres from Brigade Metropolis. We parked the car roadside and took pictures. The place and early morning light was very good for our pictures. We were there for almost an hour taking photos. The people around the place started looking differently. At last we came to an end of our photo session as we all were hungry.


We moved in the same road found a road side idly shop. Stopped a BMW and had breakfast there! Smriti said that she was not that hungry and she need only two idliys. She had four Idlys, one plate rice, three Vadais. This was our second large expense in this trip. The first one was the rent to our car. Dinesh paid for the food. The food there was good (that would be understood by Smriti’s diet). And we all had tea.


Then I taught driving to all. Dinesh was so lucky. He started his driving part of his life in an BMW. Lets his achievements grow. All drive for some 500 metres. Smriti’s drive was the fastest and Abi’s drive was the scariest! Gyan, sorry man. My mind is bad with boys’ memories. Did you actually drive the car?!

We went some far to take an U-turn. We had a railway track in between. The gates were closed. Wow! I stuck in traffic in an BMW. What an experience!



While coming back the road was full. Peak hours in Bangalore. I drove one hour the last 10 Kilometres. But that was actually fun. We lowered all the windows and played “Velai Illa Pattathaari” songs in full volume. The entire traffic had an eye on us. Especially a family with kid in a Wagon-R next to us in a signal. The wife’s stomach burning hotter than the BMW engine. And the day was an unfortunate to her Husband. Hey dude you’ll feel the heat in the night at the bed! :P

Then a girl in an Alto. It was so clear in her eyes. Jealous! Dinesh waved her. After that she never seen her right side. Not even her right mirror. :D At last our excitement came to and end. We dropped the car at 9.55 AM. And my security deposit was safe.

And at the end a selfie to complete the session. Not actually a selfie. A selfoid. Smriti took the photo and Gyan simply leaned his hand to make the photo look like a selfie. ;)


My “Breakfast drive in BMW” album in facebook got 73 likes. https://www.facebook.com/balakumaran.kannan.1/media_set?set=a.514424185360333.1073741827.100003782681809&type=3

Sunday, January 26, 2014

Linux device drivers - 3rd editions - examples

This blog entry may be helpful for people to progress the example device driver mentioned in the book.

The book is available in Internet for free to download.

The code provided here is tested in the machine having
Ubuntu-12.04 running kernel-3.2.0-23-generic.

Here the working code after finishing chapter-3
https://github.com/kumaran127/ldd3/archive/Chapter-3.tar.gz
 

Friday, May 31, 2013

Get M and O flag of most recently received Router Advertisement (Linux)

Its been two weeks after I started searching for how to get M and O flag of most recently received RA stored from Kernel. I knew it is possible because from Linux source it is clear that the flag values are stored (net/ipv6/ndisc.c in function ndisc_router_discovery). Also RFC-2462 clearly tells that any dhcp6 client should enquirer those stored flags first and act accordingly.


I tried libnl source, which comes to at top of Google results. But it didn't help very much. Because after some long traversal usually I got ended up with some function pointers. Anyway It gave some understanding of Netlink Sockets.

No ioctls, no procfs entries. Frankly speaking I didn't try ioctl. But I tried all from procfs entry. No luck! :(. Afterwards I tried to implement one procfs node that will give the flag values. But when I dig into kernel source I got some hints like the flags IFLA_PROTINFO and IFLA_INET6_FLAGS. And it was clear from the source that kernel is giving those information to user space through Netlink. [Function traversal: ndisc_router_discovery -> inet6_ifinfo_notify -> inet6_fill_ifinfo -> inet6_fill_ifla6_attrs -> nla_put_u32]


Again I started Googling with some specific keywords found in those functions. At last I got some code written by Shirley Ma, xma@us.ibm.com. Might be that is part of a library the developed. The code did exactly what I need. Yeah it reads M/O flags from kernel using Netlink sockets also some other things which I really didn't get interested.

I just extracted the necessary things. Now the code below will only print the status of M/O flag of each network interface in your machine. You can use radvd for generating RA. Enjoy!!!


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <errno.h>
#include <syslog.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/queue.h>

#define IF_RA_MANAGED 0x40
#define IF_RA_OTHERCONF 0x80

int main() {
        int sd, status;
        int seq = time(NULL);
        struct sockaddr_nl nl_addr;
        struct nlmsghdr *nlm_hdr;
        struct rtgenmsg *rt_genmsg;
        char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))];
        struct msghdr msgh;
        struct nlmsghdr *nlm;
        size_t newsize = 65536, size = 0;
        int msg_len;
        char *nbuf = NULL;
        int nlm_len;
        struct ifinfomsg *ifim;
        struct rtattr *rta, *rta1;
        size_t rtasize, rtapayload, rtasize1;
        void *rtadata;
        char if_name[10];

        sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
        if (sd < 0) {
                perror("socket");
                return -1;
        }

        memset(&nl_addr, 0, sizeof(nl_addr));
        nl_addr.nl_family = AF_NETLINK;
        if (bind(sd, (struct sockaddr *)&nl_addr, sizeof(nl_addr)) < 0) {
                perror("bind");
                return -1;
        }

        memset(&buf, 0, sizeof(buf));

        nlm_hdr = (struct nlmsghdr *)buf;
        nlm_hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*rt_genmsg));
        nlm_hdr->nlmsg_type = RTM_GETLINK;
        nlm_hdr->nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
        nlm_hdr->nlmsg_pid = getpid();
        nlm_hdr->nlmsg_seq = seq;

        memset(&nl_addr, 0, sizeof(nl_addr));
        nl_addr.nl_family = AF_NETLINK;

        rt_genmsg = (struct rtgenmsg *)NLMSG_DATA(nlm_hdr);
        rt_genmsg->rtgen_family = AF_INET6;
        status = sendto(sd, (void *)nlm_hdr, nlm_hdr->nlmsg_len, 0, (struct sockaddr *)&nl_addr, sizeof(nl_addr));
        if (status < 0) {
                perror("sendto");
                return -1;
        }

        for (;;) {
                void *newbuf = realloc(nbuf, newsize);
                if (newbuf == NULL) {
                        perror("realloc");
                        return -1;
                }

                nbuf = newbuf;

                /* If call intrupted do it again */
                do {
                        struct iovec iov = {nbuf, newsize};
                        memset(&msgh, 0, sizeof(msgh));
                        msgh.msg_name = (void *)&nl_addr;
                        msgh.msg_namelen = sizeof(nl_addr);
                        msgh.msg_iov = &iov;
                        msgh.msg_iovlen = 1;
                        msg_len = recvmsg(sd, &msgh, 0);
                }while (msg_len < 0 && errno == EINTR);

                /* If msg truncated because of less size, call it with double the size */
                if (msg_len < 0 || msgh.msg_flags & MSG_TRUNC) {
                        size = newsize;
                        newsize *= 2;
                        continue;
                } else if (msg_len == 0)
                        break;

                for (nlm = (struct nlmsghdr *)nbuf; NLMSG_OK(nlm, msg_len);
                           nlm = (struct nlmsghdr *)NLMSG_NEXT(nlm, msg_len)) {
                        if (nlm->nlmsg_type == NLMSG_DONE) {
                           printf("NLMSG_DONE\n");
                           return 0;
                        }
                        else if (nlm->nlmsg_type == NLMSG_ERROR) {
                                printf("NLMSG_ERROR\n");
                                return -1;
                        }

                        if (nlm->nlmsg_pid != getpid() || nlm->nlmsg_seq != seq)
                                continue;

                        ifim = (struct ifinfomsg *)NLMSG_DATA(nlm);
                        if (ifim->ifi_family != AF_INET6 || nlm->nlmsg_type != RTM_NEWLINK) {
                                printf("Not AF_INET6 or RTM_NEWLINK request\n");
                                return -1;
                        }

                        nlm_len = msg_len;
                        rtasize = NLMSG_PAYLOAD(nlm, nlm_len) - NLMSG_ALIGN(sizeof(*ifim));
                        for (rta = (struct rtattr *) (((char *) NLMSG_DATA(nlm)) +
                                                NLMSG_ALIGN(sizeof(*ifim))); RTA_OK(rta, rtasize);
                                                rta = RTA_NEXT(rta, rtasize)) {
                                rtadata = RTA_DATA(rta);
                                rtapayload = RTA_PAYLOAD(rta);

                                switch (rta->rta_type) {
                                        case IFLA_PROTINFO:
                                                rtasize1 = rta->rta_len;
                                                for (rta1 = (struct rtattr *)rtadata; RTA_OK(rta1, rtasize1);
                                                                rta1 = RTA_NEXT(rta1, rtasize1)) {
                                                        void *rtadata1 = RTA_DATA(rta1);
                                                        switch(rta1->rta_type) {
                                                                case IFLA_INET6_FLAGS:
                                                                        if_indextoname(ifim->ifi_index, if_name);
                                                                        printf("interface: %s\n", if_name);

                                                                        if (*((u_int32_t *)rtadata1) & IF_RA_MANAGED)
                                                                                printf("\tmanaged flag is set\n");
                                                                        else 
                                                                                printf("\tmanaged flag is not set\n");
                                                                        if (*((u_int32_t *)rtadata1) & IF_RA_OTHERCONF)
                                                                                printf("\tother flag is set\n");
                                                                        else
                                                                                printf("\tother flag is not set\n");
                                                                break;
                                                        }
                                                }
                                        break;
                                }
                        }
                }
        }
}

Tuesday, March 5, 2013

Peacock

    Photo taken by my friend.


Monday, November 19, 2012

துப்பாக்கி

நெடுநாட்கள் கழித்து விசய் நடித்த திரைப்படம் ஒன்றை பார்த்தேன். அந்த அனுபவத்தை உங்களுடன் பகிர விழைகிறேன். திருமலை மற்றும் அதன் பிறகு அதே போன்று அல்லது அதே திரைப்படம் வேறு பெயர்களில் வெளிவந்த பொது அவற்றை பார்த்து மனமுடைந்து மீண்டும் தற்கொலைக்கு முயலக்கூடாது என்று சபதமெடுத்து இதுநாள் வரை விசய் நடித்த திரைப்படங்களை தவிர்த்து வந்தேன். சமீபத்தில் வெளிவந்த நண்பன் திரைப்படத்தை அது விசய் படமல்ல சங்கரது படம் என்று என் நண்பர்கள் பலமுறை தைரியம் கூறியும், அதனை இந்தியிலேயே சப்-டைட்டிலுடன் பார்த்து திருப்திகொண்டேன். அப்படிப்பட்ட நான் இந்த துப்பாக்கி படம் பார்க்க சென்றதுக்கு ஒரே காரணம் அம்மாவின் நூறாண்டு சாதனை ஆட்சி. எங்கள் வீட்டில் இன்வெர்ட்ர் இருந்தும் கேபிள் நிலையத்தில் இன்வெர்டர் இல்லாத காரணத்தால் வேறுவழியின்றி துப்பாக்கி படத்திற்கு சென்றேன்.

திரையரங்கிற்கு சென்ற எனக்கு டிக்கெட் விலை ஏனோ சிறு தடுமாற்றத்தை ஏற்படுத்தியது. ஆனாலும் பெங்களூரில் முன்னூறு ரூபாய்க்கு skyfall படம் பார்த்ததை நினைத்து சுதாரித்துக்கொண்டு டிக்கெட் வாங்கினேன். நான் உள்ளே செல்லும் பொது ஏற்கனவே படம் தொடங்கியிருந்தது. "இளைய தளபதி டாக்டர் விசய் நடிக்கும்" என்ற வார்த்தைகளின் ஒருங்கிணைந்த அழகை கண்டுரசிக்கும் பாக்கியத்தை இழந்து டிக்கெட் விலையில் பாதியில் வீணடிதிருந்தது அப்பொழுதான் புரிந்தது.


முதல் காட்சியிலேயே விசய் யாரையோ குத்திக்கொண்டு இருந்தார். தவறாக நினைக்க வேண்டாம், யாருடனோ சண்டை போட்டுக்கொண்டிருந்தார். திருப்பி அடித்தால் படத்தின் இதர காட்சிகளை படமாக்க விசய் இருக்கமாட்டார் என்ற காரணத்தினாலோ என்னவோ அந்த நபர் கடைசிவரை அடி வாங்கிக்கொண்டே இருந்தார். அந்த காட்சிகளில் விசயின் பராக்கிரமத்தை விட ஒளிப்பதிவும் கலையும் அதிகம் பேசியிருந்தன. மற்றும் எ.ஆர்.முருகதாஸ் என்ற பெயர் திரைகதை  மீது நம்பிக்கை ஏற்படுத்தியது. ஆதலால் நான் பயந்த ஒரே விசயம் விசய் தான்.

ஆரம்ப பெண்பார்க்கும் காட்சிகளில் விசய் சற்று ஓவர் ஆக்டிங் செய்திருந்தாலும் காஜல் அகர்வால் மேலும் அதிகமாக நடித்து விசய்க்கு மதிப்பெண் வாங்கிக்கொடுத்திருக்கிறார். காஜல் அகர்வாலிடம் இருப்பது ஒன்றே ஒன்றுதான். அந்த ஒன்றும்  ஒன்றில் இரண்டானது. புரிந்திருக்கும் என்று நினைக்கிறேன். அதனை தேவையான அளவிற்கு பயன்படுத்தியிருந்தார் அல்லது காட்டியிருக்கிறார் என்று கூறலாம். ஒளிப்பதிவாளரும் அதற்க்கு மிகவும் துணைபுரிந்திருந்தார். மற்றபடி காஜலுக்கு வேறு சிறப்பான பணிகள் எதுவும் வழங்கப்படவில்லை.

நண்பன் படத்தில் கலக்கிய சத்யனுக்கு இந்த படத்தில் முக்கியத்துவம் சற்று குறைவாகவே இருந்தது. விசய்க்கு அதிக ரியாக்சன்கள் வராது என்ற காரணத்தினால், விசயின் முக பாவங்களை சத்யன் முகத்தை கடன் வாங்கி காட்டியிருந்தார் இயக்குனர். துப்பாக்கி படத்தில் காவல் உதவி ஆய்வாளராக வரும் சத்தியனுக்கு கடைசி வரை ஒருமுறை கூட துப்பாக்கி கொடுக்கப்படவில்லை என்பது கண்டனத்திற்குரியது. மேலும் மும்பையில் இருந்தாலும் தமிழ் போலீஸ் என்றால் தொப்பை இருக்கும் என்பதை சத்யன் மூலமாக இயக்குனர் தைரியமாக எடுத்துரைக்கிறார்.

கிளைமாக்ஸ் காட்சிகளை கில்லி படத்திலிருந்து உருவியிருப்பது வெட்ட வெளிச்சமாக தெரிகிறது. கிளைமாக்ஸ் காட்சிகளை மாற்றும்பொழுது 'காரசிங்கம் A/C' திரையரங்கில் மிகுந்த வரவேற்பை பெறகூடும். ஆனால் இந்த முறை வில்லனை உசுப்பேற்றுவதற்கு கதாநாயகி பயன்படுத்தப்படவில்லை. கதாநாயகி ரசிகர்களை உசுப்பேற்ற மட்டுமே பயன்பட்டுள்ளர் என்பது வேறு விஷயம். கில்லி படத்தை ஒப்பிடுகையில் துப்பாக்கியில் விசய் நுடவைதியத்தில் சற்று தேர்ச்சிபெற்று தெரிகிறார். டாக்டர் பட்டம் பெற்ற பிறகு ஏற்பட்ட அனுபவத்தின் காரணமாக இருக்கலாம்.

மட்ட்றபடி லோடு ஆன துப்பாக்கியை மீண்டும் மீண்டும் லோடு செய்வதால் துப்பாக்கிக்கு ஸ்டார்டிங் டிரபுள், ஞாபக மறதி போன்ற வியாதிகள் இருக்குமோ என்று சந்தேகிக்க தோன்றுகிறது. டி.ஐ.எ, சிலீப்பர் செல்ஸ், பிளாஸ்டிக் எக்ஸ்புளோசிவ், மைக்ரோசிப் போன்ற வார்த்தைகளை அதிகமாக பிரயோகப்படுதியிருப்பது viva எக்சாம்களை ஞாபப்படுதுகிறது. பன்ச் டயலாக் வேண்டும் என்று விசய் அடம்பிடித்து கேட்டதால் கொடுக்கப்பட்ட "ஆயிரம் பேரை கொல்ல நினைக்கும்..." என்ற வசனம் கைதட்டல்களுக்கு உரித்தானது.

கடைசிவரை இங்கு ஜெயராம் பற்றி பேசவே தோன்றவில்லை. படத்தில் அவருடைய பகுதியும் அவ்வாறே இருந்தது.

இறுதியில் இந்த விமர்சனத்தை எழுதும் அளவிற்கு நான் நல்ல மனநிலையில் இருப்பதால் மற்ற விசய் படங்களை விட துப்பாக்கி சிறப்பாகவே இருக்கிறது என்ற முடிவுக்கு வரவேண்டியுள்ளது.

நாள்: 17-Nov-2012
இடம்: பட்டுக்கோட்டை அன்னபூர்ணா திரையரங்கம்

Wednesday, September 5, 2012

GUI developed for B's sudoku

     Earlier in the previous month I have written a post about simple brute algorithm for sudoku. Even that was a working one, using that will be little tedious. Because you have to enter elements one by one. And if it went wrong somewhere, you have to start from the beginning. So I made a gui for that. It is a fully functional implementation. But it is not up to the level of a commercial application.

     Download the application here. Or download the source here and build it yourself.

Custom Kernel with CentOS

     Custom kernel in CentoOS is highly unrecommended by the CentOS team. I don't know the reason. They may want you to rely on them all the day or may be because of some security issues. But Linux is not an End User Operation System. And Linux users (can be called as Hackers sometimes) don't always following rules and recommendations. Sometimes won't even read them.

     I am using CentOS-5.8, which is running Linux-2.6.18 kernel. I want to install 2.6.35 kernel in that. I downloaded the source and did make menuconfig, make and make install. Everything went well and entry has been added in /boot/grub/menu.lst. But when I was booting with that kernel, it showed an error mount: could not find filesystem like this. The solution is very simple. Just enable CONFIG_SYSFS_DEPRECATED_V2.

#make menuconfig
Set "General Setup —> enable deprecated sysfs features". Or after doing make menuconfig, open .config file and grep for CONFIG_SYSFS_DEPRECATED_V2. Then replace that line with CONFIG_SYSFS_DEPRECATED_V2=y. And now build the kernel as usual.

#make
#make modules
#make modules_install
#make install
 Reboot with the newly installed kernel.

Friday, August 31, 2012

DNS setup in BSD machine

     Earlier I have written a blog about setting up DNS server in CentOS. You can find that here. And today I was given a task to do the same in BSD machine. wtf! Even BSD is an UNIX variant, it is not Linux. So the problem it will not have much different but a little. We have find that little one. Like earlier, I didn't find any simple method to setup dns.

     The concept is same as we did in Linux. Need to install bind. Then edit named.conf. And write a database file. I am not going to explain every steps. So just spend some 10 minutes with the page I mentioned above. Of course 10 minutes is enough to finish that article.

Install bind:
     If you have connected your BSD machine with Internet, It will be much easy. I didn't connected. If you have connected,
#cd /usr/ports/dns/bind94/
#make configure
select the options you want in the menu shown. Ans click ok. Thats all you have done.

     In case you are not having Internet like me(poor guy right?), download bind-9.4-ESV.tar.gz and bind-9.4-ESV.tar.gz.asc and copy that to your BSD machine on the location /usr/ports/distfiles. Now run the above two commands. Thats all. You thought of more complex steps. You are not supposed to expect complex things from this blog. Because basically I am a somberi.

Edit named.config
     Just copy and paste the following in the file /etc/namedb/named.conf

zone "example.org" {
     type master;
     file "/etc/namedb/master/example.org.db";
};
    Change the name and file path as per your requirement. Atleast read that above mentioned article now. You can have a little understanding of this file. Anyway you wish :@

Edit database file
       Copy and paste the following /etc/namedb/master/example.org.db"
$TTL    3600
example.org.  IN  SOA  example.org  hostmaster.example.org. ( 
    200
      112000
      15000
      312000
      22000 )

example.org.  IN  NS  ns1.example.org.

ns1.example.org.  IN  A  10.0.0.1

example.org.  IN  A  10.10.10.10
one.example.org.  IN  A  5.3.3.3
six.example.org.  IN  AAAA  3fff::e
six.example.org.  IN  AAAA  fe80::10

Alter this as per you requirement. Now you have no chance. You should read that article if you want to really alter anything here.

Some touch things and start the engine
     Now open /etc/rc.conf and the line named_enable="YES" at the end.


     Set your DNS server to the local host. Open /etc/resolv.conf and add nameserver 127.0.0.1. Or the IP may be any IP you assigned to your machine. It also can be an IPv6 address.
#echo "nameserver <ip_addr>" >/etc/resolv.conf
Okey. Steps are over. start it.
#service named start
 Whatever error it throws, don't care. Just check it by doing a look-up. You want to know the command to do a look-up? Then I am so rude I won't help you.

Troubleshooting
     If it is not working, try after rebooting the machine.
     If you make any change in the configuration file or the database file, you need to restart the dns server. you can simply use #service named restart. But in my case, it doesn't work. So #killall named and once again start the dns server with #service named start
 
    And you really want more and you have time and patience, read this. I referred this page to set up DNS server in my machine and write this article ;). I said I referred not read!

Thursday, August 23, 2012

IPv6 network commands for Free BSD

I am using a Free BSD machine in my office to run some test scripts. BSD is similar to Linux but not Linux. The commands to use in BSD interface is little different from commands being used in Linux. Many times I spent hours just find the correct syntax. So here I have given some commands' syntax.


Add IPv6 address
syntax: ifconfig <interface_name> inet6 <ipv6_address>/<prefix_len>
example: ifconfig vr0 inet6 3fff::2/64
des: Assigning ipv6 address 3fff::2 with prefix length 64 to the interface vr0

Delete IPv6 address
syntax: ifconfig <interface_name> inet6 <ipv6_address>/<prefix_len> delete
example: ifconfig vr0 inet6 3fff::2/64 delete
des: Removing ipv6 address 3fff::2 with prefix length 64 from the interface vr0

Add route in routing table
syntax: route add -inet6 <subnet> <gateway>
example: route add -inet6 ::/0 fe80::2
des: Adding default route to gateway fe80::2

Remove route from routing table
syntax: route del -inet6 <subnet> <gateway>
example: route del -inet6 ::/0 fe80::2
des: Removing default route to gateway fe80::2 from routing table

Show routing table
#netstat -nr

IPv6 address look-up (nslookup for ipv6)
syntax: nslookup -q=AAAA <domain_name>
example: nslookup -q=AAAA six.example.org
des: look up for ipv6 address to the domain six.example.org

ping ipv6 address
syntax: ping6 [-I <interface>] [-c <count>] <ipv6_address or domain having ipv6_address>
example: ping6 -I vr0 fe80::4
                 ping6 -I vr0 -c 4 six.example.org
des: first line pings to the ipv6 address fe80::4 via the interface vr0. Second line pings to the domain six.example.org which is having at-least an ipv6 address via interface vr0 four times. The second line first does DNS lookup for the name six.example.org

Wednesday, August 22, 2012

simple ipv4 and ipv6 dns server setup in linux (centos)

Here I have explained the steps to setup a simple dns server for some small experiments. My operation system is CentOS. But the configuration files will be same in other operating systems also. I used bind for dns deamon. There are other options too. But bind is popular and fully documented.
  • Install bind (#yum install bind). In some case bind may be already installed by default (in my machine it is already installed).
  • The configuration file, named.conf should be placed in /etc/. If bind-chroot is installed, the configuration file should be placed in /var/named/chroot/etc/.
  • /var/named will be bind's working directory. If bind-chroot is installed /var/named/chroot will be the working directory. (place .db files in corresponding directory)
 /etc/named.conf (/var/named/chroot/etc/named.conf incase bind-chroot)
zone "example.org" {
        type master;
        file "example.org.db";
};
/etc/named/example.org.db (var/named/chroot/example.org.db incase bind-chroot)
 @       IN      SOA     example.org.    hostmaster.example.org. (
                     2008051200      ; serial
                     1d12h                ; refresh
                     15M                   ; update
                     3W12h               ; expiry
                     2h20M   )          ; minimum

  @       IN     NS      ns1.example.org.

  one     IN     A        56.89.90.2
  @       IN     A        89.89.89.89
  six     IN     AAAA     fe80::2
  six     IN     AAAA     3fff::2
  six     IN     AAAA     fec0::15
  • Replace example.org with the name you want to use.
  • In example.org.db file, the line "one IN A 56.89.90.2" matches the name one.example.org with the IPv4 address 56.89.90.2
  • The line "@ IN A 89.89.89.89" matches the name example.org('@' refers current domain) with ipv4 address 89.89.89.89
  • And the last three lines adds three AAAA IPv6 records for the name six.example.org.
Configure dns server ip address to local machine's address. Now restart the named demon (bind dns server demon)
# service named restart
Check the functioning of dns server by issuing nslookup command.

  • Resolve one.example.org
# nslookup one.example.org
Server:         10.5.1.1
Address:        10.5.1.1#53

Name:   one.example.org
Address: 56.89.90.2

  • Resolve example.org
# nslookup example.org
Server:         10.5.1.1
Address:        10.5.1.1#53

Name:   example.org
Address: 89.89.89.89

  • Reslove six.example.org (IPv6)
# nslookup
> set q=AAAA
> six.example.org
six.example.org has AAAA address fec0::15
six.example.org has AAAA address 3fff::2
six.example.org has AAAA address fe80::2
 If you want better understanding please visit http://www.centos.org/docs/5/html/Deployment_Guide-en-US/ch-bind.html

Monday, August 13, 2012

tahi tests RFC 3315 test cases 13 14 15 16 17 18 failure

     These test cases tests the 'DHCP Unique IDentifier' (DUID) sent in the solicitation message. This DUID is defined in RFC3315 section 9. There are three types of DUIDs (DUID-LLT, DUID-EN, DUID-LL). The device is free to choose its DUID format. Among the six test cases only two are supported by any kind of device. So other four will be always 'not yet supported'. You have choose which DUID should be used for your device. Find your device's DUID and set this in the 'config.pl' file.

     To find the type of DUID used by your device, capture DHCPv6 solicitation message sent by your device in wireshark. In that expand the DHCPv6 payload. There will be a section 'client identifier'. In that 'option type' and 'option length' are generic. Leave them and compare other elements with the DUID definitions given in RFC 3315. You will identify the type.

Sunday, August 12, 2012

very simple brute force sudoku solve program in C

     In my college first year, I heard Sivakumar telling about his friend's brother who created a c program to solve sudoku. At that time I was thinking it was very hard and that guy who created that program is very intelligent. Because I never solved even a single sudoku puzzle. Sometimes I sat for hours and it will go wrong in at last.

     Till yesterday I never thought that I am able write a program to solve a sudoku puzzle. But this bike accident made me to sit alone in room and I had nothing to do. Siva came into mind as well as the puzzle program. I just gave a thought. Viola the concept is very simple. Brute-force! the all time success methodology will solve this puzz in minutes even in worst case.

    Here I have attached my code which is less than 200 lines.

    Also read the code here. And please update me if it is having any issues.

sudoku.h
#ifndef SUDOKU_H
#define SUDOKU_H 1
#define ZERO 0
#define MAX 9
#define PASS 0
#define FAIL 1
#define FIXED 0
#define WORKING 1
#define TOWORK 2
typedef struct element {
    int val;
    int status;
} element;
extern element elm[MAX][MAX];
void get_input();
int next_elm();
int check(int, int);
void show();
#endif

main.c
#include <stdio.h>
#include "sudoku.h"
element elm[MAX][MAX];
int main() {
    printf("SUDOKU PROGRAM BY theB\n");
    get_input();
    int ret = next_elm();
    if (ret == PASS) {
        show();
    }
    else {
        printf("INPUT ERROR\n");
    }
    return 0;
}

get_input.c
#include <stdio.h>
#include "sudoku.h"
void get_input() {
    int i, j;
    for (i = 0; i < MAX ; i++) {
        for (j = 0; j < MAX; j++) {
            printf("ENTER elm[%d][%d]: ", i, j);
            scanf("%d", &elm[i][j].val);

            if (elm[i][j].val)
                elm[i][j].status = FIXED;
            else
                elm[i][j].status = TOWORK;
        }
    }
}

check.c
#include <stdio.h>
#include "sudoku.h"
int check(int x, int y) {
    int i, j;
    //CHECK HORIZONTAL
    for (i = 0; i < MAX; i++) {
        if ((elm[x][y].val == elm[i][y].val) && (x != i))
            return FAIL;
    }
    //CHECK VERTICAL
    for (i = 0; i < MAX; i++) {
        if ((elm[x][y].val == elm[x][i].val) && (y != i))
            return FAIL;
    }
    //CHECK BOXES
    int startx = (x / 3) * 3;
    int starty = (y / 3) * 3;
    for (i = startx; i < startx + 3; i++) {
        for (j = starty; j < starty + 3; j++) {
            if ((elm[x][y].val == elm[i][j].val) && (x != i) && (y != j)) {
                return FAIL;
            }
        }
    }
    return PASS;
}

next_elm.c
#include <stdio.h>
#include "sudoku.h"
int next_elm() {
    int i, j, k;
    for (i = 0; i < MAX; i++) {
        for (j = 0; j < MAX; j++) {
            if (elm[i][j].status == TOWORK)
                goto L;
        }
    }
    return PASS;
    L:
    for (k = 1; k <= MAX; k++) {
        elm[i][j].val = k;
        elm[i][j].status = WORKING;
        int ret = check(i, j);
        if (ret == FAIL)
            continue;
        else if (ret == PASS) {
            ret = next_elm();
            if (ret == PASS)
                return PASS;
            else
                continue;
        }
    }
    elm[i][j].val = 0;
    elm[i][j].status = TOWORK;
    return FAIL;
}