]>
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>. | |
5eaef520 | 9 | * |
e2a67c78 | 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 | ||
5eaef520 | 33 | int mr_connect(char *server) |
e2a67c78 | 34 | { |
5eaef520 | 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"); | |
5450e3a1 | 45 | |
46 | #ifdef HESIOD | |
5eaef520 | 47 | if (!server || (strlen(server) == 0)) |
48 | { | |
49 | pp = hes_resolve("moira", "sloc"); | |
50 | if (pp) | |
51 | server = *pp; | |
5450e3a1 | 52 | } |
a43ce477 | 53 | #endif |
5450e3a1 | 54 | |
5eaef520 | 55 | if (!server || (strlen(server) == 0)) |
56 | server = MOIRA_SERVER; | |
5450e3a1 | 57 | |
5eaef520 | 58 | if (!strchr(server, ':')) |
59 | { | |
60 | p = strchr(MOIRA_SERVER, ':'); | |
61 | p++; | |
62 | sprintf(sbuf, "%s:%s", server, p); | |
63 | server = sbuf; | |
5450e3a1 | 64 | } |
65 | ||
5eaef520 | 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 | { | |
44d12d58 | 72 | int status = connection_errno(_mr_conn); |
5eaef520 | 73 | if (!status) |
74 | status = MR_CANT_CONNECT; | |
75 | mr_disconnect(); | |
76 | return status; | |
83e80378 | 77 | } |
05fb8281 | 78 | |
5eaef520 | 79 | /* |
80 | * stash hostname for later use | |
81 | */ | |
05fb8281 | 82 | |
5eaef520 | 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; | |
e2a67c78 | 88 | } |
5eaef520 | 89 | |
90 | int mr_disconnect(void) | |
e2a67c78 | 91 | { |
5eaef520 | 92 | CHECK_CONNECTED; |
93 | _mr_conn = sever_connection(_mr_conn); | |
94 | free(mr_server_host); | |
95 | mr_server_host = 0; | |
96 | return 0; | |
e2a67c78 | 97 | } |
98 | ||
5eaef520 | 99 | int mr_host(char *host, int size) |
05fb8281 | 100 | { |
5eaef520 | 101 | CHECK_CONNECTED; |
05fb8281 | 102 | |
5eaef520 | 103 | /* If we are connected, mr_server_host points to a valid string. */ |
104 | strncpy(host, mr_server_host, size); | |
105 | return 0; | |
05fb8281 | 106 | } |
107 | ||
5eaef520 | 108 | int mr_noop(void) |
e2a67c78 | 109 | { |
5eaef520 | 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 = ¶m_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; | |
6c00b7c4 | 129 | } |