Unix Programming - SOCK_RAW not supported

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2005 > SOCK_RAW not supported





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author SOCK_RAW not supported
Roman Mashak

2005-06-23, 2:48 am

Hello, All!

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
....

int sd;
if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {
perror("socket() error");
exit(1);
}
....

this causes run-time error on linux (kernel 2.4.20): "socket() error: Socket
type not supported"

What is the problem?

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


David Schwartz

2005-06-23, 2:48 am


"Roman Mashak" <mrv@tusur.ru> wrote in message
news:d9dd6v$1e8a$1@relay.tomsk.ru...

> if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {


You mean socket(PF_PACKET, SOCK_RAW, )

DS


Roman Mashak

2005-06-23, 2:48 am

Hello, David!
You wrote on Wed, 22 Jun 2005 21:41:19 -0700:

??>> if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {

DS> You mean socket(PF_PACKET, SOCK_RAW, )
no, in this case I get "socket() error!: Operation not permitted"

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


David Schwartz

2005-06-23, 2:48 am


"Roman Mashak" <mrv@tusur.ru> wrote in message
news:d9dfd8$1h01$1@relay.tomsk.ru...
> Hello, David!
> You wrote on Wed, 22 Jun 2005 21:41:19 -0700:
>
> ??>> if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {
>
> DS> You mean socket(PF_PACKET, SOCK_RAW, )
> no, in this case I get "socket() error!: Operation not permitted"


Are you root? Ordinary users can't send or receive raw packets, for
obvious reasons.

DS


Roman Mashak

2005-06-23, 2:48 am

Hello, David!
You wrote on Wed, 22 Jun 2005 21:41:19 -0700:

??>> if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {

DS> You mean socket(PF_PACKET, SOCK_RAW, )
well, I run the code with 'root' privilege now, socket is created
succesfully but 'bind' still has same error "Socket operation on
non-socket".

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Rainer Temme

2005-06-23, 2:48 am

Roman Mashak wrote:
> Hello, David!
> You wrote on Wed, 22 Jun 2005 21:41:19 -0700:
>
> ??>> if ( sd = socket(AF_INET, SOCK_RAW, 0) < 0) {
>
> DS> You mean socket(PF_PACKET, SOCK_RAW, )
> well, I run the code with 'root' privilege now, socket is created
> succesfully but 'bind' still has same error "Socket operation on
> non-socket".
>
> With best regards, Roman Mashak. E-mail: mrv@tusur.ru
>
>

Hi Roman,

don't forget what kind socket you've created ...

you wantet a raw-socket ... this is possible on packet-layer ...
now you've got a raw-socket on packet-layer (which is below tcp or udp)
you shouldn't complain that bind() is not working ... bind() is
a thing for the upper layers ... you can (and have to) create your
own packets now ... including ip-headers and upper-protocol headers

Regards ... Rainer
Roman Mashak

2005-06-23, 7:51 am

Hello, Rainer!
You wrote on Thu, 23 Jun 2005 09:37:59 +0200:

RT> don't forget what kind socket you've created ...

RT> you wantet a raw-socket ... this is possible on packet-layer ...
RT> now you've got a raw-socket on packet-layer (which is below tcp or udp)
RT> you shouldn't complain that bind() is not working ... bind() is
RT> a thing for the upper layers ... you can (and have to) create your
RT> own packets now ... including ip-headers and upper-protocol headers
Rainer, in case if I create SOCK_DGRAM socket, should I call 'bind' or to
fill in 'sockaddr_in' structure is quite enough?the same error:

unsigned short int port = 5000;
const char *saddr = "0.0.0.0";

/* fill in structure */
s_in.sin_family = AF_INET;
s_in.sin_port = htons(port);
s_in.sin_addr.s_addr = ntohl( inet_addr(saddr) ); /* convert to long
byte order */

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Rainer Temme

2005-06-23, 7:51 am

Hi Roman,

Sorry, i didn't follow the coplete thread ...
now i see you started with a
socket(AF_INET,SOCK_RAW,0)
... which failed ...
then moved to a
socket(PF_PACKET,SOCK_RAW,0)
... which works as root ...

Now I still have the feeling you're somewhat unsatisfied
with what you have ...

What I can tell you (without knowning what you want to do
with your socket) is:

There are different ways to create a raw-socket ...
like for ping you would do:

struct protoent *icmp_proto;

icmp_proto=getprotobyname("icmp");
fd=socket(AF_INET,SOCK_RAW,icmp_proto->p_proto);

quite a few manipulations on the socketbehaviour are possible with
the setsockopt() call...
have a look on the SO_BROADCAST or IP_HDRINCL options ...

But again, without knowning what you want to do it's impossible to
tell a good way.

Your repeated questions about the bind() let me think that
you don't even need a raw-socket ... you probably just
want some more controll about the way your socket works.

Regards ... Rainer
Roman Mashak

2005-06-23, 7:51 am

Hello, Rainer!
You wrote on Thu, 23 Jun 2005 13:12:34 +0200:

RT> What I can tell you (without knowning what you want to do
RT> with your socket) is:

RT> There are different ways to create a raw-socket ...
RT> like for ping you would do:

RT> struct protoent *icmp_proto;

RT> icmp_proto=getprotobyname("icmp");
RT> fd=socket(AF_INET,SOCK_RAW,icmp_proto->p_proto);

RT> quite a few manipulations on the socketbehaviour are possible with
RT> the setsockopt() call...
RT> have a look on the SO_BROADCAST or IP_HDRINCL options ...

RT> But again, without knowning what you want to do it's impossible to
RT> tell a good way.

RT> Your repeated questions about the bind() let me think that
RT> you don't even need a raw-socket ... you probably just
RT> want some more controll about the way your socket works.
Rainer, sorry if I confused you I'm just learning socket programming.
At the start I tried to use raw sockets, but later changed my mind for
SOCK_DGRAM sockets. What I clearly want is to send UDP packet, than as soon
as I get succes I gonna build up broadcast packets and finally try to use
raw sockets.

Anyway, by now I'm feeling troubles with a very simple code, I already
checked it thoroughly and can't figure out where is the issue.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netdb.h>

int main(void)
{
struct sockaddr_in s_in;
struct protoent *udp_proto;
char dp[1024];
unsigned short int port = 5000;
const char *saddr = "192.168.11.153";
int sd; /* socket descriptor */
int n; /* number of send bytes */

udp_proto = getprotobyname("udp");
if ( sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto) < 0 ) {
perror("socket() error!");
exit(1);
}

/* zero out structure */
bzero(&s_in, sizeof(s_in));

bzero(dp, sizeof(dp));

/* fill in structure */
s_in.sin_family = AF_INET;
s_in.sin_port = htons(port);
s_in.sin_addr.s_addr = inet_addr(saddr); /* convert to long byte order */

if ( (n = sendto(sd, dp, sizeof(dp), 0, (struct sockaddr *)&s_in,
sizeof(s_in))) < 0 ) {
perror("sendto() error");
exit(1);
}

close(sd);

return 0;
}

It seems to me - everything is fine here, nevertheless I get the same error:
"sendto() error: Socket operation on non-socket"

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Nils Weller

2005-06-23, 6:00 pm

In article <d9e9bm$2hmt$1@relay.tomsk.ru>, Roman Mashak wrote:
> if ( sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto) < 0 ) {


if ( (sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto)) < 0 ) {
^ ^

--
Nils R. Weller, Bremen / Germany
My real email address is ``nils<at>gnulinux<dot>nl''
.... but I'm not speaking for the Software Libre Foundation!
Roman Mashak

2005-06-23, 8:48 pm

Hello, Nils!
You wrote on 23 Jun 2005 15:48:45 GMT:

??>> if ( sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto) < 0 ) {

NW> if ( (sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto)) < 0 ) {
NW> ^ ^
Exactly! Problem was with parenthis. Oroginally 'sd' got boolean value, not
real socket ID. This is correct:

if ( (sd = socket(AF_INET, SOCK_DGRAM, udp_proto->p_proto)) < 0 )
....

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com