Monday, August 13, 2012

Non Blocking Connect with Timeout

Connect is blocking
 If there is no response  retries will be done on interval which keeps increasing for minutes .
If we need to return quicker connect should be done as nonblocking and time can be set for how long wait for SYN ACK from Server.


fd_set fdset;
struct timeval tv;
int flags ;
  //NON blocking
    if ((flags = fcntl(sock, F_GETFL))<0 br="br">    {
       printf("fcntl get FL failed\n");
       return -1;
    }
    if( fcntl(sock, F_SETFL, O_NONBLOCK)<0 br="br">    {   printf("fcntl get FL failed\n");
       return -1;
    }
 if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        {

          if (errno != EINPROGRESS)
           {
             perror("connect() failed");
             exit(0);
           }
        }
        printf("non blockinng connect() passed \n");

  FD_ZERO(&fdset);
  FD_SET(sock, &fdset);
  tv.tv_sec = 3;
  tv.tv_usec = 0;

   if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1)
   {
      int so_error;
      socklen_t len = sizeof so_error;
      getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
      if (so_error == 0)
      {
         printf("  CONNECTION ESTABLISHED\n");
         fflush(NULL);
      }
      else
      {
          printf("  CONNECTION FAILED\n");
           return-1;
      }
   }
   else
   {
      printf(" CONNECTION  TIMEOUT\n");
      fflush(NULL);
      return-1;
   }

Thursday, March 8, 2012

Traceroute

Q) How TraceRoute  works
 It works on the principle of TTL.Router decrements TTL while processing and if TTL is zero it drops the Packet and sends ICMP Error Time Exceeded Mesage back to source HOST. In the ICMP Reply Message source IP is of Router which drops the packet.
Now host which runs Trace route leverage out of this property. It sends packet with destination addr of remote host and TTL =1. In reply it know IP of first HOP(router) it saves the IP and details and send next Packet with TTL 2 and  keep incrementing TTL until it reaches Remote host .
every router in path must have respinded to source host and We got list of all HOPS on ROUTE.

Q)What kind of message are send in  traceroute?How can we determine when we've reached the destination?
In windows :
 It sends  ICMP echo Request with incrementing TTLs .ICMP Error TIme Exceeded signifies intermediate
 and ICMP echo reply signifies Host is reached
 In Unix like OS
Traceroute sends UDP datagrams to the destination host, but it chooses the destination UDP port number to be an unlikely value (larger than 30,000),
 making it improbable that an application at the destination is using that port.
 This causes the destination host's UDP module to generate an ICMP "port unreachable" error
 when the datagram arrives.  Traceroute  differentiate between the received ICMP messages—time exceeded versus port unreachable—to know when it's done.

Q Why cant we use IP record route option (RR). instead of Trace Route?
Ans:
  1. Not all routers have supported the record route option.
  2. The room allocated for options in the IP header isn't large enough today to handle most routes. There is room for only nine IP addresses in the IP header options field. In Today's world its not enough

Monday, March 5, 2012

IPC -What to use When??

IPC:
  1. Sockets BSD
  2. PIPE
  3. Named PIPE (FIFO)
  4. Signals
SYSTEM V IPC
  1.  Message  Queue
  2. Shared Memory
  3.  Semaphore
--------------------------------------------------------------------------------------------------------
PIPE v/s NAMED PIPE

PIPE
  • Used in related process which must have common ancestry .
  • These are not permanent. If creator process terminates pipe also goes off.
  • Half duplex Data flows in only one direction. 
  • Reading from a pipe whose write end is closed returns 0 indicating EOF
  • Writing on a PIPE whose read end  is closed generates signal SIGPIPE

