6 #include "arch/low/general.h"
8 #include "libs/pointer.h"
9 #include "libs/array.h"
11 #include "tcp_client_socket.h"
15 void tcp::send(const ipv4_addr
&addr
, u16 dst_port
, u16 src_port
, const buffer
&data
, tcp_flags flags
, int seq
, int ack
) {
16 p
<tcp_packet
> pkg
= new tcp_packet
;
17 pkg
->sender_port
= to_be16(src_port
);
18 pkg
->receiver_port
= to_be16(dst_port
);
19 pkg
->seq
= to_be32(seq
);
20 pkg
->ack
= to_be32(ack
);
21 pkg
->header_size
= sizeof(tcp_packet
) << 2;
23 pkg
->window_size
= to_be16(1000); /*wtf?*/
28 p
<pseudo_header
> ph
= new pseudo_header
;
29 ph
->source
= down
.cast
<ipv4
>()->get_my_ip().to_be();
30 ph
->destination
= addr
.to_be();
33 ph
->length
= to_be16(sizeof(tcp_packet
) + data
.get_size());
34 array
<u16
> ph_words
= array
<u16
>::from_buffer(buffer::to_mem(ph
));
36 buffer buf
= buffer::to_mem(pkg
);
40 array
<u16
> words
= array
<u16
>::from_buffer(buf
);
43 for (unsigned int i
= 0; i
< (sizeof(tcp_packet
) + data
.get_size()) / 2; i
++)
46 if (data
.get_size() % 2)
47 sum
+= (u16
)data
[data
.get_size() - 1];
49 for (unsigned int i
= 0; i
< sizeof(pseudo_header
) / 2; i
++)
54 sum
= (sum
& 0xffff) + (sum
>> 16);
57 buf
.cast
<tcp_packet
>()->checksum
= to_le16(~sum
);
59 down
->send_data(addr
, 6, buf
);
64 void tcp::receive(const ipv4_addr
&, const buffer
&data
) {
67 p
<client_socket
> tcp::create_client() {
68 return (client_socket
*)new tcp_client_socket(this);
71 p
<server_socket
> tcp::create_server(int port
) { }