1 /*****************************************************************************\
2  **  nameserv.c - name publish/unpublish/lookup functions
3  *****************************************************************************
4  *  Copyright (C) 2013 National University of Defense Technology.
5  *  Written by Hongjia Cao <hjcao@nudt.edu.cn>.
6  *  All rights reserved.
7  *
8  *  This file is part of Slurm, a resource management program.
9  *  For details, see <https://slurm.schedmd.com/>.
10  *  Please also read the included file: DISCLAIMER.
11  *
12  *  Slurm is free software; you can redistribute it and/or modify it under
13  *  the terms of the GNU General Public License as published by the Free
14  *  Software Foundation; either version 2 of the License, or (at your option)
15  *  any later version.
16  *
17  *  In addition, as a special exception, the copyright holders give permission
18  *  to link the code of portions of this program with the OpenSSL library under
19  *  certain conditions as described in each individual source file, and
20  *  distribute linked combinations including the two. You must obey the GNU
21  *  General Public License in all respects for all of the code used other than
22  *  OpenSSL. If you modify file(s) with this exception, you may extend this
23  *  exception to your version of the file(s), but you are not obligated to do
24  *  so. If you do not wish to do so, delete this exception statement from your
25  *  version.  If you delete this exception statement from all source files in
26  *  the program, then also delete it here.
27  *
28  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
29  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
30  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
31  *  details.
32  *
33  *  You should have received a copy of the GNU General Public License along
34  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
35  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
36 \*****************************************************************************/
37 
38 
39 #include "pmi.h"
40 #include "setup.h"
41 #include "client.h"
42 
43 typedef struct name_port {
44 	char *name;
45 	char *port;
46 	struct name_port *next;
47 } name_port_t;
48 
49 /*
50  * just a list for now.
51  * a db or directory is more useful.
52  * or execute a script to pub/unpub/lookup.
53  */
54 static name_port_t *local_name_list = NULL;
55 
56 extern char *
name_lookup_local(char * name)57 name_lookup_local (char *name)
58 {
59 	name_port_t *np;
60 
61 	np = local_name_list;
62 	while (np && xstrcmp(np->name, name))
63 		np = np->next;
64 
65 	return np ? xstrdup(np->port) : NULL;
66 }
67 
68 extern int
name_publish_local(char * name,char * port)69 name_publish_local (char *name, char *port)
70 {
71 	name_port_t *np;
72 
73 	np = local_name_list;
74 	while (np && xstrcmp(np->name, name))
75 		np = np->next;
76 	if (np) {
77 		xfree(np->port);
78 		np->port = xstrdup(port);
79 	} else {
80 		np = xmalloc(sizeof(name_port_t));
81 		np->name = xstrdup(name);
82 		np->port = xstrdup(port);
83 		np->next = local_name_list;
84 		local_name_list = np;
85 	}
86 	return SLURM_SUCCESS;
87 }
88 
89 extern int
name_unpublish_local(char * name)90 name_unpublish_local (char *name)
91 {
92 	name_port_t *np, **pprev;
93 
94 	pprev = &local_name_list;
95 	np = *pprev;
96 	while (np) {
97 		if (xstrcmp(np->name, name)) {
98 			pprev = &np->next;
99 			np = np->next;
100 		} else {
101 			*pprev = np->next;
102 			xfree(np->name);
103 			xfree(np->port);
104 			xfree(np);
105 			np = *pprev;
106 			break;
107 		}
108 	}
109 	return SLURM_SUCCESS;
110 }
111 
112 extern int
name_publish_up(char * name,char * port)113 name_publish_up(char *name, char *port)
114 {
115 	Buf buf = NULL, resp_buf = NULL;
116 	uint32_t size, tmp_32;
117 	int rc;
118 
119 	buf = init_buf(1024);
120 	pack16((uint16_t)TREE_CMD_NAME_PUBLISH, buf);
121 	packstr(name, buf);
122 	packstr(port, buf);
123 	size = get_buf_offset(buf);
124 
125 	rc = tree_msg_to_srun_with_resp(size, get_buf_data(buf), &resp_buf);
126 	free_buf(buf);
127 
128 	if (rc == SLURM_SUCCESS) {
129 		safe_unpack32(&tmp_32, resp_buf);
130 		rc = (int) tmp_32;
131 	}
132 
133 unpack_error:
134 	if (resp_buf)
135 		free_buf(resp_buf);
136 
137 	return rc;
138 }
139 
140 extern int
name_unpublish_up(char * name)141 name_unpublish_up(char *name)
142 {
143 	Buf buf = NULL, resp_buf = NULL;
144 	uint32_t size, tmp_32;
145 	int rc;
146 
147 	buf = init_buf(1024);
148 	pack16((uint16_t)TREE_CMD_NAME_UNPUBLISH, buf);
149 	packstr(name, buf);
150 	size = get_buf_offset(buf);
151 
152 	rc = tree_msg_to_srun_with_resp(size, get_buf_data(buf), &resp_buf);
153 	free_buf(buf);
154 
155 	if (rc == SLURM_SUCCESS) {
156 		safe_unpack32(&tmp_32, resp_buf);
157 		rc = (int) tmp_32;
158 	}
159 
160 unpack_error:
161 	if (resp_buf)
162 		free_buf(resp_buf);
163 
164 	return rc;
165 }
166 
167 
168 extern char *
name_lookup_up(char * name)169 name_lookup_up(char *name)
170 {
171 	Buf buf = NULL, resp_buf = NULL;
172 	uint32_t size;
173 	char * port = NULL;
174 	int rc;
175 
176 	buf = init_buf(1024);
177 	pack16((uint16_t)TREE_CMD_NAME_LOOKUP, buf);
178 	packstr(name, buf);
179 	size = get_buf_offset(buf);
180 
181 	rc = tree_msg_to_srun_with_resp(size, get_buf_data(buf), &resp_buf);
182 	free_buf(buf);
183 
184 	if (rc == SLURM_SUCCESS)
185 		safe_unpackstr_xmalloc(&port, (uint32_t *)&size, resp_buf);
186 unpack_error:
187 	if (resp_buf)
188 		free_buf(resp_buf);
189 
190 	return port;
191 }
192