Imported Upstream version 3.2.0
[debian/amanda] / xfer-src / dest-directtcp-connect.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 N Mathlida Ave, Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #include "amanda.h"
23 #include "amxfer.h"
24 #include "sockaddr-util.h"
25
26 /*
27  * Class declaration
28  *
29  * This declaration is entirely private; nothing but xfer_dest_directtcp_connect() references
30  * it directly.
31  */
32
33 GType xfer_dest_directtcp_connect_get_type(void);
34 #define XFER_DEST_DIRECTTCP_CONNECT_TYPE (xfer_dest_directtcp_connect_get_type())
35 #define XFER_DEST_DIRECTTCP_CONNECT(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_directtcp_connect_get_type(), XferDestDirectTCPConnect)
36 #define XFER_DEST_DIRECTTCP_CONNECT_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_directtcp_connect_get_type(), XferDestDirectTCPConnect const)
37 #define XFER_DEST_DIRECTTCP_CONNECT_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_directtcp_connect_get_type(), XferDestDirectTCPConnectClass)
38 #define IS_XFER_DEST_DIRECTTCP_CONNECT(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_directtcp_connect_get_type ())
39 #define XFER_DEST_DIRECTTCP_CONNECT_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_directtcp_connect_get_type(), XferDestDirectTCPConnectClass)
40
41 static GObjectClass *parent_class = NULL;
42
43 /*
44  * Main object structure
45  */
46
47 typedef struct XferDestDirectTCPConnect {
48     XferElement __parent__;
49
50     /* addresses to connect to (copied locally) */
51     DirectTCPAddr *addrs;
52 } XferDestDirectTCPConnect;
53
54 /*
55  * Class definition
56  */
57
58 typedef struct {
59     XferElementClass __parent__;
60 } XferDestDirectTCPConnectClass;
61
62 /*
63  * Implementation
64  */
65
66 static gboolean
67 setup_impl(
68     XferElement *elt)
69 {
70     XferDestDirectTCPConnect *self = (XferDestDirectTCPConnect *)elt;
71
72     g_assert(self->addrs && self->addrs->ipv4);
73     elt->input_listen_addrs = self->addrs;
74
75     return TRUE;
76 }
77
78 static void
79 finalize_impl(
80     GObject * obj_self)
81 {
82     XferDestDirectTCPConnect *self = XFER_DEST_DIRECTTCP_CONNECT(obj_self);
83
84     if (self->addrs)
85         g_free(self->addrs);
86     self->addrs = NULL;
87
88     /* chain up */
89     G_OBJECT_CLASS(parent_class)->finalize(obj_self);
90 }
91
92 static void
93 class_init(
94     XferDestDirectTCPConnectClass * selfc)
95 {
96     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
97     GObjectClass *goc = G_OBJECT_CLASS(selfc);
98     static xfer_element_mech_pair_t mech_pairs[] = {
99         { XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_NONE, 0, 0},
100         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
101     };
102
103     klass->setup = setup_impl;
104     goc->finalize = finalize_impl;
105
106     klass->perl_class = "Amanda::Xfer::Dest::DirectTCPConnect";
107     klass->mech_pairs = mech_pairs;
108
109     parent_class = g_type_class_peek_parent(selfc);
110 }
111
112 GType
113 xfer_dest_directtcp_connect_get_type (void)
114 {
115     static GType type = 0;
116
117     if G_UNLIKELY(type == 0) {
118         static const GTypeInfo info = {
119             sizeof (XferDestDirectTCPConnectClass),
120             (GBaseInitFunc) NULL,
121             (GBaseFinalizeFunc) NULL,
122             (GClassInitFunc) class_init,
123             (GClassFinalizeFunc) NULL,
124             NULL /* class_data */,
125             sizeof (XferDestDirectTCPConnect),
126             0 /* n_preallocs */,
127             (GInstanceInitFunc) NULL,
128             NULL
129         };
130
131         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestDirectTCPConnect", &info, 0);
132     }
133
134     return type;
135 }
136
137 /* create an element of this class; prototype is in xfer-element.h */
138 XferElement *
139 xfer_dest_directtcp_connect(
140         DirectTCPAddr *addrs)
141 {
142     XferDestDirectTCPConnect *self = (XferDestDirectTCPConnect *)
143                 g_object_new(XFER_DEST_DIRECTTCP_CONNECT_TYPE, NULL);
144     XferElement *elt = XFER_ELEMENT(self);
145     int i;
146
147     g_assert(addrs != NULL);
148
149     for (i = 0; addrs[i].port; i++) ;
150     self->addrs = g_memdup(addrs, (i+1) * sizeof(*addrs));
151
152     return elt;
153 }