tor_multipath_voip/src/packet.c

105 lines
2.9 KiB
C
Raw Normal View History

#include "packet.h"
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
2019-02-14 14:40:05 +00:00
ssize_t nread;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
2019-02-18 16:51:13 +00:00
if (bp->mode != BP_READING) return FDS_ERR;
while (bp->aread < pkt_size_size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, pkt_size_size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
while (bp->aread < bp->ip.ap.str.size) {
nread = read(fd, &(bp->ip.ap.raw) + bp->aread, bp->ip.ap.str.size - bp->aread);
if (nread == 0) return FDS_AGAIN;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->aread += nread;
}
2019-02-18 20:55:53 +00:00
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) {
2019-02-14 14:40:05 +00:00
ssize_t nwrite;
2019-02-18 16:51:13 +00:00
if (bp->mode != BP_WRITING) return FDS_ERR;
while (bp->awrite < bp->ip.ap.str.size) {
nwrite = send(fd, &(bp->ip.ap.raw), bp->ip.ap.str.size, 0);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite == -1) return FDS_ERR;
bp->awrite += nwrite;
}
2019-02-18 20:55:53 +00:00
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
2019-02-18 14:11:12 +00:00
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp) {
2019-02-14 16:15:13 +00:00
ssize_t nwrite;
size_t bytes_to_send;
2019-02-18 14:11:12 +00:00
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(bp->ip.ap.str.port),
};
inet_aton("127.0.0.1", &addr.sin_addr);
socklen_t addr_len = sizeof(addr);
2019-02-18 16:51:13 +00:00
if (bp->mode != BP_WRITING) return FDS_ERR;
2019-02-18 14:11:12 +00:00
bytes_to_send = bp->ip.ap.str.size - pkt_header_size;
nwrite = sendto(fd,
&(bp->ip.ap.str.payload),
bytes_to_send,
0,
(struct sockaddr*)&addr,
addr_len);
2019-02-14 16:15:13 +00:00
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite != bytes_to_send) return FDS_ERR;
2019-02-18 20:55:53 +00:00
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
2019-02-18 14:11:12 +00:00
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp) {
2019-02-14 14:40:05 +00:00
ssize_t nread;
2019-02-18 16:51:13 +00:00
if (bp->mode != BP_READING) return FDS_ERR;
struct sockaddr_in addr = {0};
socklen_t addr_len = sizeof(addr);
2019-02-18 14:11:12 +00:00
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); // We remove the payload
size_t udp_packet_size = sizeof(struct internet_packet) - pkt_header_size;
nread = recvfrom(fd,
&(bp->ip.ap.str.payload),
udp_packet_size,
MSG_TRUNC,
(struct sockaddr*)&addr,
&addr_len);
2019-02-14 14:40:05 +00:00
if ((int)nread > (int)udp_packet_size) return FDS_ERR;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
2019-02-18 14:11:12 +00:00
bp->ip.ap.str.size = nread + pkt_header_size;
2019-02-18 20:55:53 +00:00
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}