1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Netlink socket creation-packet decoder

Discussion in 'Unix' started by navneet85, Sep 20, 2007.

  1. navneet85

    navneet85 New Member

    Joined:
    Sep 17, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello,
    I have done some programming to get the information of packets recieved at the kernel, for this I have implemented one DGRAM socket at server side and tryinig to read packets arrriving at client using the netlink socket. But problem is that after the call to recvmsg, the code gets blocked, that is, it is not receiveing messages and headers ffrom either kernal or the server. If any one could help would be welcomed.I am submitting my client and server codes also.
    Code:
    /* a client program for netlink socket */
    #include<stdio.h>
    #include<netinet/in.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<linux/netlink.h>
    #include<arpa/inet.h>
    #include<netdb.h>
    #include<stdlib.h>
    #include<string.h>
    #include<errno.h>
    #define MAX_PAYLOAD 100  /* maximum payload size*/
    #define portno 41234
    
    int main()
    {
    	int sockfd,sock_fd,serlen,serverlen,sequence_number=0;
    	struct sockaddr_in server_addr;
    	struct sockaddr_nl serv_addr;
    	struct nlmsghdr *nlh=NULL;
    //	struct msghdr msg;
    	struct iovec iov;
    	char buffer[20];
    	serlen=sizeof(serv_addr);
    	serverlen=sizeof(server_addr);
    	sockfd=socket(AF_NETLINK,SOCK_RAW,NETLINK_FIREWALL);
    	printf("sockfd=%d\n",sockfd);
    	memset((struct sockaddr *)&serv_addr,0,serlen);
    	sock_fd=socket(AF_INET,SOCK_DGRAM,0);
    /* Fill in the socket header */	
    	server_addr.sin_family=AF_INET;
    	server_addr.sin_port=htons(portno);
    	server_addr.sin_addr.s_addr=INADDR_ANY;
    	strcpy(buffer,"Hello!");
    	sendto(sock_fd,buffer,20,0,(struct sockaddr *)&server_addr,serverlen);
    	printf("sending:---%s\n",buffer); 
    	serv_addr.nl_family=AF_NETLINK;      
     	serv_addr.nl_pid=0;			/* for destination as kernel */
    	serv_addr.nl_groups=0;		/* not in mcast groups */
    	nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
    /* Fill the netlink message header */
    	nlh->nlmsg_len=NLMSG_SPACE(MAX_PAYLOAD);
    	nlh->nlmsg_pid=0;	/* self pid */
    	nlh->nlmsg_flags=0;	
    	nlh->nlmsg_type=NULL;
    	nlh->nlmsg_seq=++sequence_number;
    /* Fill in the netlink message payload */
    	strcpy(NLMSG_DATA(nlh),"Hello you!");
    	iov.iov_base=(void *)nlh;
    	iov.iov_len=nlh->nlmsg_len;
    /*	msg.msg_name=(void *)&serv_addr;
    	msg.msg_namelen=serlen;
    	msg.msg_iov=&iov;
    	msg.msg_iovlen=1;*/
    	struct msghdr msg={(void *)&serv_addr,serlen,&iov,1,NULL,0,0};
    	while(strcmp(NLMSG_DATA(nlh),"exit")!=0)
    	{	
    		memset(nlh,0,NLMSG_SPACE(MAX_PAYLOAD));
    		recvmsg(sockfd,&msg,0);
    		printf("received:%s\n",NLMSG_DATA(nlh));
    	}
    	return 0;
    }
    
    
    Code:
    /* an UDP server program in the internet domain */
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<stdio.h>
    #include<string.h>
    #define portno 41234
    
    int main()
    {
    	int sockfd,serlen,clilen,n;
    	struct sockaddr_in serv_addr,cli_addr;
    	char buffer[20];
    	sockfd=socket(AF_INET,SOCK_DGRAM,0);
    	serv_addr.sin_family=AF_INET;
    	serv_addr.sin_port=htons(portno);
     	serv_addr.sin_addr.s_addr=INADDR_ANY;
    	serlen=sizeof(serv_addr);
    	bind(sockfd,(struct sockaddr *)&serv_addr,serlen);
    	clilen=sizeof(cli_addr);
    	recvfrom(sockfd,buffer,20,0,(struct sockaddr *)&cli_addr,&clilen);
    	printf("A connection has been established\n");
    	printf("%s\n",buffer);
    	while(strcmp(buffer,"exit")!=0)
    	{
    		printf("Enter the message:\n");
    		fgets(buffer,20,stdin);
    		sendto(sockfd,buffer,20,0,(struct sockaddr *)&cli_addr,clilen);
    		printf("%s sent.\n",buffer);
    	}
    	return 0;
    }
     
    Last edited by a moderator: Sep 20, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    navneet please use code blocks for code that you put into the posts for better formatting.
     

Share This Page