diff --git a/include/ip.h b/include/ip.h index 3225c18..58c214f 100644 --- a/include/ip.h +++ b/include/ip.h @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __IP_H #define __IP_H diff --git a/include/packetsender.h b/include/packetsender.h index 068736b..6787fb3 100644 --- a/include/packetsender.h +++ b/include/packetsender.h @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __PACKET_SENDER_H #define __PACKET_SENDER_H @@ -28,7 +49,7 @@ namespace Tins { IP_SOCKET, SOCKETS_END }; - static const int INVALID_RAW_SOCKET = -10; + static const int INVALID_RAW_SOCKET; std::vector _sockets; }; diff --git a/include/pdu.h b/include/pdu.h index 762c375..9aad675 100644 --- a/include/pdu.h +++ b/include/pdu.h @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __PDU_H #define __PDU_H diff --git a/include/tcp.h b/include/tcp.h index a7ee5fa..18ddf9d 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __TCP_H #define __TCP_H @@ -40,6 +61,7 @@ namespace Tins { void window(uint16_t new_window); void check(uint16_t new_check); void urg_ptr(uint16_t new_urg_ptr); + void payload(uint8_t *new_payload, uint32_t new_payload_size); void set_flag(Flags tcp_flag, uint8_t value); @@ -87,6 +109,8 @@ namespace Tins { static const uint16_t DEFAULT_WINDOW; tcphdr _tcp; + uint8_t *_payload; + uint32_t _payload_size; }; }; diff --git a/include/utils.h b/include/utils.h index ae1b2ef..aed67f0 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __UTILS_H #define __UTILS_H diff --git a/src/ip.cpp b/src/ip.cpp index 35c254d..a7b2316 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #include #include #ifndef WIN32 diff --git a/src/packetsender.cpp b/src/packetsender.cpp index 04b4fb2..23829f2 100644 --- a/src/packetsender.cpp +++ b/src/packetsender.cpp @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef WIN32 #include #include @@ -14,6 +35,8 @@ #include "packetsender.h" +const int Tins::PacketSender::INVALID_RAW_SOCKET = -10; + Tins::PacketSender::PacketSender() : _sockets(SOCKETS_END, INVALID_RAW_SOCKET) { } @@ -49,7 +72,6 @@ bool Tins::PacketSender::send_l3(PDU *pdu, const struct sockaddr* link_addr, uin bool ret_val = true; if(!open_l3_socket()) ret_val = false; - if (ret_val) { uint32_t sz; int sock = _sockets[IP_SOCKET]; diff --git a/src/pdu.cpp b/src/pdu.cpp index aef15bb..9b8e2fd 100644 --- a/src/pdu.cpp +++ b/src/pdu.cpp @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #include #include "pdu.h" diff --git a/src/tcp.cpp b/src/tcp.cpp index f599cff..bdd0656 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #include #include #ifndef WIN32 @@ -9,7 +30,7 @@ const uint16_t Tins::TCP::DEFAULT_WINDOW = 32678; -Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP) { +Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP), _payload(0), _payload_size(0) { std::memset(&_tcp, 0, sizeof(tcphdr)); _tcp.dport = Utils::net_to_host_s(dport); _tcp.sport = Utils::net_to_host_s(sport); @@ -45,6 +66,12 @@ void Tins::TCP::urg_ptr(uint16_t new_urg_ptr) { _tcp.urg_ptr = new_urg_ptr; } +void Tins::TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) { + delete[] _payload; + _payload = new_payload; + _payload_size = new_payload_size; +} + void Tins::TCP::set_flag(Flags tcp_flag, uint8_t value) { switch(tcp_flag) { case FIN: @@ -75,7 +102,7 @@ void Tins::TCP::set_flag(Flags tcp_flag, uint8_t value) { } uint16_t Tins::TCP::do_checksum() const { - const uint8_t *ptr = (const uint8_t*)&_tcp, *end = (const uint8_t*)&_tcp + sizeof(tcphdr); + const uint8_t *ptr = (const uint8_t*)_payload, *end = (const uint8_t*)_payload + _payload_size; uint16_t checksum(0); while(ptr < end) checksum += *(ptr++); @@ -83,11 +110,12 @@ uint16_t Tins::TCP::do_checksum() const { } uint32_t Tins::TCP::header_size() const { - return sizeof(tcphdr); + return sizeof(tcphdr) + _payload_size; } void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz) { - assert(total_sz >= sizeof(tcphdr)); + assert(total_sz >= header_size()); _tcp.check = Utils::net_to_host_s(do_checksum()); memcpy(buffer, &_tcp, sizeof(tcphdr)); + memcpy(buffer + sizeof(tcphdr), _payload, _payload_size); } diff --git a/src/utils.cpp b/src/utils.cpp index abfd8f6..3d09630 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,3 +1,24 @@ +/* + * libtins is a net packet wrapper library for crafting and + * interpreting sniffed packets. + * + * Copyright (C) 2011 Nasel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #include #include #include