-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,41 @@ | ||
OUTPUT = .output | ||
CC = gcc | ||
CFLAGS = -I. -g | ||
CXX = g++ | ||
CFLAGS = -g -std=c17 -Wall -Wextra | ||
CXXFLAGS = -g -std=c++17 -Wall -Wextra | ||
DEPS = common.h | ||
CLIENT_OBJ = client.o | ||
SERVER_OBJ = server.o | ||
APPS = client server | ||
INTERPS = libinterptest libinterptest_cpp | ||
|
||
%.o: %.c $(DEPS) | ||
$(CC) -c -o $@ $< $(CFLAGS) | ||
.PHONY: all | ||
all: $(APPS) $(INTERPS) | ||
|
||
client: $(CLIENT_OBJ) | ||
$(CC) -o $@ $^ $(CFLAGS) | ||
$(OUTPUT): | ||
mkdir -p $(@) | ||
|
||
server: $(SERVER_OBJ) | ||
$(CC) -o $@ $^ $(CFLAGS) | ||
$(OUTPUT)/%.o: %.c $(DEPS) | $(OUTPUT) | ||
$(CC) $(CFLAGS) -c -o $@ $< | ||
|
||
libinterptest: interptest.c | ||
$(CC) -shared -ldl -fPIC interptest.c -o libinterptest.so | ||
$(APPS): %: $(OUTPUT)/%.o | $(OUTPUT) | ||
$(CC) $(CFLAGS) -o $@ $^ | ||
|
||
$(OUTPUT)/%.so: %.c | $(OUTPUT) | ||
$(CC) $(CFLAGS) -shared -ldl -fPIC $^ -o $@ | ||
|
||
all: client server libinterptest | ||
$(OUTPUT)/%_cpp.so: %_cpp.cpp | $(OUTPUT) | ||
$(CXX) $(CXXFLAGS) -shared -ldl -fPIC $^ -o $@ | ||
|
||
$(INTERPS): %: $(OUTPUT)/%.so ; | ||
|
||
interp_server : server $(OUTPUT)/libinterptest.so | ||
LD_PRELOAD=$(OUTPUT)/libinterptest.so ./server | ||
|
||
interp_server_cpp : server $(OUTPUT)/libinterptest_cpp.so | ||
LD_PRELOAD=$(OUTPUT)/libinterptest_cpp.so ./server | ||
|
||
run_client: client | ||
./client | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f *.o *.so client server | ||
rm -rfv $(OUTPUT) $(APPS) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#define _GNU_SOURCE | ||
#include <dlfcn.h> | ||
#include <netinet/in.h> // structure for storing address information | ||
#include <netinet/tcp.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> // for socket APIs | ||
|
||
int storedSockFd = 0; | ||
|
||
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { | ||
printf("Intercepted call to 'accept'\n"); | ||
|
||
static int (*real_accept)(int, struct sockaddr *, socklen_t *) = NULL; | ||
real_accept = | ||
(int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "accept"); | ||
if (real_accept == NULL) { | ||
fprintf(stderr, "Error in dlsym when querying for 'accept': %s\n", | ||
dlerror()); | ||
return -1; | ||
} | ||
|
||
int ret = real_accept(sockfd, addr, addrlen); | ||
printf("accept for FD=%d got FD=%d\n", sockfd, ret); | ||
|
||
if (ret != -1) { | ||
struct tcp_info info; | ||
socklen_t tcp_info_length = (socklen_t)sizeof(info); | ||
if (getsockopt(ret, SOL_TCP, TCP_INFO, (void *)&info, &tcp_info_length) == | ||
0) { | ||
printf("TCP_INFO rtt=%u, rto=%u\n", info.tcpi_rtt, info.tcpi_rto); | ||
} else { | ||
printf("Error in getsockopt TCP_INFO\n"); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int close(int sockfd) { | ||
printf("Intercepted call to 'close' for FD=%d\n", sockfd); | ||
|
||
static int (*real_close)(int) = NULL; | ||
real_close = (int (*)(int))dlsym(RTLD_NEXT, "close"); | ||
if (real_close == NULL) { | ||
fprintf(stderr, "Error in dlsym when querying for 'close': %s\n", | ||
dlerror()); | ||
return -1; | ||
} | ||
|
||
return real_close(sockfd); | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef _GNU_SOURCE | ||
#define _GNU_SOURCE | ||
#endif | ||
|
||
#include <dlfcn.h> | ||
#include <netinet/in.h> // structure for storing address information | ||
#include <netinet/tcp.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> // for socket APIs | ||
|
||
#include <unordered_set> | ||
|
||
std::unordered_set<int> sockfds; | ||
|
||
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { | ||
printf("Intercepted call to 'accept'\n"); | ||
|
||
static int (*real_accept)(int, struct sockaddr *, socklen_t *) = | ||
(int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "accept"); | ||
if (real_accept == NULL) { | ||
fprintf(stderr, "Error in dlsym when querying for 'accept': %s\n", | ||
dlerror()); | ||
return -1; | ||
} | ||
|
||
int ret = real_accept(sockfd, addr, addrlen); | ||
printf("accept for FD=%d got FD=%d\n", sockfd, ret); | ||
|
||
if (ret != -1) { | ||
sockfds.insert(ret); | ||
|
||
struct tcp_info info; | ||
socklen_t tcp_info_length = (socklen_t)sizeof(info); | ||
if (getsockopt(ret, SOL_TCP, TCP_INFO, (void *)&info, &tcp_info_length) == | ||
0) { | ||
printf("TCP_INFO rtt=%u, rto=%u\n", info.tcpi_rtt, info.tcpi_rto); | ||
} else { | ||
printf("Error in getsockopt TCP_INFO\n"); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int close(int sockfd) { | ||
printf("Intercepted call to 'close' for FD=%d\n", sockfd); | ||
|
||
static int (*real_close)(int) = (int (*)(int))dlsym(RTLD_NEXT, "close"); | ||
if (real_close == NULL) { | ||
fprintf(stderr, "Error in dlsym when querying for 'close': %s\n", | ||
dlerror()); | ||
return -1; | ||
} | ||
|
||
int ret = real_close(sockfd); | ||
if (ret != -1) { | ||
if (sockfds.find(sockfd) != sockfds.end()) { | ||
sockfds.erase(sockfd); | ||
} | ||
} | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters