EchoTCP* echotcp_new(ShadowlibLogFunc log, int argc, char* argv[]) { g_assert(log); if(argc < 1) { return NULL; } EchoTCP* etcp = g_new0(EchoTCP, 1); etcp->log = log; gchar* mode = argv[0]; gboolean isError = FALSE; if(g_ascii_strncasecmp(mode, "client", 6) == 0) { if(argc < 2) { isError = TRUE; } else { gchar* serverHostName = argv[1]; struct addrinfo* serverInfo; if(getaddrinfo(serverHostName, NULL, NULL, &serverInfo) == -1) { log(G_LOG_LEVEL_WARNING, __FUNCTION__, "unable to create client: error in getaddrinfo"); isError = TRUE; } else { in_addr_t serverIP = ((struct sockaddr_in*)(serverInfo->ai_addr))->sin_addr.s_addr; etcp->client = _echotcp_newClient(log, serverIP); } freeaddrinfo(serverInfo); } } else if (g_ascii_strncasecmp(mode, "server", 6) == 0) { char myHostName[128]; gint result = gethostname(myHostName, 128); if(result == 0) { struct addrinfo* myInfo; if(getaddrinfo(myHostName, NULL, NULL, &myInfo) == -1) { log(G_LOG_LEVEL_WARNING, __FUNCTION__, "unable to create server: error in getaddrinfo"); isError = TRUE; } else { in_addr_t myIP = ((struct sockaddr_in*)(myInfo->ai_addr))->sin_addr.s_addr; log(G_LOG_LEVEL_INFO, __FUNCTION__, "binding to %s", inet_ntoa((struct in_addr){myIP})); etcp->server = _echotcp_newServer(log, myIP); } freeaddrinfo(myInfo); } else {
EchoTCP* echotcp_new(EchoLogFunc log, int argc, char* argv[]) { g_assert(log); if(argc < 1) { return NULL; } EchoTCP* etcp = g_new0(EchoTCP, 1); etcp->log = log; gchar* mode = argv[0]; gboolean isError = FALSE; if(g_ascii_strncasecmp(mode, "client", 6) == 0) { if(argc < 2) { isError = TRUE; } else { gchar* serverHostName = argv[1]; struct addrinfo* serverInfo; if(getaddrinfo(serverHostName, NULL, NULL, &serverInfo) == -1) { log(G_LOG_LEVEL_WARNING, __FUNCTION__, "unable to create client: error in getaddrinfo"); isError = TRUE; } else { in_addr_t serverIP = ((struct sockaddr_in*)(serverInfo->ai_addr))->sin_addr.s_addr; etcp->client = _echotcp_newClient(log, serverIP); } freeaddrinfo(serverInfo); } } else if (g_ascii_strncasecmp(mode, "server", 6) == 0) { char myHostName[128]; gint result = gethostname(myHostName, 128); if(result == 0) { struct addrinfo* myInfo; if(getaddrinfo(myHostName, NULL, NULL, &myInfo) == -1) { log(G_LOG_LEVEL_WARNING, __FUNCTION__, "unable to create server: error in getaddrinfo"); isError = TRUE; } else { in_addr_t myIP = ((struct sockaddr_in*)(myInfo->ai_addr))->sin_addr.s_addr; gchar ipStringBuffer[INET_ADDRSTRLEN+1]; memset(ipStringBuffer, 0, INET_ADDRSTRLEN+1); inet_ntop(AF_INET, &myIP, ipStringBuffer, INET_ADDRSTRLEN); log(G_LOG_LEVEL_INFO, __FUNCTION__, "binding to %s", ipStringBuffer); etcp->server = _echotcp_newServer(log, myIP); } freeaddrinfo(myInfo); } else { log(G_LOG_LEVEL_WARNING, __FUNCTION__, "unable to create server: error in gethostname"); isError = TRUE; } } else if (g_ascii_strncasecmp(mode, "loopback", 8) == 0) { in_addr_t serverIP = htonl(INADDR_LOOPBACK); etcp->server = _echotcp_newServer(log, serverIP); etcp->client = _echotcp_newClient(log, serverIP); } else if (g_ascii_strncasecmp(mode, "socketpair", 10) == 0) { _echotcp_newPair(log, &(etcp->client), &(etcp->server)); } else { isError = TRUE; } if(isError) { g_free(etcp); return NULL; } return etcp; }