LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Raw socket creation error (IPPROTO_IP not supported) (https://www.linuxquestions.org/questions/programming-9/raw-socket-creation-error-ipproto_ip-not-supported-194574/)

linfreak 06-17-2004 09:36 AM

Raw socket creation error (IPPROTO_IP not supported)
 
hi all,

I tried creating a raw socket in Redhat Advanced server 3.0 . Problem is socket creation fails if the protocol family is IPPROTO_IP or IPPROTO_RAW. I checked out the values for these macros in the header file . They are

IPPROTO_IP ---> 0
IPPROTO_IP ---> 255

I am interested in creating a RAW SOCKET (SOCK_RAW) only

. Socket creation succeeds for other protocol family like IPPROTO_TCP of IPPROTO_ICMP .. etc. The following is the code snippet .

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Code Starts here ^^^^^^^^^^^^^^^^^^^^^^^^

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <iostream.h>


int main()
{
int fd;
struct sockaddr_in serv_sock,cli_sock;

if ((fd = socket(AF_INET,SOCK_RAW,0)) < 0)
{
cout<<"error "<<errno<<endl;
}

//raw sockets dont require binding .. do they ??

listen(fd,5);
fd_set fdset;

while(1)
{
FD_ZERO(&fdset);
FD_SET(fd,&fdset);
int ret = select(fd+1,&fdset,NULL,NULL,NULL);
if (FD_ISSET(fd,&fdset))
{
cout<<"hello"<<endl;
break;
}

}

printf("program terminates");
return 0;
}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Code Ends here ^^^^^^^^^^^^^^^^^^^^^^^^

My problem is only with socket creation using IPPROTO_IP or IPPROTO_RAW ... the value of errno after the call is 94 which is # defined in <asm/errno.h> as

#define ESOCKTNOSUPPORT 94 /* Socket type not supported */

Does redhat AS 3.0 not allow creation of IPPROTO_IP or IPPROTO_RAW type sockets.
or
do i have to enable the support in kernel.

Thanks a ton in advance !!!

Warm Regards

N.Harris ur rahman

infamous41md 06-17-2004 12:37 PM

u need to use _TCP/_UDP/_ICMP , those others won't work. but u can do everything that u need with the above mentioned. if u want to see outgoing packets, u need to do:
Code:

    struct sockaddr_ll    sa;
    sa.sll_family = PF_PACKET;  //always this
    sa.sll_protocol = htons(ETH_P_ALL); //must set this to ALL if we want copy of outgoing packets, read below

        //make the socket; ETH_P_ALL must be set regardless of the packets we want
        //please see the pf_main.c source file for an explanation of this
        if( (sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) <= 1) //create socket
                return 0;

        //in order to recieve outgoing packets we need to bind to ETH_P_ALL
        if( (bind(sock, (struct sockaddr *)&sa, salen)) != 0 )
                err_quit();



All times are GMT -5. The time now is 07:47 AM.