I’m working on a C program where I’ve created a shared library to intercept the connect
system call and route the connection through a proxy server. The code works, but after running it for a while, I encounter a “Too Many Files Open” error, even though I’ve set ulimit -n
to unlimited.
Here’s the relevant part of my code:
#include "toralize.h"
Req *request(struct sockaddr_in *sock2) {
Req *req;
req = malloc(reqsize);
if (req == NULL) {
perror("malloc");
return NULL; // Handle the error appropriately in the calling function
}
req->vn = 4;
req->cd = 1;
req->dstip = sock2->sin_addr.s_addr;
req->dstport = sock2->sin_port;
strncpy(req->userid, USERNAME, 8);
return req;
}
int connect(int s2, const struct sockaddr *sock2, socklen_t addrlen) {
int s;
Req *req;
Res *res;
struct sockaddr_in sock;
char buf[ressize];
int success;
int (*p)(int, const struct sockaddr*, socklen_t);
p = dlsym(RTLD_NEXT, "connect");
if (p == NULL) {
perror("dlsym");
return -1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
sock.sin_family = AF_INET;
sock.sin_port = htons(PROXYPORT);
sock.sin_addr.s_addr = inet_addr(PROXY);
printf("Connecting to %s:%dn", inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
if (p(s, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
perror("connect");
close(s);
return -1;
}
printf("Connected to proxy.n");
req = request((struct sockaddr_in *)sock2);
if (req == NULL) {
close(s);
return -1;
}
if (write(s, req, reqsize) < 0) {
perror("write");
free(req);
close(s);
return -1;
}
memset(buf, 0, ressize);
if (read(s, buf, ressize) < 1) {
perror("read");
free(req);
close(s);
return -1;
}
res = (Res *)buf;
success = (res->cd == 90);
if (!success) {
fprintf(stderr, "Unable to traverse"
"the proxy, error code: %dn",
res->cd);
close(s);
free(req);
return -1;
}
printf("Connected through the proxy.n");
close(s2);
if (dup2(s, s2) < 0) {
perror("dup2");
free(req);
close(s);
return -1;
}
close(s);
free(req);
return 0;
}
Problem: Even though I’ve set ulimit -n
to unlimited, I’m still getting a “Too Many Files Open” error after a while. I suspect this might be due to file descriptor leakage, but I’m not sure where the issue lies. I’m properly closing file descriptors in most places, but something seems to be missing.
toralize.h in case if there’s an issue with that file:
/* toralize.h */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<dlfcn.h>
#define PROXY "127.0.0.1"
#define PROXYPORT 9050
#define reqsize sizeof(struct proxy_request)
#define ressize sizeof(struct proxy_response)
#define USERNAME "toraliz"
typedef unsigned char int8;
typedef unsigned short int int16;
typedef unsigned int int32;
struct proxy_request {
int8 vn;
int8 cd;
int16 dstport;
int32 dstip;
unsigned char userid[8];
};
typedef struct proxy_request Req;
struct proxy_response {
int8 vn;
int8 cd;
int16 _;
int32 __;
};
typedef struct proxy_response Res;
Req *request(struct sockaddr_in*);
int connect(int, const struct sockaddr*, socklen_t);
Lastly my build command:
gcc toralize.c -o toralize.so -fPIC -shared -ldl -D_GNU_SOURCE
What I’ve Tried:
-
Verified that
ulimit -n
is set to unlimited. -
Checked the code for places where file descriptors might not be properly closed.
Question:
-
What could be causing the “Too Many Files Open” error in this scenario?
-
How can I fix or debug this issue more effectively?
Any help or insights would be greatly appreciated!