tor_multipath_voip/src/packet.c

91 lines
2.5 KiB
C
Raw Normal View History

#include "packet.h"
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp) {
int nread;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_WRITING) 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;
}
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) {
int nwrite;
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;
}
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t addrlen) {
int nwrite, bytes_to_send;
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
if (bp->mode == BP_READING) return FDS_ERR;
bytes_to_send = bp->ip.ap.str.size - pkt_size_size;
nwrite = sendto(fd,
&(bp->ip.ap.str.payload),
bytes_to_send,
0,
addr,
addrlen);
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nwrite != bytes_to_send) return FDS_ERR;
bp->mode = BP_READING;
bp->aread = 0;
return FDS_READY;
}
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct sockaddr* addr, socklen_t* addrlen) {
int nread;
if (bp->mode == BP_WRITING) return FDS_ERR;
*addrlen = sizeof(struct sockaddr_in);
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
size_t udp_packet_size = sizeof(struct internet_packet) - pkt_size_size;
nread = recvfrom(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC,
addr, addrlen);
if (nread > udp_packet_size) return FDS_ERR;
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
if (nread == -1) return FDS_ERR;
bp->ip.ap.str.size = nread + pkt_size_size;
bp->mode = BP_WRITING;
bp->awrite = 0;
return FDS_READY;
}