62
61
static unsigned int remote_mode = 0;
62
static char* socket_basename;
64
64
static int sim_ctl(struct sim_client *sc, int type, void *data, int len)
144
144
static char *get_name(union name_t *name)
147
147
return inet_ntoa(name->name_i.sin_addr);
149
149
return name->name_u.sun_path + 1;
153
152
static int sim_attach(int fd, union name_t *name, size_t size)
159
158
DEBUG("attempt to connect to %s (attempt %d)",
160
159
get_name(name), retries);
163
connect(fd, (struct sockaddr *)name, size)) >= 0)
161
if ((r = connect(fd, (struct sockaddr *)name, size)) >= 0)
166
164
if (r < 0 && errno == ECONNREFUSED) {
178
static int sim_connect(struct sim_client *sc, int id, int qp, char *nodeid)
176
static int sim_connect(struct sim_client *sc, int id, int qp, char *nodeid,
180
179
struct sim_client_info info = { 0 };
202
204
return sim_ctl(sc, SIM_CTL_DISCONNECT, 0, 0);
205
static int sim_init(struct sim_client *sc, int qp, char *nodeid)
207
static int sim_init(struct sim_client *sc, char *nodeid, char *issm)
207
209
union name_t name;
215
217
connect_port = getenv("IBSIM_SERVER_PORT");
216
218
connect_host = getenv("IBSIM_SERVER_NAME");
219
socket_basename = getenv("IBSIM_SOCKNAME");
222
socket_basename = SIM_BASENAME;
218
224
if (connect_host && *connect_host)
221
DEBUG("init client pid=%d, qp=%d nodeid=%s",
222
pid, qp, nodeid ? nodeid : "none");
227
DEBUG("init client pid=%d, nodeid=%s", pid, nodeid ? nodeid : "none");
224
229
if ((fd = socket(remote_mode ? PF_INET : PF_LOCAL, SOCK_DGRAM, 0)) < 0)
225
230
IBPANIC("can't get socket (fd)");
227
232
if ((ctlfd = socket(remote_mode ? PF_INET : PF_LOCAL, SOCK_DGRAM, 0)) < 0)
228
233
IBPANIC("can't get socket (ctlfd)");
230
size = make_name(&name, NULL, 0, "%s:ctl%d", SIM_BASENAME, pid);
235
size = make_name(&name, NULL, 0, "%s:ctl%d", socket_basename, pid);
232
237
if (bind(ctlfd, (struct sockaddr *)&name, size) < 0)
233
238
IBPANIC("can't bind ctl socket");
236
241
pid, ctlfd, get_name(&name));
238
243
port = connect_port ? atoi(connect_port) : IBSIM_DEFAULT_SERVER_PORT;
239
size = make_name(&name, connect_host, port, "%s:ctl", SIM_BASENAME);
244
size = make_name(&name, connect_host, port, "%s:ctl", socket_basename);
241
246
sim_attach(ctlfd, &name, size);
243
248
sc->fd_ctl = ctlfd;
245
size = make_name(&name, NULL, 0, "%s:in%d", SIM_BASENAME, pid);
250
size = make_name(&name, NULL, 0, "%s:in%d", socket_basename, pid);
247
252
if (bind(fd, (struct sockaddr *)&name, size) < 0)
248
253
IBPANIC("can't bind input socket");
253
258
IBPANIC("can't read data from bound socket");
254
259
port = ntohs(name.name_i.sin_port);
256
sc->clientid = sim_connect(sc, remote_mode ? port : pid, qp, nodeid);
261
sc->clientid = sim_connect(sc, remote_mode ? port : pid, 0, nodeid, issm);
257
262
if (sc->clientid < 0)
258
263
IBPANIC("connect failed");
260
265
port = connect_port ? atoi(connect_port) : IBSIM_DEFAULT_SERVER_PORT;
261
266
size = make_name(&name, connect_host, port + sc->clientid + 1,
262
"%s:out%d", SIM_BASENAME, sc->clientid);
267
"%s:out%d", socket_basename, sc->clientid);
264
269
sim_attach(fd, &name, size);
280
285
return sim_ctl(sc, SIM_CTL_SET_ISSM, &issm, sizeof(int));
283
int sim_client_init(struct sim_client *sc, char *nodeid)
288
int sim_client_init(struct sim_client *sc)
286
nodeid = getenv("SIM_HOST");
287
if (sim_init(sc, 0, nodeid) < 0)
293
nodeid = getenv("SIM_HOST");
294
issm = getenv("SIM_SET_ISSM");
295
if (sim_init(sc, nodeid, issm) < 0)
289
if (sim_ctl(sc, SIM_CTL_GET_VENDOR, &sc->vendor, sizeof(sc->vendor)) <
297
if (sim_ctl(sc, SIM_CTL_GET_VENDOR, &sc->vendor,
298
sizeof(sc->vendor)) < 0)
292
300
if (sim_ctl(sc, SIM_CTL_GET_NODEINFO, sc->nodeinfo,
293
301
sizeof(sc->nodeinfo)) < 0)
304
sc->portinfo[0] = 0; // portno requested
296
305
if (sim_ctl(sc, SIM_CTL_GET_PORTINFO, sc->portinfo,
297
306
sizeof(sc->portinfo)) < 0)
299
308
if (sim_ctl(sc, SIM_CTL_GET_PKEYS, sc->pkeys, sizeof(sc->pkeys)) < 0)
301
if (getenv("SIM_SET_ISSM"))
302
311
sim_client_set_sm(sc, 1);
305
314
sim_disconnect(sc);
306
315
sc->fd_ctl = sc->fd_pktin = sc->fd_pktout = -1;
310
319
void sim_client_exit(struct sim_client *sc)