Saturday, December 10, 2011

Use of '!!' (double negation) in C programming

In some of the 'C' codes, I saw use of '!!' in expressions. I was just wondering about use of such a redundant operation (since '!!' negated the negation).
In one of the mails in LKML (Linux Kernel Mailing List), I came to know about its significance. The '!!' is used to convert an expression to authentic boolean value.

For example:

int a = 0xFF;
if (!!(a & 0x0F))
    //do something
else
    //do some other thing

Here (a & 0x0F) resolves to '0x0F' while !!(a & 0x0F) resolves to '1'.
That means use of '!!' results in authentic boolean value i.e. either '0' or '1'.

We can see the same kind of expressions in linux kernel code.

#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)

In the first case, the expression has to evaluate to any value other than '0' for likely condition to happen. However, may be to reduce side effects or maintain consistency, the precaution would have been taken and expression is converted to authentic boolean value.

It seems redundant for the unlikely condition though.

Please share if you have any alternate thoughts.

Tuesday, December 6, 2011

Setting up wireless router in opensuse linux 12.1 box

It is just continuation of what I scribbled sometimes back regarding 11.4. The steps and interfaces are still the same.

However there is good news. You need not add routes manually. Everything is automated now. Good work by SUSE team. I tried with openSUSE 12.1 x86_64. When the wlan interface comes up, the routes get automatically added with wireless router IP as default gateway. May be I feel it was bug in openSUSE 11.4 however everything is fine now.

Monday, October 17, 2011

Importance of 'typedef'ing

Some people ask me why do we need to typedef some datatypes. This is basically to maintain portability across compilers.

We put all typedefs in a single header file. When porting across compilers if any datatype related problem arrives, it is required to change only the header file. So we have single place changes rather than refactoring the entire code which would have been cumbersome.

For ex:

Assume a legacy code like this. In "C" we did not have bool until C99. The older code may look like this

typedef enum __BOOL__ { FALSE, TRUE }BOOL;

and wherever we need to use it we apply as "BOOL var = TRUE" etc..

gcc allows _Bool by default nowadays. So above code can be easily ported to _Bool. Here it goes.

typedef _Bool BOOL;
#define FALSE 0
#define TRUE 1

Bingo!! In a single place we did the modification and entire modification has same effect as earlier. The modification applies to all places without any changes to existing BOOL types.

Note that without 'typedef'ing enum __BOOL__ to BOOL, it would have been difficult to make changes at one shot (because of enum __BOOL__ declaration).

There may be lot other advantages. If someone reading the blog, comment on the same if you have more suggestions.

Friday, September 23, 2011

Setting up wireless router in opensuse linux 11.4 box

I have a DELL inspiron Laptop with default Windows-Vista as OS. I installed OpenSUSE 11.4 on a different partition. It is easy to setup anything in Win-Vista. However its quite challenging to setup in Linux box. It is very interesting and helpful if you are learning linux.

I have broadband cable connection whose ethernet cable pin was broken. Even I wanted to browse from anywhere in house. So one of my friend suggested to buy a wireless router through which I can achieve flexibility in Internet browsing.

I bought a wireless router from ebay site costing around 1k. It arrived 2 days after the order was placed. Initially we need to connect the output of cable modem to the Internet port of Wireless router. We can connect upto 4 ethernet cables (standalone; however can be increased using switch or hub). Once booted various LED indicators are present to show progress of router. The router is also linux based. It has embedded HTTP, DNS and SCCP server. The nmap output says it.

nandakumar@linux:~> sudo nmap -sT -O 192.168.11.0/24

root's password:

Starting Nmap 5.21 ( http://nmap.org ) at 2011-09-04 15:39 IST

Nmap scan report for 192.168.11.1

Host is up (0.0071s latency).

Not shown: 996 closed ports

PORT STATE SERVICE

53/tcp open domain

80/tcp open http

2000/tcp open cisco-sccp

49152/tcp open unknown

MAC Address: 4C:E6:76:4D:F6:90 (Unknown)

Device type: general purpose

Running: Linux 2.6.X

OS details: Linux 2.6.15 - 2.6.27

Network Distance: 1 hop

By default router has static IP of 192.168.11.1. It assigns the LAN members with IP with network-id 192.168.11.0.

(Caution: Please do not use nmap scan in WAN/(organisation LAN) environment. It is not legal in INDIA. I ran the command since it was in my own LAN environment.)

Setting up Wireless:

Before nmap or any other things can be done, we need to setup wireless network in the linux-box. The default wireless modules do not work properly or do not work. We need to obtain the vendor specific driver of our system and compile the code against native kernel and deploy it. In my case the wireless hardware was from broadcom corporation.

nandakumar@linux:~> /sbin/lspci

00:00.0 Host bridge: Intel Corporation Mobile 4 Series Chipset Memory Controller Hub (rev 07)

00:02.0 VGA compatible controller: Intel Corporation Mobile 4 Series Chipset Integrated Graphics Controller (rev 07)

00:02.1 Display controller: Intel Corporation Mobile 4 Series Chipset Integrated Graphics Controller (rev 07)

00:1a.0 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #4 (rev 03)

00:1a.1 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #5 (rev 03)

00:1a.2 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #6 (rev 03)

00:1a.7 USB Controller: Intel Corporation 82801I (ICH9 Family) USB2 EHCI Controller #2 (rev 03)

00:1b.0 Audio device: Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 03)

