First try to implement a network module.
This commit is contained in:
parent
051d18ba40
commit
e6c94b64a0
81
src/net.c
Normal file
81
src/net.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
#include "net.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
int net_client_init(net_t *const n, const int af_family, const char *const raddr, const int rport) {
|
||||||
|
if ((n->fd = socket(af_family, SOCK_DGRAM, 0)) == -1 )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memset(&n->inaddr, 0, sizeof(n->inaddr));
|
||||||
|
n->inaddr_len = sizeof(n->inaddr);
|
||||||
|
|
||||||
|
memset(&n->raddr, 0, sizeof(n->raddr));
|
||||||
|
n->raddr.sin_family = af_family;
|
||||||
|
n->raddr.sin_addr.s_addr = inet_addr(raddr);
|
||||||
|
n->raddr.sin_port = htons(rport);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_server_init(net_t *const n, const int af_family, const char *const laddr, const int lport) {
|
||||||
|
if ((n->fd = socket(af_family, SOCK_DGRAM, 0)) == -1 )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memset(&n->inaddr, 0, sizeof(n->inaddr));
|
||||||
|
n->inaddr_len = sizeof(n->inaddr);
|
||||||
|
|
||||||
|
memset(&n->laddr, 0, sizeof(n->laddr));
|
||||||
|
n->laddr.sin_family = af_family;
|
||||||
|
n->laddr.sin_addr.s_addr = inet_addr(laddr);
|
||||||
|
n->laddr.sin_port = htons(lport);
|
||||||
|
|
||||||
|
memset(&n->raddr, 0, sizeof(n->raddr));
|
||||||
|
n->raddr_len = sizeof(n->raddr);
|
||||||
|
|
||||||
|
if (bind(n->fd, (struct sockaddr *)&n->laddr, sizeof(n->laddr)) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void net_destroy(net_t *n) {
|
||||||
|
if (n->fd > 0) {
|
||||||
|
close(n->fd);
|
||||||
|
n->fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (n->fd == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (sendto(n->fd, buf, buf_len, 0, (struct sockaddr *)&n->raddr, sizeof(n->raddr)) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_recvfrom(net_t *const n) {
|
||||||
|
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)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (n->raddr.sin_addr.s_addr != n->inaddr.sin_addr.s_addr) {
|
||||||
|
/* Incoming packet doesn't originate from the one we are talking to. Drop it. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
24
src/net.h
Normal file
24
src/net.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef _NET_H_
|
||||||
|
#define _NET_H_
|
||||||
|
|
||||||
|
#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);
|
||||||
|
|
||||||
|
#endif /* _NET_H_ */
|
Loading…
Reference in New Issue
Block a user