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