Skip to content

Commit b792db1

Browse files
committed
Fixing some witness related things
1 parent ee40e71 commit b792db1

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/bio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void *bioProcessBackgroundJobs(void *arg) {
205205
/* Iterate through the witnesses and send GC cmds */
206206
for (int i = 0; i < server.numWitness; ++i) {
207207
udpWrite(s, SRC_ADDR, server.addrToWitness[i], WITNESS_PORT,
208-
WITNESS_PORT, witness_data(cmd), false);
208+
WITNESS_PORT, witness_data(cmd), witness_size(cmd), false);
209209
}
210210
/* TODO: Wait for ACKS? */
211211
/* Free cmd and close the socket */

src/udp.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ unsigned short csum(unsigned short *ptr,int nbytes) {
4242
}
4343

4444
int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dport,
45-
char* buf, bool chksum)
45+
char* buf, int len, bool chksum)
4646
{
4747
// Datagram to represent the packet
48-
char datagram[4096] , source_ip[32] , *data , *pseudogram;
48+
char datagram[4096];
49+
char *data , *pseudogram;
4950
// Zero out the packet buffer
5051
memset (datagram, 0, 4096);
5152
// IP header
@@ -60,8 +61,6 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
6061
data = datagram + sizeof(struct iphdr) + sizeof(struct udphdr);
6162
strcpy(data , buf);
6263

63-
64-
strcpy(source_ip , saddr);
6564
sin.sin_family = AF_INET;
6665
sin.sin_port = htons(80);
6766
sin.sin_addr.s_addr = inet_addr(daddr);
@@ -70,13 +69,13 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
7069
iph->ihl = 5;
7170
iph->version = 4;
7271
iph->tos = 0;
73-
iph->tot_len = sizeof (struct iphdr) + sizeof (struct udphdr) + strlen(data);
72+
iph->tot_len = sizeof (struct iphdr) + sizeof (struct udphdr) + len;
7473
iph->id = htons(10); //Id of this packet
7574
iph->frag_off = 0;
7675
iph->ttl = 255;
7776
iph->protocol = IPPROTO_UDP;
7877
iph->check = 0; //Set to 0 before calculating checksum
79-
iph->saddr = inet_addr ( source_ip ); //Spoof the source ip address
78+
iph->saddr = inet_addr(saddr); //Spoof the source ip address
8079
iph->daddr = sin.sin_addr.s_addr;
8180

8281
//Compute checksum if needed
@@ -85,27 +84,27 @@ int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dpo
8584
//UDP header
8685
udph->uh_sport = htons(sport);
8786
udph->uh_dport = htons(dport);
88-
udph->uh_ulen = htons(8 + strlen(data)); //tcp header size
87+
udph->uh_ulen = htons(8 + len); //udp header size
8988
udph->uh_sum = 0; //leave checksum 0 now, filled later by pseudo header
9089

9190
//Now the UDP checksum using the pseudo header
9291
if (chksum) {
93-
psh.source_address = inet_addr( source_ip );
92+
psh.source_address = inet_addr(saddr);
9493
psh.dest_address = sin.sin_addr.s_addr;
9594
psh.placeholder = 0;
9695
psh.protocol = IPPROTO_UDP;
97-
psh.udp_length = htons(sizeof(struct udphdr) + strlen(data) );
96+
psh.udp_length = htons(sizeof(struct udphdr) + len);
9897

99-
int psize = sizeof(struct pseudo_header) + sizeof(struct udphdr) + strlen(data);
98+
int psize = sizeof(struct pseudo_header) + sizeof(struct udphdr) + len;
10099
pseudogram = (char *) malloc(psize);
101100

102101
memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
103-
memcpy(pseudogram + sizeof(struct pseudo_header) , udph , sizeof(struct udphdr) + strlen(data));
102+
memcpy(pseudogram + sizeof(struct pseudo_header) , udph , sizeof(struct udphdr) + len);
104103
udph->uh_sum = csum( (unsigned short*) pseudogram , psize);
105104
}
106105

107106
//Send the packet
108-
if (sendto (s, datagram, iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
107+
if (sendto (s, data, len, 0, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
109108
perror("sendto failed");
110109
return 1;
111110
}

src/udp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ unsigned short csum(unsigned short *ptr, int nbytes);
2727

2828
int createSocket();
2929
int udpWrite(int s, const char* saddr, const char* daddr, short sport, short dport,
30-
char* buf, bool chksum);
30+
char* buf, int len, bool chksum);
3131
#endif

0 commit comments

Comments
 (0)