Named pipe(FIFO) : FIFO is a special file whose semantics differ significantly from those of a regular file
  • Can be used between unrelated Process. Either of them or any other (command mknod) can create FIFO.
  • These are Permanent Need to be Removed Explicitly
  • Treated  just as file have size owner access permissions Can be opended closed or deleted like any other file.
  • Bytes written to a FIFO are always written as though they were written in append mode.
  • Bytes read from a FIFO are always read from the beginning of the file and then removed.  
  • When all processes which have the FIFO open close the FIFO, any bytes remaining in the FIFO are removed.
  • Writing to a FIFO which no process has open for reading results in an error condition.
  • SIGPIPE is generated when writer writes to a FIFO on which either reader is died or have closed reading file descriptor. if reader is not at all created  then by default  open()  fifo=open("fifo_writer",O_WRONLY); call will block. When reader comes writer open() unblocks and writes successfully. Now after reading if reader goes away or just closes reading fd SIGPIPE will be generated.
  • If open is NON_Blocking  fifo=open("fifo_writer",O_WRONLY| O_NONBLOCK);  then even though pipe exists and Reader is not there open will return error -1 No such device or address
On Prompt it can be created by command mknod
mknod ( FIFO_NAME, S_IFIFO | 0666, 0);    0 is the device no.  ignored .
mkfifio(name , permission ) is better

----------------------------------------------------------------------------------------------------------
 System V IPCs :   Message Queue  ,Semaphore, Shared Memory :
 Creation(msgget,semget,shmget) requires  key of type Key_t (long integer defined in sys/types.h). Key is converted in identifier by kernel

 How  to share key between unrelated Processes:
  1.   Key can be created by one process using IPC_PRIVATE and written in file (disk operation is required )
  2.    Key can be put in Header file
  3.    ftok() to generate key based on two arguments passed path name and project UNIQUE ID(any no between 0 -255)
  Each ipc will have associated struct ipc_perm wich can be changed by semctl,msgctl,shmctl functions.
  SYSTEM V IPC are system wide  do not have any refrence count
  They remain in SYSTEM untill they are deleted explixitly (Pipe are deleted as soon as the Last process refrencing Pipe terminates )
  IPC structures are not known as name in file system cant use ls,shmod.rm command on that.
  ipcs ipcrm are created to tdo these operation.
  Dont use File descriptor I/O multiplexing is not possible cant use poll() select().


Shared Memory :
  • Shared Memory is fastest form of IPC. As there is no copy of data form sender to receiver. after shmat  at different process. individudal Virtual pages maps to same physical page from RAM. And it can be accessed as any other buffer.
  • Can be used between any Processes and random access is allowed.
  • In shared memory data has to be deleted explicitly. 
  • Reader has to be notified for the data.

Why is shared memory the fastest form of IPC?
 Once the memory is mapped into the address space of the processes that are sharing
 the memory region, processes do not execute any system calls into
 the kernel in passing data between processes, which would otherwise be required.
 Shared memory lets two or more processes share a region of memory.
 The processes must, of course, coordinate and synchronize (using Semaphore )their use of the shared memory between themselves to prevent data loss.

Accesses to the shared memory region have to be mutually exclusive; this is achieved via the use of the semaphore

 Generally Shared Memory can be used in three ways to
 mmap ,PoSIX(shm_open, mmap) and System V IPC(shm_get,shmat())

Message Queues: 
  1. The primary difference between a System V message queue and a socket or named pipe is that message queues may have multiple processes reading and writing from and to them, or no readers at all. 
  2. Allows prioritizing of messages.
  3.  Message queues have a size limit and a surprisingly small one.  16K for Linux and 4k for Solaris. 
  4. Individual messages have size limits.  8K for Linux and 2K for Solaris.
  5. Usually faster than pipes
  6. Difference from Shared Memory:   In Message queue Message passed is deleted as soon as its read by Reader . In shared memory data has to be deleted explicitly 


Some Questions :

Q Can many apps listen to same Msg Queue ? Can many app post to came queue ?
Answer:    Many Application can listen on same Msg Queue for their corresponding Message TYPE. Message is removed once listening app reads message to its type.
Yes multiple APP can write also.
Q What to use for two way Communication for two APPS?
Socket has to be used in this case . not possible on Message queue

Digital Signature

What is digital Signature?
A digital signature scheme typically consists of three algorithms:
  • A key generation algorithm that selects a private key uniformly at random from a set of possible private keys. The algorithm outputs the private key and a corresponding public key.
  • A signing algorithm that, given a message and a private key, produces a signature.
  • A signature verifying algorithm that, given a message, public key and a signature, either accepts or rejects the message's claim to authenticity.

