]> andersk Git - moira.git/blob - lib/mr_connect.c
6ff90b6a7c28b072a5ed1fa8f63fb2428c96bea8
[moira.git] / lib / mr_connect.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987, 1990 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  *      This routine is part of the client library.  It handles
11  *      creating a connection to the mr server.
12  */
13
14 #ifndef lint
15 static char *rcsid_sms_connect_c = "$Header$";
16 #endif
17
18 #include <mit-copyright.h>
19 #include "mr_private.h"
20 #include <moira_site.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <hesiod.h>
24
25 static char *mr_server_host = 0;
26
27 /*
28  * Open a connection to the mr server.  Looks for the server name
29  * 1) passed as an argument, 2) in environment variable, 3) by hesiod
30  * 4) compiled in default (from sms_app.h).
31  */
32
33 int mr_connect(char *server)
34 {
35   extern int errno;
36   char *p, **pp, sbuf[256];
37
38   if (!mr_inited)
39     mr_init();
40   if (_mr_conn)
41     return MR_ALREADY_CONNECTED;
42
43   if (!server || (strlen(server) == 0))
44     server = getenv("MOIRASERVER");
45
46 #ifdef HESIOD
47   if (!server || (strlen(server) == 0))
48     {
49       pp = hes_resolve("moira", "sloc");
50       if (pp)
51         server = *pp;
52     }
53 #endif
54
55   if (!server || (strlen(server) == 0))
56     server = MOIRA_SERVER;
57
58   if (!strchr(server, ':'))
59     {
60       p = strchr(MOIRA_SERVER, ':');
61       p++;
62       sprintf(sbuf, "%s:%s", server, p);
63       server = sbuf;
64     }
65
66   errno = 0;
67   _mr_conn = start_server_connection(server, "");
68   if (_mr_conn == NULL)
69     return errno;
70   if (connection_status(_mr_conn) == CON_STOPPED)
71     {
72       int status = connection_errno(_mr_conn);
73       if (!status)
74         status = MR_CANT_CONNECT;
75       mr_disconnect();
76       return status;
77     }
78
79   /*
80    * stash hostname for later use
81    */
82
83   mr_server_host = strsave(server);
84   if ((p = strchr(mr_server_host, ':')))
85     *p = '\0';
86   mr_server_host = canonicalize_hostname(mr_server_host);
87   return 0;
88 }
89
90 int mr_disconnect(void)
91 {
92   CHECK_CONNECTED;
93   _mr_conn = sever_connection(_mr_conn);
94   free(mr_server_host);
95   mr_server_host = 0;
96   return 0;
97 }
98
99 int mr_host(char *host, int size)
100 {
101   CHECK_CONNECTED;
102
103   /* If we are connected, mr_server_host points to a valid string. */
104   strncpy(host, mr_server_host, size);
105   return 0;
106 }
107
108 int mr_noop(void)
109 {
110   int status;
111   mr_params param_st;
112   struct mr_params *params = NULL;
113   struct mr_params *reply = NULL;
114
115   CHECK_CONNECTED;
116   params = &param_st;
117   params->mr_version_no = sending_version_no;
118   params->mr_procno = MR_NOOP;
119   params->mr_argc = 0;
120   params->mr_argl = NULL;
121   params->mr_argv = NULL;
122
123   if ((status = mr_do_call(params, &reply)) == 0)
124     status = reply->mr_status;
125
126   mr_destroy_reply(reply);
127
128   return status;
129 }
This page took 0.277818 seconds and 3 git commands to generate.