Skip to content

Commit b26d455

Browse files
committed
fix Gnutella build (safe-string)
close #108
1 parent cb0da53 commit b26d455

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/networks/gnutella/gnutellaProto.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,10 @@ let server_msg_to_string pkt =
583583
a hops = 0 *)
584584
buf_int buf 0;
585585
write buf pkt.pkt_payload;
586-
let s = Buffer.contents buf in
587-
let len = String.length s - 23 in
586+
let s = Bytes.unsafe_of_string @@ Buffer.contents buf in
587+
let len = Bytes.length s - 23 in
588588
str_int s 19 len;
589-
s
589+
Bytes.unsafe_to_string s
590590

591591
let new_packet t =
592592
{
@@ -623,7 +623,7 @@ let udp_send ip port msg =
623623
(Ip.to_string ip) port
624624
(String.escaped s);
625625
end;
626-
UdpSocket.write sock false s ip port;
626+
UdpSocket.write sock false (Bytes.unsafe_of_string s) ip port;
627627
(* UdpSocket.write sock s Ip.localhost !!client_port *)
628628
with e ->
629629
lprintf "Exception %s in udp_send\n" (Printexc2.to_string e)
@@ -653,15 +653,16 @@ let server_send_new s t =
653653

654654
let gnutella_handler parse f handler sock =
655655
let b = TcpBufferedSocket.buf sock in
656+
let bbuf = Bytes.unsafe_to_string b.buf in
656657
(* lprintf "GNUTELLA HANDLER\n";
657658
dump (String.sub b.buf b.pos b.len); *)
658659
try
659660
while b.len >= 23 do
660-
let msg_len = get_int b.buf (b.pos+19) in
661+
let msg_len = get_int bbuf (b.pos+19) in
661662
if b.len >= 23 + msg_len then
662663
begin
663-
let pkt_uid = get_md4 b.buf b.pos in
664-
let pkt_type = match get_uint8 b.buf (b.pos+16) with
664+
let pkt_uid = get_md4 bbuf b.pos in
665+
let pkt_type = match get_uint8 bbuf (b.pos+16) with
665666
0 -> PING
666667
| 1 -> PONG
667668
| 2 -> BYE
@@ -672,9 +673,9 @@ let gnutella_handler parse f handler sock =
672673
| 129 -> QUERY_REPLY
673674
| n -> UNKNOWN n
674675
in
675-
let pkt_ttl = get_uint8 b.buf (b.pos+17) in
676-
let pkt_hops = get_uint8 b.buf (b.pos+18) in
677-
let data = String.sub b.buf (b.pos+23) msg_len in
676+
let pkt_ttl = get_uint8 bbuf (b.pos+17) in
677+
let pkt_hops = get_uint8 bbuf (b.pos+18) in
678+
let data = String.sub bbuf (b.pos+23) msg_len in
678679
buf_used b (msg_len + 23);
679680
let pkt = {
680681
pkt_uid = pkt_uid;
@@ -831,7 +832,7 @@ let create_qrt_table words table_size =
831832
((array.(i*2) - old_array.(i*2)) land 15) lsl 4) +
832833
((array.(i*2+1) - old_array.(i*2+1)) land 15))
833834
done;
834-
table
835+
Bytes.unsafe_to_string table
835836

836837
let send_qrt_sequence s update_table =
837838

src/networks/gnutella2/g2Pandora.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let new_packet t (n : int) ip1 port1 ip2 port2 s =
7676
(* lprintf "Could not parse: %s\n" (Printexc2.to_string e) *) ()
7777

7878
let hescaped s =
79-
String2.replace_char s '\r' ' ';s
79+
String2.replace_char s '\r' ' '
8080

8181
let rec iter s pos =
8282
if pos < String.length s then
@@ -150,24 +150,24 @@ let piter s1 deflate h msgs =
150150
if deflate then
151151
let z = Zlib.inflate_init true in
152152
let _ =
153-
let s2 = String.make 100000 '\000' in
153+
let s2 = Bytes.make 100000 '\000' in
154154
let f = Zlib.Z_SYNC_FLUSH in
155-
let (_,used_in, used_out) = Zlib.inflate z s1 0 len s2 0 100000 f in
155+
let (_,used_in, used_out) = Zlib.inflate z (Bytes.unsafe_of_string s1) 0 len s2 0 100000 f in
156156
lprintf "decompressed %d/%d\n" used_out len;
157-
String.sub s2 0 used_out
157+
Bytes.sub_string s2 0 used_out
158158
in
159159
begin
160160
(* First of all, deflate in one pass *)
161161
try
162162
(* lprintf "PARSE ONE BEGIN...\n%s\n" (String.escaped s1); *)
163163
let z = Zlib.inflate_init true in
164164
let s =
165-
let s2 = String.make 1000000 '\000' in
165+
let s2 = Bytes.make 1000000 '\000' in
166166
let f = Zlib.Z_SYNC_FLUSH in
167167
let len = String.length s1 in
168-
let (_,used_in, used_out) = Zlib.inflate z s1 0 len s2
168+
let (_,used_in, used_out) = Zlib.inflate z (Bytes.unsafe_of_string s1) 0 len s2
169169
0 1000000 f in
170-
String.sub s2 0 used_out
170+
Bytes.sub_string s2 0 used_out
171171
in
172172
ignore (parse_string s);
173173
(* lprintf "...PARSE ONE END\n"; *)
@@ -184,12 +184,12 @@ let piter s1 deflate h msgs =
184184
let m = if offset > 0 then String.sub m offset (len - offset) else m in
185185
let rem = rem ^ m in
186186
let len = String.length rem in
187-
let s2 = String.make 100000 '\000' in
187+
let s2 = Bytes.make 100000 '\000' in
188188
let f = Zlib.Z_SYNC_FLUSH in
189189
(* lprintf "deflating %d bytes\n" len; *)
190-
let (_,used_in, used_out) = Zlib.inflate z rem 0 len s2 0 100000 f in
190+
let (_,used_in, used_out) = Zlib.inflate z (Bytes.unsafe_of_string rem) 0 len s2 0 100000 f in
191191
(* lprintf "decompressed %d/%d[%d]\n" used_out len used_in; *)
192-
let m = buf ^ (String.sub s2 0 used_out) in
192+
let m = buf ^ (Bytes.sub_string s2 0 used_out) in
193193

194194
let buf =
195195
try
@@ -266,4 +266,4 @@ let commit () =
266266
lprintf "\nEND OF CONNECTION\n---------------------------------------\n";
267267
) connections;
268268

269-
269+

src/networks/gnutella2/g2Proto.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ let resend_udp_packets () =
884884
Fifo.take udp_packet_waiting_for_ack in
885885
if not !acked then begin
886886
if !verbose then lprintf_nl "resend_udp_packets %s %d: %d" (Ip.to_string ip) port seq;
887-
UdpSocket.write sock false s ip port;
887+
UdpSocket.write sock false (Bytes.unsafe_of_string s) ip port;
888888
if times < 3 then
889889
Fifo.put udp_packet_waiting_for_ack (s, ip, port, seq,
890890
times+1,
@@ -923,7 +923,7 @@ let udp_send ip port msg =
923923
end;
924924
Fifo.put udp_packet_waiting_for_ack
925925
(s, ip, port, !udp_counter, 0, last_time () + 10, ref false);
926-
UdpSocket.write sock false s ip port;
926+
UdpSocket.write sock false (Bytes.unsafe_of_string s) ip port;
927927
(* UdpSocket.write sock s Ip.localhost !!client_port *)
928928
with e ->
929929
lprintf "Exception %s in udp_send\n" (Printexc2.to_string e)
@@ -938,7 +938,7 @@ let udp_send_ack ip port counter =
938938
(char_of_int ((counter lsr 8) land 0xff))
939939
in
940940
(* if !verbose then lprintf_nl "udp_send_ack: %s %d" (Ip.to_string ip) port; *)
941-
UdpSocket.write sock false s ip port
941+
UdpSocket.write sock false (Bytes.unsafe_of_string s) ip port
942942
with e ->
943943
lprintf "Exception %s in udp_send\n" (Printexc2.to_string e)
944944

@@ -963,12 +963,12 @@ let g2_handler f gconn sock =
963963

964964
if !verbose then begin
965965
lprintf_nl "g2_handler:";
966-
AnyEndian.dump_hex (String.sub b.buf b.pos b.len);
966+
AnyEndian.dump_hex (Bytes.sub_string b.buf b.pos b.len);
967967
end;
968968

969969
try
970970
while b.len >= 2 do
971-
let s = b.buf in
971+
let s = Bytes.unsafe_to_string b.buf in
972972
(* if !verbose then lprintf_nl "g2_tcp_packet_handler"; *)
973973
let cb = get_uint8 s b.pos in
974974
let len_len = (cb lsr 6) land 3 in
@@ -993,8 +993,8 @@ let g2_handler f gconn sock =
993993
if b.len < msg_len then raise Not_found;
994994

995995
(* if !verbose then lprintf_nl "One gnutella2 packet received"; *)
996-
let name = String.sub b.buf (b.pos + pos) name_len in
997-
let packet = String.sub b.buf (b.pos + pos + name_len) len in
996+
let name = String.sub s (b.pos + pos) name_len in
997+
let packet = String.sub s (b.pos + pos + name_len) len in
998998
let has_children = cb land 4 <> 0 in
999999
TcpBufferedSocket.buf_used b msg_len;
10001000
f gconn (g2_parse [name] has_children be packet)
@@ -1479,14 +1479,14 @@ let on_send_pings () =
14791479
let server_send_push s uid uri = ()
14801480

14811481
let bitv_to_string bitv =
1482-
let s = String.make ((Bitv.length bitv) / 8) '\000' in
1482+
let s = Bytes.make ((Bitv.length bitv) / 8) '\000' in
14831483
Bitv.iteri_true (fun i ->
14841484
let pos = i / 8 in
14851485
let bit = 7 - (i mod 8) in
14861486
let x = (1 lsl bit) in
1487-
s.[pos] <- char_of_int ( (int_of_char s.[pos]) lor x );
1487+
Bytes.set s pos (char_of_int ( (int_of_char @@ Bytes.get s pos) lor x ));
14881488
) bitv;
1489-
s
1489+
Bytes.unsafe_to_string s
14901490

14911491
(* http://www.gnutella2.com/index.php/Query_Hash_Tables *)
14921492
let create_qrt_table words table_size =

0 commit comments

Comments
 (0)