int init_client(int *sock, char *host, int port, int type) { unsigned char ip[4]; memset(ip, '\0', sizeof ip); if (!host_ip(host,&(ip[0]))) return 0; return init_client_ip(sock,ip,port,type); }
int init_client(int *sock, const char *host, int port, int type) { unsigned char ip[4]; ip[0] = ip[1] = ip[2] = ip[3] = 0; if (!host_ip(host, &(ip[0]))) return 0; return init_client_ip(sock, ip, port, type); }
int init_client(int *sock, char *host, int port, int type) { unsigned char ip[4]; if (!host_ip(host,&(ip[0]))) { return(0); } return(init_client_ip(sock,ip,port,type)); }
int init_client(int *sock, char *host, int port) { unsigned char ip[4]; short p=0; if (!host_ip(host,&(ip[0]))) { return(0); } if (p != 0) port=p; return(init_client_ip(sock,ip,port)); }
static void smpp_listener_connection_callback(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) { struct event_base *base = evconnlistener_get_base(listener); SMPPServer *smpp_server = ctx; Octstr *ip = NULL; //host_ip(address); if (address->sa_family == AF_INET) { struct sockaddr_in *sin = (struct sockaddr_in *) address; ip = host_ip(*sin); } debug("smpp.listener.connection.callback", 0, "Got connection from %s", octstr_get_cstr(ip)); if(octstr_len(ip)) { if(smpp_listener_ip_is_blocked(smpp_server, ip)) { debug("smpp.listener.connection.callback", 0, "%s is temporarily banned from connecting. Rejecting.", octstr_get_cstr(ip)); evutil_closesocket(fd); octstr_destroy(ip); return; } } struct event *event_container; SMPPEsme *smpp_esme = smpp_esme_create(); smpp_esme->conn = conn_wrap_fd(fd, smpp_server->enable_ssl); smpp_esme->connected = 1; smpp_esme->smpp_server = smpp_server; smpp_esme->time_connected = time(NULL); smpp_esme->id = counter_value(smpp_server->esme_counter); smpp_esme->ip = ip; smpp_esme->max_open_acks = smpp_server->default_max_open_acks; counter_increase(smpp_server->esme_counter); event_container = event_new(base, fd, EV_TIMEOUT|EV_READ|EV_PERSIST, smpp_listener_event, smpp_esme); event_add(event_container, NULL); smpp_esme->event_container = event_container; }
static Boxc *accept_boxc(int fd, int ssl) { Boxc *newconn; Octstr *ip; int newfd; struct sockaddr_in client_addr; socklen_t client_addr_len; client_addr_len = sizeof(client_addr); newfd = accept(fd, (struct sockaddr *)&client_addr, &client_addr_len); if (newfd < 0) return NULL; ip = host_ip(client_addr); // if (is_allowed_ip(box_allow_ip, box_deny_ip, ip) == 0) { // info(0, "Box connection tried from denied host <%s>, disconnected", // octstr_get_cstr(ip)); // octstr_destroy(ip); // close(newfd); // return NULL; // } newconn = boxc_create(newfd, ip, ssl); /* * check if the SSL handshake was successfull, otherwise * this is no valid box connection any more */ #ifdef HAVE_LIBSSL if (ssl && !conn_get_ssl(newconn->smsbox_connection)) return NULL; #endif if (ssl) info(0, "Client connected from <%s> using SSL", octstr_get_cstr(ip)); else info(0, "Client connected from <%s>", octstr_get_cstr(ip)); /* XXX TODO: do the hand-shake, baby, yeah-yeah! */ return newconn; }
int gw_accept(int fd, Octstr **client_addr) { struct sockaddr_in addr; socklen_t addrlen; int new_fd; if (gwthread_pollfd(fd, POLLIN, -1.0) != POLLIN) { debug("gwlib.socket", 0, "gwthread_pollfd interrupted or failed"); return -1; } addrlen = sizeof(addr); new_fd = accept(fd, (struct sockaddr *) &addr, &addrlen); if (new_fd == -1) { error(errno, "accept system call failed."); return -1; } *client_addr = host_ip(addr); debug("test_smsc", 0, "accept() succeeded, client from %s", octstr_get_cstr(*client_addr)); return new_fd; }
int extract_host_port(char *str, char **host_ptr, unsigned char *ip, short *port_ptr) { char *h,*p; h=str; p=strchr(str,':'); if (p == NULL) { BIO_printf(bio_err,"no port defined\n"); return(0); } *(p++)='\0'; if ((ip != NULL) && !host_ip(str,ip)) goto err; if (host_ptr != NULL) *host_ptr=h; if (!extract_port(p,port_ptr)) goto err; return(1); err: return(0); }
static void cgw_listener(void *arg) { SMSCConn *conn = arg; PrivData *privdata = conn->data; struct sockaddr_in server_addr; socklen_t server_addr_len; Octstr *ip; Connection *server; int s, ret; /* Make sure we log into our own log-file if defined */ log_thread_to(conn->log_idx); while (!privdata->shutdown) { server_addr_len = sizeof(server_addr); ret = gwthread_pollfd(privdata->listening_socket, POLLIN, -1); if (ret == -1) { if (errno == EINTR) continue; error(0, "Poll for cgw smsc connections failed, shutting down"); break; } if (privdata->shutdown) break; if (ret == 0) /* This thread was woken up from elsewhere, but * if we're not shutting down nothing to do here. */ continue; s = accept(privdata->listening_socket, (struct sockaddr *) & server_addr, &server_addr_len); if (s == -1) { warning(errno, "cgw_listener: accept() failed, retrying..."); continue; } ip = host_ip(server_addr); if (!is_allowed_ip(privdata->allow_ip, privdata->deny_ip, ip)) { info(0, "CGW smsc connection tried from denied host <%s>, disconnected", octstr_get_cstr(ip)); octstr_destroy(ip); close(s); continue; } server = conn_wrap_fd(s, 0); if (server == NULL) { error(0, "cgw_listener: conn_wrap_fd failed on accept()ed fd"); octstr_destroy(ip); close(s); continue; } conn_claim(server); info(0, "cgw: smsc connected from %s", octstr_get_cstr(ip)); octstr_destroy(ip); cgw_receiver(conn, server); conn_destroy(server); } if (close(privdata->listening_socket) == -1) warning(errno, "smsc_cgw: couldn't close listening socket at shutdown"); gwthread_wakeup(privdata->sender_thread); }
static void fake_listener(void *arg) { SMSCConn *conn = arg; PrivData *privdata = conn->data; struct sockaddr_in client_addr; socklen_t client_addr_len; Octstr *ip; Connection *client; int s, ret; Msg *msg; /* Make sure we log into our own log-file if defined */ log_thread_to(conn->log_idx); while (1) { client_addr_len = sizeof(client_addr); ret = gwthread_pollfd(privdata->listening_socket, POLLIN, -1); if (ret == -1) { if (errno == EINTR) continue; error(0, "Poll for fakesmsc connections failed, shutting down"); break; } if (privdata->shutdown) break; if (ret == 0) /* * This thread was woke up from elsewhere, but * if we're not shutting down nothing to do here. */ continue; s = accept(privdata->listening_socket, (struct sockaddr *)&client_addr, &client_addr_len); if (s == -1) { warning(errno, "fake_listener: accept() failed, retrying..."); continue; } ip = host_ip(client_addr); if (!is_allowed_ip(privdata->allow_ip, privdata->deny_ip, ip)) { info(0, "Fakesmsc connection tried from denied host <%s>, " "disconnected", octstr_get_cstr(ip)); octstr_destroy(ip); close(s); continue; } client = conn_wrap_fd(s, 0); if (client == NULL) { error(0, "fake_listener: conn_wrap_fd failed on accept()ed fd"); octstr_destroy(ip); close(s); continue; } conn_claim(client); info(0, "Fakesmsc client connected from %s", octstr_get_cstr(ip)); octstr_destroy(ip); mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_ACTIVE; conn->connect_time = time(NULL); mutex_unlock(conn->flow_mutex); bb_smscconn_connected(conn); main_connection_loop(conn, client); if (privdata->shutdown) break; mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_RECONNECTING; mutex_unlock(conn->flow_mutex); while ((msg = gwlist_extract_first(privdata->outgoing_queue)) != NULL) { bb_smscconn_send_failed(conn, msg, SMSCCONN_FAILED_TEMPORARILY, NULL); } } if (close(privdata->listening_socket) == -1) warning(errno, "smsc_fake: couldn't close listening socket at shutdown"); mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_DEAD; while ((msg = gwlist_extract_first(privdata->outgoing_queue)) != NULL) { bb_smscconn_send_failed(conn, msg, SMSCCONN_FAILED_SHUTDOWN, NULL); } gwlist_destroy(privdata->outgoing_queue, NULL); octstr_destroy(privdata->allow_ip); octstr_destroy(privdata->deny_ip); gw_free(privdata); conn->data = NULL; mutex_unlock(conn->flow_mutex); debug("bb.sms", 0, "smsc_fake connection has completed shutdown."); bb_smscconn_killed(); }
QString Parser::sshRequest(QString commandline) { QString host_ip(this->sharedHost); QString user_name=this->sharedUser; QString pass_word=this->sharedPass; //---------- connection -------------- int libssh2_error = libssh2_init(0); if(libssh2_error) { qDebug("libssh2_init() error: %d", libssh2_error); //return -2; } QTcpSocket socket; socket.connectToHost(host_ip, 22); if(!socket.waitForConnected()) { qDebug("Error connecting to host %s", host_ip.toLocal8Bit().constData()); //return -1; } LIBSSH2_SESSION *session = libssh2_session_init(); if(!session) { qDebug("libssh2_session_init() failed"); //return -2; } libssh2_error = libssh2_session_startup(session, socket.socketDescriptor()); if(libssh2_error) { qDebug("libssh2_session_startup() error: %d", libssh2_error); //return -3; } { /* At this point we havn't yet authenticated. The first thing to do * is check the hostkey's fingerprint against our known hosts Your app * may have it hard coded, may go to a file, may present it to the * user, that's your call */ const char *fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); } libssh2_userauth_list(session, user_name.toLocal8Bit().constData(), user_name.toLocal8Bit().length()); if(libssh2_userauth_password( session, user_name.toLocal8Bit().constData(), pass_word.toLocal8Bit().constData() )) { qDebug("Password authentication failed"); socket.disconnectFromHost(); libssh2_session_disconnect(session, "Client disconnecting for error"); libssh2_session_free(session); libssh2_exit(); //return -4; } // command channel //------------setup channel ---------------------- LIBSSH2_CHANNEL *channel = NULL; channel = libssh2_channel_open_session(session); int rc; if ( channel == NULL ) { qDebug()<<"Failed to open a new channel\n"; socket.disconnectFromHost(); //return -1; } libssh2_channel_set_blocking(channel, 1); while ((rc=libssh2_channel_exec(channel, commandline.toLocal8Bit().constData()))==LIBSSH2_ERROR_EAGAIN ); if (rc) { //return -1; } //-------read channel----------- int read; QByteArray byte_array; byte_array.resize(4096); char* buffer=byte_array.data(); int buffer_size=byte_array.size(); QString myOutPut; while(true) { { read = libssh2_channel_read(channel, buffer, buffer_size); QByteArray debug = QByteArray(buffer, read); //qDebug()<<"STDOUT: "<<debug.constData(); myOutPut = debug.constData(); qDebug() << myOutPut; if(LIBSSH2_ERROR_EAGAIN == read) { qDebug("LIBSSH2_ERROR_EAGAIN"); break; } else if(read < 0) { qDebug(" error reading from channel"); closeChannel(channel); goto next_channel; } } { read = libssh2_channel_read_stderr(channel, buffer, buffer_size); QByteArray debug = QByteArray(buffer, read); qDebug()<<"STDERR: "<<debug.constData(); if(LIBSSH2_ERROR_EAGAIN == read) { qDebug("LIBSSH2_ERROR_EAGAIN"); break; } else if(read < 0) { qDebug(" error reading from channel"); closeChannel(channel); goto next_channel; } } int i=0; i = libssh2_channel_eof(channel); if(i) { qDebug("libssh2_channel_eof %i", i); closeChannel(channel); goto next_channel; } } next_channel: //------------ clean session socket.disconnectFromHost(); libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_free(session); libssh2_exit(); return myOutPut; }