Пример #1
0
GEN
chartoGENstr(char c)
{
  GEN x = cgetg(2, t_STR);
  char *t = GSTR(x);
  t[0] = c; t[1] = 0; return x;
}
Пример #2
0
static GVariant*
_field_to_gvariant(gpointer bean, guint position)
{
	gpointer pf = FIELD(bean, position);

	if (!_bean_has_field(bean, position))
		return g_variant_new_tuple(NULL, 0);

	switch (DESCR_FIELD(bean,position)->type) {
		case FT_BOOL:
			return g_variant_new_byte(*((gboolean*)pf) ? 1 : 0);
		case FT_INT:
			return g_variant_new_int64(*((gint64*)pf));
		case FT_REAL:
			return g_variant_new_double(*((gdouble*)pf));
		case FT_TEXT:
			if (!*((gpointer*)(pf)))
				return g_variant_new_tuple(NULL, 0);
			return g_variant_new_string(GSTR(pf)->str);
		case FT_BLOB:
			if (!*((gpointer*)(pf)))
				return g_variant_new_tuple(NULL, 0);
			return _gba_to_gvariant(GBA(pf));
		default:
			g_assert_not_reached();
			break;
	}

	g_assert_not_reached();
	return NULL;
}
Пример #3
0
void GServerBaseConsole::ProcessPoll()
{
	int count=poll_manager.Poll();

	if(count)
	{	
		int a;

		a = (int)GetTime() ^ count;
		truerng.AddEntropy((char*)&a, truerng.Rnd(4), EENTROPY_NET_EVENT);

		for (a=0;a<count;a++)
		{
			epoll_event * ev=poll_manager.GetEvent(a);
			GSocket * s=reinterpret_cast<GSocket*>(ev->data.ptr);
			switch(GetServiceTypeBasic(s->GetServiceType()))
			{
			case ESTAcceptBase:AnalizeAccept(ev);break;
			case ESTClientBase:AnalizeClient(ev);break;
			case ESTAcceptBaseConsole:AnalizeAcceptConsole(ev);break;
			case ESTClientBaseConsole:AnalizeClientConsole(ev);break;
			case ESTInternalClient:AnalizeServerClient(ev);break;
			default:
				{SRAP(ERROR_UNKNOWN);RAPORT("%ld - UNKNOWN SERVICE",GSTR(ERROR_UNKNOWN));};
				break;
			}
		}
		poll_manager.ForceWritePoll();
	}
}
Пример #4
0
/* return the first n0 chars of s as a GEN [s may not be 0-terminated] */
GEN
strntoGENstr(const char *s, long n0)
{
  long n = nchar2nlong(n0+1);
  GEN x = cgetg(n+1, t_STR);
  char *t = GSTR(x);
  strncpy(t, s, n0); t[n0] = 0; return x;
}
Пример #5
0
static gpointer
_row_to_bean(const struct bean_descriptor_s *descr, sqlite3_stmt *stmt)
{
	const struct field_descriptor_s *fd;
	gpointer res;
	int col, s;

	res = _bean_create(descr);

	for (fd=descr->fields; fd->name ;fd++) {
		col = fd->position;
#if 0
		GRID_TRACE2("SQL column[%d,%d/%s,%s] field[%d,%ld,%d/%s,%s]",
				col,
				sqlite3_column_type(stmt, col),
				sqlite_strtype(sqlite3_column_type(stmt, col)),
				sqlite3_column_name(stmt, col),
				fd->position,
				fd->offset,
				fd->type,
				bean_strtype(fd->type),
				fd->name);
#endif
		if (sqlite3_column_type(stmt, col) == SQLITE_NULL) {
			_bean_del_field(res, fd->position);
			continue;
		}

		_bean_set_field(res, fd->position);
		gpointer pf = ((guint8*)res) + descr->offset_fields + fd->offset;
		switch (fd->type) {
			case FT_BOOL:
				*((gboolean*)pf) = sqlite3_column_int(stmt, col);
				continue;
			case FT_INT:
				*((gint64*)pf) = sqlite3_column_int64(stmt, col);
				continue;
			case FT_REAL:
				*((gdouble*)pf) = sqlite3_column_double(stmt, col);
				continue;
			case FT_TEXT:
				s = sqlite3_column_bytes(stmt, col);
				g_string_append_len(GSTR(pf), (const gchar*)sqlite3_column_text(stmt, col), s);
				continue;
			case FT_BLOB:
				s = sqlite3_column_bytes(stmt, col);
				g_byte_array_append(GBA(pf), (guint8*)sqlite3_column_blob(stmt, col), s);
				continue;
			default:
				g_assert_not_reached();
				continue;
		}
	}

	return res;
}
Пример #6
0
/* assume A or B is a t_STR */
static GEN
strconcat(GEN x, GEN y)
{
  size_t l, lx;
  char *sx = GENtostr_unquoted(x);
  char *sy = GENtostr_unquoted(y), *str;
  lx = strlen(sx);
  l = nchar2nlong(lx + strlen(sy) + 1);
  x = cgetg(l + 1, t_STR); str = GSTR(x);
  strcpy(str,   sx);
  strcpy(str+lx,sy); return x;
}
Пример #7
0
static GEN
catmanySTR(GEN y1, GEN y2)
{
  long L = 1; /* final \0 */
  GEN z, y;
  char *s;
  for (y = y1; y <= y2; y++)
  {
    char *c = GSTR( gel(y,0) );
    L += strlen(c);
  }
  z = cgetg(nchar2nlong(L)+1, t_STR);
  s = GSTR(z);
  for (y = y1; y <= y2; y++)
  {
    char *c = GSTR( gel(y,0) );
    long nc = strlen(c);
    if (nc) { (void)strncpy(s, c, nc); s += nc; }
  }
  *s = 0; return z;
}
Пример #8
0
GSocketConsole * GServerBaseConsole::SocketConsoleAdd()
{
	GSocketConsole * socket=NULL;
	try{
		socket=new GSocketConsole();
		SRAP(INFO_CONSOLE_NEW);
	}
	catch(const std::bad_alloc &)
	{
		SRAP(ERROR_CONSOLE_NEW_ALLOCATION_ERROR);
		RAPORT(GSTR(ERROR_CONSOLE_NEW_ALLOCATION_ERROR));
		socket=NULL;
	};
	if (socket)
	{
		socket->socketid = seq_socket_id++;
		socket_console_map.insert(pair<INT32, GSocket *>(socket->socketid, socket));
	}
	return socket;
}
Пример #9
0
static int
extract_selector_ok(long lx, GEN L)
{
  long i, l;
  switch (typ(L))
  {
    case t_INT: {
      long maxj;
      if (!signe(L)) return 1;
      l = lgefint(L)-1;
      maxj = BITS_IN_LONG - bfffo(*int_MSW(L));
      return ((l-2) * BITS_IN_LONG + maxj < lx);
    }
    case t_STR: {
      long first, last, cmpl;
      return get_range(GSTR(L), &first, &last, &cmpl, lx);
    }
    case t_VEC: case t_COL:
      l = lg(L);
      for (i=1; i<l; i++)
      {
        long j = itos(gel(L,i));
        if (j>=lx || j<=0) return 0;
      }
      return 1;
    case t_VECSMALL:
      l = lg(L);
      for (i=1; i<l; i++)
      {
        long j = L[i];
        if (j>=lx || j<=0) return 0;
      }
      return 1;
  }
  return 0;
}
Пример #10
0
bool GFireWall::Test(const in_addr addr)
{
	INT32 a;
	a=0;
	memcpy(&a,&addr,4);
	if(a==0 || a==1) 
	{
		SRAP(INFO_FIREWALL_BAD_ADDR);
		RAPORT(GSTR(INFO_FIREWALL_BAD_ADDR));
		return false;
	}

	if(global_serverconfig->firewall.use)
	{
		dense_hash_set<in_addr, hash_addr, eq_addr>::iterator it;
		it=wall.find(addr);
		if(it!=wall.end()) return false;
		return true;
	}

	if(global_serverconfig->firewall.use_limits)
	{
		dense_hash_map<const in_addr, int, hash_addr, eq_addr>::iterator it;

		it=minute_1[last_minute_1].find(addr);
		if(it!=minute_1[last_minute_1].end())
		{
			minute_1[last_minute_1][addr]++;
		}
		else
		{
			minute_1[last_minute_1].insert(pair<in_addr,int>(addr,1));
		}

		it=minutes_5[last_minutes_5].find(addr);
		if(it!=minutes_5[last_minutes_5].end())
		{
			minutes_5[last_minutes_5][addr]++;
		}
		else
		{
			minutes_5[last_minutes_5].insert(pair<in_addr,int>(addr,1));
		}


		u_int hit_minute_1=0;
		u_int hit_minutes_5=0;
		u_int hit_minutes_15=0;
		u_int hit_minutes_60=0;

		hit_minute_1=minute_1[GetMinute1Back(0)][addr];
		int a;
		for (a=0;a<5;a++) hit_minutes_5+=minute_1[GetMinute1Back(a)][addr];
		for (a=0;a<3;a++) hit_minutes_15+=minutes_5[GetMinutes5Back(a)][addr];
		for (a=0;a<12;a++) hit_minutes_60+=minutes_5[GetMinutes5Back(a)][addr];

		if(global_serverconfig->firewall.limit1)
		if(hit_minute_1>global_serverconfig->firewall.limit1) 
		{
			SRAP(INFO_FIREWALL_1);
			RAPORT("%s IP:%s, TRY: %d",GSTR(INFO_FIREWALL_1),inet_ntoa(addr),hit_minute_1);
			return false;
		}
		if(global_serverconfig->firewall.limit5)
		if(hit_minutes_5>global_serverconfig->firewall.limit5) 
		{
			SRAP(INFO_FIREWALL_5);
			RAPORT("%s IP:%s, TRY: %d",GSTR(INFO_FIREWALL_5),inet_ntoa(addr),hit_minutes_5);
			return false;
		}
		if(global_serverconfig->firewall.limit15)
		if(hit_minutes_15>global_serverconfig->firewall.limit15) 
		{
			SRAP(INFO_FIREWALL_15);
			RAPORT("%s IP:%s, TRY: %d",GSTR(INFO_FIREWALL_15),inet_ntoa(addr),hit_minutes_15);
			return false;
		}
		if(global_serverconfig->firewall.limit60)
		if(hit_minutes_60>global_serverconfig->firewall.limit60) 
		{
			SRAP(INFO_FIREWALL_60);
			RAPORT("%s IP:%s, TRY: %d",GSTR(INFO_FIREWALL_60),inet_ntoa(addr),hit_minutes_60);
			return false;
		}
	}
	return true;
}
Пример #11
0
long
eval_mnemonic(GEN str, const char *tmplate)
{
  pari_sp av=avma;
  ulong retval = 0;
  const char *etmplate = NULL;
  const char *arg;

  if (typ(str)==t_INT) return itos(str);
  if (typ(str)!=t_STR) pari_err_TYPE("eval_mnemonic",str);

  arg=GSTR(str);
  etmplate = strchr(tmplate, '\n');
  if (!etmplate)
    etmplate = tmplate + strlen(tmplate);

  while (1)
  {
    long numarg;
    const char *e, *id;
    const char *negated;                /* action found with 'no'-ID */
    int negate;                 /* Arg has 'no' prefix removed */
    ulong l, action = 0, first = 1, singleton = 0;
    char *buf, *inibuf;
    static char b[80];

    while (isspace((int)*arg)) arg++;
    if (!*arg)
      break;
    e = arg;
    while (IS_ID(*e)) e++;
    /* Now the ID is whatever is between arg and e. */
    l = e - arg;
    if (l >= sizeof(b))
      pari_err(e_MISC,"id too long in a stringified flag");
    if (!l)                             /* Garbage after whitespace? */
      pari_err(e_MISC,"a stringified flag does not start with an id");
    strncpy(b, arg, l);
    b[l] = 0;
    arg = e;
    e = inibuf = buf = b;
    while (('0' <= *e) && (*e <= '9'))
      e++;
    if (*e == 0)
      pari_err(e_MISC,"numeric id in a stringified flag");
    negate = 0;
    negated = NULL;
find:
    id = tmplate;
    while ((id = strstr(id, buf)) && id < etmplate)
    {
      if (IS_ID(id[l])) {       /* We do not allow abbreviations yet */
        id += l;                /* False positive */
        continue;
      }
      if ((id >= tmplate + 2) && (IS_ID(id[-1])))
      {
        const char *s = id;

        if ( !negate && s >= tmplate+3
            && ((id[-1] == '_') || (id[-1] == '-')) )
          s--;
        /* Check whether we are preceeded by "no" */
        if ( negate             /* buf initially started with "no" */
            || (s < tmplate+2) || (s[-1] != 'o') || (s[-2] != 'n')
            || (s >= tmplate+3 && IS_ID(s[-3]))) {
          id += l;              /* False positive */
          continue;
        }
        /* Found noID in the template! */
        id += l;
        negated = id;
        continue;               /* Try to find without 'no'. */
      }
      /* Found as is */
      id += l;
      break;
    }
    if ( !id && !negated && !negate
        && (l > 2) && buf[0] == 'n' && buf[1] == 'o' ) {
      /* Try to find the flag without the prefix "no". */
      buf += 2; l -= 2;
      if ((buf[0] == '_') || (buf[0] == '-')) { buf++; l--; }
      negate = 1;
      if (buf[0])
        goto find;
    }
    if (!id && negated) /* Negated and AS_IS forms, prefer AS_IS */
    {
      id = negated;     /* Otherwise, use negated form */
      negate = 1;
    }
    if (!id)
      pari_err(e_MISC,"Unrecognized id '%s' in a stringified flag", inibuf);
    if (singleton && !first)
      pari_err(e_MISC,"Singleton id non-single in a stringified flag");
    if (id[0] == '=') {
      if (negate)
        pari_err(e_MISC,"Cannot negate id=value in a stringified flag");
      if (!first)
        pari_err(e_MISC,"Assign action should be first in a stringified flag");
      action = A_ACTION_ASSIGN;
      id++;
      if (id[0] == '=') {
        singleton = 1;
        id++;
      }
    } else if (id[0] == '^') {
      if (id[1] != '~')
        pari_err(e_MISC, "Unrecognized action in a template");
      id += 2;
      if (negate)
        action = A_ACTION_SET;
      else
        action = A_ACTION_UNSET;
    } else if (id[0] == '|') {
      id++;
      if (negate)
        action = A_ACTION_UNSET;
      else
        action = A_ACTION_SET;
    }

    e = id;

    while ((*e >= '0' && *e <= '9')) e++;
    while (isspace((int)*e))
      e++;
    if (*e && (*e != ';') && (*e != ','))
      pari_err(e_MISC, "Non-numeric argument of an action in a template");
    numarg = atol(id);          /* Now it is safe to get it... */
    switch (action) {
    case A_ACTION_SET:
      retval |= numarg;
      break;
    case A_ACTION_UNSET:
      retval &= ~numarg;
      break;
    case A_ACTION_ASSIGN:
      retval = numarg;
      break;
    default:
      pari_err(e_MISC,"error in parse_option_string");
    }
    first = 0;
    while (isspace((int)*arg))
      arg++;
    if (*arg && !(ispunct((int)*arg) && *arg != '-'))
      pari_err(e_MISC,"Junk after an id in a stringified flag");
    /* Skip punctuation */
    if (*arg)
      arg++;
  }
  avma=av;
  return retval;
}
Пример #12
0
void GServerBaseConsole::AnalizeAcceptConsole(epoll_event * ev)
{
	if(ev->events & (EPOLLERR|EPOLLHUP)) 
	{
		SRAP(ERROR_CONSOLE_ACCEPT_ERROR);
		RAPORT(GSTR(ERROR_CONSOLE_ACCEPT_ERROR));
		return;
	}
	unsigned int			accept_count=0;
	SOCKET					client_sock=0;
	struct	sockaddr_in		client_addr;
	DWORD					addr;
	while (client_sock != -1 && 
		accept_count<global_serverconfig->net.accept_limit && 
		poll_manager.Free())
	{
		socklen_t sin_size=sizeof(struct sockaddr_in);
		GSocketConsole * s=reinterpret_cast<GSocketConsole*>(ev->data.ptr);
		SOCKET fd=s->GetSocket();
		client_sock=accept(fd,(struct sockaddr *)&client_addr,&sin_size);
		if(client_sock==-1) break;
		memcpy(&addr,&client_addr.sin_addr,4);

		accept_count++;
		SRAP(INFO_CONSOLE_ACCEPT_COUNT);

		GSocketConsole* socket=SocketConsoleAdd();
		if(socket)
		{
			GPollManager & pm=poll_manager;
			GMemoryManager & mm=memory_manager;
			socket->Init(raport_interface,client_sock,GetServiceTypeAssociate(s->GetServiceType()),&mm,&pm);
			socket->SetTimeConnection(clock.Get());
			if(socket->RegisterPoll())
			{
				socket->AllocateOut();
				socket->MemoryOut()<<GetServiceName(s->GetServiceType());
				socket->MemoryOut()<<lend;
				socket->Write();
			}
			else
			{
				SRAP(ERROR_CONSOLE_REGISTER_POLL);
				GSTR(ERROR_CONSOLE_REGISTER_POLL);
				SocketConsoleRemove(socket);
			}
		}
		else
		{
			closesocket(client_sock);
		}
	}
	if(client_sock != -1)
	{
		if(accept_count==global_serverconfig->net.accept_limit) 
		{
			SRAP(WARNING_SERVER_ACCEPT_LIMIT_EXCEED);
			RAPORT(GSTR(WARNING_SERVER_ACCEPT_LIMIT_EXCEED));
		}
		if(!poll_manager.Free()) 
		{
			SRAP(WARNING_SERVER_POLL_LIMIT_EXCEED);
			RAPORT(GSTR(WARNING_SERVER_POLL_LIMIT_EXCEED));
		}
	}
}