bool SocketAddress::setIntern(Exception& ex,const string& hostAndPort,bool resolveHost) { ASSERT_RETURN(!hostAndPort.empty(),false); string host, port; auto it = hostAndPort.begin(); auto& end = hostAndPort.end(); if (*it == '[') { ++it; while (it != end && *it != ']') host += *it++; if (it == end) { ex.set(Exception::NETADDRESS,"Malformed IPv6 address ", hostAndPort); return false; } ++it; } else { while (it != end && *it != ':') host += *it++; } if (it != end && *it == ':') { ++it; while (it != end) port += *it++; } else { ex.set(Exception::NETADDRESS, "Missing port number in ", hostAndPort); return false; } return setIntern(ex,host, resolveService(ex,port),resolveHost); }
SocketAddress::SocketAddress(const std::string& hostAndPort) { poco_assert (!hostAndPort.empty()); std::string host; std::string port; std::string::const_iterator it = hostAndPort.begin(); std::string::const_iterator end = hostAndPort.end(); if (*it == '[') { ++it; while (it != end && *it != ']') host += *it++; if (it == end) throw InvalidArgumentException("Malformed IPv6 address"); ++it; } else { while (it != end && *it != ':') host += *it++; } if (it != end && *it == ':') { ++it; while (it != end) port += *it++; } else throw InvalidArgumentException("Missing port number"); init(host, resolveService(port)); }
int lanrecv_init(void) { if (lan_active == 0) return (1); bzero(&lan_sin, sizeof(lan_sin)); lan_sin.sin_family = AF_INET; lan_sin.sin_addr.s_addr = htonl(INADDR_ANY); lan_sin.sin_port = htons(resolveService(default_lan_service)); lan_sin_len = sizeof(lan_sin); lan_socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0); if (lan_socket_descriptor == -1) { syslog(LOG_ERR, "%s\n", "LAN: socket"); return (-1); } lan_bind_rc = bind(lan_socket_descriptor, (struct sockaddr *) &lan_sin, sizeof(lan_sin)); if (lan_bind_rc == -1) { syslog(LOG_ERR, "%s\n", "LAN: bind"); return (-2); } lan_save_file_flags = fcntl(lan_socket_descriptor, F_GETFL); lan_save_file_flags |= O_NONBLOCK; if (fcntl(lan_socket_descriptor, F_SETFL, lan_save_file_flags) == -1) { syslog(LOG_ERR, "%s\n", "trying non-blocking"); return (-3); } return (0); }
void SocketAddress::init(const std::string& hostAndPort) { poco_assert (!hostAndPort.empty()); std::string socketHost; std::string socketPort; std::string::const_iterator it = hostAndPort.begin(); std::string::const_iterator end = hostAndPort.end(); #if defined(POCO_OS_FAMILY_UNIX) if (*it == '/') { newLocal(hostAndPort); return; } #endif if (*it == '[') { ++it; while (it != end && *it != ']') socketHost += *it++; if (it == end) throw InvalidArgumentException("Malformed IPv6 address"); ++it; } else { while (it != end && *it != ':') socketHost += *it++; } if (it != end && *it == ':') { ++it; while (it != end) socketPort += *it++; } else throw InvalidArgumentException("Missing port number"); init(socketHost, resolveService(socketPort)); }
int lan_send_init(void) { if (lan_active == 0) return (1); for (node = 0; node < nodes; node++) { bc_hostbyname[node] = gethostbyname(bc_hostaddress[node]); if (bc_hostbyname[node] == NULL) { syslog(LOG_ERR, "%s\n", "LAN: gethostbyname failed"); return (-1); } bzero(&bc_address[node], sizeof(bc_address[node])); /* empty data structure */ bc_address[node].sin_family = AF_INET; memcpy(&bc_address[node].sin_addr.s_addr, bc_hostbyname[node]->h_addr, sizeof(bc_address[node].sin_addr.s_addr)); bc_address[node].sin_port = htons(resolveService(bc_hostservice[node])); syslog(LOG_INFO, "open socket: to %d.%d.%d.%d:%d\n", (ntohl(bc_address[node].sin_addr.s_addr) & 0xff000000) >> 24, (ntohl(bc_address[node].sin_addr.s_addr) & 0x00ff0000) >> 16, (ntohl(bc_address[node].sin_addr.s_addr) & 0x0000ff00) >> 8, (ntohl(bc_address[node].sin_addr.s_addr) & 0x000000ff) >> 0, ntohs(bc_address[node].sin_port)); bc_socket_descriptor[node] = socket(AF_INET, SOCK_DGRAM, 0); if (bc_socket_descriptor[node] == -1) { syslog(LOG_ERR, "%s\n", "LAN: socket call failed"); return (-1); } } return (0); }
Address::Address(const std::string& hostAndPort) { assert(!hostAndPort.empty()); std::string host; std::string port; std::string::const_iterator it = hostAndPort.begin(); std::string::const_iterator end = hostAndPort.end(); if (*it == '[') { ++it; while (it != end && *it != ']') host += *it++; if (it == end) throw std::runtime_error("Invalid address: Malformed IPv6 address"); ++it; } else { while (it != end && *it != ':') host += *it++; } if (it != end && *it == ':') { ++it; while (it != end) port += *it++; } else throw std::runtime_error("Invalid address: Missing port number"); init(host, resolveService(port)); }
SocketAddress::SocketAddress(const std::string& addr, const std::string& port) { init(addr, resolveService(port)); }
SocketAddress::SocketAddress(const std::string& hostAddress, const std::string& portNumber) { init(hostAddress, resolveService(portNumber)); }
int MDnsSdListener::Handler::runCommand(SocketClient *cli, int argc, char **argv) { if (argc < 2) { char* msg = NULL; asprintf( &msg, "Invalid number of arguments to mdnssd: %i", argc); ALOGW("%s", msg); cli->sendMsg(ResponseCode::CommandParameterError, msg, false); free(msg); return -1; } char* cmd = argv[1]; if (strcmp(cmd, "discover") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd discover", false); return 0; } int requestId = atoi(argv[2]); char *serviceType = argv[3]; discover(cli, NULL, serviceType, NULL, requestId, 0); } else if (strcmp(cmd, "stop-discover") == 0) { stop(cli, argc, argv, "discover"); } else if (strcmp(cmd, "register") == 0) { if (argc != 6) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd register", false); return 0; } int requestId = atoi(argv[2]); char *serviceName = argv[3]; char *serviceType = argv[4]; int port = atoi(argv[5]); char *interfaceName = NULL; // will use all char *domain = NULL; // will use default char *host = NULL; // will use default hostname int textLen = 0; void *textRecord = NULL; serviceRegister(cli, requestId, interfaceName, serviceName, serviceType, domain, host, port, textLen, textRecord); } else if (strcmp(cmd, "stop-register") == 0) { stop(cli, argc, argv, "register"); } else if (strcmp(cmd, "resolve") == 0) { if (argc != 6) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd resolve", false); return 0; } int requestId = atoi(argv[2]); char *interfaceName = NULL; // will use all char *serviceName = argv[3]; char *regType = argv[4]; char *domain = argv[5]; resolveService(cli, requestId, interfaceName, serviceName, regType, domain); } else if (strcmp(cmd, "stop-resolve") == 0) { stop(cli, argc, argv, "resolve"); } else if (strcmp(cmd, "start-service") == 0) { if (mMonitor->startService()) { cli->sendMsg(ResponseCode::CommandOkay, "Service Started", false); } else { cli->sendMsg(ResponseCode::ServiceStartFailed, "Service already running", false); } } else if (strcmp(cmd, "stop-service") == 0) { if (mMonitor->stopService()) { cli->sendMsg(ResponseCode::CommandOkay, "Service Stopped", false); } else { cli->sendMsg(ResponseCode::ServiceStopFailed, "Service still in use", false); } } else if (strcmp(cmd, "sethostname") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd sethostname", false); return 0; } int requestId = atoi(argv[2]); char *hostname = argv[3]; setHostname(cli, requestId, hostname); } else if (strcmp(cmd, "stop-sethostname") == 0) { stop(cli, argc, argv, "sethostname"); } else if (strcmp(cmd, "getaddrinfo") == 0) { if (argc != 4) { cli->sendMsg(ResponseCode::CommandParameterError, "Invalid number of arguments to mdnssd getaddrinfo", false); return 0; } int requestId = atoi(argv[2]); char *hostname = argv[3]; char *interfaceName = NULL; // default int protocol = 0; // intelligient heuristic (both v4 + v6) getAddrInfo(cli, requestId, interfaceName, protocol, hostname); } else if (strcmp(cmd, "stop-getaddrinfo") == 0) { stop(cli, argc, argv, "getaddrinfo"); } else { if (VDBG) ALOGE("Unknown cmd %s", cmd); cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown mdnssd cmd", false); return 0; } return 0; }