bool Connection::connect(const char* db, const char* server, const char* user, const char* password, unsigned int port) { // Figure out what the server parameter means, then try to establish // the connection. error_message_.clear(); string host, socket_name; copacetic_ = parse_ipc_method(server, host, port, socket_name) && driver_->connect(host.c_str(), (socket_name.empty() ? 0 : socket_name.c_str()), port, db, user, password); // If it failed, decide how to tell the user if (!copacetic_ && throw_exceptions()) { throw ConnectionFailed(error(), errnum()); } else { return copacetic_; } }
bool Connection::shutdown() { error_message_.clear(); if (connected()) { if (driver_->shutdown()) { return true; } else { if (throw_exceptions()) { throw ConnectionFailed(error(), errnum()); } return false; } } else { build_error_message("shutdown database server"); if (throw_exceptions()) { throw ConnectionFailed(error_message_.c_str()); } return false; } }
bool Connection::select_db(const std::string& db) { error_message_.clear(); if (connected()) { if (driver_->select_db(db.c_str())) { return true; } else { if (throw_exceptions()) { throw DBSelectionFailed(error(), errnum()); } return false; } } else { build_error_message("select a database"); if (throw_exceptions()) { throw DBSelectionFailed(error_message_.c_str()); } return false; } }
bool jdk_inet_client_socket::make_connection( const char *hostname, int port, jdk_dns_cache *cache, bool use_ssl_ ) { struct sockaddr_in my_address; close(); // make a socket sock_id = ::jdk_socket_lpsocket( AF_INET, SOCK_STREAM, 0 ); if( sock_id==JDK_SOCKET_ERROR ) { return false; } readable=true; writable=true; // set socket options for the socket setup_socket(); // get the remote address via the dns cache if( cache ) { if( cache->gethostbyname(hostname,&my_address)<0 ) { // cant find host return false; } } else { if( jdk_gethostbyname(hostname, &my_address )<0 ) { // cant find host return false; } } #if JDK_IS_MACOSX extern bool jdk_socket_override_filtering; if( jdk_socket_override_filtering ) { // do magic handshake with driver struct sockaddr_in from; int len = sizeof( struct sockaddr_in ); from.sin_family = AF_INET; from.sin_addr.s_addr = htonl(0x00000001); from.sin_port = htons(1); bind( sock_id, (struct sockaddr*)&from, len ); from.sin_port = htons(0); bind( sock_id, (struct sockaddr*)&from, len ); } #endif my_address.sin_port = htons( port ); // try connect if( ::jdk_socket_lpconnect( sock_id, (struct sockaddr *)&my_address, sizeof(my_address))==SOCKET_ERROR && (errnum()!=EINPROGRESS) // allow nonblocking connect to succeed at this point ) { // connect failed. close the socket and return. close(); return false; } // connection succeeded, initialize ssl if needed #if JDK_HAS_SSL if( use_ssl_ ) { use_ssl = true; ssl_client_method = SSLv2_client_method(); ssl_ctx = SSL_CTX_new(ssl_client_method); use_ssl=false; SSL_set_fd(ssl, sock_id ); SSL_connect( ssl ); // X509 *ssl_server_cert; // ssl_server_cert = SSL_get_peer_certificate(ssl); // X509_free( ssl_server_cert ); } #endif return true; }
bool jdk_inet_client_socket::make_connection_from( const char *hostname, int port, const char *bindname, int bindport, jdk_dns_cache *cache, bool use_ssl_ ) { if( sock_id!=JDK_SOCKET_ERROR ) { // if we already had a socket, close it first. ::closesocket(sock_id); sock_id=JDK_SOCKET_ERROR; } // open a socket sock_id = ::jdk_socket_lpsocket(AF_INET, SOCK_STREAM, 0); if( sock_id==JDK_SOCKET_ERROR ) { return false; // didnt work! } int flag = 1; setsockopt ( sock_id, SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(flag) ); // bind it to the specified address struct sockaddr_in server_address; int len = sizeof(server_address); memset((char*) &server_address, 0, len); // null, blank, or "0" means listen to all local ips // ip address or hostname must be converted if( bindname && *bindname && strcmp(bindname,"0")!=0 ) { jdk_gethostbyname( bindname, &server_address ); } else { server_address.sin_addr.s_addr = htonl(INADDR_ANY); } server_address.sin_family = AF_INET; server_address.sin_port = htons(bindport); if (::bind(sock_id, (sockaddr *)&server_address, len) != 0) { // bind failed. close socket and return false. ::closesocket(sock_id); sock_id=JDK_SOCKET_ERROR; return false; } readable=true; writable=true; // set socket options for the socket setup_socket(); // get the remote address via the dns cache struct sockaddr_in my_address; if( cache ) { if( cache->gethostbyname(hostname,&my_address)<0 ) { // cant find host return false; } } else { if( jdk_gethostbyname(hostname, &my_address )<0 ) { // cant find host return false; } } my_address.sin_port = htons( port ); // try connect if( ::jdk_socket_lpconnect( sock_id, (struct sockaddr *)&my_address, sizeof(my_address))==SOCKET_ERROR && (errnum()!=EINPROGRESS) // allow nonblocking connect to succeed at this point ) { // connect failed. close the socket and return. close(); return false; } // connection succeeded, initialize ssl if needed #if JDK_HAS_SSL if( use_ssl_ ) { use_ssl = true; ssl_client_method = SSLv2_client_method(); ssl_ctx = SSL_CTX_new(ssl_client_method); use_ssl=false; SSL_set_fd(ssl, sock_id ); SSL_connect( ssl ); // X509 *ssl_server_cert; // ssl_server_cert = SSL_get_peer_certificate(ssl); // X509_free( ssl_server_cert ); } #endif return true; }
/* * Full implementation of the three error correcting Peterson decoder. For * t<6, it is faster than Massey - Berlekamp. It is also somewhat more * intuitive. */ extern void ecc_decode(uint8_t code[ECC_CAPACITY], uint8_t mesg[ECC_CAPACITY], int *errcode) { REVERSE(code, ECC_CAPACITY); uint8_t syn[ECC_OFFSET + 1], deter, z[4], e0, e1, e2, n0, n1, n2, w0, w1, w2, x0, x[3]; int sols; *errcode = 0; /* * First, get the message out of the code, so that even if we can't correct * it, we return an estimate. */ for (int i = 0; i < ECC_PAYLOAD; i++) mesg[i] = code[(ECC_CAPACITY - 1) - i]; syndrome(code, syn); if (syn[0] == 0) return; /* * We now know we have at least one error. If there are no errors detected, * we assume that something funny is going on, and so return with errcode 4, * else pass the number of errors back via errcode. */ errnum(syn, &deter, errcode); if (*errcode == 4) return; /* Having obtained the syndrome, the number of errors, and the determinant, * we now proceed to correct the block. If we do not find exactly the * number of solutions equal to the number of errors, we have exceeded our * error capacity, and return with the block uncorrected, and errcode 4. */ switch (*errcode) { case 1: x0 = GF_MUL(syn[2], GF_INV(syn[1])); w0 = GF_MUL(GF_EXP(syn[1], 2), GF_INV(syn[2])); if (v2e[x0] > 5) mesg[(ECC_CAPACITY - 1) - v2e[x0]] = GF_ADD(mesg[(ECC_CAPACITY - 1) - v2e[x0]], w0); return; case 2: z[0] = GF_MUL(GF_ADD(GF_MUL(syn[1], syn[3]), GF_EXP(syn[2], 2)), GF_INV(deter)); z[1] = GF_MUL(GF_ADD(GF_MUL(syn[2], syn[3]), GF_MUL(syn[1], syn[4])), GF_INV(deter)); z[2] = 1; z[3] = 0; polysolve(z, x, &sols); if (sols != 2) { *errcode = 4; return; } w0 = GF_MUL(z[0], syn[1]); w1 = GF_ADD(GF_MUL(z[0], syn[2]), GF_MUL(z[1], syn[1])); n0 = (ECC_CAPACITY - 1) - v2e[GF_INV(x[0])]; n1 = (ECC_CAPACITY - 1) - v2e[GF_INV(x[1])]; e0 = GF_MUL(GF_ADD(w0, GF_MUL(w1, x[0])), GF_INV(z[1])); e1 = GF_MUL(GF_ADD(w0, GF_MUL(w1, x[1])), GF_INV(z[1])); if (n0 < ECC_PAYLOAD) mesg[n0] = GF_ADD(mesg[n0], e0); if (n1 < ECC_PAYLOAD) mesg[n1] = GF_ADD(mesg[n1], e1); return; case 3: z[3] = 1; z[2] = GF_MUL(syn[1], GF_MUL(syn[4], syn[6])); z[2] = GF_ADD(z[2], GF_MUL(syn[1], GF_MUL(syn[5], syn[5]))); z[2] = GF_ADD(z[2], GF_MUL(syn[5], GF_MUL(syn[3], syn[3]))); z[2] = GF_ADD(z[2], GF_MUL(syn[3], GF_MUL(syn[4], syn[4]))); z[2] = GF_ADD(z[2], GF_MUL(syn[2], GF_MUL(syn[5], syn[4]))); z[2] = GF_ADD(z[2], GF_MUL(syn[2], GF_MUL(syn[3], syn[6]))); z[2] = GF_MUL(z[2], GF_INV(deter)); z[1] = GF_MUL(syn[1], GF_MUL(syn[3], syn[6])); z[1] = GF_ADD(z[1], GF_MUL(syn[1], GF_MUL(syn[5], syn[4]))); z[1] = GF_ADD(z[1], GF_MUL(syn[4], GF_MUL(syn[3], syn[3]))); z[1] = GF_ADD(z[1], GF_MUL(syn[2], GF_MUL(syn[4], syn[4]))); z[1] = GF_ADD(z[1], GF_MUL(syn[2], GF_MUL(syn[3], syn[5]))); z[1] = GF_ADD(z[1], GF_MUL(syn[2], GF_MUL(syn[2], syn[6]))); z[1] = GF_MUL(z[1], GF_INV(deter)); z[0] = GF_MUL(syn[2], GF_MUL(syn[3], syn[4])); z[0] = GF_ADD(z[0], GF_MUL(syn[3], GF_MUL(syn[2], syn[4]))); z[0] = GF_ADD(z[0], GF_MUL(syn[3], GF_MUL(syn[5], syn[1]))); z[0] = GF_ADD(z[0], GF_MUL(syn[4], GF_MUL(syn[4], syn[1]))); z[0] = GF_ADD(z[0], GF_MUL(syn[3], GF_MUL(syn[3], syn[3]))); z[0] = GF_ADD(z[0], GF_MUL(syn[2], GF_MUL(syn[2], syn[5]))); z[0] = GF_MUL(z[0], GF_INV(deter)); polysolve (z, x, &sols); if (sols != 3) { *errcode = 4; return; } w0 = GF_MUL(z[0], syn[1]); w1 = GF_ADD(GF_MUL(z[0], syn[2]), GF_MUL(z[1], syn[1])); w2 = GF_ADD(GF_MUL(z[0], syn[3]), GF_ADD(GF_MUL(z[1], syn[2]), GF_MUL(z[2], syn[1]))); n0 = (ECC_CAPACITY - 1) - v2e[GF_INV(x[0])]; n1 = (ECC_CAPACITY - 1) - v2e[GF_INV(x[1])]; n2 = (ECC_CAPACITY - 1) - v2e[GF_INV(x[2])]; e0 = GF_ADD(w0, GF_ADD(GF_MUL(w1, x[0]), GF_MUL(w2, GF_EXP(x[0], 2)))); e0 = GF_MUL(e0, GF_INV(GF_ADD(z[1], GF_EXP(x[0], 2)))); e1 = GF_ADD(w0, GF_ADD(GF_MUL(w1, x[1]), GF_MUL(w2, GF_EXP(x[1], 2)))); e1 = GF_MUL(e1, GF_INV(GF_ADD(z[1], GF_EXP(x[1], 2)))); e2 = GF_ADD(w0, GF_ADD(GF_MUL(w1, x[2]), GF_MUL(w2, GF_EXP(x[2], 2)))); e2 = GF_MUL(e2, GF_INV(GF_ADD(z[1], GF_EXP(x[2], 2)))); if (n0 < ECC_PAYLOAD) mesg[n0] = GF_ADD(mesg[n0], e0); if (n1 < ECC_PAYLOAD) mesg[n1] = GF_ADD(mesg[n1], e1); if (n2 < ECC_PAYLOAD) mesg[n2] = GF_ADD(mesg[n2], e2); return; } }
std::string errstr () const { return errnumToString (errnum ()); }