Beispiel #1
0
void *callback(void *arg) {
	Socket_t S = (Socket_t)arg;
	String_t Str = tb_String(NULL);
	int rc;
	//char buff[100];

	tb_warn("Server: New cx \n");
	tb_Dump(S);

	// -- for educative purpose only. -optional- behaviour
	// setup timeout to 5mn 
	tb_setSockTO(S, 5*60, 0);
	// disable Naggle's algo (Don't mess with this if you don't known what's about)
	tb_setSockNoDelay(S);
	// -------------
	//tb_Dump(S);

	tb_readSock(S, Str, MAX_BUFFER);

	tb_warn("Server: recv<%s>\n", tb_toStr(Str));
	/*
	for(i=10;i>0; i--) {
		snprintf(buff, 100, "cb[%d] countdown: %d\n", pthread_self(), i);
		tb_writeSock(S, buff);
		sleep(1);
	}
	*/
	rc = tb_writeSock(S, "BYE");
	tb_warn("Server: wrote %d\n", rc);
	tb_Free(Str);

	return NULL;
}
Beispiel #2
0
void tb_scalar_dump( tb_Object_t O, int level ) {
	if(! TB_VALID(O, TB_SCALAR)) {
		tb_warn("Dump: %x not a tb_Scalar\n");
		return;
	}
	fprintf(stderr, "<TB_SCALAR TYPE=\"%d\" ADDR=\"%p\"/>\n",
					O->isA, O);
}
Beispiel #3
0
int udp_ip(void *arg) {
	Socket_t Serv    = tb_Socket(TB_UDP_IP, "localhost", 55552);
	Socket_t Client  = tb_Socket(TB_UDP_IP, "localhost", 55551);
	String_t Str = tb_String(NULL);
	int rc;
	tb_initServer(Serv, NULL, NULL);
	tb_Connect(Client, 1, 1);
						 
	tb_Dump(Serv);
  tb_setSockTO(Serv, 15, 0);
	rc = tb_readSock(Serv, Str, MAX_BUFFER);
	if( rc != 0 ) {
		tb_warn("UDP/IP Server: got <%S>\n", Str);
		tb_writeSock(Client, "UDP/IP:BYE"); 
		tb_Clear(Str);
	}
	tb_warn("UDP/IP Server exiting\n");
		
	return 0;
}
Beispiel #4
0
int udp_ux(void *arg) {
	Socket_t Serv    = tb_Socket(TB_UDP_UX, "/tmp/ux-sock.2", 0);
	Socket_t Client  = tb_Socket(TB_UDP_UX, "/tmp/ux-sock.1", 0);
	String_t Str = tb_String(NULL);
	int rc;
	tb_initServer(Serv, NULL, NULL);
	tb_Connect(Client, 1, 1);
						 
	tb_Dump(Serv);
	tb_Dump(Client);
  tb_setSockTO(Serv, 15, 0);
	rc = tb_readSock(Serv, Str, MAX_BUFFER);
	if( rc != 0 ) {
		tb_warn("UDP/UX Server: got <%S>\n", Str);
		tb_writeSock(Client, "UDP/UNIX:BYE");
		tb_Clear(Str);
	}
	tb_warn("UDP/UX Server exiting\n");
		
	return 0;
}
Beispiel #5
0
void *tst_client(void *dummy) {
	Socket_t Client  = tb_Socket(TB_TCP_IP, "192.168.0.35", 55553);
	String_t S  = tb_String(NULL); 

	tb_Connect(Client, 1, 1);
	tb_setSockTO(Client, 20,0);
	if(tb_getSockStatus(Client) == TB_CONNECTED) {
		tb_writeSock(Client, "TEST TCP/IP");
		while( ! tb_matchRegex(S, "BYE", 0)) {
			if( tb_readSock(Client, S, 500) >0) {
				tb_warn("client: got <%s>\n", S2sz(S));
				tb_Clear(S);
			}
		}
	} else {
		tb_warn("Not connected !!\n");
	}
	
	tb_Free(S);

	return NULL;
}
Beispiel #6
0
static Num_t tb_num_unmarshall(XmlElt_t xml_entity) {
	Num_t N;
	if(! streq(S2sz(XELT_getName(xml_entity)), "int")) {
		tb_warn("tb_num_unmarshall: not an int Elmt\n");
		return NULL;
	}
	XmlElt_t Xe = XELT_getFirstChild(xml_entity);
	if(Xe != NULL) {
		N = tb_Num(tb_toInt(XELT_getText(Xe)));
	} else {
		N = tb_Num(0); // we must have a default, anyway
	}

	return N;
}
Beispiel #7
0
static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
			       const void *buf, size_t size)
{
	struct tb *tb = data;

	if (!tb->cm_ops->handle_event) {
		tb_warn(tb, "domain does not have event handler\n");
		return true;
	}

	switch (type) {
	case TB_CFG_PKG_XDOMAIN_REQ:
	case TB_CFG_PKG_XDOMAIN_RESP:
		return tb_xdomain_handle_request(tb, type, buf, size);

	default:
		tb->cm_ops->handle_event(tb, type, buf, size);
	}

	return true;
}
Beispiel #8
0
/** read on a Socket_t using timeout and retries values as set in object.
 * \ingroup Socket
 * Target Socket_t must have been fully initialised by tb_Connect or tb_initServer. Read result is appended in 'msg' String_t.  
 *
 * @return number of read bytes, or -1 if error.
 * Example:
 * \code
 * ...
 * String_t S = tb_String(NULL);
 * Socket_t So = tb_Socket(TB_TCP_UX, "/tmp/my_unix_sock", 0);
 * tb_Connect(So, 1, 1);
 * int rc;
 *
 * while(( rc = tb_readSock(So, S, 1024)) > 0);
 * switch( rc ) {
 * case -1: // error occurs
 * case  0: // read reached eof (or timed out)
 * ...
 * \endcode
 * S will contains a concatened string of all 1024's buffers read 
 *
 * other Examples : 
 * see test/srv_test.c , test/socket_test.c in build tree
 *
 * \remarks ERROR:
 * in case of error tb_errno will be set to :
 * - TB_ERR_INVALID_OBJECT : So not a TB_SOCKET
 * - TB_ERR_BAD : msg is not a TB_STRING
 * 
 * @see tb_Socket, tb_writeSock, tb_writeSockBin, tb_readSockLine
*/
int tb_readSock(Socket_t S, tb_Object_t Msg, int maxlen) {
  fd_set set ;
  struct timeval tps ;
  int rc, retval = 0;
	sock_members_t So;
	char buff[maxlen+1];

	no_error;

	if(! TB_VALID(S,   TB_SOCKET)) {
		set_tb_errno(TB_ERR_INVALID_TB_OBJECT);
		return TB_ERR;
	}

	if(! TB_VALID(Msg, TB_STRING) && ! TB_VALID(Msg, TB_RAW)) {
		set_tb_errno(TB_ERR_BAD);
		return TB_ERR;
	}

#ifdef WITH_XTI
	if( XSock(S)->addr_family   == TB_X25 ) {
		return tb_readSock_X25(S, Msg, maxlen);
	}
#endif


 
	So = XSock(S);


	if( So->buffer != NULL && tb_getSize(So->buffer) >0) {
		int n;
		n = TB_MIN( (maxlen), (tb_getSize(So->buffer)));
		tb_notice("readSock: already %d bytes in buffer\n", tb_getSize(So->buffer));
		if( n ) {
			if( tb_isA(Msg) == TB_STRING) {
				tb_StrnAdd(Msg, n, -1, "%S", So->buffer);
			} else {
			tb_RawAdd(Msg, n, -1, S2sz(So->buffer));
			}
			tb_StrDel(So->buffer, 0, n);
			if( n == maxlen) {
				return n;
			} else {
				maxlen -= n;
				retval = n;
			}
		}
	} 

	restart_r_select:

  FD_ZERO(&set);
  FD_SET(So->sock, &set);
  tb_getSockTO(S, &(tps.tv_sec), &(tps.tv_usec));

  rc = select(So->sock+1, &set, NULL, NULL, &tps);
  switch (rc)
    {
    case -1:
			if( errno == EINTR ) goto restart_r_select;
      tb_warn("tb_readSock[%d]: select failed (%s)\n", 
							 So->sock, strerror(errno)); 
      // invalid fd ==> we're disconnected
      if( errno == EBADF ) So->status = TB_DISCONNECTED;
			set_tb_errno(TB_ERR_DISCONNECTED);
      retval = TB_ERR;
      break; 
    case 0:
      /* Time out */
      tb_notice("tb_readSock[%d]: select timed out\n", So->sock); 
      So->status = TB_TIMEDOUT;
      retval = TB_KO;
			break ;
    default:
			if( ! FD_ISSET(So->sock, &set)) {
				tb_notice("tb_readSock[%d]: select rc=%d but fd is not ready\n", 
									So->sock, rc); 
				retval = TB_KO;
				break ;
			}
		restart_r_read:
#ifdef WITH_SSL
			if( So->ssl ) {
				tb_info("SSL_read: try to read %d bytes\n", maxlen);
				rc = SSL_read(XSsl(S)->cx, buff, maxlen);
				switch (SSL_get_error(XSsl(S)->cx,rc)) {
				case SSL_ERROR_NONE:
					buff[rc] = 0;
					tb_StrAdd(Msg, -1, "%s", buff);
					if( rc < maxlen ) {
						tb_info("SSL_read: got only %d/%d\n", rc, maxlen);
						maxlen -= rc;
						goto restart_r_select;
					}
					return rc;
				case SSL_ERROR_SYSCALL:
					if( errno ) {
						tb_warn("tb_readSock[%d(SSL)]: read error (%s)\n",
										 So->sock, strerror(errno));
					}
					/* fall through */
				case SSL_ERROR_WANT_WRITE:
				case SSL_ERROR_WANT_READ:
				case SSL_ERROR_WANT_X509_LOOKUP:
				case SSL_ERROR_ZERO_RETURN:
				case SSL_ERROR_SSL:
					ERR_print_errors_fp(stderr);
					return TB_ERR;
				}
			} else {
				set_nonblock_flag(So->sock, 1);
				rc = read(So->sock, buff, maxlen);
				set_nonblock_flag(So->sock, 0);
			}
#else
			set_nonblock_flag(So->sock, 1);
			rc = read(So->sock, buff, maxlen);
			set_nonblock_flag(So->sock, 0);
#endif			

      switch( rc ) {
      case -1:
				if( errno == EINTR ) goto restart_r_read;
				if( errno != EWOULDBLOCK ) {
					tb_error("tb_readSock[%d]: read error (%s)\n", So->sock, strerror(errno));
					retval = TB_ERR;
					So->status = TB_DISCONNECTED;
					break;
				}
      case 0:  // eof
				*buff = 0;
      default:
				retval += rc;
				tb_RawAdd(Msg, rc, -1, buff); 
				break ;
      }
      break;
    }
  return retval;
}
Beispiel #9
0
void ssl_barf_out(Socket_t S) {
	BIO *ebio;
	char buf[BUFSIZ], *p;
	sock_ssl_t m = XSsl(S);

  if (tb_errorlevel >= TB_NOTICE) {
		STACK      * sk;

    if ((ebio=BIO_new(BIO_s_file())) == NULL) {
      tb_warn("Cannot create new BIO\n");
      ERR_print_errors_fp(stderr);
      return;
    }
    BIO_set_fp(ebio,stderr,BIO_NOCLOSE);
    if ((sk=(STACK *)SSL_get_peer_cert_chain(m->cx)) != NULL) {
			int i;
      BIO_printf(ebio,"---\nCertificate chain\n");
      for (i=0; i<sk_num(sk); i++) {
        X509_NAME_oneline(X509_get_subject_name((X509*)sk_value(sk,i)),buf,BUFSIZ);
        BIO_printf(ebio,"%2d s:%s\n",i,buf);
        X509_NAME_oneline(X509_get_issuer_name((X509 *)sk_value(sk,i)),buf,BUFSIZ);
        BIO_printf(ebio,"   i:%s\n",buf);
      }
    }
    BIO_printf(ebio,"---\n");
    if ((m->peer=SSL_get_peer_certificate(m->cx)) != NULL) {
      BIO_printf(ebio,"Peer certificate\n");
      PEM_write_bio_X509(ebio,m->peer);
      X509_NAME_oneline(X509_get_subject_name(m->peer),buf,BUFSIZ);
      BIO_printf(ebio,"subject=%s\n",buf);
      X509_NAME_oneline(X509_get_issuer_name(m->peer),buf,BUFSIZ);
      BIO_printf(ebio,"issuer=%s\n",buf);
    }
    else
      BIO_printf(ebio,"no peer certificate available\n");
    if (((sk=SSL_get_client_CA_list(m->cx)) != NULL) && (sk_num(sk) > 0)) {
			int i;
      BIO_printf(ebio,"---\nAcceptable peer certificate CA names\n");
      for (i=0; i<sk_num(sk); i++) {
        m->xn=(X509_NAME *)sk_value(sk,i);
        X509_NAME_oneline(m->xn,buf,sizeof(buf));
        BIO_write(ebio,buf,strlen(buf));
        BIO_write(ebio,"\n",1);
      }
    }
    else {
      BIO_printf(ebio,"---\nNo peer certificate CA names sent\n");
    }
    if ((p=SSL_get_shared_ciphers(m->cx,buf,BUFSIZ)) != NULL) {
			int i, j;
      BIO_printf(ebio,"---\nCiphers common between both SSL endpoints:\n");
      j=i=0;
      while (*p) {
        if (*p != ':') {
          BIO_write(ebio,p,1);j++;
        } else {
          BIO_write(ebio,"                ",15-j%25);i++;j=0;
          BIO_write(ebio,((i%3)?" ":"\n"),1);
        }
        p++;
      }
      BIO_write(ebio,"\n",1);
    }
    BIO_printf(ebio,
               "---\nSSL handshake has read %ld bytes and written %ld bytes\n",
               BIO_number_read(SSL_get_rbio(m->cx)),
               BIO_number_written(SSL_get_wbio(m->cx)));
    BIO_printf(ebio,((m->cx->hit)?"---\nReused, ":"---\nNew, "));
    m->sc=SSL_get_current_cipher(m->cx);
    BIO_printf(ebio,"%s, Cipher is %s\n",
               SSL_CIPHER_get_version(m->sc),SSL_CIPHER_get_name(m->sc));
    if(m->peer != NULL) {
      EVP_PKEY *pktmp;
      pktmp = X509_get_pubkey(m->peer);
      BIO_printf(ebio,"Server public key is %d bit\n", EVP_PKEY_bits(pktmp));
      EVP_PKEY_free(pktmp);
    }
    SSL_SESSION_print(ebio,SSL_get_session(m->cx));
    BIO_printf(ebio,"---\n");
    if(m->peer != NULL)
      X509_free(m->peer);
    BIO_free(ebio);
  }
}
Beispiel #10
0
int tb_connectSSL( Socket_t S ) {
  BIO* sbio=NULL,* dbio;
	int l;
	sock_ssl_t m = XSsl(S);

	tb_info("tb_connectSSL in\n");

  /* Ordinarily, just decorate the SSL connection with the socket file
   * descriptor.  But for super-cool shazaam debugging, build your
   * own socket BIO, decorate it with a BIO debugging callback, and
   * presto, see a dump of the bytes as they fly.  See ssl/ssl_lib.c
   * for details.  
   */
  if (tb_errorlevel <= TB_NOTICE) { 
    SSL_set_fd(m->cx, tb_getSockFD(S));
	} else {
    if (!(sbio=BIO_new_socket(tb_getSockFD(S),BIO_NOCLOSE))) {
      tb_warn("tb_connectSSL: Cannot create new socket BIO\n");
      ERR_print_errors_fp(stderr);
      goto err;
    }
    SSL_set_bio(m->cx,sbio,sbio);
    m->cx->debug=1;
    dbio=BIO_new_fp(stdout,BIO_NOCLOSE);
    BIO_set_callback(sbio,(void *)bio_dump_cb);
    BIO_set_callback_arg(sbio,dbio);
  }
	tb_info("ssl cx linked to socket\n");

  /* Initialize the state of the connection so the first i/o operation
   * knows to do the SSL connect.  Strictly speaking, this not necessary,
   * as this code goes ahead and calls SSL_connect() anyway (so I can 
   * put the connect tracing stuff in one handy spot).  But look closely
   * in ssl/s_client.c for a call to SSL_connect.  You won't find one.
   */
  SSL_set_connect_state(m->cx);
	//	SSL_CTX_set_session_cache_mode(XSSL(S)->ctx, SSL_SESS_CACHE_CLIENT);
	if(m->session != NULL ) {
		if(! SSL_set_session(m->cx, m->session)) {
			ERR_print_errors_fp(stderr);
		}
		tb_notice("tb_ConnectSSL: got a session to reuse ! (%X)\n", m->session);
	}


  /* Now that we've finally finished customizing the context and the
   * connection, go ahead and see if it works.  This function call 
   * invokes all the SSL connection handshake and key exchange logic,
   * (which is why there's so much to report on after it completes).
   */

 retry_connect:
  l = SSL_connect(m->cx);

	tb_info("ssl connect returns: %d\n", l);

  switch (SSL_get_error(m->cx,l)) { 
  case SSL_ERROR_NONE:
    break;
  case SSL_ERROR_SYSCALL:
    if ((l != 0) && errno)  tb_warn("tb_connectSSL: Write errno=%d\n", errno);
    goto err;
    break;
    /* fall through */
  case SSL_ERROR_WANT_WRITE:
		tb_info("SSL_ERROR_WANT_WRITE\n");
		goto retry_connect;
  case SSL_ERROR_WANT_READ:
		tb_info("SSL_ERROR_WANT_READ\n");
		goto retry_connect;
  case SSL_ERROR_WANT_X509_LOOKUP:
		tb_info("SSL_ERROR_WANT_X509_LOOKUP\n");
		goto retry_connect;
  case SSL_ERROR_ZERO_RETURN:
		tb_info("SSL_ERROR_ZERO_RETURN\n");
  case SSL_ERROR_SSL:
		tb_info("SSL_ERROR_SSL\n");
  default:
    ERR_print_errors_fp(stderr);
    goto err;
    break;
  }


	tb_info("connected\n");

	if(m->session == NULL ) {
		m->session = SSL_get_session(m->cx); //fixme: will leak !
		tb_notice("save session for later reuse (%X)\n", m->session);
		
	}

  /* Report on what happened now that we've successfully connected. */
  if (tb_errorlevel >= TB_NOTICE) ssl_barf_out(S);
  
	tb_info("tb_connectSSL out\n");

	return TB_OK;
 err:
	tb_info("tb_connectSSL err out\n");
	tb_Clear(S);
	return TB_ERR;
}
Beispiel #11
0
retcode_t tb_initSSL(Socket_t    S, 
										 enum ssl_mode  mode,       // SSL_CLIENT | SSL_SERVER
										 ssl_meth_t  method,     // SSL1 | SSL2 | SSL3 | TLS1
										 char      * CA_path,
										 char      * CA_file,
										 char      * cert,
										 char      * pwd,
										 char      * cipher) {
	SSL_METHOD * meth;
	sock_ssl_t m;

	tb_info("tb_initSSL in\n");

	if(!TB_VALID(S, TB_SOCKET)) {
		set_tb_errno(TB_ERR_INVALID_TB_OBJECT);
		return TB_ERR;
	}
	if(XSock(S)->ssl != NULL ) {
		tb_warn("tb_initSSL: Socket_t allready SSL initialized\n");
		set_tb_errno(TB_ERR_ALLREADY);
		return TB_ERR;
	}

	m = tb_xcalloc(1, sizeof(struct sock_ssl));
	XSock(S)->ssl = m;
	m->ssl_method = method;

	m->mode = method;


	if( CA_path ) m->CA_path = tb_xstrdup(CA_path);
	if( CA_file ) m->CA_file = tb_xstrdup(CA_file);
	if( cert ) 		m->cert    = tb_xstrdup(cert);
	if( pwd ) 		m->pwd     = tb_xstrdup(pwd);		
	if( cipher ) 	m->cipher  = tb_xstrdup(cipher);
	

	__tb_init_SSL_once();

	switch (m->ssl_method) {
  case 1:
		meth = (mode == SSL_CLIENT) ? SSLv23_client_method() : SSLv23_server_method();
    break;
  case 2:
		meth = (mode == SSL_CLIENT) ? SSLv2_client_method() : SSLv2_server_method();
    break;
  case 3:
		meth = (mode == SSL_CLIENT) ? SSLv3_client_method() : SSLv3_server_method();
    break;
  case 4:
		meth = (mode == SSL_CLIENT) ? TLSv1_client_method() : TLSv1_server_method();
    break;
	default:
		meth = NULL;
		goto err;
  }


	if (!(m->ctx = SSL_CTX_new(meth))) {
    tb_warn("tb_initSSL: Cannot create new SSL context\n");
    ERR_print_errors_fp(stderr);
		XSock(S)->status = TB_BROKEN;
		return TB_ERR; 
  }

	if(tb_errorlevel == TB_DEBUG) SSL_CTX_set_info_callback(m->ctx,info_cb);

  if(m->pwd) {
		SSL_CTX_set_default_passwd_cb(m->ctx, pass_cb);
		SSL_CTX_set_default_passwd_cb_userdata(m->ctx, S);
	}
	

	if(m->cert ) {
		if(SSL_CTX_use_certificate_file(m->ctx, m->cert, SSL_FILETYPE_PEM) <= 0) {
			ERR_print_errors_fp(stderr);
			goto err;
		}
		if (SSL_CTX_use_PrivateKey_file(m->ctx, m->cert, SSL_FILETYPE_PEM) <= 0) {
			tb_error("tb_initSSL: Unable to get private key from '%s'\n", 
							 m->cert);
			ERR_print_errors_fp(stderr);
			goto err;
		}
		tb_info("privkey loaded\n");		
		if (!SSL_CTX_check_private_key(m->ctx)) {
			tb_error("tb_initSSL: Private key does not match the certificate public key\n");
			goto err;
		}
		tb_info("tb_initSSL: privkey validated\n");
		tb_info("tb_initSSL: certificate loaded\n");
	}

	if(mode == SSL_CLIENT) {
		SSL_CTX_set_session_cache_mode(m->ctx, SSL_SESS_CACHE_CLIENT);
	} else {
		SSL_CTX_set_session_cache_mode(m->ctx, SSL_SESS_CACHE_SERVER);
		SSL_CTX_set_session_id_context(m->ctx,  "try this one", 12);
	}

  if(m->CA_file || m->CA_path) {
		tb_info("tb_initSSL: loading CAs ...\n");
    if(!SSL_CTX_load_verify_locations(m->ctx, m->CA_file, m->CA_path)) {
			XSock(S)->status = TB_BROKEN;
      tb_warn("tb_initSSL: Cannot load verify locations %s and %s\n",
              m->CA_file, m->CA_path);
      ERR_print_errors_fp(stderr);
			goto err;
    }
		tb_info("tb_initSSL: CA  <%s/%s> loaded\n", m->CA_path, m->CA_file);
		SSL_CTX_set_verify(m->ctx, SSL_VERIFY_PEER, verify_cb);
		SSL_CTX_set_default_verify_paths(m->ctx);
  }

 /* Create and configure SSL connection. */

  if (!(m->cx = (SSL *)SSL_new(m->ctx))) {
    tb_warn("tb_initSSL: Cannot create new SSL context\n");
    ERR_print_errors_fp(stderr);
		goto err;
  }
	tb_info("tb_initSSL: ssl ctx initialized\n");

  /* Use OpenSSL ciphers -v to see the cipher strings and their SSL
   * versions, key exchange, authentication, encryption, and message
   * digest algorithms, and key length restrictions.  See the OpenSSL
   * for the syntax to combine ciphers, e.g. !SSLv2:RC4-MD5:RC4-SHA.
   * If you don't specify anything, you get the same as "DEFAULT", which
   * means "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP".
   */
  if(m->cipher) {
    if(!SSL_CTX_set_cipher_list(m->ctx, m->cipher)) {
      tb_warn("tb_inittSSL: Cannot use cipher list %s\n", m->cipher);
			goto err;
    }
		tb_info("tb_initSSL: cipher set to <%s>\n", m->cipher);
	}


	tb_info("tb_initSSL out\n");
	return TB_OK;

 err:
	// fixme: context not totally freed (CA_path, pwd ...)
	SSL_CTX_free(m->ctx);
	m->ctx = NULL;
	XSock(S)->status = TB_BROKEN;
	return TB_ERR;
}
Beispiel #12
0
int main(int argc, char **argv) {
	Vector_t V, W;
	String_t S, T;

	void *p;

	char str[] = "10803551 10817246 12120887 13158625 13158627 13158628 13158630 13158632 13158633 13158634 13158635 13158637";

	char str2[] = "9175671 9205348 9261196 9327975 9328060 9330988 9331049 9334260 9387462 9387554 9439690 9440498 9570101 9570134 10086719 10103019 10465797 10466474 10466665 10584336 10586793 10600940 10631836 10634039 10654429 10827350 10829669 10838160 11139404 11142142 11178380 11178385 11475166 11475684 11507366 13070161 13073652 13089439 13101587";

	setenv("TB_MEM_DEBUG", "vector.dbg", 1);
	//	tb_memDebug();

	/*
	tb_setGetNameFnc(getmyname);
	tb_setTraceFnc(my_trace);

	tb_errorlevel = TB_DEBUG;
	*/


	V = tb_Vector();
	tb_tokenize(V, str, " ", 0);
	tb_Dump(V);
	while(tb_getSize(V)) {
		tb_Object_t O = tb_Pop(V);
		tb_Free(O);
		//		tb_Dump(V);
	}
	//	tb_Dump(V);
	tb_Clear(V);
	tb_tokenize(V, str2, " ", 0);
	tb_Dump(V);
	while(tb_getSize(V)) {
		tb_Object_t O = tb_Pop(V);
		tb_Free(O);
		tb_Dump(V);
	}
	tb_Dump(V);

	tb_Free(V);

	V = tb_Vector();

	S = tb_String("%s", "1/2/3/4/5/6/7/8/9");
	tb_Dump(S);
	tb_warn("tb_tokenize rc = %d\n", 
					 tb_tokenize(V, tb_toStr(S), "/", 0));
	tb_Dump(V);




	{
		Vector_t W;
		//		tb_errorlevel = TB_DEBUG;

		tb_Free(S);
		S = tb_Marshall(V);
		fprintf(stderr, "-->\n%s\n<--\n", S2sz(S));
		W = tb_unMarshall(S);
		tb_Dump(W);
	}



	tb_StrAdd(tb_Clear(S), 0, "%s", "Blip"); 

	T = tb_String("%s", "un deux 'trois quatre' ' cinq"); 

	tb_Clear(V);
	tb_Dump(V);
	tb_tokenize(V, tb_toStr(S), "/", 0);
	tb_Dump(V);


	fprintf(stderr, "#0: [%s]\n", tb_toStr(V,0));


	W = tb_Vector();
	tb_tokenize(W, tb_toStr(T), " ", TK_ESC_QUOTES);
	tb_Dump(W);

	//	exit(0);

	tb_Replace(W, S, 5);
	tb_Replace(W, S, 2);
	tb_Dump(W);
	tb_Dump(tb_Pop(W));
	tb_Dump(W);
	tb_Dump(tb_Shift(W));
	tb_Dump(W);
	tb_StrAdd(tb_Clear(T), 0, "%s", "Bloup"); 
	tb_Unshift(W, T);
	tb_Dump(W);
	tb_Free(V);
	V = tb_Clone(W);
	tb_Dump(V);
	tb_Dump(tb_Sort(V, NULL));
	tb_Dump(tb_Reverse(V));
	tb_Free(tb_Get(V, 1));
	tb_Dump(V);
	tb_Remove(V, 0);
	tb_Dump(V);
	tb_trace(TB_WARN, "2str <%s>\n", tb_toStr(V,0));
	tb_Free(V);
	tb_Free(S);

	fm_Dump();
	
	//	*/
	return 0;
}
Beispiel #13
0
int main(int argc, char **argv) {

	int test_udp_ip   = 0;
	int test_udp_unix = 0;
	int test_tcp_ip   = 1;
	int test_tcp_unix = 1;

/* 	tb_profile("Version: %s", tb_getVersion());  */
/* 	tb_profile("Build: %s", tb_getBuild());  */

	if(test_udp_ip) {
		Socket_t Serv    = tb_Socket(TB_UDP_IP, "localhost", 55551);
		Socket_t Client  = tb_Socket(TB_UDP_IP, "localhost", 55552);

		String_t S  = tb_String(NULL); 
		pthread_t pt;

		tb_profile(" ------------------- test UDP/IP --------\n");
		tb_initServer(Serv, NULL, NULL);

		pthread_create(&pt, NULL, (void *)udp_ip, NULL); 
		pthread_detach(pt);

		sleep(1);

		tb_Connect(Client, 1, 1);
		
		if(tb_getSockStatus(Client) == TB_CONNECTED) {
			tb_writeSock(Client, "TEST UDP/IP");
			if( tb_readSock(Serv, S, 500) >0) {
				tb_warn("client: got <%S>\n", S);
			}
		} else {
			tb_warn("Not connected !!\n");
		}

		tb_Free(S);
		tb_Free(Serv);
		tb_Free(Client);
	}

	if(test_udp_unix) {
		Socket_t Serv    = tb_Socket(TB_UDP_UX, "/tmp/ux-sock.1", 0);
		Socket_t Client  = tb_Socket(TB_UDP_UX, "/tmp/ux-sock.2", 0);

		String_t S  = tb_String(NULL); 
		pthread_t pt;

		tb_profile(" ------------------- test UDP/UNIX --------\n");

		tb_Dump(Serv);
		tb_Dump(Client);
		tb_initServer(Serv, NULL, NULL);
		tb_Dump(Serv);

		pthread_create(&pt, NULL, (void *)udp_ux, NULL); 
		pthread_detach(pt);

		sleep(1);
		

		tb_Connect(Client, 1, 1);
		tb_Dump(Client);

		if(tb_getSockStatus(Client) == TB_CONNECTED) {
			tb_writeSock(Client, "TEST UDP/UNIX");
			if( tb_readSock(Serv, S, 500) >0) {
				tb_warn("client: got <%S>\n", S);
			}
		} else {
			tb_warn("Not connected !!\n");
		}

		tb_Free(S);
		tb_Free(Serv);
		tb_Free(Client);
	}

	if(test_tcp_ip) {
		//Socket_t Serv    = tb_Socket(TB_TCP_IP, "10.33.100.38", 55553);
		Socket_t Serv    = tb_Socket(TB_TCP_IP, "", 55553);
		//Socket_t Client  = tb_Socket(TB_TCP_IP, "10.33.100.38", 55553);
		//		Socket_t Client  = tb_Socket(TB_TCP_IP, "", 55553);
		//		String_t S  = tb_String(NULL); 
		pthread_t pt;
		int i;

		tb_errorlevel = TB_INFO;
		tb_profile(" ------------------- test TCP/IP --------\n");

		tb_initServer(Serv, callback, NULL);
		// setup max allowed threads to 5
		tb_setServMAXTHR(Serv, 5);


		pthread_create(&pt, NULL, tb_Accept, Serv); 
		pthread_detach(pt);

		for(i=0; i<1; i++) {
			pthread_create(&pt, NULL, tst_client, NULL); 
			pthread_detach(pt);
		}

		/*
		tb_Connect(Client, 1, 1);
		
		if(tb_getSockStatus(Client) == TB_CONNECTED) {
			tb_writeSock(Client, "TEST TCP/IP");
			if( tb_readSock(Client, S, 500) >0) {
				tb_warn("client: got <%s>\n", S2sz(S));
			}
		} else {
			tb_trace(TB_WARN, "Not connected !!\n");
		}

		tb_Free(S);
		*/

		sleep(12);
		tb_stopServer(Serv);
		tb_Free(Serv);
		//		tb_Free(Client);
	}

	if(test_tcp_unix) {
		Socket_t Serv    = tb_Socket(TB_TCP_UX, "/tmp/ux-sock.1", 0);
		Socket_t Client  = tb_Socket(TB_TCP_UX, "/tmp/ux-sock.1", 0);
		String_t S  = tb_String(NULL); 
		pthread_t pt;

		tb_profile(" ------------------- test TCP/UNIX --------\n");

		tb_initServer(Serv, callback, NULL);

		tb_Dump(Serv);
		tb_Dump(Client);


		pthread_create(&pt, NULL, tb_Accept, Serv); 
		pthread_detach(pt);

		sleep(2);
 
		tb_Connect(Client, 1, 1);
		
		if(tb_getSockStatus(Client) == TB_CONNECTED) {
			int rc;
			tb_warn("client: connected\n");
			rc = tb_writeSock(Client, "TEST TCP/UNIX");
			tb_warn("writesock : %d\n", rc);
			if((rc = tb_readSock(Client, S, 500)) >0) {
				tb_warn("client: got <%S>\n", S);
			} else {
				tb_warn("readsock : %d\n", rc);
			}
		} else {
			tb_warn("Not connected !!\n");
		}

		tb_stopServer( Serv );
		tb_Free( Serv );
		tb_Free( Client );
		tb_Free( S );
	}

	return 0;
}
Beispiel #14
0
int main(int argc, char **argv) {
	Hash_t H, Z, T;
	String_t S;
	Vector_t V = NULL;
	int i;
	char key[10];
	
//	tb_errorlevel = TB_DEBUG;

	while(1) {
		H = tb_Hash();
		tb_Dump(H);

		Z = tb_HashX(KT_POINTER, 0);


		//		tb_HashFreeze(H);

		tb_profile("create 100 entries :\n");
		for(i = 0; i < 100; i++) {
			sprintf(key, "%04d", i);
			tb_Replace(H, tb_String("test value %04d", i), key);
			S = tb_String("test value %04d", i);
			tb_Replace(Z, S, S);
		}
		tb_profile("done\n");
		//		exit(0);
		tb_Dump(Z);

		//		tb_HashThaw(H);
		//		tb_Dump(H);

		tb_profile("Clone H:\n");

		Z = tb_Clone(H);
		tb_profile("done\n");

		tb_profile("Clear H:\n");
		tb_Clear(H);
		tb_profile("done\n");
		tb_Dump(H);


		tb_warn("exists key '0001' in H ? : %d ", tb_Exists(H, "0001"));
		tb_warn("exists key '0001' in Z ? : %d ", tb_Exists(Z, "0001"));

		tb_profile("Peek Z{'0002'} :\n");
		tb_Dump(tb_Get(Z, "0002"));
		tb_profile("try to free  Z{'0002'} :\n");
		tb_Free(tb_Get(Z, "0002"));
		tb_Dump(tb_Get(Z, "0002"));
		tb_profile("Get Z{'0002'} :\n");
		tb_Dump(S = tb_Take(Z, "0002"));

		tb_Dump(tb_Get(Z, "0001"));
		tb_warn("exists key '0002' in Z ? :%d ", tb_Exists(Z, "0002"));
		tb_Dump(S);
		tb_Remove(Z, "0003");
		tb_warn("exists key '0003' in Z ? :%d ", tb_Exists(Z, "0003"));

		V = tb_HashKeys(Z);
		tb_warn("Got %d keys", tb_getSize(V));


		//		tb_HashNormalize(Z);
		//		tb_profile("tb_errno = %d\n", tb_errno);



		//-->

		tb_Insert(H, tb_String("ABC"), "abC");
		tb_Insert(H, tb_String("DEF"), "AbC");

		//		tb_profile("tb_errno = %d\n", tb_errno);

		tb_Replace(H, tb_String("GHI"), "aBc");

		tb_Dump(H);
		tb_Free(H);

		//		tb_HashNormalize(H);

		H = tb_HashX(KT_STRING_I, 0);

		tb_Insert(H, tb_String("ABC"), "abC");

		tb_Free(S);
		if( tb_Insert(H, S = tb_String("DEF"), "AbC") != TB_OK ) {
			tb_Free(S);
		}


		// --


		//		tb_profile("tb_errno = %d\n", tb_errno);


		tb_Replace(H, tb_String("GHI"), "aBc");

		tb_Dump(H);

		tb_Free(V);


		tb_Clear(H);
		tb_Clear(Z);
		T = tb_Hash();


		//		tb_errorlevel = TB_NOTICE;
		tb_profile("add in H");
		S = tb_String("A");

		tb_Replace(H, S, "first");
		tb_Dump(H);
		tb_profile("add same in Z");
		tb_Replace(Z, tb_Get(H, "first"), "second");
		tb_Dump(Z);
		tb_profile("add same in T");
		tb_Replace(T, tb_Get(H, "first"), "third");
		tb_Dump(T);
		tb_profile("remove from H");
		tb_Remove(H, "first");
		tb_Dump(H);
		tb_Dump(S);
		tb_profile("free H");
		tb_Free(H);
		tb_Dump(S);

		tb_profile("free T");
		tb_Free(T);
		tb_Dump(S);
		tb_profile("show Z");
		tb_Dump(Z);

		{
			XmlElt_t X;
			Hash_t W;
			//			tb_errorlevel = TB_DEBUG;
			
			tb_Clear(S);
			tb_profile("marshall --------------------");
			X = tb_Marshall(Z);
			tb_Dump(X);
			tb_profile("unmarshall --------------------");
			W = tb_unMarshall(X);
			tb_Dump(W);
		}


		tb_Free(Z);




#if 1
		break;
#endif
	}
	return 0;
}