bool bot_set_port(struct bot *bot, char *serv_port) { DEBUG(LOG_VERBOSE, "bot set port\n"); return socket_set_port(bot->socket, serv_port); }
net_socket_t udp_open(uint16_t port) { net_socket_t sockid = socket_alloc(SOCKET_TYPE_UDP); if (sockid == SOCKET_INVALID) return sockid; socket_set_port(sockid, port); socket_cmd(sockid, SOCKET_CMD_OPEN); return sockid; }
static int HandleOP(SetLoggingSocket) { int32_t portnum; krb5_socket_t sock; int ret; ret32(c, portnum); logmessage(c, __FILE__, __LINE__, 0, "logging port on peer is: %d", (int)portnum); socket_set_port((struct sockaddr *)(&c->sa), htons(portnum)); sock = socket(((struct sockaddr *)&c->sa)->sa_family, SOCK_STREAM, 0); if (sock == rk_INVALID_SOCKET) return 0; ret = connect(sock, (struct sockaddr *)&c->sa, c->salen); if (ret < 0) { logmessage(c, __FILE__, __LINE__, 0, "failed connect to log port: %s", strerror(errno)); rk_closesocket(sock); return 0; } if (c->logging) krb5_storage_free(c->logging); c->logging = krb5_storage_from_socket(sock); rk_closesocket(sock); krb5_store_int32(c->logging, eLogSetMoniker); store_string(c->logging, c->moniker); logmessage(c, __FILE__, __LINE__, 0, "logging turned on"); return 0; }
static int doit_active (kx_context *kc) { int otherside; int nsockets; struct x_socket *sockets; u_char msg[1024], *p; int len; int tmp, tmp2; char *str; int i; size_t rem; uint32_t other_port; int error; const char *host = kc->host; otherside = connect_host (kc); if (otherside < 0) return 1; #if defined(SO_KEEPALIVE) && defined(HAVE_SETSOCKOPT) if (kc->keepalive_flag) { int one = 1; setsockopt (otherside, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(one)); } #endif p = msg; rem = sizeof(msg); *p++ = INIT; --rem; len = strlen(kc->user); tmp = kx_put_int (len, p, rem, 4); if (tmp < 0) return 1; p += tmp; rem -= tmp; memcpy(p, kc->user, len); p += len; rem -= len; *p++ = (kc->keepalive_flag ? KEEP_ALIVE : 0); --rem; str = getenv("DISPLAY"); if (str == NULL || (str = strchr(str, ':')) == NULL) str = ":0"; len = strlen (str); tmp = kx_put_int (len, p, rem, 4); if (tmp < 0) return 1; rem -= tmp; p += tmp; memcpy (p, str, len); p += len; rem -= len; str = getenv("XAUTHORITY"); if (str == NULL) str = ""; len = strlen (str); tmp = kx_put_int (len, p, rem, 4); if (tmp < 0) return 1; p += len; rem -= len; memcpy (p, str, len); p += len; rem -= len; if (kx_write (kc, otherside, msg, p - msg) != p - msg) err (1, "write to %s", host); len = kx_read (kc, otherside, msg, sizeof(msg)); if (len < 0) err (1, "read from %s", host); p = (u_char *)msg; if (*p == ERROR) { uint32_t u32; p++; p += kx_get_int (p, &u32, 4, 0); errx (1, "%s: %.*s", host, (int)u32, p); } else if (*p != ACK) { errx (1, "%s: strange msg %d", host, *p); } tmp2 = get_xsockets (&nsockets, &sockets, kc->tcp_flag); if (tmp2 < 0) errx(1, "Failed to open sockets"); display_num = tmp2; if (kc->tcp_flag) snprintf (display, display_size, "localhost:%u", display_num); else snprintf (display, display_size, ":%u", display_num); error = create_and_write_cookie (xauthfile, xauthfile_size, cookie, cookie_len); if (error) errx(1, "failed creating cookie file: %s", strerror(error)); status_output (kc->debug_flag); for (;;) { fd_set fdset; pid_t child; int fd, thisfd = -1; socklen_t zero = 0; FD_ZERO(&fdset); for (i = 0; i < nsockets; ++i) { if (sockets[i].fd >= FD_SETSIZE) errx (1, "fd too large"); FD_SET(sockets[i].fd, &fdset); } if (select(FD_SETSIZE, &fdset, NULL, NULL, NULL) <= 0) continue; for (i = 0; i < nsockets; ++i) if (FD_ISSET(sockets[i].fd, &fdset)) { thisfd = sockets[i].fd; break; } fd = accept (thisfd, NULL, &zero); if (fd < 0) { if (errno == EINTR) continue; else err(1, "accept"); } p = msg; *p++ = NEW_CONN; if (kx_write (kc, otherside, msg, p - msg) != p - msg) err (1, "write to %s", host); len = kx_read (kc, otherside, msg, sizeof(msg)); if (len < 0) err (1, "read from %s", host); p = (u_char *)msg; if (*p == ERROR) { uint32_t val; p++; p += kx_get_int (p, &val, 4, 0); errx (1, "%s: %.*s", host, (int)val, p); } else if (*p != NEW_CONN) { errx (1, "%s: strange msg %d", host, *p); } else { p++; p += kx_get_int (p, &other_port, 4, 0); } ++nchild; child = fork (); if (child < 0) { warn("fork"); continue; } else if (child == 0) { int s; for (i = 0; i < nsockets; ++i) close (sockets[i].fd); close (otherside); socket_set_port(kc->thataddr, htons(tmp)); s = socket (kc->thataddr->sa_family, SOCK_STREAM, 0); if (s < 0) err(1, "socket"); #if defined(TCP_NODELAY) && defined(HAVE_SETSOCKOPT) { int one = 1; setsockopt (s, IPPROTO_TCP, TCP_NODELAY, (void *)&one, sizeof(one)); } #endif #if defined(SO_KEEPALIVE) && defined(HAVE_SETSOCKOPT) if (kc->keepalive_flag) { int one = 1; setsockopt (s, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(one)); } #endif if (connect (s, kc->thataddr, kc->thataddr_len) < 0) err(1, "connect"); return active_session (fd, s, kc); } else { close (fd); } } }
static int doit_passive (kx_context *kc) { int otherside; u_char msg[1024], *p; int len; uint32_t tmp; const char *host = kc->host; otherside = connect_host (kc); if (otherside < 0) return 1; #if defined(SO_KEEPALIVE) && defined(HAVE_SETSOCKOPT) if (kc->keepalive_flag) { int one = 1; setsockopt (otherside, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(one)); } #endif p = msg; *p++ = INIT; len = strlen(kc->user); p += kx_put_int (len, p, sizeof(msg) - 1, 4); memcpy(p, kc->user, len); p += len; *p++ = PASSIVE | (kc->keepalive_flag ? KEEP_ALIVE : 0); if (kx_write (kc, otherside, msg, p - msg) != p - msg) err (1, "write to %s", host); len = kx_read (kc, otherside, msg, sizeof(msg)); if (len <= 0) errx (1, "error reading initial message from %s: " "this probably means it's using an old version.", host); p = (u_char *)msg; if (*p == ERROR) { p++; p += kx_get_int (p, &tmp, 4, 0); errx (1, "%s: %.*s", host, (int)tmp, p); } else if (*p != ACK) { errx (1, "%s: strange msg %d", host, *p); } else p++; p += kx_get_int (p, &tmp, 4, 0); memcpy(display, p, tmp); display[tmp] = '\0'; p += tmp; p += kx_get_int (p, &tmp, 4, 0); memcpy(xauthfile, p, tmp); xauthfile[tmp] = '\0'; p += tmp; status_output (kc->debug_flag); for (;;) { pid_t child; len = kx_read (kc, otherside, msg, sizeof(msg)); if (len < 0) err (1, "read from %s", host); else if (len == 0) return 0; p = (u_char *)msg; if (*p == ERROR) { p++; p += kx_get_int (p, &tmp, 4, 0); errx (1, "%s: %.*s", host, (int)tmp, p); } else if(*p != NEW_CONN) { errx (1, "%s: strange msg %d", host, *p); } else { p++; p += kx_get_int (p, &tmp, 4, 0); } ++nchild; child = fork (); if (child < 0) { warn("fork"); continue; } else if (child == 0) { int fd; int xserver; close (otherside); socket_set_port(kc->thataddr, htons(tmp)); fd = socket (kc->thataddr->sa_family, SOCK_STREAM, 0); if (fd < 0) err(1, "socket"); #if defined(TCP_NODELAY) && defined(HAVE_SETSOCKOPT) { int one = 1; setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void *)&one, sizeof(one)); } #endif #if defined(SO_KEEPALIVE) && defined(HAVE_SETSOCKOPT) if (kc->keepalive_flag) { int one = 1; setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(one)); } #endif if (connect (fd, kc->thataddr, kc->thataddr_len) < 0) err(1, "connect(%s)", host); { int d = 0; char *s; s = getenv ("DISPLAY"); if (s != NULL) { s = strchr (s, ':'); if (s != NULL) d = atoi (s + 1); } xserver = connect_local_xsocket (d); if (xserver < 0) return 1; } return passive_session (xserver, fd, kc); } else { } } }