Thread: How to get packet IP?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    How to get packet IP?

    I see when I create the socket:
    socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
    and I set the netcard to PROMISC,I can receive
    all packet including ether,but I want to
    receive packet IP,including ICMP,UDP,TCP,not ether,
    how to create the socket?and how to set it up?

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I don't quite understand what your asking.
    Are you asking how to create a socket that'll receive IMCP, UDP, and TCP communications together, without having to deal with raw sockets?
    If so: It doesn't work that way. You'll have to create a socket for each protocol you want to handle, but bind them all to the same port & address, then multiplex them.
    Or are you just asking how to get the IP of the peer you're communicating with?
    If that, then: See the man page for recvfrom(), it's pretty straight forward from there.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    50
    Yes,I wand to know how to create a socket that'll receive IMCP, UDP, and TCP communications together,you said I must create a socket for each protocol ,
    I see I go to try.Thank you

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    50
    Hello,Yarin:
    I create a socket:
    socket(AF_INET,SOCK_RAW,IPPROTO_ICMP|IPPROTO_UDP|I PPROTO_TCP);
    but when I recieve I find I can't some packet;
    so I create three socket:
    sk1=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    sk2=socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
    sk3=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
    and bind them all to the same port & address,but I don't how to multiplex them?
    Could you tell me?Thank you very much.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by leetow2003 View Post
    I don't how to multiplex them?
    Could you tell me?Thank you very much.
    select()

    Waiting for I/O - The GNU C Library

    Nb. that current linux/POSIX systems require:

    Code:
    #include <sys/select.h>
    with select(), which is not mentioned in that documentation.
    Last edited by MK27; 07-11-2011 at 07:29 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    50
    I create a socket:
    socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    I want to know how to set this socket to promisc?

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by leetow2003 View Post
    I create a socket:
    socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
    I want to know how to set this socket to promisc?
    All right, your asking this again got me curious... I'm afraid I fed you some misinformation.
    You _can_, in fact, get the raw data for all protocols (promiscuous mode). A la:
    Code:
    int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    More information on this.

    On multiplexing: As MK27 as pointed out, select() is one way of doing it, poll() is another. Both are widely supported (though select() is older). Unless performance is a considerable issue for you, which one to use is pretty much about preference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. packet sniffer
    By l2u in forum Networking/Device Communication
    Replies: 6
    Last Post: 09-20-2007, 08:53 PM
  2. packet analyzer in c
    By althagafi in forum Networking/Device Communication
    Replies: 16
    Last Post: 08-06-2004, 03:35 PM
  3. packet analyzer in c
    By althagafi in forum C Programming
    Replies: 1
    Last Post: 07-26-2004, 11:46 PM
  4. Packet Simulation
    By RM1205 in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-18-2004, 11:05 PM
  5. TCP/IP packet run-together-ing
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-10-2003, 04:42 PM