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