// ------------------------------------------------------------- // bindSocket() -- bind the socket to an address, and configure // the send and receive buffers. // ------------------------------------------------------------- bool TcpServerSingle::bindSocket() { // --- // Our base class will bind the socket // --- bool ok = BaseClass::bindSocket(); if (ok) { struct sockaddr_in addr; // Working address structure bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; if (getLocalAddr() != 0) addr.sin_addr.s_addr = getLocalAddr (); if (getLocalPort() != 0) addr.sin_port = htons (getLocalPort()); else addr.sin_port = htons(getPort()); // Only in server do we bind if (::bind(socketNum, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR ) { std::perror("TcpHandler::bindSocket(): bind error"); return false; } if (!setSendBuffSize()) return false; if (!setRecvBuffSize()) return false; } return ok; }
unsigned long MySocket::localHost() { unsigned long host = 0; unsigned short port = 0; getLocalAddr(host, port); return host; }
bool SocketUtil::isSelfConnect(ZL_SOCKET sockfd) { struct sockaddr_in localaddr = getLocalAddr(sockfd); struct sockaddr_in peeraddr = getPeerAddr(sockfd); return localaddr.sin_port == peeraddr.sin_port && localaddr.sin_addr.s_addr == peeraddr.sin_addr.s_addr; }
bool isSelfConnect(int sockfd) { struct sockaddr_in localaddr = getLocalAddr(sockfd); struct sockaddr_in peeraddr = getPeerAddr(sockfd); return localaddr.sin_port == peeraddr.sin_port && localaddr.sin_addr.s_addr == peeraddr.sin_addr.s_addr; }
IpAddr ip_utils::getInterfaceAddr(const std::string &interface, pj_uint16_t family) { if (interface == DEFAULT_INTERFACE) return getLocalAddr(family); const auto unix_family = family == pj_AF_INET() ? AF_INET : AF_INET6; IpAddr addr = {}; int fd = socket(unix_family, SOCK_DGRAM, 0); if (fd < 0) { ERROR("Could not open socket: %m"); return addr; } if (unix_family == AF_INET6) { int val = family != pj_AF_UNSPEC(); if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &val, sizeof(val)) < 0) { ERROR("Could not setsockopt: %m"); close(fd); return addr; } } ifreq ifr; strncpy(ifr.ifr_name, interface.c_str(), sizeof ifr.ifr_name); // guarantee that ifr_name is NULL-terminated ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); ifr.ifr_addr.sa_family = unix_family; ioctl(fd, SIOCGIFADDR, &ifr); close(fd); addr = ifr.ifr_addr; if (addr.isUnspecified()) return getLocalAddr(addr.getFamily()); ERROR("ip_utils::getInterfaceAddr %s %s", interface.c_str(), addr.toString().c_str()); return addr; }
static void defaultAfterAccept(struct BufferEvent* bevent, void* arg) { assert(bevent != NULL); int connfd = bevent->event->fd; char local[25]; char peer[25]; printf("%s -> %s.\n", getPeerAddr(connfd, peer, sizeof(peer)), getLocalAddr(connfd, local, sizeof(local))); }
void start(Server* server) { assert(server != NULL); server->sockfd = tcpListen(server->port); assert(server->sockfd >= 0); getLocalAddr(server->sockfd, server->hostport, sizeof(server->hostport)); #ifdef DEBUG printf("start server at: %s\n", server->hostport); #endif server->acceptor = newEvent(server->sockfd, EV_READ, onAccept, NULL, server, server->loop); assert(server->acceptor != NULL); eventLoopAdd(server->loop, server->acceptor); eventLoopDispatch(server->loop); }
MyServerSocket::MyServerSocket() { p = 0; fd = socket(PF_INET, SOCK_STREAM, 0); if (fd == -1) { log(L_WARN, "Can't create socket %s", strerror(errno)); return; } #ifndef WIN32 int fl = fcntl(fd, F_GETFL, 0); if (fl != -1) fcntl(fd, F_SETFL, fl | O_NONBLOCK); #endif struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); struct sockaddr_in addr; memset(&addr.sin_zero, 0, 8); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(fd, (struct sockaddr *)&addr, sizeof(sockaddr_in)) < 0){ log(L_WARN, "Can't bind %s", strerror(errno)); close(); return; } unsigned long host; getLocalAddr(host, p); if (::listen(fd, 256) == -1) { log(L_WARN, "Can't listen %s", strerror(errno)); close(); return; } return; }
IpAddr ip_utils::getInterfaceAddr(const std::string &interface, pj_uint16_t family) { if (interface == DEFAULT_INTERFACE) return getLocalAddr(family); const auto unix_family = family == pj_AF_INET() ? AF_INET : AF_INET6; IpAddr addr = {}; #ifndef _WIN32 int fd = socket(unix_family, SOCK_DGRAM, 0); if (fd < 0) { RING_ERR("Could not open socket: %m"); return addr; } if (unix_family == AF_INET6) { int val = family != pj_AF_UNSPEC(); if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &val, sizeof(val)) < 0) { RING_ERR("Could not setsockopt: %m"); close(fd); return addr; } } ifreq ifr; strncpy(ifr.ifr_name, interface.c_str(), sizeof ifr.ifr_name); // guarantee that ifr_name is NULL-terminated ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); ifr.ifr_addr.sa_family = unix_family; ioctl(fd, SIOCGIFADDR, &ifr); close(fd); addr = ifr.ifr_addr; if (addr.isUnspecified()) return getLocalAddr(addr.getFamily()); #else // _WIN32 struct addrinfo hints; struct addrinfo *result = NULL; struct sockaddr_in *sockaddr_ipv4; struct sockaddr_in6 *sockaddr_ipv6; ZeroMemory(&hints, sizeof(hints)); DWORD dwRetval = getaddrinfo(interface.c_str(), "0", &hints, &result); if (dwRetval != 0) { RING_ERR("getaddrinfo failed with error: %d", dwRetval); return addr; } switch (result->ai_family) { sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr; addr = sockaddr_ipv4->sin_addr; break; case AF_INET6: sockaddr_ipv6 = (struct sockaddr_in6 *) result->ai_addr; addr = sockaddr_ipv6->sin6_addr; break; default: break; } if (addr.isUnspecified()) return getLocalAddr(addr.getFamily()); #endif // !_WIN32 return addr; }