~ctrlproxy/ctrlproxy/trunk

« back to all changes in this revision

Viewing changes to transport.c

  • Committer: jelmer
  • Date: 2003-10-18 22:02:02 UTC
  • Revision ID: jelmer@samba.org-20031018220202-6801a76318fb4d13
Update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
        ctrlproxy: A modular IRC proxy
 
3
        (c) 2003 Jelmer Vernooij <jelmer@nl.linux.org>
 
4
 
 
5
        This program is free software; you can redistribute it and/or modify
 
6
        it under the terms of the GNU General Public License as published by
 
7
        the Free Software Foundation; either version 2 of the License, or
 
8
        (at your option) any later version.
 
9
 
 
10
        This program is distributed in the hope that it will be useful,
 
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
        GNU General Public License for more details.
 
14
 
 
15
        You should have received a copy of the GNU General Public License
 
16
        along with this program; if not, write to the Free Software
 
17
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
*/
 
19
 
 
20
#include "internals.h"
 
21
#include "config.h"
 
22
 
 
23
static GList *transports = NULL;
 
24
extern FILE *debugfd;
 
25
 
 
26
void register_transport(struct transport *functions)
 
27
{
 
28
        struct transport *functions1 = malloc(sizeof(struct transport));
 
29
        memcpy(functions1, functions, sizeof(struct transport));
 
30
        functions1->reference_count = 0;
 
31
        transports = g_list_append(transports, functions1);
 
32
}
 
33
 
 
34
gboolean unregister_transport(char *name)
 
35
{
 
36
        GList *gl = transports;
 
37
        while(gl) {
 
38
                struct transport *t = (struct transport *)gl->data;
 
39
                if(!strcmp(t->name, name)) {
 
40
                        if(t->reference_count == 0) {
 
41
                                transports = g_list_remove(transports, gl->data);
 
42
                                free(t);
 
43
                                return TRUE;
 
44
                        } else {
 
45
                                return FALSE; /* Transport is still in use */
 
46
                        }
 
47
                }
 
48
                gl = gl->next;
 
49
        }
 
50
        return FALSE;
 
51
}
 
52
 
 
53
struct transport_context *transport_connect(const char *name, xmlNodePtr p, receive_handler r_h, disconnect_handler d_h, void *d)
 
54
{
 
55
        struct transport_context *ret;
 
56
        struct transport *t;
 
57
        GList *g = transports;
 
58
 
 
59
        while(g) {
 
60
                t = (struct transport *)g->data;
 
61
                if(!strcmp(t->name, name))break;
 
62
                g = g->next;
 
63
        }
 
64
 
 
65
        if(!g) return NULL;
 
66
 
 
67
        t->reference_count++;
 
68
        
 
69
        ret = malloc(sizeof(struct transport_context));
 
70
        ret->configuration = p;
 
71
        ret->functions = t;
 
72
        ret->data = NULL;
 
73
        ret->caller_data = d;
 
74
        ret->on_new_client = NULL;
 
75
        ret->on_disconnect = d_h;
 
76
        ret->on_receive = r_h;
 
77
        
 
78
        if(!ret->functions->connect || ret->functions->connect(ret) == -1)
 
79
        { 
 
80
                free(ret);
 
81
                return NULL;
 
82
        }
 
83
        
 
84
        return ret;
 
85
}
 
86
 
 
87
struct transport_context *transport_listen(const char *name, xmlNodePtr p, newclient_handler n_h, void *d)
 
88
{
 
89
        struct transport_context *ret;
 
90
        struct transport *t;
 
91
        GList *g = transports;
 
92
 
 
93
        while(g) {
 
94
                t = (struct transport *)g->data;
 
95
                if(!strcmp(t->name, name))break;
 
96
                g = g->next;
 
97
        }
 
98
 
 
99
        if(!g) return NULL;
 
100
        
 
101
        t->reference_count++;
 
102
        
 
103
        ret = malloc(sizeof(struct transport_context));
 
104
        ret->configuration = p;
 
105
        ret->functions = t;
 
106
        ret->data = NULL;
 
107
        ret->caller_data = d;
 
108
        ret->on_new_client = n_h;
 
109
        ret->on_disconnect = NULL;
 
110
        ret->on_receive = NULL;
 
111
        
 
112
        if(!ret->functions->listen || ret->functions->listen(ret) == -1)
 
113
        { 
 
114
                free(ret);
 
115
                return NULL;
 
116
        }
 
117
        
 
118
        return ret;
 
119
}
 
120
 
 
121
void transport_free(struct transport_context *t)
 
122
{
 
123
        if(!t) return;
 
124
        t->functions->reference_count++;
 
125
        if(t->functions->close) t->functions->close(t);
 
126
        free(t);
 
127
}
 
128
 
 
129
int transport_write(struct transport_context *t, char *l)
 
130
{
 
131
        if(debugfd)fprintf(debugfd, "[TO] %s\n", l);
 
132
        if(!t->functions->write)return -1;
 
133
        return t->functions->write(t, l);
 
134
}
 
135
 
 
136
void transport_set_disconnect_handler(struct transport_context *t, disconnect_handler d_h)
 
137
{
 
138
        t->on_disconnect = d_h;
 
139
}
 
140
 
 
141
void transport_set_receive_handler(struct transport_context *t, receive_handler r_h)
 
142
{
 
143
        t->on_receive = r_h;
 
144
}
 
145
 
 
146
void transport_set_newclient_handler(struct transport_context *t, newclient_handler n_h)
 
147
{
 
148
        t->on_new_client = n_h;
 
149
}
 
150
 
 
151
void transport_set_data(struct transport_context *t, void *data)
 
152
{
 
153
        t->caller_data = data;
 
154
}