Sockets & Proof of Concept

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Sockets & Proof of Concept

    Hello,

    I am trying to familiarize myself with sockets, etc. for a project I
    am working on. In light of this, I tried earlier to write a simple
    server and client using a raw socket, and construcing the ip header
    myself. Rather unfortunately, I can't get it to work - and I can't
    figure out why:( Tcpdump doesn't show any packets being sent out
    between the two machines, and the receiving program simply blocks
    within the recvfrom call even after I excecute a sendto to the
    appropriate port from another machine.

    Anyway, the code I'm using is below...any info that will help my
    conceptual confusion with this would be greatly appreciated.

    The code that sends the data to another machine using sendto is called
    in the following manner
    ./ip_udp_test <destination host name> <destination port> <source
    port>

    The code that receives the data is called in the following manner
    ./ip_udp_test <port on which data will be received>

    I am of course making sure that destination port = port on which data
    will be received.

    In the code below, note that I'm padding the iphdr with 12 bytes in
    order to make the header 32bytes, I seem to recall reading somewhere
    that that was necessary...is this so?

    Also, are my ip header values set properly?


    Thank you for all your help.
    Raja


    /* Code to send some random data to another machine using a raw
    socket, and udp */

    struct ip_udp_packet {
    struct iphdr ip_bits;
    char iphdr_padding[12];
    struct udphdr udp_bits;
    char payload[100];
    };
    typedef struct ip_udp_packet ip_udp_packet_t;

    int main(int argc, char **argv) {
    int sock;
    int hdrincl = 1;
    ip_udp_packet_t packet;
    char host_name[256];
    struct hostent *ret;
    long int source_addr;
    long int dest_addr;
    int source_port;
    int dest_port;
    struct sockaddr_in to;
    int retval;
    int blah;

    /* Error checking */
    if (argc != 4 ) {
    printf("Error : Options - desination name, dest port, source
    port");
    exit(-1);
    }

    /* Initialization */
    memset( &packet, 0, sizeof(ip_udp_packet_t));

    /* Get our name */
    gethostname( host_name, 256 );
    ret = gethostbyname(host_name);
    memcpy( &source_addr, *(ret->h_addr_list), 4);
    source_addr = ntohl( source_addr );

    /* Get name of host we wish to send to */
    ret = gethostbyname(argv[1]);
    memcpy( &dest_addr, *(ret->h_addr_list), 4);
    dest_addr = ntohl( dest_addr );

    /* Get the destination port */
    sscanf( argv[2], "%d", &dest_port );
    sscanf( argv[3], "%d", &source_port );


    /* Create a new socket using the raw protocol */
    sock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
    if ( setsockopt(sock, IPPROTO_IP,
    IP_HDRINCL, &hdrincl, sizeof(hdrincl)) == -1 ) {
    printf("Failed to set socket options.\n");
    exit(-1);
    }

    /* Create the packet */
    packet.ip_bits.version = IPVERSION; /* 4 */
    packet.ip_bits.ihl = 5; /* Minimum IP header
    length */
    packet.ip_bits.tos = 0; /* Filled in by kernel */
    packet.ip_bits.tot_len = htons(sizeof(ip_udp_packet_t));
    packet.ip_bits.id = 0; /* Let Kernel handle this
    */
    packet.ip_bits.frag_off = 0; /* No fragmentation */
    packet.ip_bits.ttl = IPDEFTTL; /* 64 */
    packet.ip_bits.protocol = IPPROTO_UDP; /* 17 */
    packet.ip_bits.check = 0; /* Checksum filled in by
    kernel! */
    packet.ip_bits.saddr = htonl( source_addr );
    packet.ip_bits.daddr = htonl( dest_addr );

    packet.udp_bits.source = htons( source_port );
    packet.udp_bits.dest = htons( dest_port );
    packet.udp_bits.len = htons(sizeof(struct udphdr) + 100);

    memset( packet.payload, 1, 256 );


    /* Send the packet */
    to.sin_family = AF_INET;
    to.sin_port = htons( source_port);
    to.sin_addr.s_addr = htonl(dest_addr);
    retval = sendto( sock, &packet , sizeof(ip_udp_packet_t),
    0, (struct sockaddr *)&to, sizeof(struct sockaddr));
    if ( retval == -1 ) {
    printf("sendtofailed\n");
    exit(-1);
    }



    /* Code that receives the packet on another machine */
    int main( int argc, char **argv) {
    int sock;
    char buf[100];
    int listen_port;
    struct sockaddr_in my;
    struct sockaddr_in their;
    int their_len;

    /* Get input */
    sscanf( argv[1], "%d", &listen_port );


    /* Create a new socket using the raw protocal */
    sock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
    if ( sock == -1 ) {
    printf("Cannot create socket: %. \n", strerror(errno));
    }

    /* Bind the socket */
    my.sin_family = AF_INET;
    my.sin_port = htons(listen_port);
    my.sin_addr.s_addr = INADDR_ANY;
    memset( &(my.sin_zero), '\0', 8);
    if( bind( sock, (struct sockaddr *)&my, sizeof(struct sockaddr)) ==
    -1) {
    printf("Cannot bind socket: %s.\n", strerror(errno));
    exit(-1);
    }

    /* Attempt to receive from the socket */
    their_len = sizeof(struct sockaddr);
    if( recvfrom(sock, buf, 100, 0,
    (struct sockaddr *)&their, &their_len) == -1 ) {
    printf("Cannot receive: %s", strerror(errno));
    exit(-1);
    }

    close( sock );
    exit(0);
    }
    Raja Sambasivan Guest

  2. Similar Questions and Discussions

    1. Bullet Proof?
      Does anyone know where the bullets are? As in text bullet points.. Andy
    2. Design Concept
      Hello, Im in the making of designing a webpage for myself and I was wanting to know other peoples opinion on it. you will notice it is abit...
    3. Proof reader please
      Could someone please look at the syntax below and notice if there are any obvious problems? What I want to achieve is that when the URL...
    4. PostBack Concept
      I want to know if the PostBack concept applies to HTML web-based forms, regardless of what programming technologies we use: For example, ASP,...
    5. concept check
      Hey, I am looking for some oppinions of my concept to replace a template I am currently using. the concept... Just a mock nothing works...
  3. #2

    Default Re: Sockets & Proof of Concept

    Too long. Isolate the point of failure with a trival program. Then post it
    with a description of what happend as well as what you expected to happen.

    Mike
    Michael B Allen Guest

  4. #3

    Default Re: Sockets & Proof of Concept

    In article <f394e693.0307181442.219d2fa7@posting.google.com >,
    Raja Sambasivan <RS1999ent@aol.com> wrote:
    >Hello,
    >
    >I am trying to familiarize myself with sockets, etc. for a project I
    >am working on. In light of this, I tried earlier to write a simple
    >server and client using a raw socket
    Raw sockets are a pretty advanced feature. If this is your first
    experience with sockets, I suggest you start with more basic features and
    work your way up.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  5. #4

    Default Re: Sockets & Proof of Concept

    Barry Margolin wrote:
    > In article <f394e693.0307181442.219d2fa7@posting.google.com >,
    > Raja Sambasivan <RS1999ent@aol.com> wrote:
    >
    >>Hello,
    >>
    >>I am trying to familiarize myself with sockets, etc. for a project I
    >>am working on. In light of this, I tried earlier to write a simple
    >>server and client using a raw socket
    >
    > Raw sockets are a pretty advanced feature. If this is your first
    > experience with sockets, I suggest you start with more basic features and
    > work your way up.
    [url]http://www.gnu.org/manual/glibc-2.2.5/html_node/Sockets.html#Sockets[/url]

    Russell Shaw Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139