Skip to content

Commit

Permalink
Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
ruti committed Feb 18, 2024
1 parent 4982846 commit 7666899
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 157 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
TARGET = ciadpi
CC ?= gcc
CFLAGS += -std=c99 -O2 -D_XOPEN_SOURCE=500
SOURCES = packets.c main.c conev.c proxy.c desync.c
SOURCES = packets.c main.c conev.c proxy.c desync.c error.c

all:
$(CC) $(CFLAGS) $(SOURCES) -I . -o $(TARGET)

windows:
$(CC) $(CFLAGS) $(SOURCES) -I . -lws2_32 -o $(TARGET).exe

clean:
rm -f $(TARGET) *.o
6 changes: 3 additions & 3 deletions conev.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#define CONEV_H
#include <conev.h>
#include <error.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>


struct poolhd *init_pool(int count)
Expand Down Expand Up @@ -68,7 +68,7 @@ struct eval *add_event(struct poolhd *pool, enum eid type,
struct pollfd *pfd = &(pool->pevents[pool->count]);

pfd->fd = fd;
pfd->events = POLLIN | POLLERR | POLLRDHUP | e;
pfd->events = POLLIN | e;
pfd->revents = 0;
#endif

Expand Down Expand Up @@ -181,7 +181,7 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *typel)
for (int i = *offs; ; i--) {
if (i < 0) {
if (poll(pool->pevents, pool->count, -1) <= 0) {
perror("poll");
uniperror("poll");
return 0;
}
i = pool->count - 1;
Expand Down
33 changes: 21 additions & 12 deletions conev.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#include <stdint.h>
#include <netinet/in.h>

#ifndef __linux__
#define NOEPOLL
#define NOEPOLL
#endif

#ifndef NOEPOLL
#include <sys/epoll.h>
#define POLLIN EPOLLIN
#define POLLOUT EPOLLOUT
#define POLLERR EPOLLERR
#define POLLHUP EPOLLHUP
#define POLLRDHUP EPOLLRDHUP
#ifdef _WIN32
#include <ws2tcpip.h>
#define poll(fds, cnt, to) WSAPoll(fds, cnt, to)
#define close(fd) closesocket(fd)
#else
#include <sys/poll.h>
#ifndef POLLRDHUP
#define POLLRDHUP POLLHUP
#include <netinet/in.h>
#include <unistd.h>

#ifndef NOEPOLL
#include <sys/epoll.h>
#define POLLIN EPOLLIN
#define POLLOUT EPOLLOUT
#define POLLERR EPOLLERR
#define POLLHUP EPOLLHUP
#define POLLRDHUP EPOLLRDHUP
#else
#include <sys/poll.h>
#endif
#endif

#ifndef POLLRDHUP
#define POLLRDHUP POLLHUP
#endif

enum eid {
Expand Down
68 changes: 38 additions & 30 deletions desync.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/mman.h>

#ifdef __linux__
#include <sys/sendfile.h>
#define _sendfile(outfd, infd, start, len) sendfile(outfd, infd, start, len)
#else
#include <sys/uio.h>
#define _sendfile(outfd, infd, start, len) sendfile(infd, outfd, start, len, 0, 0)
#endif
#ifndef _WIN32
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/mman.h>

#ifdef __linux__
#include <sys/sendfile.h>
#define _sendfile(outfd, infd, start, len) sendfile(outfd, infd, start, len)
#else
#include <sys/uio.h>
#define _sendfile(outfd, infd, start, len) sendfile(infd, outfd, start, len, 0, 0)
#endif

#ifdef MFD_CLOEXEC
#include <sys/syscall.h>
#define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags);
#ifdef MFD_CLOEXEC
#include <sys/syscall.h>
#define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags);
#else
#define memfd_create(name, flags) fileno(tmpfile())
#endif
#else
#define memfd_create(name, flags) fileno(tmpfile())
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include <params.h>
#include <packets.h>
#include <error.h>


static inline int get_family(struct sockaddr *dst)
Expand All @@ -46,20 +53,20 @@ int setttl(int fd, int ttl, int family) {

if (family == AF_INET) {
if (setsockopt(fd, IPPROTO_IP,
IP_TTL, &_ttl, sizeof(_ttl)) < 0) {
perror("setsockopt IP_TTL");
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IP_TTL");
return -1;
}
}
else if (setsockopt(fd, IPPROTO_IPV6,
IPV6_UNICAST_HOPS, &_ttl, sizeof(_ttl)) < 0) {
perror("setsockopt IPV6_UNICAST_HOPS");
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
uniperror("setsockopt IPV6_UNICAST_HOPS");
return -1;
}
return 0;
}


#ifndef _WIN32
int fake_attack(int sfd, char *buffer,
size_t n, int cnt, int pos, int fa)
{
Expand Down Expand Up @@ -113,7 +120,7 @@ int fake_attack(int sfd, char *buffer,
close(ffd);
return status;
}

#endif

int disorder_attack(int sfd, char *buffer,
ssize_t n, int pos, int fa)
Expand All @@ -123,14 +130,14 @@ int disorder_attack(int sfd, char *buffer,
return -1;
}
if (send(sfd, buffer, pos, 0) < 0) {
perror("send");
uniperror("send");
return -1;
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
perror("send");
uniperror("send");
return -1;
}
return 0;
Expand Down Expand Up @@ -190,25 +197,26 @@ int desync(int sfd, char *buffer, size_t bfsize,
(!type && params.de_known))
{
if (send(sfd, buffer, n, 0) < 0) {
perror("send");
uniperror("send");
return -1;
}
}
else switch (params.attack) {
#ifndef _WIN32
case DESYNC_FAKE:
return fake_attack(sfd, buffer, n, type, pos, fa);
#endif
case DESYNC_DISORDER:
return disorder_attack(sfd, buffer, n, pos, fa);

case DESYNC_SPLIT:
default:
if (send(sfd, buffer, pos, 0) < 0) {
perror("send");
uniperror("send");
return -1;
}
if (send(sfd, buffer + pos, n - pos, 0) < 0) {
perror("send");
uniperror("send");
return -1;
}
}
Expand Down
46 changes: 46 additions & 0 deletions error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifdef _WIN32
#include <winsock2.h>
#endif

int unie(int e)
{
#ifdef _WIN32
switch (e) {
case WSAEWOULDBLOCK:
return EAGAIN;
case WSAETIMEDOUT:
return ETIMEDOUT;
case WSAENETUNREACH:
return ENETUNREACH;
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
case WSAECONNREFUSED:
return ECONNREFUSED;
}
#endif
return e;
}

int get_e()
{
#ifdef _WIN32
int e = WSAGetLastError();
return unie(e);
#else
return errno;
#endif
}

void uniperror(char *str)
{
#ifdef _WIN32
int e = WSAGetLastError();
fprintf(stderr, "%s: %d\n", str, e);
#else
perror(str);
#endif
}
4 changes: 4 additions & 0 deletions error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int unie(int e);
void uniperror(char *str);
int get_e();
void uniperror(char *str);
Loading

0 comments on commit 7666899

Please sign in to comment.