00:1c.0 PCI bridge: Intel Corporation 82801I (ICH9 Family) PCI Express Port 1 (rev 03)

00:1c.1 PCI bridge: Intel Corporation 82801I (ICH9 Family) PCI Express Port 2 (rev 03)

00:1c.2 PCI bridge: Intel Corporation 82801I (ICH9 Family) PCI Express Port 3 (rev 03)

00:1c.4 PCI bridge: Intel Corporation 82801I (ICH9 Family) PCI Express Port 5 (rev 03)

00:1d.0 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #1 (rev 03)

00:1d.1 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #2 (rev 03)

00:1d.2 USB Controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #3 (rev 03)

00:1d.7 USB Controller: Intel Corporation 82801I (ICH9 Family) USB2 EHCI Controller #1 (rev 03)

00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev 93)

00:1f.0 ISA bridge: Intel Corporation ICH9M LPC Interface Controller (rev 03)

00:1f.2 SATA controller: Intel Corporation ICH9M/M-E SATA AHCI Controller (rev 03)

00:1f.3 SMBus: Intel Corporation 82801I (ICH9 Family) SMBus Controller (rev 03)

09:00.0 Ethernet controller: Marvell Technology Group Ltd. 88E8040 PCI-E Fast Ethernet Controller (rev 13)

0c:00.0 Network controller: Broadcom Corporation BCM4312 802.11b/g LP-PHY (rev 01)

In my case it is BCM4312. Visit broadcom site and download the appropriate driver (32 or 64 bit depending on OS build version) for BCM4312 and compile against kernel. The README gives complete steps on how to patch, compile and deploy wireless module.

Once the wireless module successfully deployed, the NetworkManager shows up the new interface that is detected. I have disabled NetworkManager and used traditional ifup method. Once setup, goto

Yast->Network Devices->Network Settings

In the Overview Tab, select the interface which is wireless and click Edit.

Under Address TAB, configure interface to obtain address from DHCP. This option provides IP for the interface from wireless router. Once this is done, the wireless interface gets IP from router. Then connect to the router with router IP (in my case 192.168.11.1) and configure router. For increased security, disable SSID broadcast and enable WPA encryption with AES algorithm and provide strong password. Make sure you change password at-least once in a month. Disabling SSID broadcast ensures that your router is not visible outside including to your laptop.

(Note: initially router will not have any security enabled and you can configure as it is i.e. system will be able to scan the wireless network successfully. However, the configuration of router is for tight security. You may skip the configuration section if you do not wish to have security however it is not recommended)

Once router setup is done, we need to get into the network device setup in YAST. Now after the DHCP settings which was done earlier, click next to setup the wireless Network. Here enter the SSID of your router (It will be either MAC or custom as configured by you in router web page). Enter security type of your router and the password for the router and click Next and then OK. Now the setup is done for your router and you are ready to connect to internet.

Now there is a problem. The Internet is in WAN environment and we are in LAN environment. The wireless router does everything for us to connect to internet. But how will linux machine identify that whatever is IP has to connect? For name resolution, router has DNS server however, once IP is obtained, the linux box will not have any idea where to connect for that IP.

To resolve the problem, we need to manually add route to the linux routing table. This is one of the drawback of linux system. We can do this by adding routing table entry manually to connect to internet.

sudo route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.11.1 dev eth1

The above command adds roue entry to the kernel saying that any IP must be routed to the wireless router (which is 192.168.11.1 in my case).

If you follow above method, you will have to add the route every time system starts up. If you need to add the route permanently, you can configure the routing under Network Settings as mentioned in previous sections.

Hope this helps. Comments are always welcome

Friday, July 22, 2011

'format' attribute in gcc

We can check the format of a function against the set of arguments using 'format' attribute. This is is very much useful when we write our own logger function. The format attributes check for the format of a particular function against format of predefined function. The index of format and the variable arguments in the function have to be passed into the attribute.

Ex: void log_inline(const char* format, ...) __attribute__((format(printf,1,2)));

The format of log_inline is checked against the printf function. The format that has to be checked is the first argument of log_inline, while arguments start with second position of log_inline. This is specified as (1,2) in format attribute.

There is an example program:

#include<stdio.h>
#include<stdarg.h>

void log_inline(const char* format, ...) __attribute__((format(printf,1,2)));

void log_inline(const char* format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}

int main()
{
log_inline("%s\n", "SOMETHING");
log_inline("%s\n",1); //Generates warning message
}

We need to compile with -Wall option of gcc to identify the warning.

