Skip to content

Commit

Permalink
ruleset: revert src param
Browse files Browse the repository at this point in the history
Signed-off-by: hexian000 <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Aug 16, 2023
1 parent 15d1e4d commit eef60b8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 56 deletions.
12 changes: 1 addition & 11 deletions src/http_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,7 @@ static bool proxy_dial(
}
req = dialreq_new(&ctx->addr, 0);
} else {
sockaddr_max_t peer_addr;
socklen_t len = sizeof(peer_addr);
struct sockaddr *peer = &peer_addr.sa;
if (getpeername(ctx->accepted_fd, peer, &len) != 0) {
const int err = errno;
HTTP_CTX_LOG_F(
LOG_LEVEL_WARNING, ctx, "getpeername: %s",
strerror(err));
peer = NULL;
}
req = ruleset_resolve(ruleset, addr_str, peer);
req = ruleset_resolve(ruleset, addr_str);
}
if (req == NULL) {
return false;
Expand Down
34 changes: 9 additions & 25 deletions src/ruleset.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ static int ruleset_request_(lua_State *restrict L)
{
const char *func = lua_topointer(L, 1);
const char *request = lua_topointer(L, 2);
const struct sockaddr *sa = lua_topointer(L, 3);
if (find_callback(L, 1) != 1) {
struct dialreq *req = request_accept(request);
lua_pushlightuserdata(L, req);
Expand All @@ -462,16 +461,6 @@ static int ruleset_request_(lua_State *restrict L)
lua_replace(L, 1);
(void)lua_pushstring(L, request);
lua_replace(L, 2);
{
char addr_buf[64];
if (sa != NULL &&
format_sa(sa, addr_buf, sizeof(addr_buf)) > 0) {
lua_pushstring(L, addr_buf);
} else {
lua_pushnil(L);
}
}
lua_replace(L, 3);
lua_call(L, 1, LUA_MULTRET);
const int n = lua_gettop(L);
if (n < 1) {
Expand All @@ -494,37 +483,32 @@ static int ruleset_request_(lua_State *restrict L)
return 1;
}

static struct dialreq *dispatch_req(
struct ruleset *restrict r, const char *func, const char *request,
const struct sockaddr *src)
static struct dialreq *
dispatch_req(struct ruleset *restrict r, const char *func, const char *request)
{
lua_State *restrict L = r->L;
const int ret = ruleset_pcall(
r, ruleset_request_, 2, 1, (void *)func, (void *)request,
(void *)src);
r, ruleset_request_, 2, 1, (void *)func, (void *)request);
if (ret != LUA_OK) {
LOGE_F("ruleset.%s: %s", func, lua_tostring(L, -1));
return NULL;
}
return (struct dialreq *)lua_topointer(L, -1);
}

struct dialreq *ruleset_resolve(
struct ruleset *r, const char *request, const struct sockaddr *src)
struct dialreq *ruleset_resolve(struct ruleset *r, const char *request)
{
return dispatch_req(r, "resolve", request, src);
return dispatch_req(r, "resolve", request);
}

struct dialreq *ruleset_route(
struct ruleset *r, const char *request, const struct sockaddr *src)
struct dialreq *ruleset_route(struct ruleset *r, const char *request)
{
return dispatch_req(r, "route", request, src);
return dispatch_req(r, "route", request);
}

struct dialreq *ruleset_route6(
struct ruleset *r, const char *request, const struct sockaddr *src)
struct dialreq *ruleset_route6(struct ruleset *r, const char *request)
{
return dispatch_req(r, "route6", request, src);
return dispatch_req(r, "route6", request);
}

size_t ruleset_memused(struct ruleset *restrict r)
Expand Down
9 changes: 3 additions & 6 deletions src/ruleset.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ const char *ruleset_loadfile(struct ruleset *r, const char *filename);
void ruleset_gc(struct ruleset *r);
void ruleset_free(struct ruleset *r);

struct dialreq *ruleset_resolve(
struct ruleset *r, const char *request, const struct sockaddr *src);
struct dialreq *ruleset_route(
struct ruleset *r, const char *request, const struct sockaddr *src);
struct dialreq *ruleset_route6(
struct ruleset *r, const char *request, const struct sockaddr *src);
struct dialreq *ruleset_resolve(struct ruleset *r, const char *request);
struct dialreq *ruleset_route(struct ruleset *r, const char *request);
struct dialreq *ruleset_route6(struct ruleset *r, const char *request);

size_t ruleset_memused(struct ruleset *r);
const char *ruleset_stats(struct ruleset *r, double dt);
Expand Down
17 changes: 3 additions & 14 deletions src/socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,26 +656,15 @@ static struct dialreq *make_dialreq(struct socks_ctx *restrict ctx)
return req;
}

sockaddr_max_t peer_addr;
socklen_t len = sizeof(peer_addr);
struct sockaddr *peer = &peer_addr.sa;
if (getpeername(ctx->accepted_fd, peer, &len) != 0) {
const int err = errno;
SOCKS_CTX_LOG_F(
LOG_LEVEL_WARNING, ctx, "getpeername: %s",
strerror(err));
peer = NULL;
}

char request[FQDN_MAX_LENGTH + 1 + 5 + 1];
(void)dialaddr_format(&ctx->addr, request, sizeof(request));
switch (ctx->addr.type) {
case ATYP_DOMAIN:
return ruleset_resolve(ruleset, request, peer);
return ruleset_resolve(ruleset, request);
case ATYP_INET:
return ruleset_route(ruleset, request, peer);
return ruleset_route(ruleset, request);
case ATYP_INET6:
return ruleset_route6(ruleset, request, peer);
return ruleset_route6(ruleset, request);
default:
SOCKS_CTX_LOG_F(
LOG_LEVEL_ERROR, ctx, "unsupported address type: %d",
Expand Down

0 comments on commit eef60b8

Please sign in to comment.