5 #include <sys/socket.h>
6 #include <netinet/in.h>
9 #include "raw_ethernet_packet.h"
16 RawEthernetPacket::RawEthernetPacket(){
18 size = (size_t*)(pkt + (sizeof(char) * 2));
19 data = pkt + (sizeof(char) * 2) + sizeof(size_t);
23 RawEthernetPacket::RawEthernetPacket(const RawEthernetPacket &rhs)
26 this->size = (size_t*)(pkt + (sizeof(char) * 2));
27 this->data = pkt + (sizeof(char) * 2) + sizeof(size_t);
28 // *(this->size)=*(rhs.size);
29 this->set_size(rhs.get_size());
30 memcpy(this->type, rhs.type, sizeof(char) * 2);
31 memcpy(this->data,rhs.data,*(this->size));
34 RawEthernetPacket::RawEthernetPacket(const char *data, const size_t size)
37 this->size = (size_t*)(pkt + (sizeof(char) * 2));
38 this->data = pkt + (sizeof(char) * 2) + sizeof(size_t);
40 memcpy(this->data,data,size);
44 const RawEthernetPacket & RawEthernetPacket::operator= (const RawEthernetPacket &rhs)
47 this->size = (size_t*)(pkt + (sizeof(char) * 2));
48 this->data = pkt + (sizeof(char) * 2) + sizeof(size_t);
49 this->set_size(rhs.get_size());
50 memcpy(data, rhs.data, *(size_t*)(this->size));
56 RawEthernetPacket::~RawEthernetPacket()
60 size_t RawEthernetPacket::get_size() const {
62 memcpy(&t_size, pkt+2, sizeof(size_t));
66 void RawEthernetPacket::set_size(size_t new_size) {
67 memcpy(this->pkt + (sizeof(char) * 2), &new_size, sizeof(size_t));
70 char * RawEthernetPacket::get_type() {
74 void RawEthernetPacket::set_type (const char * new_type) {
75 memcpy(this->pkt, new_type, sizeof(char) * 2);
78 char * RawEthernetPacket::get_data() {
79 return this->pkt + (sizeof(char) * 2) + sizeof(size_t);
84 int RawEthernetPacket::Serialize(const SOCK fd, SSL * ssl) const {
85 int length = (sizeof(char)*2) + this->get_size() + sizeof(size_t);
88 ret = Send(fd, ssl, pkt, length, true);
89 if (ret != (int)length) {
96 int RawEthernetPacket::Unserialize(const SOCK fd, SSL * ssl) {
99 ret = Receive(fd, ssl, pkt, sizeof(char) * 2 + sizeof(size_t), true);
101 JRLDBG("TCP socket closed\n");
103 } else if (ret != (sizeof(char) * 2 + sizeof(size_t))) {
104 JRLDBG("Error unserializing packet header from tcp socket\n");
108 JRLDBG("Receiving TCP data. size=%d, offset=%d\n", this->get_size(), *(pkt + 2));
110 ret = Receive(fd, ssl, data, this->get_size(), true);
112 JRLDBG("TCP Socket closed\n");
114 } else if (ret != (int)this->get_size()) {
115 JRLDBG("Error unserializing packet from tcp socket\n");
122 int RawEthernetPacket::Serialize(const SOCK fd) const {
123 int length = (sizeof(char)*2) + this->get_size() + sizeof(size_t);
126 ret = Send(fd, pkt, length, true);
127 if (ret != (int)length) {
134 int RawEthernetPacket::Unserialize(const SOCK fd) {
137 ret = Receive(fd, pkt, sizeof(char) * 2 + sizeof(size_t), true);
139 JRLDBG("TCP socket closed\n");
141 } else if (ret != (sizeof(char) * 2 + sizeof(size_t))) {
142 JRLDBG("Error unserializing packet header from tcp socket\n");
146 JRLDBG("Receiving TCP data. size=%d, offset=%d\n", this->get_size(), *(pkt + 2));
148 ret = Receive(fd, data, this->get_size(), true);
150 JRLDBG("TCP Socket closed\n");
152 } else if (ret != (int)this->get_size()) {
153 JRLDBG("Error unserializing packet from tcp socket\n");
162 /* SRC_ROUTING: We need to add a long * to the arguments that we will
163 write the client address (int form) into */
164 int RawEthernetPacket::UdpUnserialize(const SOCK fd) {
165 struct sockaddr_in clientaddr; /*the client address strcuture */
166 int clientlen = sizeof(clientaddr);
168 int length = 2 + sizeof(size_t)+ ETHERNET_PACKET_LEN;
171 rcvd = recvfrom(fd, pkt,length,0,(struct sockaddr *)&clientaddr,(socklen_t *)&clientlen);
176 int RawEthernetPacket::UdpSerialize(const SOCK fd, struct sockaddr * serveraddr) const {
177 int len = sizeof(*serveraddr);
181 length = sizeof(char)* 2 + this->get_size() + sizeof(size_t);
183 sent = sendto(fd,pkt,length,0,serveraddr,len);
189 int RawEthernetPacket::VtpUnserialize(const SOCK fd, struct in_addr * serveraddr) {
191 this->set_size((unsigned int)(-1));
193 ret = Receive(fd, pkt, sizeof(char) * 2, true);
195 JRLDBG("VTP connection has Closed\n");
197 } else if (ret != (int)sizeof(char) * 2) {
198 JRLDBG("Could not read type from VTP packet\n");
202 ret = Receive(fd, (char *)serveraddr, sizeof(struct in_addr), true);
204 JRLDBG("VTP connection has closed\n");
206 } else if (ret != (int)sizeof(struct in_addr)) {
207 JRLDBG("Could not read VTP address info\n");
211 ret = Receive(fd, (char *)size, sizeof(size_t), true);
213 JRLDBG("VTP connection has closed\n");
215 } else if (ret != sizeof(size_t)) {
216 JRLDBG("Could not read VTP size\n");
220 ret = Receive(fd, data, this->get_size(), true);
222 JRLDBG("VTP connection has closed\n");
224 } else if (ret != (int)this->get_size()) {
225 JRLDBG("Could not read VTP packet data\n");
232 int RawEthernetPacket::VtpSerialize(const SOCK fd, struct in_addr * serveraddr ) const {
236 ret = Send(fd, type, sizeof(char) * 2, true);
237 if (ret != sizeof(char) * 2) {
238 JRLDBG("Error writing type to VTP socket\n");
242 ret = Send(fd, (char *)serveraddr, sizeof(struct in_addr), true);
243 if (ret != sizeof(struct in_addr)) {
244 JRLDBG("Error writing dest addr to VTP socket\n");
248 length = this->get_size() + sizeof(size_t);
250 ret = Send(fd, pkt + (sizeof(char) * 2), length, true);
251 if (ret != (int)length) {
252 JRLDBG("ERROR writing packet length and data to VTP socket\n");
263 #define MIN(x,y) ((x)<(y) ? (x) : (y))
265 void RawEthernetPacket::Print(unsigned size, FILE *out) const
267 fprintf(out,"raw_ethernet_packet: size %-4u first %u bytes: ", *(this->size), MIN(*(this->size),size));
268 printhexbuffer(out, data, MIN(*(this->size),size));
272 ostream & RawEthernetPacket::Print(ostream &os) const
278 snprintf(buf,2048,"RawEthernetPacket(size=%u, bytes=", this->get_size());
280 for (i=0;i<this->get_size();i++) {
281 bytetohexbyte(data[i],&(buf[n+2*i]));
286 for (i=0;i<this->get_size();i++) {
288 if (c>=32 && c<=126) {