Exemple #1
0
int ipc_wait_for_server(u16 port)
{
    struct sockaddr_in sa;
    int fd;

    MZERO(sa);
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = inet_addr("127.0.0.1");
    sa.sin_port = port;

    if ((fd = sock_socket(SOCK_STREAM, 0, 0)) < 0)
    {
	rg_error(LERR, "failed creating socket");
	return -1;
    }
    
    /* Try to connect to server infinitely */ 
    while (1)
    {
 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) >= 0)
	    break;

	/* Sleep for 10ms before trying again */
	usleep(10000);
    }

    /* Once the server accepts the connection, it tries to send the client a
     * sync message. It will fail if we close the socket now. So we keep the
     * socket open until a sync is received, to keep the server happy. */
    ipc_client_sync(fd);

    socket_close(fd);
    
    return 0;
}
Exemple #2
0
char *
mstr(unsigned int *a)
{
	int k, n, r, sign;
	char c;

	if (str == NULL) {
		str = (char *) malloc(1000);
		len = 1000;
	}

	// estimate string size

	n = 10 * MLENGTH(a) + 2;

	if (n > len) {
		free(str);
		str = (char *) malloc(n);
		len = n;
	}

	sign = MSIGN(a);

	a = mcopy(a);

	k = len - 1;

	str[k] = 0;

	for (;;) {
		k -= 9;
		r = divby1billion(a);
		c = str[k + 9];
		sprintf(str + k, "%09d", r);
		str[k + 9] = c;
		if (MZERO(a))
			break;
	}

	// remove leading zeroes

	while (str[k] == '0')
		k++;

	if (str[k] == 0)
		k--;

	// sign

	if (sign == -1) {
		k--;
		str[k] = '-';
	}

	mfree(a);

	return str + k;
}
void
qsub(void)
{
	unsigned int *a, *ab, *b, *ba, *c;

	save();

	p2 = pop();
	p1 = pop();

	ab = mmul(p1->u.q.a, p2->u.q.b);
	ba = mmul(p1->u.q.b, p2->u.q.a);

	a = msub(ab, ba);

	mfree(ab);
	mfree(ba);

	// zero?

	if (MZERO(a)) {
		mfree(a);
		push(zero);
		restore();
		return;
	}

	b = mmul(p1->u.q.b, p2->u.q.b);

	c = mgcd(a, b);

	MSIGN(c) = MSIGN(b);

	p1 = alloc();

	p1->k = NUM;

	p1->u.q.a = mdiv(a, c);
	p1->u.q.b = mdiv(b, c);

	mfree(a);
	mfree(b);
	mfree(c);

	push(p1);

	restore();
}
/* normally should be called with src_ip==0 and src_port==0 */
int sock_socket(int type, u32 src_ip, u16 src_port)
{
    int sock = -1;
    struct sockaddr_in sa;

    if ((sock = socket(AF_INET, type, 0))<0)
	return rg_error(LERR, "sock_socket: failed socket()");
    MZERO(sa);
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = src_ip;
    sa.sin_port = src_port; 
    if (src_port)
    {
	int opt = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
	    (void *)&opt, sizeof(opt))<0)
	{
	    rg_error(LERR, "sock_socket: failed setsockopt(SO_REUSEADDR)");
	    goto Error;
	}
	if ((bind(sock, (struct sockaddr *)&sa, sizeof(sa)))<0)
	{
	    rg_error(LERR, "sock_socket: failed bind(): %s", strerror(errno));
	    goto Error;
	}
    }
    else
    {
	if ((bind(sock, (struct sockaddr *)&sa, sizeof(sa)))<0)
	{
	    rg_error(LERR, "sock_socket: failed bind()");
	    goto Error;
	}
    }
    
    rg_error(LDEBUG, "sock_socket: created fd %d for ip %s port %d", sock,
	inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
    return sock;
    
Error:
    if (sock>=0)
	close(sock);
    return -1;
}
Exemple #5
0
static int __ipc_connect_ip(u16 port, u32 addr, int quiet)
{
    struct sockaddr_in sa;
    int fd = -1;

    while (1)
    {
	if ((fd = sock_socket(SOCK_STREAM, 0, 0)) < 0)
	    return -1;
	
	MZERO(sa);
	sa.sin_family = AF_INET;
	sa.sin_addr.s_addr = addr;
	sa.sin_port = port;
 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
	{
	    socket_close(fd);
	    if (!quiet)
		rg_error(LERR, "failed ipc connect %m");
	    return -1;
	}
	/* Linux 2.4 kernel has a bug with localhost connections.
	 * After the server calls listen(), then the client calls
	 * connect() and returns with success immediately, before
	 * the server calls accept()!
	 * Therefore, a SYNC_STR command is used to make sure the connect
	 * succeeded.
	 * This has not been tested on Linux 2.2 whether it too has
	 * this bug. It probably does not exist on VxWorks, since VxWorks
	 * TCP/IP stack is completely different (its BSD based).
	 */
	if (!ipc_client_sync(fd))
	    break;
	socket_close(fd);
	if (!quiet)
	    rg_error(LWARN, "failed ipc connect sync - retrying");
	sys_sleep(1);
    }

    return fd;
}
Exemple #6
0
unsigned int *
mgcd(unsigned int *u, unsigned int *v)
{
	int i, k, n;
	unsigned int *t;

	if (MZERO(u)) {
		t = mcopy(v);
		MSIGN(t) = 1;
		return t;
	}

	if (MZERO(v)) {
		t = mcopy(u);
		MSIGN(t) = 1;
		return t;
	}

	u = mcopy(u);
	v = mcopy(v);

	MSIGN(u) = 1;
	MSIGN(v) = 1;

	k = 0;

	while ((u[0] & 1) == 0 && (v[0] & 1) == 0) {
		mshiftright(u);
		mshiftright(v);
		k++;
	}

	if (u[0] & 1) {
		t = mcopy(v);
		MSIGN(t) *= -1;
	} else
		t = mcopy(u);

	while (1) {

		while ((t[0] & 1) == 0)
			mshiftright(t);

		if (MSIGN(t) == 1) {
			mfree(u);
			u = mcopy(t);
		} else {
			mfree(v);
			v = mcopy(t);
			MSIGN(v) *= -1;
		}

		mfree(t);

		t = msub(u, v);

		if (MZERO(t)) {
			mfree(t);
			mfree(v);
			n = (k / 32) + 1;
			v = mnew(n);
			MSIGN(v) = 1;
			MLENGTH(v) = n;
			for (i = 0; i < n; i++)
				v[i] = 0;
			mp_set_bit(v, k);
			t = mmul(u, v);
			mfree(u);
			mfree(v);
			return t;
		}
	}
}
Exemple #7
0
getname(char *defname, char *defpasswd)

