patterncMinor
Dynamic library to intercept oddsock hostname lookups
Viewed 0 times
interceptlookupshostnamelibrarydynamicoddsock
Problem
Just for the fun of it, I decided I would try to create sort of an intermediate DNS system for the oddsock SOCKS proxy. With this, the domain name extension .unet is statically resolved when requested. All of this is hooked into the oddsocks proxy system via inserting into the DYLD before libevent does. Tell me what you think.
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct unetent { char name[64], ip[13]; };
struct lnode
{
struct unetent data;
struct lnode *next;
};
struct lnode list_insert(struct lnode list, char name, char ip);
int list_size(struct lnode *list);
struct unetent list_get(struct lnode list, int p);
struct lnode list_getp(struct lnode list, int p);
struct lnode insert_from_file(struct lnode list, FILE f, int code);
int (original_bufferevent_socket_connect_hostname)(void bev, void evdns_base, int family, const char hostname, int port) = NULL;
struct lnode *node_list = NULL;
time_t refresh_list_after = 0;
struct lnode list_insert(struct lnode list, char name, char ip)
{
struct lnode ptr = malloc(sizeof(struct lnode)), tmp = NULL;
strcpy(ptr->data.name, name);
strcpy(ptr->data.ip, ip);
ptr->next = NULL;
if (list == NULL) return ptr;
for (tmp = list; tmp->next != NULL; tmp = tmp->next);
tmp->next = ptr;
return list;
}
int list_size(struct lnode *list)
{
int i;
struct lnode *tmp = list;
for (i = 0; tmp != NULL; i++, tmp = tmp->next);
return i;
}
struct unetent list_get(struct lnode list, int p)
{
int i;
struct lnode *tmp = list;
for (i = 0; i next;
return &tmp->data;
}
struct lnode list_getp(struct lnode list, int p)
{
int i;
struct lnode *tmp = list;
if (p next;
return tmp;
}
struct lnode insert_from_file(struct lnode list, FILE f, int code)
{
struct unetent tmp;
if (code == NULL)
code = malloc(sizeof(int));
*code = fread(
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct unetent { char name[64], ip[13]; };
struct lnode
{
struct unetent data;
struct lnode *next;
};
struct lnode list_insert(struct lnode list, char name, char ip);
int list_size(struct lnode *list);
struct unetent list_get(struct lnode list, int p);
struct lnode list_getp(struct lnode list, int p);
struct lnode insert_from_file(struct lnode list, FILE f, int code);
int (original_bufferevent_socket_connect_hostname)(void bev, void evdns_base, int family, const char hostname, int port) = NULL;
struct lnode *node_list = NULL;
time_t refresh_list_after = 0;
struct lnode list_insert(struct lnode list, char name, char ip)
{
struct lnode ptr = malloc(sizeof(struct lnode)), tmp = NULL;
strcpy(ptr->data.name, name);
strcpy(ptr->data.ip, ip);
ptr->next = NULL;
if (list == NULL) return ptr;
for (tmp = list; tmp->next != NULL; tmp = tmp->next);
tmp->next = ptr;
return list;
}
int list_size(struct lnode *list)
{
int i;
struct lnode *tmp = list;
for (i = 0; tmp != NULL; i++, tmp = tmp->next);
return i;
}
struct unetent list_get(struct lnode list, int p)
{
int i;
struct lnode *tmp = list;
for (i = 0; i next;
return &tmp->data;
}
struct lnode list_getp(struct lnode list, int p)
{
int i;
struct lnode *tmp = list;
if (p next;
return tmp;
}
struct lnode insert_from_file(struct lnode list, FILE f, int code)
{
struct unetent tmp;
if (code == NULL)
code = malloc(sizeof(int));
*code = fread(
Solution
-
I'd recommend staying consistent with curly brace usage for conditionals. It can help with readability and also ease maintainability if you end up needing to add additional lines.
However, it's okay to leave them out if you have a single-line conditional that you know will never require more than one line:
-
Your loop counters should be declared right before the loop statement as opposed to the start of the function (before the other variables). Doing so will keep a minimum scope on the counter, and everything will be in one place should the loop ever need to be removed.
-
It can just have one line
If the return value of
I'd recommend staying consistent with curly brace usage for conditionals. It can help with readability and also ease maintainability if you end up needing to add additional lines.
if (someCondition)
{
// do something...
// can still do something else...
}However, it's okay to leave them out if you have a single-line conditional that you know will never require more than one line:
if (someConditional) return something;-
Your loop counters should be declared right before the loop statement as opposed to the start of the function (before the other variables). Doing so will keep a minimum scope on the counter, and everything will be in one place should the loop ever need to be removed.
-
write_data() doesn't need two lines: size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}It can just have one line
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}If the return value of
fwrite() will just be returned right away, then another variable isn't needed. The name also doesn't add much to what is already known about the function.Code Snippets
if (someCondition)
{
// do something...
// can still do something else...
}if (someConditional) return something;size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}Context
StackExchange Code Review Q#51316, answer score: 8
Revisions (0)
No revisions yet.