remove container_of, remove some underscores in the process

From the original patch by hawken93 <hawken@thehawken.org> 5th Oct 2014
This commit is contained in:
Bob Mottram 2016-04-14 14:49:25 +01:00
commit cf48ac6210
6 changed files with 82 additions and 64 deletions

View file

@ -27,28 +27,35 @@
static void network_proxy_http_destroy(struct network_proxy *proxy)
{
struct _network_proxy_http *self = container_of(proxy, struct _network_proxy_http, proxy);
struct network_proxy_http *self = (struct network_proxy_http *)proxy->privdata;
g_free(self->password);
_network_proxy_destroy(proxy);
g_free(self);
_network_proxy_destroy(proxy);
g_free(proxy);
}
static struct network_proxy *
network_proxy_http_clone(struct network_proxy const *proxy)
static struct network_proxy *network_proxy_http_clone(struct network_proxy const *proxy)
{
struct _network_proxy_http *self = container_of(proxy, struct _network_proxy_http, proxy);
struct _network_proxy_http *res;
struct network_proxy_http *self = (struct network_proxy_http *)proxy->privdata;
struct network_proxy_http *priv;
struct network_proxy *res;
res = g_malloc0(sizeof *res);
res = g_malloc0(sizeof(struct network_proxy));
_network_proxy_clone(&res->proxy, &self->proxy);
res->password = g_strdup(self->password);
return &res->proxy;
_network_proxy_clone(res, proxy);
priv = g_malloc0(sizeof(struct network_proxy_http));
res->privdata = (void *)priv;
priv->password = g_strdup(self->password);
return res;
}
static bool send_connect(struct _network_proxy_http *proxy, GIOChannel *ch,
static bool send_connect(struct network_proxy_http *proxy, GIOChannel *ch,
char const *address, uint16_t port)
{
char port_str[6];
@ -67,7 +74,7 @@ static bool send_connect(struct _network_proxy_http *proxy, GIOChannel *ch,
return true;
}
static int read_response(struct _network_proxy_http *proxy, GIOChannel *ch)
static int read_response(struct network_proxy_http *proxy, GIOChannel *ch)
{
GIOStatus status;
GString line = { .str = NULL };
@ -126,7 +133,7 @@ err:
static GIOChannel *network_proxy_http_connect(struct network_proxy const *proxy, IPADDR const *hint_ip,
char const *address, int port)
{
struct _network_proxy_http *self = container_of(proxy, struct _network_proxy_http, proxy);
struct network_proxy_http *self = (struct network_proxy_http *)proxy->privdata;
GIOChannel *ch;
GIOFlags old_flags;
GError *err = NULL;
@ -134,9 +141,9 @@ static GIOChannel *network_proxy_http_connect(struct network_proxy const *proxy,
gint line_term_sz;
if (hint_ip)
ch = net_connect_ip(hint_ip, self->proxy.port, NULL);
ch = net_connect_ip(hint_ip, proxy->port, NULL);
else
ch = net_connect(self->proxy.host, self->proxy.port, NULL);
ch = net_connect(proxy->host, proxy->port, NULL);
if (!ch)
return NULL;
@ -172,18 +179,23 @@ err:
}
struct network_proxy *_network_proxy_http_create(void)
struct network_proxy *network_proxy_http_create(void)
{
struct _network_proxy_http *res;
struct network_proxy *res;
struct network_proxy_http *priv;
res = g_malloc0(sizeof *res);
res = g_malloc0(sizeof(struct network_proxy));
_network_proxy_create(&res->proxy);
res->password = g_strdup(settings_get_str("proxy_password"));
_network_proxy_create(res);
res->proxy.destroy = network_proxy_http_destroy;
res->proxy.connect = network_proxy_http_connect;
res->proxy.clone = network_proxy_http_clone;
priv = g_malloc0(sizeof(struct network_proxy_http));
res->privdata = (void *)priv;
return &res->proxy;
priv->password = g_strdup(settings_get_str("proxy_password"));
res->destroy = network_proxy_http_destroy;
res->connect = network_proxy_http_connect;
res->clone = network_proxy_http_clone;
return res;
}

View file

@ -19,11 +19,10 @@
#include "network-proxy.h"
struct _network_proxy_http {
struct network_proxy proxy;
struct network_proxy_http {
char *password;
};
struct network_proxy *_network_proxy_http_create(void);
struct network_proxy *network_proxy_http_create(void);
#endif /* H_IRSSI_SRC_CORE_PROXY_HTTP_H */

View file

@ -20,14 +20,10 @@
#include "settings.h"
#include <stdbool.h>
/* stolen from linux kernel */
#define container_of(ptr, type, member) __extension__ ({ \
const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
inline static void _network_proxy_create(struct network_proxy *dst)
{
// TODO: Initialize all fields, to bring the struct to a known state
dst->privdata = NULL;
dst->port = settings_get_int("proxy_port");
dst->host = g_strdup(settings_get_str("proxy_address"));
}

View file

@ -63,25 +63,30 @@ struct server_response
static void network_proxy_socks5_destroy(struct network_proxy *proxy)
{
struct _network_proxy_socks5 *self = container_of(proxy, struct _network_proxy_socks5, proxy);
struct network_proxy_socks5 *self = (struct network_proxy_socks5 *)proxy->privdata;
g_free(self->password);
g_free(self->username);
_network_proxy_destroy(proxy);
g_free(self);
_network_proxy_destroy(proxy);
g_free(proxy);
}
static struct network_proxy *network_proxy_socks5_clone(struct network_proxy const *proxy)
{
struct _network_proxy_socks5 *self = container_of(proxy, struct _network_proxy_socks5, proxy);
struct _network_proxy_socks5 *res;
struct network_proxy_socks5 *self = (struct network_proxy_socks5 *)proxy->privdata;
struct network_proxy_socks5 *priv;
struct network_proxy *res;
res = g_malloc0(sizeof *res);
res = g_malloc0(sizeof(struct network_proxy));
_network_proxy_clone(res, proxy);
_network_proxy_clone(&res->proxy, &self->proxy);
res->username = g_strdup(self->username);
res->password = g_strdup(self->password);
return &res->proxy;
priv = g_malloc0(sizeof(struct network_proxy_socks5));
res->privdata = (void *)priv;
priv->username = g_strdup(self->username);
priv->password = g_strdup(self->password);
return res;
}
static bool socks5_connect_unauthorized(GIOChannel *ch)
@ -92,9 +97,9 @@ static bool socks5_connect_unauthorized(GIOChannel *ch)
}
/* TODO: test this method! */
static bool socks5_connect_plain(struct _network_proxy_socks5 const *proxy, GIOChannel *ch)
static bool socks5_connect_plain(const struct network_proxy_socks5 *proxy, GIOChannel *ch)
{
uint8_t ver = 0x01;
uint8_t ver = 0x01;
uint8_t ulen = strlen(proxy->username);
uint8_t plen = proxy->password ? strlen(proxy->password) : 0;
struct server_response_plain resp;
@ -122,7 +127,7 @@ static bool socks5_connect_plain(struct _network_proxy_socks5 const *proxy, GIOC
return true;
}
static bool socks5_connect(struct _network_proxy_socks5 const *proxy, GIOChannel *ch,
static bool socks5_connect(const struct network_proxy_socks5 *proxy, GIOChannel *ch,
char const *address, uint16_t port)
{
bool rc;
@ -265,7 +270,7 @@ err:
static GIOChannel *network_proxy_socks5_connect(struct network_proxy const *proxy, IPADDR const *hint_ip,
char const *address, int port)
{
struct _network_proxy_socks5 *self = container_of(proxy, struct _network_proxy_socks5, proxy);
struct network_proxy_socks5 *self = (struct network_proxy_socks5 *)proxy->privdata;
GIOChannel *ch;
GIOFlags old_flags;
@ -274,9 +279,9 @@ static GIOChannel *network_proxy_socks5_connect(struct network_proxy const *prox
GError *err = NULL;
if (hint_ip)
ch = net_connect_ip(hint_ip, self->proxy.port, NULL);
ch = net_connect_ip(hint_ip, proxy->port, NULL);
else
ch = net_connect(self->proxy.host, self->proxy.port, NULL);
ch = net_connect(proxy->host, proxy->port, NULL);
if (!ch)
return NULL;
@ -313,19 +318,24 @@ err:
return NULL;
}
struct network_proxy *_network_proxy_socks5_create(void)
struct network_proxy *network_proxy_socks5_create(void)
{
struct _network_proxy_socks5 *res;
struct network_proxy *res;
struct network_proxy_socks5 *priv;
res = g_malloc0(sizeof *res);
res = g_malloc0(sizeof(struct network_proxy));
_network_proxy_create(&res->proxy);
res->username = g_strdup(settings_get_str("proxy_username"));
res->password = g_strdup(settings_get_str("proxy_password"));
_network_proxy_create(res);
res->proxy.destroy = network_proxy_socks5_destroy;
res->proxy.connect = network_proxy_socks5_connect;
res->proxy.clone = network_proxy_socks5_clone;
priv = g_malloc0(sizeof(struct network_proxy_socks5));
res->privdata = (void *)priv;
return &res->proxy;
priv->username = g_strdup(settings_get_str("proxy_username"));
priv->password = g_strdup(settings_get_str("proxy_password"));
res->destroy = network_proxy_socks5_destroy;
res->connect = network_proxy_socks5_connect;
res->clone = network_proxy_socks5_clone;
return res;
}

View file

@ -19,13 +19,11 @@
#include "network-proxy.h"
struct _network_proxy_socks5 {
struct network_proxy proxy;
struct network_proxy_socks5 {
char *username;
char *password;
};
struct network_proxy *_network_proxy_socks5_create(void);
struct network_proxy *network_proxy_socks5_create(void);
#endif /* H_IRSSI_SRC_CORE_PROXY_SOCKS5_H */

View file

@ -35,6 +35,9 @@ struct network_proxy_send_string_info
};
struct network_proxy {
/* Contains private data for the chosen proxy type */
void *privdata;
/* destroys the network_proxy structure which must not be used anymore
* after; this memberfunction is mandatory */
void (*destroy)(struct network_proxy *);