rawsock -> ethsock

This commit is contained in:
Joseph C. Lehner 2016-01-29 19:21:15 +02:00
parent 3f608e1079
commit f99ee379bc
3 changed files with 18 additions and 17 deletions

View file

@ -2,26 +2,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <pcap.h> #include <pcap.h>
#include "ethsock.h"
#ifndef MIN #ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif #endif
struct rawsock struct ethsock
{ {
pcap_t *pcap; pcap_t *pcap;
struct timeval timeout; struct timeval timeout;
int fd; int fd;
}; };
struct rawsock *rawsock_create(const char *interface, uint16_t protocol) struct ethsock *ethsock_create(const char *interface, uint16_t protocol)
{ {
char buf[PCAP_ERRBUF_SIZE]; char buf[PCAP_ERRBUF_SIZE];
struct bpf_program fp; struct bpf_program fp;
struct rawsock *sock; struct ethsock *sock;
int err; int err;
sock = malloc(sizeof(struct rawsock)); sock = malloc(sizeof(struct ethsock));
if (!sock) { if (!sock) {
perror("malloc"); perror("malloc");
return NULL; return NULL;
@ -71,7 +72,7 @@ cleanup_malloc:
return NULL; return NULL;
} }
ssize_t rawsock_recv(struct rawsock *sock, void *buf, size_t len) ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len)
{ {
struct pcap_pkthdr* hdr; struct pcap_pkthdr* hdr;
const u_char *capbuf; const u_char *capbuf;
@ -107,7 +108,7 @@ ssize_t rawsock_recv(struct rawsock *sock, void *buf, size_t len)
} }
} }
int rawsock_send(struct rawsock *sock, void *buf, size_t len) int ethsock_send(struct ethsock *sock, void *buf, size_t len)
{ {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
if (pcap_sendpacket(sock->pcap, buf, len) == 0) { if (pcap_sendpacket(sock->pcap, buf, len) == 0) {
@ -126,14 +127,14 @@ int rawsock_send(struct rawsock *sock, void *buf, size_t len)
#endif #endif
} }
int rawsock_close(struct rawsock *sock) int ethsock_close(struct ethsock *sock)
{ {
pcap_close(sock->pcap); pcap_close(sock->pcap);
free(sock); free(sock);
return 0; return 0;
} }
int rawsock_set_timeout(struct rawsock *sock, unsigned msec) int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
{ {
sock->timeout.tv_sec = msec / 1000; sock->timeout.tv_sec = msec / 1000;
sock->timeout.tv_usec = (msec % 1000) * 1000; sock->timeout.tv_usec = (msec % 1000) * 1000;

9
ethsock.h Normal file
View file

@ -0,0 +1,9 @@
#include <stdint.h>
struct ethsock;
struct ethsock *ethsock_create(const char *interface, uint16_t protocol);
int ethsock_close(struct ethsock *sock);
int ethsock_send(struct ethsock *sock, void *buf, size_t len);
ssize_t ethsock_recv(struct ethsock *sock, void *buf, size_t len);
int ethsock_set_timeout(struct ethsock *sock, unsigned msec);

View file

@ -1,9 +0,0 @@
#include <stdint.h>
struct rawsock;
struct rawsock *rawsock_create(const char *interface, uint16_t protocol);
int rawsock_close(struct rawsock *sock);
int rawsock_send(struct rawsock *sock, void *buf, size_t len);
ssize_t rawsock_recv(struct rawsock *sock, void *buf, size_t len);
int rawsock_set_timeout(struct rawsock *sock, unsigned msec);