/* Let person identify themselves from w */
{
  register char ch;
  int     secondsLeft = 199, laststate;
  char    tempstr[40];
  LONG    lasttime;
  char   *namptr, *passptr;
  register int j;
  struct timeval timeout;
  fd_set  readfds;

  autolog = (*defpasswd && *defname) ? 1 : 0;

  MZERO(mystats, sizeof(struct stats));

  mystats->st_tticks = 1;
  for (j = 0; j < 95; j++)
    {
      mystats->st_keymap[j] = j + 32;
      mystats->st_keymap[j + 96] = j + 32 + 96;

#ifdef MOUSE_AS_SHIFT
      mystats->st_keymap[j + 192] = j + 32;
      mystats->st_keymap[j + 288] = j + 32;
      mystats->st_keymap[j + 384] = j + 32;
#endif
    }
  mystats->st_keymap[95] = 0;
  mystats->st_flags = ST_MAPMODE + ST_NAMEMODE + ST_SHOWSHIELDS +
      ST_KEEPPEACE + ST_SHOWLOCAL * 2 + ST_SHOWGLOBAL * 2;

  lasttime = time(NULL);

  if (ghoststart)
    return;

  tempname[0] = '\0';
  password1[0] = '\0';
  password2[0] = '\0';

  laststate = state = ST_GETNAME;
  displayStartup(defname);
  while (1)
    {
      handleWEvents(defname);

      if (!autolog)
  {

#ifndef HAVE_WIN32
    W_FullScreen(baseWin);
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
#else
    /* Since we don't have a socket to check on Win32 for windowing *
     * system events, we set the timeout to zero and effectively poll.
     * * Yes, I could do the correct thing and call *
     * WaitForMultipleObjects() etc. but I don't feel like it */
    timeout.tv_sec = 0;
    timeout.tv_usec = 100000;
#endif

    FD_ZERO(&readfds);
    FD_SET(sock, &readfds);
    if (udpSock >= 0)
      FD_SET(udpSock, &readfds);

#ifndef HAVE_WIN32
    FD_SET(W_Socket(), &readfds);
#endif

    if (SELECT(32, &readfds, 0, 0, &timeout) < 0)
      {
        perror("select");
        continue;
      }

    if (FD_ISSET(sock, &readfds)
        || (udpSock >= 0 && FD_ISSET(udpSock, &readfds)))
      readFromServer(&readfds);

#ifndef HAVE_WIN32
    if (FD_ISSET(W_Socket(), &readfds))
#else
    if (W_EventsPending())
#endif

      handleWEvents(defname);
  }
      else
  {
    readFromServer(&readfds);
  }

      if (isServerDead())
  {
    printf("Shit, we were ghostbusted\n");

#ifdef HAVE_XPM
    W_GalacticBgd(GHOST_PIX);
#endif

#ifdef AUTOKEY
    if (autoKey)
      W_AutoRepeatOn();
#endif

    terminate(0);
  }

      if (time(0) != lasttime)
  {
    lasttime++;
    secondsLeft--;
    showreadme();
    if (!autolog)
      {
        sprintf(tempstr, "Seconds to go: %d ", secondsLeft);
        W_WriteText(w, 100, 400, textColor, tempstr, strlen(tempstr),
        W_RegularFont);
      }
    if (secondsLeft == 0)
      {
        me->p_status = PFREE;
        printf("Timed Out.\n");

#ifdef AUTOKEY
        if (autoKey)
    W_AutoRepeatOn();
#endif

        terminate(0);
      }
  }
      if (state == ST_DONE)
  {
    W_ClearWindow(w);
    W_ClearWindow(mapw);
    return;
  }
      if (autolog)
  {
    switch (state)
      {
      case ST_GETNAME:
        tempname[0] = '\0';
        ch = 13;
        j = 0;
        break;

      case ST_GETPASS:
      case ST_MAKEPASS1:
      case ST_MAKEPASS2:
        ch = defpasswd[j++];
        if (ch == '\0')
    {
      j = 0;
      ch = 13;
    }
        break;

      default:
        break;
      }

    loginproced(ch, defname);

  }

      laststate = state;
    }
}
Exemple #8
0
static int
mprimef(unsigned int *n, unsigned int *q, int k)
{
	int i, j;
	unsigned int *t, *x, *y;

	// generate x

	t = mcopy(n);

	while (1) {
		for (i = 0; i < MLENGTH(t); i++)
			t[i] = rand();
		x = mmod(t, n);
		if (!MZERO(x) && !MEQUAL(x, 1))
			break;
		mfree(x);
	}

	mfree(t);

	// exponentiate

	y = mmodpow(x, q, n);

	// done?

	if (MEQUAL(y, 1)) {
		mfree(x);
		mfree(y);
		return 1;
	}

	j = 0;

	while (1) {

		// y = n - 1?

		t = msub(n, y);

		if (MEQUAL(t, 1)) {
			mfree(t);
			mfree(x);
			mfree(y);
			return 1;
		}

		mfree(t);

		if (++j == k) {
			mfree(x);
			mfree(y);
			return 0;
		}

		// y = (y ^ 2) mod n

		t = mmul(y, y);
		mfree(y);
		y = mmod(t, n);
		mfree(t);

		// y = 1?

		if (MEQUAL(y, 1)) {
			mfree(x);
			mfree(y);
			return 0;
		}
	}
}
unsigned int *
mfactor(unsigned int *n)
{
	unsigned int *r, *root, *t, *two, *x, *y;

	two = mint(2);

	root = msqrt(n);

	// y = 1;

	y = mint(1);

	// x = 2 isqrt(n) + 1

	t = madd(root, root);
	x = madd(t, y);
	mfree(t);

	// r = isqrt(n) ^ 2 - n

	t = mmul(root, root);
	r = msub(t, n);
	mfree(t);

	mfree(root);

	while (1) {

		if (MZERO(r)) {

			// n = (x - y) / 2

			t = msub(x, y);
			n = mdiv(t, two);
			mfree(t);

			mfree(r);
			mfree(x);
			mfree(y);
			mfree(two);

			return n;
		}

		// r = r + x

		t = madd(r, x);
		mfree(r);
		r = t;

		// x = x + 2

		t = madd(x, two);
		mfree(x);
		x = t;

		while (1) {

			// r = r - y

			t = msub(r, y);
			mfree(r);
			r = t;

			// y = y + 2

			t = madd(y, two);
			mfree(y);
			y = t;

			if (MSIGN(r) == -1 || MZERO(r))
				break;
		}
	}
}