Subscribe for email updates

Get all updates to your inbox to stay tune.

Curve tracing programs - Draw sin(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Graphics programming in C

Learn how to write basic and advanced graphics programming in c language

Curve tracing programs - Draw Cos(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Make file tutorial

A step by step guide to learn how to write make file in Linux with examples

Thursday, May 31, 2012

Calculate SHA Digest

Digest is a practical technique to verify that a file is not corrupted. Digest is a fixed length of string which is a result of hash on blocks of input message. Depending on the hash technique, the digest length varies.

There are various types of standards of calculating digest. For example, MD2,MD4,MD5,SHA1,SHA..etc.
MD5 generates 16bytes of Digest where as SHA digest is of 20bytes(160 bits) long.

We have seen how to generate MD5 digest using c program and openSSL command in last article. Read Calculate MD5 Digest.

In this post, we will see how to generate SHA digest using c program and openSSL command line interface.

Tuesday, May 22, 2012

calculate MD5 Digest

Digest is a practical technique to verify that a file is not corrupted. For example, If a rpm file is transferred over internet, Before installing, it is always good practice to verify that the file is not corrupted. The most popular practices are verifying the digest of the received file. If you have a digest already with you, then generate digest on the received file and compare both. If both matches, then it is confirmed that the file is correct. else file is corrupted. Digest will definitely vary even if a single bit varies in a file.

Digest is a fixed length of string which is a result of hash on blocks of input message.
Depending on the hash technique, the digest length varies.

There are various types of standards of calculating digest. For example, MD2,MD4,MD5,SHA1,SHA..etc.
MD5 generates 16bytes of Digest where as SHA digest is of 20bytes long.

Thursday, May 17, 2012

Bash tip to extract path from absolute filename

Thanks to Bash advanced features. We have seen Search and replace technique in last post. Now lets try another technique to extract content from a variable based on some filter.

Lets take a use case.
1. Extract path from a file name which is an absolute file path.

For this, we need to extract path except the filename. Lets see how to achieve this using advance bash scripting.

Syntax is:
${S1%S2}
S1 - String on which filter is required
S2 - String which needs to be removed from the end.

Which means in the fillename, from the end it removes characters till it finds "/", including "/"

Example1:  Extract path from file name

[bash] # var="/home/product/module/libTest.a"

[bash] # var=${var%/*}
[bash] # echo $var

/home/product/module

Example2:  Remove world from "Helloworld"

[bash] # var="Helloworld"

[bash] # var=${var%world}

[bash] # echo $var
Hello
[bash] #


Monday, May 14, 2012

The SLR Camera Simulator - good to learn photography basics

Photography is a passion, I just started learning about DSLR basics. Really it is a good start to learn about the concepts what does ISO means, what is aperture ? how shutter speed plays a vital role in night shots or day shots. Thanks to google and http://camerasim.com, here I found a SLR simulator, which is a good start. It has several control, by varying these controls - see how picture appears in different aperture,shutter speed and ISO settings. I enjoyed in playing with this simulator. Hope you will enjoy.

Thursday, May 10, 2012

Program to validate hostname and IPV4 address


Here we will see how to validate a given IPV4 address or given hostname. In the last post (IPV4 address format validation), we have seen different IPV4 address format. We will verify all those formats practically here.

Lets have a quick glance on the functions used in this program.

getaddrinfo(), function returns addresses associated with the given IP address or hostname. If this function returns error means that the input address is not in a valid IPV4 format or the host name is not resolved. The following program is based on this output. It is written and tested on Linux.


freeaddrinfo() function used to free the address structure returned by getaddrinfo.
gai_strerror() function used to get the error string for the error returned by getaddrinfo().

/* IPV4 and hostname validation */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/*Validate IPV4 or hostname function */
int validateIPV4Addr(char *addr)
{
    int rc=0;
    struct addrinfo hints;
    struct addrinfo *result=NULL, *rp=NULL;
    int s;

   /* Obtain address(es) matching host */
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
    hints.ai_flags = 0;
    hints.ai_protocol = 0;          /* Any protocol */

   s = getaddrinfo(addr, NULL, &hints, &result);
    if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        rc=1;
    } else {

  for (rp = result; rp != NULL; rp = rp->ai_next) {
     struct sockaddr_in *a=(struct sockaddr_in *)rp->ai_addr;
     fprintf(stdout,"%s\n",inet_ntoa(a->sin_addr));
    }

  }

  if( result != NULL )
    freeaddrinfo(result);

  return rc;
}


int
main(int argc, char *argv[])
{
  int rc;

   if (argc < 2) {
        fprintf(stderr, "Usage: %s [ipv4|hostname]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

   rc=validateIPV4Addr(argv[1]);
   if( rc!=0 ){
     fprintf(stderr,"IP address/hostname is invalid\n");
     exit(EXIT_FAILURE);
   }

   fprintf(stdout,"IP address/hostname is valid\n");
   return 0;

}
Output:
The following cases shows output of the program on different formats of IPaddress and hostname validation. 

 ncooltips# ./validateip
Usage: ./validateip [ipv4|hostname]

 ncooltips# ./validateip 1
0.0.0.1
IP address/hostname is valid

 ncooltips# ./validateip 1.2
1.0.0.2
IP address/hostname is valid

 ncooltips# ./validateip 1.2.3
1.2.0.3
IP address/hostname is valid

 ncooltips# ./validateip 1.2.3.4
1.2.3.4
IP address/hostname is valid

 ncooltips# ./validateip 1.2.3.489
getaddrinfo: Name or service not known
IP address/hostname is invalid

 ncooltips# ./validateip localhost
0.0.0.0
127.0.0.1
IP address/hostname is valid

 ncooltips# ./validateip localhostnotvalid
getaddrinfo: Name or service not known
IP address/hostname is invalid


Monday, May 7, 2012

IPV4 address format validation

This is a usual practice to validate a IPV4 address format read from a configuration file before consuming it. For this one should know what are all the valid IPV4 address formats.

1. IPV4 address consists of 32bits , i.e  in human readable format it is four octets devided by period ( dot)
    A.B.C.D
 Here each octet takes value from 0-255 so the valid IPv4 adress ranges from 0.0.0.0 to 255.255.255.255

2. The following formats are also valid

i.   8
     This address is treated as 32 bit integer so it is equal to 0.0.0.8

ii.   1.8
     In this format first octet is treated as first 8 bits and last octet is treated 24 bits. It is equal to 1.0.0.8

iii.   1.2.8
     In this format first two octets are treated as first 16bits and last octet is treated as next 16 bits of ip address. It is equal to 1.2.0.8

So, to validate any IPV4 address, one should consider all of the above valid IPV4 address formats.