Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndma_ctrl_conn.c
1 /*
2  * Copyright (c) 1998,1999,2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *
35  */
36
37
38 #include "ndmagents.h"
39
40
41 #ifndef NDMOS_OPTION_NO_CONTROL_AGENT
42
43
44 int
45 ndmca_connect_xxx_agent (
46   struct ndm_session *sess,
47   struct ndmconn **connp,
48   char *prefix,
49   struct ndmagent *agent)
50 {
51         struct ndmconn *        conn = *connp;
52         char *                  err;
53         int                     rc;
54
55         if (conn)
56                 return 0;               /* already connected */
57
58         if (agent->conn_type == NDMCONN_TYPE_NONE) {
59                 ndmalogf (sess, 0, 0, "agent %s not give", prefix+1);
60                 return -1;
61         }
62
63         conn = ndmconn_initialize (0, prefix);
64         if (!conn) {
65                 ndmalogf (sess, prefix, 0, "can't init connection");
66                 return -1;
67         }
68
69         if (sess->control_acb.job.time_limit > 0)
70             conn->time_limit = sess->control_acb.job.time_limit;
71
72         ndmconn_set_snoop (conn, &sess->param.log, sess->param.log_level);
73
74         conn->call = ndma_call;
75         conn->context = sess;
76         conn->unexpected = ndma_dispatch_ctrl_unexpected;
77
78         rc = ndmconn_connect_agent (conn, agent);
79         if (rc) {
80                 err = "Can't connect";
81                 goto error_out;
82         }
83
84         rc = ndmconn_auth_agent (conn, agent);
85         if (rc) {
86                 err = "Can't auth (bad pw?)";
87                 goto error_out;
88         }
89
90         *connp = conn;
91         return 0;
92
93   error_out:
94         ndmalogf (sess, prefix, 0, "err %s", ndmconn_get_err_msg (conn));
95         //ndmconn_destruct (conn);
96         *connp = conn;
97         return -1;
98
99 }
100
101 int
102 ndmca_connect_data_agent (struct ndm_session *sess)
103 {
104         int             rc;
105
106         rc = ndmca_connect_xxx_agent (sess,
107                                 &sess->plumb.data,
108                                 "#D",
109                                 &sess->control_acb.job.data_agent);
110         if (rc == 0) {
111                 if (sess->plumb.data->conn_type == NDMCONN_TYPE_RESIDENT) {
112                         sess->data_acb.protocol_version =
113                                         sess->plumb.data->protocol_version;
114                 }
115         }
116
117         return rc;
118 }
119
120 int
121 ndmca_connect_tape_agent (struct ndm_session *sess)
122 {
123         int             rc;
124
125         if (sess->control_acb.job.tape_agent.conn_type == NDMCONN_TYPE_NONE) {
126                 rc = ndmca_connect_data_agent (sess);
127                 if (rc) {
128                         ndmconn_destruct (sess->plumb.data);
129                         return rc;
130                 }
131                 sess->plumb.tape = sess->plumb.data;
132                 rc = 0;
133         } else {
134                 rc = ndmca_connect_xxx_agent (sess,
135                                 &sess->plumb.tape,
136                                 "#T",
137                                 &sess->control_acb.job.tape_agent);
138                 ndmalogf (sess, 0, 7, "ndmca_connect_tape_agent: %d %p", rc, sess->plumb.tape);
139         }
140
141         if (rc == 0) {
142                 if (sess->plumb.tape->conn_type == NDMCONN_TYPE_RESIDENT) {
143                         sess->tape_acb.protocol_version =
144                                         sess->plumb.tape->protocol_version;
145                 }
146         }
147
148         return rc;
149 }
150
151 int
152 ndmca_connect_robot_agent (struct ndm_session *sess)
153 {
154         int             rc;
155
156         if (sess->control_acb.job.robot_agent.conn_type == NDMCONN_TYPE_NONE) {
157                 rc = ndmca_connect_tape_agent (sess);
158                 if (rc) return rc;
159                 sess->plumb.robot = sess->plumb.tape;
160                 rc = 0;
161         } else {
162                 rc = ndmca_connect_xxx_agent (sess,
163                                 &sess->plumb.robot,
164                                 "#R",
165                                 &sess->control_acb.job.robot_agent);
166         }
167
168         if (rc == 0) {
169                 if (sess->plumb.robot->conn_type == NDMCONN_TYPE_RESIDENT) {
170                         sess->robot_acb.protocol_version =
171                                         sess->plumb.robot->protocol_version;
172                 }
173         }
174
175         return rc;
176 }
177
178 int
179 ndmca_connect_control_agent (struct ndm_session *sess)
180 {
181         struct ndmagent control_agent;
182         int             rc;
183
184         ndmagent_from_str (&control_agent, ".");        /* resident */
185
186         rc = ndmca_connect_xxx_agent (sess,
187                                 &sess->plumb.control,
188                                 "#C.",
189                                 &control_agent);
190
191         return rc;
192 }
193
194 #endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */