]> andersk Git - moira.git/blob - lib/mr_connect.c
366166ad9c06ae31c0cad5f52da0b1b7125226ae
[moira.git] / lib / mr_connect.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 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 sms server.
12  */
13
14 #ifndef lint
15 static char *rcsid_sms_connect_c = "$Header$";
16 #endif lint
17
18 #include <mit-copyright.h>
19 #include "sms_private.h"
20 #include <sms_app.h>
21 #include <strings.h>
22
23 static char *sms_server_host = 0;
24
25 /*
26  * Open a connection to the sms server.  Looks for the server name
27  * 1) passed as an argument, 2) in environment variable, 3) by hesiod
28  * 4) compiled in default (from sms_app.h).
29  */
30
31 int sms_connect(server)
32 char *server;
33 {
34     extern int errno;
35     char *p, **pp, sbuf[256];
36     extern char *getenv(), **hes_resolve();
37         
38     if (!sms_inited) sms_init();
39     if (_sms_conn) return SMS_ALREADY_CONNECTED;
40                 
41     if (!server || (strlen(server) == 0)) {
42         server = getenv("MOIRASERVER");
43     }
44
45 #ifdef HESIOD
46     if (!server || (strlen(server) == 0)) {
47         pp = hes_resolve("moira", "sloc");
48         if (pp) server = *pp;
49     }
50 #endif HESIOD
51
52     if (!server || (strlen(server) == 0)) {
53         server = SMS_SERVER;
54     }
55
56     if (!index(server, ':')) {
57         p = index(SMS_SERVER, ':');
58         p++;
59         sprintf(sbuf, "%s:%s", server, p);
60         server = sbuf;
61     }
62
63     errno = 0;
64     _sms_conn = start_server_connection(server, ""); 
65     if (_sms_conn == NULL)
66         return errno;
67     if (connection_status(_sms_conn) == CON_STOPPED) {
68         register status = connection_errno(_sms_conn);
69         if (!status) status = SMS_CANT_CONNECT;
70         sms_disconnect();
71         return status;
72     }
73
74     /*
75      * stash hostname for later use
76      */
77
78     sms_server_host = strsave(server);
79     if (p = index(sms_server_host, ':'))
80         *p = 0;
81     sms_server_host = canonicalize_hostname(sms_server_host);
82     return 0;
83 }
84         
85 int sms_disconnect()
86 {
87     CHECK_CONNECTED;
88     _sms_conn = sever_connection(_sms_conn);
89     free(sms_server_host);
90     sms_server_host = 0;
91     return 0;
92 }
93
94 int sms_host(host, size)
95   char *host;
96   int size;
97 {
98     CHECK_CONNECTED;
99
100     /* If we are connected, sms_server_host points to a valid string. */
101     strncpy(host, sms_server_host, size);
102     return(0);
103 }
104
105 int sms_noop()
106 {
107     int status;
108     sms_params param_st;
109     struct sms_params *params = NULL;
110     struct sms_params *reply = NULL;
111
112     CHECK_CONNECTED;
113     params = &param_st;
114     params->sms_version_no = sending_version_no;
115     params->sms_procno = SMS_NOOP;
116     params->sms_argc = 0;
117     params->sms_argl = NULL;
118     params->sms_argv = NULL;
119         
120     if ((status = sms_do_call(params, &reply)) == 0)
121         status = reply->sms_status;
122         
123     sms_destroy_reply(reply);
124
125     return status;
126 }
This page took 0.04172 seconds and 3 git commands to generate.