When is public / private key pair are used?
Answer  : used in two ways
1 Public key encryption :
message encrypted with a recipient's public key cannot be decrypted by anyone except a possessor of the matching private key. Used for Confidentiality
Asymmetric key algorithms : uses different(but Mathematically Related ) keys for Encryption and Decryption
Public encryption Key  and Private Decryption key
2  Digital Signature: message signed with a sender's private key can be verified by anyone who has access to the sender's public key. Used to check for Authenticity  of Message

What is EAP protocol Packet content?
What is PKI?

TCP IP

1Tell the scenarios in tcp connection establishmnet in which server doesnt reply.What all kind of errors can heppen

2 When is RESET used in TCP.
Ans:
1) When connection request (SYN )arrives and no process is listening on the destination port.
This is Hard Error ECONNRFUSED is returned to client as soon as RST is  reccieved
(In UDP  ICMP port unreachable  error will be sent to host)
2) In  TCP half-open connection (in a established connection if server reboots)If clients sedns data based on Preboot connection Server responds with RST
3) Receipt of any TCP segment from any device with which the device receiving the segment does not currently have a connection (other than a SYN requesting a new connection.)
4)  Receipt of a message with an invalid or incorrect Sequence Number or Acknowledgment Number field, indicating the message may belong to a prior connection or is spurious in some other way.

example: The client's host has crashed and rebooted. Here the server will receive a response
 to its keepalive probe, but the response will be a reset, causing the server to terminate the connection
sockets API provides this capability by using the "linger on close" socket option
This causes the abort to be sent when the connection is closed, instead of the normal FIN.
--RST segment elicits no response from the other end— it is not acknowledged at all.

Q) What is Linger on option in Socket?
Answer :The effect of an setsockopt(..., SO_LINGER,...) depends on what the values in the linger structure (the third parameter passed to setsockopt()) are:
Case 1:  linger->l_onoff is zero (linger->l_linger has no meaning):
            This is the default.
On close(), the underlying stack attempts to gracefully shutdown the connection after ensuring all unsent data is sent. In the case of connection-oriented protocols such as TCP, the stack also ensures that sent data is acknowledged by the peer.  The stack will perform the above-mentioned graceful shutdown in the background (after the call to close() returns), regardless of whether the socket is blocking or non-blocking.
Case 2: linger->l_onoff is non-zero and linger->l_linger is zero:
A close() returns immediately. The underlying stack discards any unsent data, and, in the case of connection-oriented protocols such as TCP, sends a RST (reset) to the peer (this is termed a hard or abortive close). All subsequent attempts by the peer's application to read()/recv() data will result in an ECONNRESET.
Case 3: linger->l_onoff is non-zero and linger->l_linger is non-zero:
A close() will either block (if a blocking socket) or fail with EWOULDBLOCK (if non-blocking) until a graceful shutdown completes or the time specified in linger->l_linger elapses (time-out). Upon time-out the stack behaves as in case 2 above.

Friday, February 17, 2012

Mutex

If a thread takes Mutex and dies.What happens to that mutex can take other take it ?

Threads

How to create Thread safe Function?
What is thread specific Data?
how to kill all threads spawned in Process.

Friday, February 10, 2012

64 bit processor what is the size of ptr and size of int

size of ptr is 64 bits. As Address bus is of 64 bit  2power64 size of memory can be accessed directly..
while in 32 bit 2 power 32 i.e 4 GB memory can be addressed RAM physical memory ant go beyond more than 4 GB
Size of int depends on the implementation generally its 32 bit as in
OS ------------------Data Model-----shor-t------int-------long----longlong-----ptr

Window--------------LLP64----------16---------32------32-------64----------64
Linux-----------------LP4-------------16---------32------64-------64----------64
SPARC--------------ILP64-----------16---------64------64-------64----------64
--------------------------------------------------------------------------------------------

