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