]> andersk Git - moira.git/blame - lib/mr_connect.c
return MR_ARG_TOO_LONG rather than silently truncating long inputs
[moira.git] / lib / mr_connect.c
CommitLineData
7ac48069 1/* $Id $
e2a67c78 2 *
7ac48069 3 * This routine is part of the client library. It handles
4 * creating a connection to the moira server.
5eaef520 5 *
7ac48069 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>.
e2a67c78 9 */
10
babbc197 11#include <mit-copyright.h>
7ac48069 12#include <moira.h>
8defc06b 13#include <moira_site.h>
7ac48069 14#include "mr_private.h"
15
16#include <errno.h>
a43ce477 17#include <stdlib.h>
7ac48069 18#include <string.h>
19
a43ce477 20#include <hesiod.h>
05fb8281 21
7ac48069 22RCSID("$Header$");
23
8defc06b 24static char *mr_server_host = 0;
e2a67c78 25
26/*
8defc06b 27 * Open a connection to the mr server. Looks for the server name
5450e3a1 28 * 1) passed as an argument, 2) in environment variable, 3) by hesiod
59ec8dae 29 * 4) compiled in default (from moira_site.h).
e2a67c78 30 */
31
5eaef520 32int mr_connect(char *server)
e2a67c78 33{
5eaef520 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");
5450e3a1 43
44#ifdef HESIOD
5eaef520 45 if (!server || (strlen(server) == 0))
46 {
47 pp = hes_resolve("moira", "sloc");
48 if (pp)
49 server = *pp;
5450e3a1 50 }
a43ce477 51#endif
5450e3a1 52
5eaef520 53 if (!server || (strlen(server) == 0))
54 server = MOIRA_SERVER;
5450e3a1 55
5eaef520 56 if (!strchr(server, ':'))
57 {
58 p = strchr(MOIRA_SERVER, ':');
59 p++;
60 sprintf(sbuf, "%s:%s", server, p);
61 server = sbuf;
5450e3a1 62 }
63
5eaef520 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 {
44d12d58 70 int status = connection_errno(_mr_conn);
5eaef520 71 if (!status)
72 status = MR_CANT_CONNECT;
73 mr_disconnect();
74 return status;
83e80378 75 }
05fb8281 76
5eaef520 77 /*
78 * stash hostname for later use
79 */
05fb8281 80
7ac48069 81 mr_server_host = strdup(server);
5eaef520 82 if ((p = strchr(mr_server_host, ':')))
83 *p = '\0';
84 mr_server_host = canonicalize_hostname(mr_server_host);
85 return 0;
e2a67c78 86}
5eaef520 87
88int mr_disconnect(void)
e2a67c78 89{
5eaef520 90 CHECK_CONNECTED;
91 _mr_conn = sever_connection(_mr_conn);
92 free(mr_server_host);
93 mr_server_host = 0;
94 return 0;
e2a67c78 95}
96
5eaef520 97int mr_host(char *host, int size)
05fb8281 98{
5eaef520 99 CHECK_CONNECTED;
05fb8281 100
5eaef520 101 /* If we are connected, mr_server_host points to a valid string. */
102 strncpy(host, mr_server_host, size);
103 return 0;
05fb8281 104}
105
5eaef520 106int mr_noop(void)
e2a67c78 107{
5eaef520 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;
6c00b7c4 127}
This page took 0.102677 seconds and 5 git commands to generate.