+/**
+ * \brief Creates and sends an IPv4 Packet
+ * \param Iface Interface
+ * \param Address Destination IP
+ * \param Protocol Protocol ID
+ * \param ID Some random ID number
+ * \param Length Data Length
+ * \param Data Packet Data
+ * \return Boolean Success
+ */
+int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data)
+{
+ tMacAddr to = ARP_Resolve4(Iface, Address);
+ int bufSize = sizeof(tIPv4Header) + Length;
+ char buf[bufSize];
+ tIPv4Header *hdr = (void*)buf;
+ int ret;
+
+ if( MAC_EQU(to, cMAC_ZERO) ) {
+ // No route to host
+ Log_Notice("IPv4", "No route to host %i.%i.%i.%i",
+ Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
+ return 0;
+ }
+
+ // OUTPUT Firewall rule go here
+ ret = IPTables_TestChain("OUTPUT",
+ 4, (tIPv4*)Iface->Address, &Address,
+ Protocol, 0,
+ Length, Data);
+ if(ret > 0) {
+ // Just drop it (with an error)
+ Log_Notice("IPv4", "Firewall dropped packet");
+ return 0;
+ }
+
+ memcpy(&hdr->Options[0], Data, Length);
+ hdr->Version = 4;
+ hdr->HeaderLength = sizeof(tIPv4Header)/4;
+ hdr->DiffServices = 0; // TODO: Check
+
+ hdr->Reserved = 0;
+ hdr->DontFragment = 0;
+ hdr->MoreFragments = 0;
+ hdr->FragOffLow = 0;
+ hdr->FragOffHi = 0;
+
+ hdr->TotalLength = htons( bufSize );
+ hdr->Identifcation = htons( ID ); // TODO: Check
+ hdr->TTL = DEFAULT_TTL;
+ hdr->Protocol = Protocol;
+ hdr->HeaderChecksum = 0; // Will be set later
+ hdr->Source = *(tIPv4*)Iface->Address;
+ hdr->Destination = Address;
+ hdr->HeaderChecksum = htons( IPv4_Checksum(hdr, sizeof(tIPv4Header)) );
+
+ Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
+ Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
+ Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
+ return 1;
+}
+