Note that without attribute in function declaration, warning is not produced.

Saturday, July 2, 2011

World IPv6 day.

8th June 2011 was world ipv6 day. If you refer the link "http://www.worldipv6day.org/", it says many of the companies (including linux kernel site) did switch to ipv6 stack to have global scale trial. If you refer the resource section, some countries have their own ipv6 day. Hope India will also have one in future. The event passed on without making any news at-least to me!! Here is the logo



WORLD IPV6 DAY is 8 June 2011 – The Future is Forever

May be every year this event happens however this is first time I am noticing though; roughly a month after event passed off!!

Monday, June 13, 2011

'warn_unused_result' attribute in gcc

If you want to enforce checking return value of your function you can specify warn_unused_result attribute in your function declaration (ofcourse attributes can be placed only in declarations not definitions). If this attibute is specified and caller does not check the return value, the compiler outputs warning message for not checking return value. This does not mean you need to check the return value but bare assignment also removes this warning. Ofcourse it is difficult as well as ugly to check against condition. Warning is well sufficient. Here is an example. Using this attribute in void function has no effect. gcc simply ignores the attribute and same is displayed in compilation output.

int check() __attribute__((warn_unused_result));
int check()
{
return 0;
}

int main()
{
check();
/*
compiler warns of not checking return value --
warning: ignoring return value of check,
declared with attribute warn_unused_result
*/

int k = check();//no warning
}

Saturday, June 11, 2011

Achieivng memcpy and memmove in single function

Somebody asked me is it possible to simulate memcpy and memmove in a single function. Initially I did not get it. I thought for a while and finally came up with the following solution. This is quite crude code however satisfies the functionality. glibc expects that for memcpy, pointers should not overlap even after copying. If they overlap, then memmove must be used.

As shown in figure there are three cases



Here:

s = source pointer
d = destination pointer
b = boundary
size = number of bytes to copy

1) d >= s+size
2) d < s
3) d < s+size

If d=s, we do not do anything. simply return

The cases 1 and 2 are trivial. In the sense, plain copying does not corrupt destination 'd' buffer. However in case '2', source 's' data will be modified.
The problem is with case 3. When we do plain copy, the source data buffer at the tail gets corrupted and destination 'd' will not have replica of source 's'. We can avoid this by copying from the last i.e. copy from 's+size' to 'd+size' and move towards 's' and 'd'.

Here is the program. The example may be trivial however I hope this works.

#include<stdio.h>

void my_memcpy(void* src, void* dst, size_t bytes);

void my_memcpy(void* src, void* dst, size_t bytes)
{
unsigned char* s = (unsigned char*)src;
unsigned char* d = (unsigned char*)dst;

if(s == d)
{
//same
return;
}

size_t k = 0;
if(s+bytes > d)
{
//Overlapping
k = bytes;
while(k > 0)
{
d[k-1] = s[k-1];
k--;
}
return;
}

//Other cases
while(k < bytes)
{
d[k] = s[k];
k++;
}
}

int main()
{
int i[10] = {0x1234, 0x2345, 0x3456, 0x4567};
my_memcpy(i, &i[2], sizeof(int) * 4);
int j = 0;
for(;j<6;j++)
printf("%x\n", i[j]);
}


When I browsed through glibc code, it used page copy technique in case of memcpy. It tries to get page boundary and copies data in pages. This technique is quite faster for huge data. For data which are not page aligned, it does plain copy. For i386, seems like it uses inline assembly.

Thursday, June 9, 2011

'weak' attribute in gcc

Recently I came to know about gcc attributes which can be used in code for specific purpose. One of them I understood early was 'weak' attribute. A 'weak' attribute may be specified if you don want compiler to throw any error if it was unable to resolve external symbols. This is useful if you are planning to provide function or variable in near future and do not want to modify the main source at that time.

#include<stdio.h>

extern int k() __attribute__((weak));
extern int k2 __attribute__((weak));

int main()
{
if(k == NULL)
{
if(&k2 == NULL)
{
printf("No symbol k or k2 found\n");
}
}
else
{
k();
}
}

This example compiles just fine and gives no linkage error even if the symbols are not found. However if we are using those symbols we need to check NULLness of those symbols else the program will result in seg fault.

Sunday, May 29, 2011

Casting away consts in C++

This could be dangerous sometimes. Stroustrup in his book has noted that const variables may be put in read only section of the memory. Hence casting away their consts and modifying them definitely lead to memory access violation. A small example can be like this.

#include <iostream>
#include <string.h>

const char val[] = {"Test1"};

void bad_modify(char* str, char* str2, const size_t len)
{
memcpy(str, str2, len);
}

int main()
{
char* s = "Value";
char* p = const_cast
<char*>(val);
bad_modify(p, s, 5);
std::cout<
<p<<std::endl;
}


g++ puts the varibale val in readonly section of the memory.
Hence when we cast away const from val and try to modify it, we get segmentation fault.

Comments are always welcome