]> andersk Git - moira.git/blob - lib/mr_connect.c
fix prototyping problems
[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[256];
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       sprintf(sbuf, "%s:%s", server, p);
61       server = sbuf;
62     }
63
64   errno = 0;
65   _mr_conn = start_server_connection(server, "");
66   if (_mr_conn == NULL)
67     return errno;
68   if (connection_status(_mr_conn) == CON_STOPPED)
69     {
70       int status = connection_errno(_mr_conn);
71       if (!status)
72         status = MR_CANT_CONNECT;
73       mr_disconnect();
74       return status;
75     }
76
77   /*
78    * stash hostname for later use
79    */
80
81   mr_server_host = strdup(server);
82   if ((p = strchr(mr_server_host, ':')))
83     *p = '\0';
84   mr_server_host = canonicalize_hostname(mr_server_host);
85   return 0;
86 }
87
88 int mr_disconnect(void)
89 {
90   CHECK_CONNECTED;
91   _mr_conn = sever_connection(_mr_conn);
92   free(mr_server_host);
93   mr_server_host = 0;
94   return 0;
95 }
96
97 int mr_host(char *host, int size)
98 {
99   CHECK_CONNECTED;
100
101   /* If we are connected, mr_server_host points to a valid string. */
102   strncpy(host, mr_server_host, size);
103   return 0;
104 }
105
106 int mr_noop(void)
107 {
108   int status;
109   mr_params param_st;
110   struct mr_params *params = NULL;
111   struct mr_params *reply = NULL;
112
113   CHECK_CONNECTED;
114   params = &param_st;
115   params->mr_version_no = sending_version_no;
116   params->mr_procno = MR_NOOP;
117   params->mr_argc = 0;
118   params->mr_argl = NULL;
119   params->mr_argv = NULL;
120
121   if ((status = mr_do_call(params, &reply)) == 0)
122     status = reply->mr_status;
123
124   mr_destroy_reply(reply);
125
126   return status;
127 }
This page took 0.044567 seconds and 5 git commands to generate.