static void testSocketClient(void *opaque) { struct testClientData *data = opaque; char c; virNetSocketPtr csock = NULL; if (data->path) { if (virNetSocketNewConnectUNIX(data->path, false, NULL, &csock) < 0) return; } else { if (virNetSocketNewConnectTCP(data->cnode, data->portstr, AF_UNSPEC, &csock) < 0) return; } virNetSocketSetBlocking(csock, true); if (virNetSocketRead(csock, &c, 1) != 1) { VIR_DEBUG("Cannot read from server"); goto done; } if (virNetSocketWrite(csock, &c, 1) != 1) { VIR_DEBUG("Cannot write to server"); goto done; } done: virObjectUnref(csock); }
virNetClientPtr virNetClientNewTCP(const char *nodename, const char *service) { virNetSocketPtr sock; if (virNetSocketNewConnectTCP(nodename, service, &sock) < 0) return NULL; return virNetClientNew(sock, nodename); }
static int testSocketTCPAccept(const void *opaque) { virNetSocketPtr *lsock = NULL; /* Listen socket */ size_t nlsock = 0, i; virNetSocketPtr ssock = NULL; /* Server socket */ virNetSocketPtr csock = NULL; /* Client socket */ const struct testTCPData *data = opaque; int ret = -1; char portstr[100]; snprintf(portstr, sizeof(portstr), "%d", data->port); if (virNetSocketNewListenTCP(data->lnode, portstr, AF_UNSPEC, &lsock, &nlsock) < 0) goto cleanup; for (i = 0; i < nlsock; i++) { if (virNetSocketListen(lsock[i], 0) < 0) goto cleanup; } if (virNetSocketNewConnectTCP(data->cnode, portstr, AF_UNSPEC, &csock) < 0) goto cleanup; virObjectUnref(csock); for (i = 0; i < nlsock; i++) { if (virNetSocketAccept(lsock[i], &ssock) != -1 && ssock) { char c = 'a'; if (virNetSocketWrite(ssock, &c, 1) != -1 && virNetSocketRead(ssock, &c, 1) != -1) { VIR_DEBUG("Unexpected client socket present"); goto cleanup; } } virObjectUnref(ssock); ssock = NULL; } ret = 0; cleanup: virObjectUnref(ssock); for (i = 0; i < nlsock; i++) virObjectUnref(lsock[i]); VIR_FREE(lsock); return ret; }