|
Home > Archive > Unix Programming > September 2006 > raw sockets in Linux
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 |
raw sockets in Linux
|
|
| Roman Mashak 2006-09-12, 7:53 pm |
| Hello,
"UNIX network programming, vol.1" explains that received UDP and TCP packets
are never passed to raw socket. But I have small example, which succefully
allows to receive UDP datagrams (specifically DHCP). Is linux exception?
<skip headers and some variable definitions for the sake of space>
int parsePacket(unsigned char *packet, int size, msg_t *m)
{
/* parse header, look for DHCP packets only */
...
}
int main(void)
{
int sock;
struct ifreq req;
/* create raw socket */
if ((sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) {
perror("socket() error");
exit(EXIT_FAILURE);
}
/* bind device to the socket */
strncpy(req.ifr_name, "eth0", sizeof(req.ifr_name));
if (ioctl(sock, SIOCGIFFLAGS, &req) < 0) {
perror("ioctl() error");
exit(EXIT_FAILURE);
}
/* set the interface promiscous mode */
req.ifr_flags |= IFF_PROMISC;
if (ioctl(sock, SIOCSIFFLAGS, &req) < 0) {
perror("ioctl() error");
exit(EXIT_FAILURE);
}
while (1) {
if ( (n = read(sock, buffer, BUFSIZE)) <= 0 ) {
printf("read() <= 0\n");
continue;
}
rc = parsePacket(buffer, n, msg);
......
}
return 0;
}
With best regards, Roman Mashak. E-mail: mrv@tusur.ru
| |
| Barry Margolin 2006-09-13, 1:30 am |
| In article <ee7jn3$20mn$1@relay.tomsk.ru>,
"Roman Mashak" <mrv@tusur.ru> wrote:
> Hello,
>
> "UNIX network programming, vol.1" explains that received UDP and TCP packets
> are never passed to raw socket. But I have small example, which succefully
> allows to receive UDP datagrams (specifically DHCP). Is linux exception?
Haven't you posted this question 2 or 3 times already?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Roman Mashak 2006-09-13, 1:30 am |
| Hello, Barry!
You wrote on Tue, 12 Sep 2006 21:56:22 -0400:
??>> Hello,
??>>
??>> "UNIX network programming, vol.1" explains that received UDP and TCP
??>> packets are never passed to raw socket. But I have small example,
??>> which succefully allows to receive UDP datagrams (specifically DHCP).
??>> Is linux exception?
BM> Haven't you posted this question 2 or 3 times already?
Yes, I have. But I'm still looking for clear answer. I searched this
newsgroup, and the only hint I got is "linux handles raw sockets in his own
way, non-standard" or something like that.
With best regards, Roman Mashak. E-mail: mrv@tusur.ru
| |
| David Schwartz 2006-09-13, 1:30 am |
|
Roman Mashak wrote:
> ??>> Hello,
> ??>>
> ??>> "UNIX network programming, vol.1" explains that received UDP and TCP
> ??>> packets are never passed to raw socket. But I have small example,
> ??>> which succefully allows to receive UDP datagrams (specifically DHCP).
> ??>> Is linux exception?
> BM> Haven't you posted this question 2 or 3 times already?
> Yes, I have. But I'm still looking for clear answer. I searched this
> newsgroup, and the only hint I got is "linux handles raw sockets in his own
> way, non-standard" or something like that.
Then why ask the question so that you are begging for precisely the
same answer? You ask, "is linux an exception". The only possible
answers to this are "yes, linux is an exception" and "no, linux is
typical." You have already gotten one of those answers, so why ask the
question again? Do you want the other answer?
Please use some common sense. If the question you are asking isn't
getting you the answer you want or expect, do two things:
1) Continue the thread, asking follow-up and clarifying questions
rather than starting back from scratch and frustrating those most able
to help you.
2) Don't ask just yes or no questions if you don't want just yes or no
questions. If you are experiencing some actual problem, don't just ask
if the behavior you see is expected, state what behavior you want and
why and ask how to get it.
DS
| |
| Roman Mashak 2006-09-13, 1:30 am |
| Hello, David!
You wrote on 12 Sep 2006 21:09:00 -0700:
??>> Yes, I have. But I'm still looking for clear answer. I searched this
??>> newsgroup, and the only hint I got is "linux handles raw sockets in
??>> his own way, non-standard" or something like that.
DS> Then why ask the question so that you are begging for precisely the
DS> same answer? You ask, "is linux an exception". The only possible
DS> answers to this are "yes, linux is an exception" and "no, linux is
DS> typical." You have already gotten one of those answers, so why ask the
DS> question again? Do you want the other answer?
I didn't actually want to bother anybody. I only hoped to get more detailed
explanation or link on it, that's it. Next time I'll follow "common sense"
rules 
With best regards, Roman Mashak. E-mail: mrv@tusur.ru
| |
| Barry Margolin 2006-09-13, 1:30 am |
| In article <ee7s25$2gjq$1@relay.tomsk.ru>,
"Roman Mashak" <mrv@tusur.ru> wrote:
> Hello, Barry!
> You wrote on Tue, 12 Sep 2006 21:56:22 -0400:
>
> ??>> Hello,
> ??>>
> ??>> "UNIX network programming, vol.1" explains that received UDP and TCP
> ??>> packets are never passed to raw socket. But I have small example,
> ??>> which succefully allows to receive UDP datagrams (specifically DHCP).
> ??>> Is linux exception?
>
> BM> Haven't you posted this question 2 or 3 times already?
> Yes, I have. But I'm still looking for clear answer. I searched this
> newsgroup, and the only hint I got is "linux handles raw sockets in his own
> way, non-standard" or something like that.
So don't just start a new thread with exactly the same question. Post
followup questions in the old thread, asking for clarification. This is
supposed to be a DISCUSSION group, so DISCUSS it.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Nils O. Selåsdal 2006-09-16, 1:43 pm |
| Roman Mashak wrote:
> Hello,
>
> "UNIX network programming, vol.1" explains that received UDP and TCP packets
> are never passed to raw socket. But I have small example, which succefully
> allows to receive UDP datagrams (specifically DHCP). Is linux exception?
>
> <skip headers and some variable definitions for the sake of space>
Here's what the docs says:
Raw sockets may tap all IP protocols in Linux, even protocols like ICMP
or TCP which have a protocol module in the kernel. In this case the
packets are passed to both the kernel module and the raw socket(s).
This should not be relied upon in portable programs, many other BSD
socket implementation have limitations here.
| |
| Roman Mashak 2006-09-16, 1:43 pm |
| Hello, "Nils!
You wrote on Wed, 13 Sep 2006 08:42:00 +0200:
NOS> Roman Mashak wrote:
??>> Hello,
??>>
??>> "UNIX network programming, vol.1" explains that received UDP and TCP
??>> packets are never passed to raw socket. But I have small example,
??>> which succefully allows to receive UDP datagrams (specifically DHCP).
??>> Is linux exception? <skip headers and some variable definitions for
??>> the sake of space>
NOS> Here's what the docs says:
NOS> Raw sockets may tap all IP protocols in Linux, even protocols like
ICMP
NOS> or TCP which have a protocol module in the kernel. In this case
the
NOS> packets are passed to both the kernel module and the raw socket(s).
NOS> This should not be relied upon in portable programs, many other BSD
NOS> socket implementation have limitations here.
Thank you, that's what I've been looking for. Can you give me a link on
quoted document?
With best regards, Roman Mashak. E-mail: mrv@tusur.ru
| |
| Nils O. Selåsdal 2006-09-16, 1:43 pm |
| Roman Mashak wrote:
> Hello, "Nils!
> You wrote on Wed, 13 Sep 2006 08:42:00 +0200:
>
> NOS> Roman Mashak wrote:
> ??>> Hello,
> ??>>
> ??>> "UNIX network programming, vol.1" explains that received UDP and TCP
> ??>> packets are never passed to raw socket. But I have small example,
> ??>> which succefully allows to receive UDP datagrams (specifically DHCP).
> ??>> Is linux exception? <skip headers and some variable definitions for
> ??>> the sake of space>
> NOS> Here's what the docs says:
>
> NOS> Raw sockets may tap all IP protocols in Linux, even protocols like
> ICMP
> NOS> or TCP which have a protocol module in the kernel. In this case
> the
> NOS> packets are passed to both the kernel module and the raw socket(s).
> NOS> This should not be relied upon in portable programs, many other BSD
> NOS> socket implementation have limitations here.
> Thank you, that's what I've been looking for. Can you give me a link on
> quoted document?
>
> With best regards, Roman Mashak. E-mail: mrv@tusur.ru
It was in man 7 raw
| |
| Chris Friesen 2006-09-16, 1:43 pm |
| Nils O. Selåsdal wrote:
> Raw sockets may tap all IP protocols in Linux, even protocols like ICMP
> or TCP which have a protocol module in the kernel. In this case the
> packets are passed to both the kernel module and the raw socket(s).
> This should not be relied upon in portable programs, many other BSD
> socket implementation have limitations here.
And if you *don't* want the packet to go to the kernel as well
(userspace protocol stack, for instance), one fairly simple way to work
around it is to insert a netfilter module that mangles the protocol
number (on those specific packets) to something that the kernel doesn't
know how to deal with. Then your userspace app can convert it back and
handle the packet normally.
Chris
|
|
|
|
|