ssize_t eth_send(eth_t *eth, const void *buf, size_t len) { PacketInitPacket(eth->pkt, (void *)buf, (UINT) len); PacketSendPacket(eth->lpa, eth->pkt, TRUE); return ((ssize_t) len); }
void WritePacket(int Snaplen) { LPPACKET lpPacket = 0; // define a pointer to a PACKET structure if (Snaplen > sizeof(tx_buffer)) Snaplen = sizeof(tx_buffer); lpPacket = CreatePacket((char*)tx_buffer, Snaplen); PacketSetNumWrites(lpAdapter, 1); PacketSendPacket(lpAdapter, lpPacket, TRUE); ReleasePacket(lpPacket); }
INT PacketSendPackets(LPADAPTER AdapterObject, PVOID PacketBuff, ULONG Size, BOOLEAN Sync) { struct dump_bpf_hdr *winpcap_hdr; PCHAR EndOfUserBuff = (PCHAR)PacketBuff + Size; LPPACKET PacketToSend; BOOLEAN res; // Start from the first packet winpcap_hdr = (struct dump_bpf_hdr*)PacketBuff; if( (PCHAR)winpcap_hdr + winpcap_hdr->caplen + sizeof(struct dump_bpf_hdr) > EndOfUserBuff ) { // Malformed buffer return 0; } while( TRUE ){ if(winpcap_hdr->caplen ==0 || winpcap_hdr->caplen > 65536) { // Malformed header return 0; } // Set up the LPPACKET structure PacketToSend=PacketAllocatePacket(); PacketInitPacket(PacketToSend, (PCHAR)winpcap_hdr + sizeof(struct dump_bpf_hdr), winpcap_hdr->caplen); // Send the packet res = PacketSendPacket (AdapterObject, PacketToSend, TRUE); // Free the just used LPPACKET structure PacketFreePacket(PacketToSend); if(res == FALSE){ // Error sending the packet return (PCHAR)winpcap_hdr - (PCHAR)PacketBuff; } // Step to the next packet in the buffer (PCHAR)winpcap_hdr += winpcap_hdr->caplen + sizeof(struct dump_bpf_hdr); // Check if the end of the user buffer has been reached if( (PCHAR)winpcap_hdr >= EndOfUserBuff ) { return (PCHAR)winpcap_hdr - (PCHAR)PacketBuff; } } }
long sockSendData(void *pData, int Size) { u8 *data = (u8*)pData; // printf("_sendPacket %d (time=%d)\n", Size, timeGetTime()); while (Size > 0) { PacketInitPacket(lpSendPacket, data, Size > 1024 ? 1024 : Size); if(PacketSendPacket(lpAdapter,lpSendPacket,FALSE)==FALSE){ printf("Error: PacketSendPacket failed\n"); return (-1); } data+= 1024; Size-= 1024; PacketFreePacket(lpSendPacket); } return 0; }
static unsigned int WINAPI ether_thread_write_packets(void *arg) { LPPACKET Packet; thread_active_1 = true; D(bug("ether_thread_write_packets start\n")); while(thread_active) { // must be alertable, otherwise write completion is never called WaitForSingleObjectEx(int_send_now,INFINITE,TRUE); while( thread_active && (Packet = get_send_head()) != 0 ) { switch (net_if_type) { case NET_IF_ROUTER: if(router_write_packet((uint8 *)Packet->Buffer, Packet->Length)) { Packet->bIoComplete = TRUE; recycle_write_packet(Packet); } break; case NET_IF_FAKE: Packet->bIoComplete = TRUE; recycle_write_packet(Packet); break; case NET_IF_B2ETHER: if(!PacketSendPacket( fd, Packet, FALSE, TRUE )) { // already recycled if async } break; case NET_IF_TAP: if (!tap_send_packet(fd, Packet, FALSE, TRUE)) { // already recycled if async } break; case NET_IF_SLIRP: slirp_input((uint8 *)Packet->Buffer, Packet->Length); Packet->bIoComplete = TRUE; recycle_write_packet(Packet); break; } } } D(bug("ether_thread_write_packets exit\n")); thread_active_1 = false; return(0); }
/** * Send a packet * * @param adapter adapter handle received by a call to init_adapter * @param buffer complete packet to send (including ETH header; without CRC) * @param len length of the packet (including ETH header; without CRC) */ int packet_send(void *adapter, void *buffer, int len) { struct packet_adapter *pa = (struct packet_adapter*)adapter; LPPACKET lpPacket; if (pa == NULL) { return -1; } if ((lpPacket = PacketAllocatePacket()) == NULL) { return -1; } PacketInitPacket(lpPacket, buffer, len); if (!PacketSendPacket(pa->lpAdapter, lpPacket, TRUE)) { return -1; } PacketFreePacket(lpPacket); return 0; }
/* Send a packet to the network */ int pcap_sendpacket(pcap_t *p, u_char *buf, int size){ LPPACKET PacketToSend; if (p->adapter==NULL) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Writing a packet is allowed only on a physical adapter"); return -1; } PacketToSend=PacketAllocatePacket(); PacketInitPacket(PacketToSend,buf,size); if(PacketSendPacket(p->adapter,PacketToSend,TRUE) == FALSE){ PacketFreePacket(PacketToSend); return -1; } PacketFreePacket(PacketToSend); return 0; }
void bx_win32_pktmover_c::sendpkt(void *buf, unsigned io_len) { #if BX_ETH_WIN32_LOGGING fprintf (pktlog_txt, "a packet from guest to host, length %u\n", io_len); Bit8u *charbuf = (Bit8u *)buf; unsigned n; for (n=0; n<io_len; n++) { if (((n % 16) == 0) && n>0) fprintf (pktlog_txt, "\n"); fprintf (pktlog_txt, "%02x ", (unsigned)charbuf[n]); } fprintf (pktlog_txt, "\n--\n"); fflush (pktlog_txt); #endif // SendPacket Here. PacketInitPacket(pkSend, (char *)buf, io_len); if (!PacketSendPacket(lpAdapter, pkSend, TRUE)) { fprintf(stderr, "[ETH-WIN32] Error sending packet: %lu\n", GetLastError()); } }
/* * Our WinPcap send function. * Not sure if partial writes are possible with NPF. Retry if rc != length. */ int pkt_send (const void *tx, int length) { const ADAPTER *adapter; PACKET pkt; int tx_cnt, rc = 0; ASSERT_PKT_INF (0); adapter = (const ADAPTER*) _pkt_inf->adapter; PROFILE_START ("pkt_send"); for (tx_cnt = 1 + pkt_txretries; tx_cnt > 0; tx_cnt--) { pkt.Buffer = (void*) tx; pkt.Length = length; if (PacketSendPacket (adapter, &pkt, TRUE)) { rc = length; break; } STAT (macstats.num_tx_retry++); } if (rc == length) { num_tx_pkt++; num_tx_bytes += length; } else { num_tx_errors++; /* local copy */ STAT (macstats.num_tx_err++); } PROFILE_STOP(); return (rc); }
//发包 int NetworkInterface::SendPacket(u_char *buffer, int packetLen, int repeatNum) { LPPACKET lpPacket; memcpy((buffer + 6), srcMAC, 6); //分配并初始化一个包结构,将用于发送数据包 if ( (lpPacket = PacketAllocatePacket()) == NULL ) { LastErrCode = GetLastError(); strcpy_s(LastErrStr, "Error: failed to allocate the LPPACKET structure."); return (-1); } PacketInitPacket(lpPacket, buffer, packetLen); if (PacketSetNumWrites(adapter.lpAdapter, repeatNum) == FALSE) { LastErrCode = GetLastError(); strcpy_s(LastErrStr, "Unable to send more than one packet in a single write!"); PacketFreePacket(lpPacket); return -1; } if(PacketSendPacket(adapter.lpAdapter, lpPacket, TRUE) == FALSE) { LastErrCode = GetLastError(); strcpy_s(LastErrStr, "Error sending the packets!"); PacketFreePacket(lpPacket); return -1; } PacketFreePacket(lpPacket); //将发送的数据包转存到文件中 //PrintPacket(buffer, packetLen, 1); return 0; }
int libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s) { static BYTE dst[ETHER_ADDR_LEN]; static BYTE src[ETHER_ADDR_LEN]; uint8_t *packet = NULL; uint32_t packet_s; LPPACKET lpPacket = NULL; DWORD remoteip = 0; DWORD BytesTransfered; NetType type; struct libnet_ipv4_hdr *ip_hdr = NULL; memset(dst, 0, sizeof(dst)); memset(src, 0, sizeof(src)); packet_s = payload_s + l->link_offset; packet = (uint8_t *)malloc(packet_s); if (packet == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): failed to allocate packet\n", __func__); return (-1); } /* we have to do the IP checksum */ if (libnet_do_checksum(l, payload, IPPROTO_IP, LIBNET_IPV4_H) == -1) { /* error msg set in libnet_do_checksum */ return (-1); } /* MACs, IPs and other stuff... */ ip_hdr = (struct libnet_ipv4_hdr *)payload; memcpy(src, libnet_get_hwaddr(l), sizeof(src)); remoteip = ip_hdr->ip_dst.S_un.S_addr; /* check if the remote station is the local station */ if (remoteip == libnet_get_ipaddr4(l)) { memcpy(dst, src, sizeof(dst)); } else { memcpy(dst, libnet_win32_get_remote_mac(l, remoteip), sizeof(dst)); } PacketGetNetType(l->lpAdapter, &type); switch(type.LinkType) { case NdisMedium802_3: libnet_win32_build_fake_ethernet(dst, src, ETHERTYPE_IP, payload, payload_s, packet, l , 0); break; case NdisMedium802_5: libnet_win32_build_fake_token(dst, src, ETHERTYPE_IP, payload, payload_s, packet, l, 0); break; case NdisMediumFddi: break; case NdisMediumWan: case NdisMediumAtm: case NdisMediumArcnet878_2: default: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): network type (%d) is not supported\n", __func__, type.LinkType); return (-1); break; } BytesTransfered = -1; if ((lpPacket = PacketAllocatePacket()) == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): failed to allocate the LPPACKET structure\n", __func__); return (-1); } PacketInitPacket(lpPacket, packet, packet_s); /* PacketSendPacket returns a BOOLEAN */ if (PacketSendPacket(l->lpAdapter, lpPacket, TRUE)) { BytesTransfered = packet_s; } PacketFreePacket(lpPacket); free(packet); return (BytesTransfered); }