Example #1
0
int worker_send(worker_t *w, mpudp_packet_t *p, int type)
{
    eth_frame_t eth_frame;
    ip_packet_t ip_packet;
    udp_dgram_t udp_dgram;

    unsigned char *eth_payload, *ip_payload, *udp_payload;

    /* printf("[%d] - sending packet... %d len: %d\n", w->id, p->id, p->len); */

    int eth_len, ip_len, udp_len;

    if(type == SEND_UCAST)
    {
        eth_build_frame(&eth_frame, w->dst_mac, w->src_mac, ETH_TYPE_IP);
        ip_build_packet(&ip_packet, w->src_ip, w->dst_ip);
        udp_build_dgram_hdr(&udp_dgram, w->src_port, w->dst_port);
    }
    else if(type == SEND_BCAST)
    {
        eth_build_frame(&eth_frame, w->bcast_mac, w->src_mac, ETH_TYPE_IP);
        ip_build_packet(&ip_packet, w->src_ip, w->bcast_ip);
        udp_build_dgram_hdr(&udp_dgram, w->src_port, w->dst_port);
    }

    uint8_t *payload;
    int len = mpudp_packet2chars(p, &payload);
    /* printf("Packet len: %d\n", *(uint32_t*)payload+5); */

    udp_set_data(&udp_dgram, payload, len);
    udp_len = udp_dgram2chars(&udp_dgram, &udp_payload);

    ip_set_data(&ip_packet, udp_payload, udp_len);
    ip_len = ip_packet2chars(&ip_packet, &ip_payload);

    eth_set_data(&eth_frame, ip_payload, ip_len);
    eth_len = eth_frame2chars(&eth_frame, &eth_payload);

    pcap_sendpacket(w->if_handle, eth_payload, eth_len);

    free(udp_dgram.data);
    free(ip_packet.payload);
    free(eth_frame.data);

    free(eth_payload);
    free(ip_payload);
    free(udp_payload);
    free(payload);

    return 0;
}
Example #2
0
/*!
 * Function name: arp_parse
 * \return ERR_OK or ERR_DEVICE_DRIVER
 * \param eth_frame : [in] Ethernet frame.
 * \param net_adapter : [in] Adpater providing the frame.
 * \brief Parse the IP frame.
 * *******************************************************************/
err_t arp_parse(u8_t* eth_frame, NETIF_T *net_adapter)
{
  err_t err = ERR_OK;
  ARP_HEADER_T* arp_header = (ARP_HEADER_T*)((u8_t*)eth_frame + sizeof(ETHER_HEADER_T));
  if( ntohl(arp_header->target_ip_addr) == net_adapter->ip_addr) { //Update ARP table only if the peer device is the target.
    if(arp_header->operation == htons(ARP_REQUEST)) { //The peer device sends an arp request to cIPS. cIPS replies.
      u32_t framelen = eth_build_frame(arp_header->sender_hw_addr, net_adapter->mac_address, ntohl(arp_header->sender_ip_addr), net_adapter->ip_addr, net_adapter->control_buffer, ETH_ARP_REPLY);
      err = netif_send(net_adapter, net_adapter->control_buffer, framelen);
    } else if (arp_header->operation == htons(ARP_RESPONSE)) { //cIPS sent an arp request to a peer device. The peer device replied and here is the reply processing.
        err = arp_update_cache(&(net_adapter->arp_cache), ntohl(arp_header->sender_ip_addr), arp_header->sender_hw_addr);
    }
  }

  return err;
}