What should be taken care while porting from 32  to 64 Arch?
If client is in 32 and server is in 64 bit what care should be taken?

--padding , ediannesss,pointers ?

What is Time_wait when does Socket goes in that its uses ?

Active Close / Passive Close ??

Linger option for sockets??

Wednesday, September 14, 2011

Packet flow in Network

DATA FLOW
A typical data flow which we deal with on daily bases in our homes and offices is like this:
Host A -> Switch -> Router R -> Switch -> Host B
  1. The 1.x host A wants to send to 2.x host B.
  2. Host A realises B is on a different network judging from the subnet mask and ip addresses and knows it need help from a router.
  3. Host A does an ARP operation to find out the MAC address of its default gateway, which is a router R.
  4. Host A sends to router R with source MAC = A‘s MAC and destination MAC = R‘s MAC. The switches in between them pass the frames intact.
  5. Router R sends to host B through the switch in between, rewriting the source MAC to R‘s MAC and the destination MAC to B‘s MAC (before this it should have an ARP entry of host B; otherwise it would do an ARP operation to find out B‘s MAC).

packet creation layer  from url type to page opening ? how pkt flows ? if there are 2 router in subnet ?
when is dns query fired ???  

    Wednesday, June 16, 2010

    Some question

    CA
    Remote login Through C code ?
    Return of printf?  printf("%d",m) whede m = 987

    -------------juniper

    Endianess happens because of what ? why cant OS take  care of it ?
    how to write code which can be easily ported across OS?
    Static Function?
    Hashing how to decide hash funciton?
    AVL tree ,  bst--Binary tree ? Complexity ot binary tree?  how nlogn comes?
    ---------------------------------
    LG.
    Q If i do malloc for 64 MB will it go if yes how .  Does process memory increases .
    Q Is Stack frame Size constant .  what is stack corruption.
    Q Use of extern in C++
    Q Can we have static  Ctor od Dtor  if not why??
    Press ENTER to look up in Wiktionary or CTRL+ENTER to look up in Wikipedia

    Friday, June 11, 2010

    Linux Find If Processor / CPU is 64 bit / 32 bit ( long mode ~ lm )

    for OS

    uname -a

     CPU
    grep flags /proc/cpuinfo 

    CPU Modes:

    lm means Long mode - 64 bit CPU

    Real mode 16 bit CPU

    Protected Mode is 32-bit CPU

     

    Monday, May 10, 2010

    Extensible Authentication Protocol - EAP

    * Extension to PPP
    * Authentication Framework , not a Specific Authentication Mechanism
    * Desired Authentication Method can be Negotiated  like EAP-TTLS, EAP-MD5, EAP-TLS,EAP-PSK
    * In 80.11 it is used to negotiate Secure PMK (Pairwise Master key )

    TTLS -Tunneled Transport Layer Security
    PSK- Pre Shared Key







    Codes :
    EAP                                          Radius
    1--Request                              1--Access Request
    2--Success                              2--Access Accept
    3--Response                           3--Access Reject
    4--Failure                               4--Accounting Request
                                                  5--Accounting Response

    Authentication in Wimax System

















    Protocols  Stack :

      

    Thursday, May 6, 2010

    Any real problem faced

    I face a problem wehre RAw Socket was in promniiscous mode and lietening to all intefaces  though it was intended to listen on one Inteface .
    // creation of raw socket 

     read_sockfd = socket( AF_PACKET , SOCK_RAW, htons(ETH_P_ALL)) ;
    This stmt was success but not solving purpose
    setsockopt(read_sockfd , SOL_SOCKET , SO_BINDTODEVICE , BIND_DEV , devlen+1 )


    Solution
        struct sockaddr_ll sa
        int interfaceId =-1;
        struct ifreq ifr;

        memset(&sa, 0, sizeof(sa));
        strncpy(ifr.ifr_name,BIND_DEV, devlen+1);
        sa.sll_family = AF_PACKET;
    // get interface index
        interfaceId = ioctl(read_sockfd ,SIOCGIFINDEX,&ifr);
        sa.sll_protocol = htons(ETH_P_ALL);
        sa.sll_ifindex =ifr.ifr_ifindex 
       bind( g_frame_read_sockfd , (struct sockaddr*) &sa, sizeof(sa));

    Thing is we have to explicitly bind raw socket to socket address structure(with proper interface index )
    Man page clearly says
    By default all packets of the specified protocol type are passed  to  a    packet  socket.    To  only  get  packets    from  a specific interface use  bind(2) specifying an address in  a  struct  sockaddr_ll  to  bind  the
     packet    socket     to  an  interface.  Only  the    sll_protocol  and  the  sll_ifindex address fields are used for purposes of binding.

    ------------------------------------------
     tcpdump  -d   vlan and ip  
    tcpdump -dd vlan and \(ip or arp \)
    tcpdump -d ether src 00:80:42:1B:1D:94 and vlan and \(ip or arp \)
    tcpdump -d not ether src 00:80:42:1B:1D:94 and vlan and \(ip or arp \)

    Friday, April 16, 2010

    Compiling Shared Libraries .so


    Need to compile Library in Position independent code 

    When the object files are generated, we have no idea where in memory they will be inserted in a program that will use them. Many different programs may use the same library, and each load it into a different memory in address. Thus, we need that all jump calls ("goto", in assembly speak) and subroutine calls will use relative addresses, and not absolute addresses. Thus, we need to use a compiler flag that will cause this type of code to be generated

    gcc -fPIC -c util_file.c
    gcc -fPIC -c util_net.c
    gcc -fPIC -c util_math.c
    gcc -shared libutil.so util_file.o util_net.o util_math.o
    Why is this Required ??
    dlopen()  --Thing is inspite of Loading library at begining . 
    From program we can specify when to Load and unload a Library 
    and what to use. 
    dlopen returns a handler on success 
    lib_handle = dlopen("/full/path/to/library", RTLD_LAZY); 
    
    
     
    RTLD_LAZY  -- Lazy aproach --defining whether all 
    symbols refered to by the library need to be checked immediately or When used 
    
    dlopen() UNLOAD


    Reference:
    http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

    Thursday, April 8, 2010

    Random C question

    Q How to write Multi line MACRO ???  
    #define MUL_LINE_MACRO(x)    do{  x=123; } while (0)
     http://c-faq.com/cpp/multistmt.html

    Dont put ; after Macro let user put it  MUL_LINE_MACRO(x);
    This is required as 
    if in code macro is used in if else  condition like;
    if (cond )
        MUL_LINE_MACRO(x);
    else
       some ;
    and macro also has an  ";"  then complilation fails 
    if (cond )
          do{  x=123; } while (0);
      ; // this will cause compilation failure
    else
       some ;


    Tuesday, March 30, 2010

    How to do Multicast and Broadcast ? Is it possible for both TCP and UDP

    Multicast can not happen on TCP it works only in UDP or RAW socket.
    Same applies to Broadcast also .

    TCP sockets are always Unicast.

    A reliable multicast protocol, like PGM(Pragmatic General Multicast ), adds the ability for receivers to detect lost and/or out-of-order messages and take corrective action (similar in principle to TCP), resulting in a gap-free, in-order message stream.

    http://tldp.org/HOWTO/Multicast-HOWTO-6.html

    Monday, March 29, 2010

    Can two application Listen on same port

    No. Only one process can bind to a port on an address at any one time. You can bind two processes to the same port number, but on different IP addresses Different NIC , hence different ports.

    For UDP (Multicasts), multiple applications can subscribe to the same port.
    With multicast sockets more than one application can bind to a port as long as SO_REUSEADDR is set in each socket's options.
    We can  accomplish this by writing a "master" process, which accepts and processes all connections, then hands them off to your two applications who need to listen on the same port. This is the approach that Web servers and such take, since many processes need to listen to 80.

    Wednesday, February 24, 2010

    Kernel debugging

    what is KDB
    http://www.ibm.com/developerworks/linux/library/l-kdbug/

    Q Can we put assembly code in C ?
    Ans Yes by asm(" ");

    Q How to compile SO?
    Ans -f PIC (Position independent code )
      http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html