1
0

A buffer was removed from a struct net_t. Use external buffer for data receiving. net_sendto -> net_send and net_recvfrom -> net_recv.

This commit is contained in:
Alexander Andreev 2024-03-24 16:31:10 +04:00
parent 842659e879
commit e5c6f23cfb
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 5 additions and 12 deletions

View File

@ -16,7 +16,6 @@ int net_client_init(net_t *const n, const int af_family, const char *const raddr
n->raddr.sin_family = af_family;
n->raddr.sin_addr.s_addr = inet_addr(raddr);
n->raddr.sin_port = htons(rport);
return 0;
}
@ -50,11 +49,9 @@ void net_destroy(net_t *n) {
memset(&n->raddr, 0, sizeof(n->raddr));
n->raddr_len = sizeof(n->raddr);
memset(&n->laddr, 0, sizeof(n->laddr));
memset(&n->buffer, 0, NET_BUFLEN);
}
int net_sendto(net_t *const n, const char *const buf, const int buf_len) {
int net_send(net_t *const n, const char *const buf, const int buf_len) {
if (n->fd == -1)
return -1;
@ -64,11 +61,11 @@ int net_sendto(net_t *const n, const char *const buf, const int buf_len) {
return 0;
}
int net_recvfrom(net_t *const n) {
int net_recv(net_t *const n, void *buf, size_t buf_len) {
if (n->fd == -1 || n->raddr.sin_addr.s_addr == 0)
return -1;
if ((recvfrom(n->fd, n->buffer, NET_BUFLEN, 0, (struct sockaddr *)&n->inaddr, &n->inaddr_len)) == -1)
if ((recvfrom(n->fd, buf, buf_len, 0, (struct sockaddr *)&n->inaddr, &n->inaddr_len)) == -1)
return -1;
if (n->raddr.sin_addr.s_addr != n->inaddr.sin_addr.s_addr) {

View File

@ -4,21 +4,17 @@
#include <sys/socket.h>
#include <netinet/in.h>
#define NET_BUFLEN 256
typedef struct net_t {
int fd;
struct sockaddr_in laddr, raddr, inaddr;
socklen_t raddr_len, inaddr_len;
char buffer[NET_BUFLEN];
} net_t;
int net_client_init(net_t *const n, const int af_family, const char *const raddr, const int rport);
int net_server_init(net_t *const n, const int af_family, const char *const laddr, const int lport);
void net_destroy(net_t *n);
int net_sendto(net_t *const n, const char *const buf, const int buf_len);
int net_recvfrom(net_t *const n);
int net_send(net_t *const n, const char *const buf, const int buf_len);
int net_recv(net_t *const n, void *buf, size_t buf_len);
#endif /* _NET_H_ */