summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHåkon Nessjøen <haakon.nessjoen@gmail.com>2011-02-23 12:11:29 +0100
committerHåkon Nessjøen <haakon.nessjoen@gmail.com>2011-02-23 12:11:29 +0100
commit0e0ff418da0d95f9e7b8a25d7643dd4c5e8731d8 (patch)
tree91f48b4e433dcd35ed9f11d22f0d297f8875e1da
parent59cdd34a3248acac61fd3edebca860066fd4c2fd (diff)
downloadMAC-Telnet-0e0ff418da0d95f9e7b8a25d7643dd4c5e8731d8.tar.gz
MAC-Telnet-0e0ff418da0d95f9e7b8a25d7643dd4c5e8731d8.zip
Added endianness checks in udp.c, for raw packets
-rw-r--r--udp.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/udp.c b/udp.c
index b1e6540..76a68f3 100644
--- a/udp.c
+++ b/udp.c
@@ -50,7 +50,7 @@ unsigned short udp_sum_calc(unsigned char *src_addr,unsigned char *dst_addr, uns
/* header+data */
for (i = 0; i < len + padd; i += 2){
word16 = ((data[i] << 8) & 0xFF00) + (data[i + 1] & 0xFF);
- sum += (unsigned long)word16;
+ sum += word16;
}
/* source ip */
@@ -65,6 +65,7 @@ unsigned short udp_sum_calc(unsigned char *src_addr,unsigned char *dst_addr, uns
sum += word16;
}
+ len = len;
sum += prot_udp + len;
while (sum>>16)
@@ -113,7 +114,11 @@ int send_custom_udp(const int socket, const int ifindex, const unsigned char *so
/* Init SendTo struct */
socket_address.sll_family = PF_PACKET;
+#if BYTE_ORDER == LITTLE_ENDIAN
socket_address.sll_protocol = htons(ETH_P_IP);
+#else
+ socket_address.sll_protocol = ETH_P_IP;
+#endif
socket_address.sll_ifindex = ifindex;
socket_address.sll_hatype = ARPHRD_ETHER;
socket_address.sll_pkttype = PACKET_OTHERHOST;
@@ -127,9 +132,15 @@ int send_custom_udp(const int socket, const int ifindex, const unsigned char *so
ip->version = 4;
ip->ihl = 5;
ip->tos = 0x10;
+#if BYTE_ORDER == LITTLE_ENDIAN
ip->tot_len = htons(datalen + 8 + 20);
ip->id = htons(id++);
- ip->frag_off = 0x0040;
+ ip->frag_off = htons(0x4000);
+#else
+ ip->tot_len = datalen + 8 + 20;
+ ip->id = id++;
+ ip->frag_off = 0x4000;
+#endif
ip->ttl = 64;
ip->protocol = 17; /* UDP */
ip->check = 0x0000;
@@ -140,15 +151,26 @@ int send_custom_udp(const int socket, const int ifindex, const unsigned char *so
ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));
/* Init UDP Header */
+#if BYTE_ORDER == LITTLE_ENDIAN
udp->source = htons(sourceport);
udp->dest = htons(destport);
- udp->check = 0;
udp->len = htons(sizeof(struct udphdr) + datalen);
+ udp->check = 0;
+#else
+ udp->source = sourceport;
+ udp->dest = destport;
+ udp->check = 0;
+ udp->len = sizeof(struct udphdr) + datalen;
+#endif
/* Insert actual data */
memcpy(rest, data, datalen);
- udp->check = htons(udp_sum_calc((unsigned char *)&(ip->saddr), (unsigned char *)&(ip->daddr), (unsigned char *)udp, sizeof(struct udphdr) + datalen));
+ /* Add UDP checksum */
+ udp->check = udp_sum_calc((unsigned char *)&(ip->saddr), (unsigned char *)&(ip->daddr), (unsigned char *)udp, sizeof(struct udphdr) + datalen);
+#if BYTE_ORDER == LITTLE_ENDIAN
+ udp->check = htons(udp->check);
+#endif
/* Send the packet */
send_result = sendto(socket, buffer, datalen+8+14+20, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));