Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: bmeck/interposed
base: 195b90f9b9
...
head fork: bmeck/interposed
compare: 02e5892079
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
12 fixtures/build.sh
@@ -1,2 +1,10 @@
-gcc test.c --no-builtin-memset -flat_namespace
-gcc -dynamiclib -o mac.dylib -flat_namespace mac.c
+OS=$(uname -s)
+if [ "$OS" == "Darwin" ]; then
+ echo "BUILDING FOR MAC"
+ gcc test.c --no-builtin-memset -flat_namespace
+ gcc -dynamiclib -o nix.dylib -flat_namespace nix.c
+else
+ gcc test.c --no-builtin-memset
+ gcc -shared -o nix.so -flat_namespace nix.c
+ echo 1
+fi
View
52 fixtures/mac.c
@@ -1,52 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <dlfcn.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-
-#define RTLD_SEND(params, ret) __RTLD_SEND(__FUNCTION__, params, ret)
-void __RTLD_SEND(const char name[], char* params, char* ret) {
- dprintf(4, "{\"method\":\"%s\",\"params\":%s,\"result\":%s}\n",name,params,ret);
-}
-
-void sockaddr_json(const struct sockaddr *addr, char addr_str[128]) {
- struct sockaddr_in* in_addr = (struct sockaddr_in*)addr;
- snprintf(addr_str, 128, "{\"port\":%d}", in_addr->sin_port);
-}
-
-// our fopen override implmentation
-int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
-{
- static const int (*original_bind) (int, const struct sockaddr *, socklen_t) = NULL;
- if (!original_bind) {
- original_bind = dlsym(RTLD_NEXT, "bind");
- }
- int result = original_bind(sockfd, addr, addrlen);
- char ret[15];
- snprintf(ret, 15, "%d", result);
- char params[256];
- char addr_str[128];
- sockaddr_json(addr, addr_str);
- snprintf(params, 64, "[%d,%s,%d]", sockfd, addr_str, addrlen);
- RTLD_SEND(params,ret);
- // return the result
- return result;
-}
-
-// our fopen override implmentation
-int listen(int sockfd, int backlog)
-{
- static const int (*original_listen) (int, int) = NULL;
- if (!original_listen) {
- original_listen = dlsym(RTLD_NEXT, "listen");
- }
- int result = original_listen(sockfd, backlog);
- char ret[15];
- snprintf(ret, 15, "%d", result);
- char params[64];
- snprintf(params, 64, "[%d,%d]", sockfd, backlog);
- RTLD_SEND(params,ret);
- // return the result
- return result;
-}
View
80 fixtures/nix.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dlfcn.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+static int __interposed_CHANNEL_FD = 1;
+
+#pragma CALL_ON_MODULE_BIND initptrs
+#pragma CALL_ON_LOAD initptrs
+#pragma init(initptrs)
+__attribute__((constructor))
+static void __interposed_init()
+{
+ char* env_channel_fd = getenv("NODE_CHANNEL_FD");
+ int found = 0;
+ if (env_channel_fd) {
+ found = sscanf(env_channel_fd,"%d", &__interposed_CHANNEL_FD);
+ }
+ if (!found) {
+ __interposed_CHANNEL_FD = 1;
+ }
+}
+
+#define RTLD_SEND(params, ret) __interposed_RTLD_SEND(__FUNCTION__, params, ret)
+void __interposed_RTLD_SEND(const char name[], char* params, char* ret) {
+ dprintf(__interposed_CHANNEL_FD, "{\"method\":\"%s\",\"params\":%s,\"result\":%s}\n",name,params,ret);
+}
+
+void sockaddr_json(struct sockaddr_in *in_addr, char addr_str[256]) {
+ char str[INET_ADDRSTRLEN];
+ int size = inet_ntop(in_addr->sin_family, &(in_addr->sin_addr), str, INET_ADDRSTRLEN);
+ snprintf(addr_str, 256, "{\"port\":%d,\"address\":\"%s\"}", ntohs(in_addr->sin_port), str);
+}
+
+int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
+{
+ static const int (*original_bind) (int, const struct sockaddr *, socklen_t) = NULL;
+ if (!original_bind) {
+ original_bind = dlsym(RTLD_NEXT, "bind");
+ }
+ if (addr->sa_family == AF_INET) {
+ struct sockaddr_in* in_addr = (struct sockaddr_in*)addr;
+ int desired = in_addr->sin_port;
+ int result = original_bind(sockfd, addr, addrlen);
+ while (result = -1 && (errno == EADDRINUSE || errno == EACCES)) {
+ in_addr->sin_port = ntohs(htons(in_addr->sin_port)+1);
+ in_addr->sin_addr.s_addr = INADDR_ANY;
+ result = original_bind(sockfd, addr, addrlen);
+ }
+ return result;
+ }
+ return original_bind(sockfd, addr, addrlen);
+}
+
+int listen(int sockfd, int backlog)
+{
+ static const int (*original_listen) (int, int) = NULL;
+ if (!original_listen) {
+ original_listen = dlsym(RTLD_NEXT, "listen");
+ }
+ int result = original_listen(sockfd, backlog);
+ char ret[15];
+ snprintf(ret, 15, "%d", result);
+ char params[512];
+ struct sockaddr_in addr;
+ static socklen_t addr_len = sizeof addr;
+ int len = getsockname(sockfd, (struct sockaddr*)&addr, &addr_len);
+ if (len <= sizeof addr && addr.sin_family == AF_INET) {
+ char addr_str[256];
+ sockaddr_json(&addr, addr_str);
+ snprintf(params, 512, "{\"fd\":%d,\"address\":%s,\"backlog\":%d]", sockfd, addr_str, backlog);
+ RTLD_SEND(params,ret);
+ // return the result
+ }
+ return result;
+}
View
7 fixtures/run.sh
@@ -1 +1,6 @@
-DYLD_INSERT_LIBRARIES=mac.dylib ./a.out
+OS=$(uname -s)
+if [ "$OS" == "Darwin" ]; then
+ DYLD_INSERT_LIBRARIES=nix.dylib ./a.out
+else
+ LD_PRELOAD=nix.so ./a.out
+fi
View
22 fixtures/test.c
@@ -1,18 +1,22 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
+#include <unistd.h>
int main(int argc, char** argv) {
- int sock = socket(AF_INET,SOCK_STREAM,0);
+ int sock;
struct sockaddr_in sin;
- int PORT = 1337;
+ short PORT = 1000;
/* complete the socket structure */
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
- sin.sin_port = htons(PORT);
-
- bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr));
- listen(sock, 128);
+ while (1) {
+ sock = socket(AF_INET,SOCK_STREAM,0);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(PORT);
+ bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr));
+ listen(sock, 128);
+ close(sock);
+ }
return 0;
}
View
1  fixtures/test.js
@@ -0,0 +1 @@
+require('http').createServer(function () {}).listen(80);
View
0  fixtures/win.c
No changes.

No commit comments for this range

Something went wrong with that request. Please try again.