ExtFunc void SendPacket(NetPacketType type, int size, void *data) { netint2 header[2]; header[0] = hton2(type); header[1] = hton2(size + HEADER_SIZE); if (MyWrite(sock, header, HEADER_SIZE) != HEADER_SIZE) die("write"); if (size > 0 && data && MyWrite(sock, data, size) != size) die("write"); }
static int arp_sendpkt(uint16 operation, uint8 macaddr[], uint32 ipaddr) { struct netbuf *pkt; struct arp_header *arphdr; uint32 ipaddr_n; pkt = kz_kmalloc(DEFAULT_NETBUF_SIZE); memset(pkt, 0, DEFAULT_NETBUF_SIZE); pkt->cmd = ETHERNET_CMD_SEND; pkt->top = pkt->data + 64; pkt->size = sizeof(struct arp_header); arphdr = (struct arp_header *)pkt->top; arphdr->hardware = hton2(ARP_HARDWARE_ETHER); arphdr->protocol = hton2(ETHERNET_TYPE_IP); arphdr->macaddr_size = MACADDR_SIZE; arphdr->ipaddr_size = IPADDR_SIZE; arphdr->operation = hton2(operation); memcpy(arphdr->sender_macaddr, my_macaddr, MACADDR_SIZE); ipaddr_n = hton4(my_ipaddr); memcpy(arphdr->sender_ipaddr, &ipaddr_n, IPADDR_SIZE); memcpy(arphdr->target_macaddr, (macaddr ? macaddr : (uint8 *)"\x00\x00\x00\x00\x00\x00"), MACADDR_SIZE); ipaddr_n = hton4(ipaddr); memcpy(arphdr->target_ipaddr, &ipaddr_n, IPADDR_SIZE); memcpy(pkt->option.ethernet.send.dst_macaddr, (macaddr ? macaddr : (uint8 *)"\xff\xff\xff\xff\xff\xff"), MACADDR_SIZE); pkt->option.ethernet.send.type = ETHERNET_TYPE_ARP; kz_send(MSGBOX_ID_ETHPROC, 0, (char *)pkt); return 0; }
static int icmp_sendpkt(uint32 ipaddr, uint8 type, uint8 code, uint16 id, uint16 sequence_number, int datasize, char *data) { struct netbuf *pkt; struct icmp_header *icmphdr; int i; char c; pkt = kz_kmalloc(DEFAULT_NETBUF_SIZE); memset(pkt, 0, DEFAULT_NETBUF_SIZE); pkt->cmd = IP_CMD_SEND; pkt->top = pkt->data + 64; pkt->size = sizeof(struct icmp_header) + datasize; icmphdr = (struct icmp_header *)pkt->top; icmphdr->type = type; icmphdr->code = code; icmphdr->param.id = hton2(id); icmphdr->param.sequence_number = hton2(sequence_number); for (i = 0; i < datasize; i++) { c = data ? data[i] : (i & 0xff); ((char *)icmphdr + sizeof(*icmphdr))[i] = c; } icmphdr->checksum = 0; icmphdr->checksum = hton2(ip_calc_checksum(pkt->size, icmphdr)); pkt->option.ip.send.protocol = IP_PROTOCOL_ICMP; pkt->option.ip.send.dst_addr = ipaddr; kz_send(MSGBOX_ID_IPPROC, 0, (char *)pkt); return 0; }