DBD-NET

 view release on metacpan or  search on metacpan

dbagent/net.c  view on Meta::CPAN

REQ rq;
RES rep;
char ttt[16*1024];
int max_rows[10];

fcnt=0;

for(;;) {
n=netread(&rq,sizeof(REQ));
rq.req=ntohl(rq.req);
rq.len=ntohl(rq.len);
#if	0
printf("len:%d\n", rq.len);
#endif
if (rq.len > 0) {
  if (rq.len + sizeof(REQ) > sizeof(REQD)) {
#if	0
	p_err("buf size is too small");
#endif
	continue;
  }
  switch (rq.req) {
  	case NET_GETDBS:
		rep.res=getdbs(ttt, &n);
		if (!rep.res)
			puterr();
		else {
			rep.res=htonl(rep.res);
			netwrite(&rep,sizeof(RES));
		}
  		continue;
	case NET_CONNECTDB:
	{
		CONNECT_REQ crq;
		struct passwd *pwp;
		char *pwd;
		struct sockaddr_in frm;
		int adrlen=sizeof(frm);
		struct timeval tp;
		struct timezone tzp;
		char buf[100];

		gettimeofday(&tp, &tzp);
		strftime(buf,sizeof(buf),"%D %T", localtime(&tp.tv_sec));

		if (getpeername(netfd,&frm, &adrlen)<0) {
			p_error("Cannot get peer name");
		}
		netread(&crq, rq.len);
		printf("%s %s %s %s\n",buf, whereis(&frm), crq.login_name, crq.dbname);
		if (!(pwp=getpwnam(crq.login_name))) {
			reterr("Invalid login name '%s'",crq.login_name);
			close_exit();
		}
		pwd=pwp->pw_passwd;
		if (strcmp(crypt(crq.passwd,pwd), pwd)) {
			reterr("Incorrect passwd");
			close_exit();
		}
		setuid(pwp->pw_uid);
		setgid(pwp->pw_gid);
		rep.res=connect_db(crq.dbname);
		if (!rep.res)
			puterr();
		else {
			rep.res=htonl(rep.res);
			netwrite(&rep,sizeof(RES));
		}
		continue;
	}
	case NET_PREPARE:
	{
		netread(ttt,rq.len);
		rep.res=sql_prepare(ttt,&cursorn,&descn);
		if (!rep.res)
			puterr();
		else {
			PREPARE_REP pp;
			pp.cursorn=htonl(cursorn);
			pp.descn=htonl(descn);
			netwrite(&rep,sizeof(rep));
			netwrite(&pp,sizeof(PREPARE_REP));
			max_rows[cursorn]=16;
		}
		continue;
	}
	case NET_FETCH:
	{
		char *tt[MAXCOLUMN];
		int i,j, tlen,ofs,nrow;
		int colen[MAXCOLUMN],tcolen;
		char bf[128*1024] ;
		FETCH_REP qq;
		RES res;
		FETCH_REQ pp;
		int rtcol,datan,max_row;

		netread(&pp,sizeof(pp));
		cursorn=ntohl(pp.cursorn);
		/* Yes, it is dangerous if a row is too large, will use malloc */
		max_row=max_rows[cursorn]=128;
		for(datan=nrow=0;nrow<max_row;nrow++) {
			if (!(res.res=sql_fetch(cursorn,bf+datan,colen,&tcolen,
					&rtcol, &descn))) {
					break;
			}
			fcnt++;
			datan+=rtcol;
		}
		if (res.res) {
			max_row=sizeof(bf)/tcolen;
			max_rows[cursorn]=max_row;
		}
		qq.descn=htonl(descn);
		qq.tcolen=htonl(tcolen);
		qq.nrow=htonl(nrow);
		qq.datan=htonl(datan);
		if (nrow==0) {
			res.res=0;
			netwrite(&res,sizeof(res));
			continue;



( run in 1.360 second using v1.01-cache-2.11-cpan-5735350b133 )