Example #1
0
void
handleagentopen(Msg *m)
{
	int i;
	uint32_t remote;

	assert(m->type == SSH_SMSG_AGENT_OPEN);
	remote = getlong(m);
	debug(DBG_AUTH, "agent open %d\n", remote);

	for(i=0; i<nelem(achan); i++)
		if(achan[i].open == 0 && achan[i].needeof == 0 && achan[i].needclosed == 0)
			break;
	if(i == nelem(achan)){
		m = allocmsg(m->c, SSH_MSG_CHANNEL_OPEN_FAILURE, 4);
		putlong(m, remote);
		sendmsg(m);
		return;
	}

	debug(DBG_AUTH, "\tremote %d is local %d\n", remote, i);
	achan[i].open = 1;
	achan[i].needeof = 1;
	achan[i].needclosed = 1;
	achan[i].nlbuf = 0;
	achan[i].chan = remote;
	m = allocmsg(m->c, SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 8);
	putlong(m, remote);
	putlong(m, i);
	sendmsg(m);
}
Example #2
0
int main()
{
	long d = 1000000;
	putlong(d);
	putlong(d, 0);
	return 0;
}
Example #3
0
int mr_send(int fd, struct mr_params *params)
{
  u_long length;
  int written;
  int i, *argl;
  char *buf, *p;

  length = 16; /* length + version + opcode/status + argc */

  if (params->mr_argl)
    {
      argl = params->mr_argl;
      for (i = 0; i < params->mr_argc; i++)
	length += 8 + argl[i];
    }
  else
    {
      argl = malloc(params->mr_argc * sizeof(int));
      if (params->mr_argc && !argl)
	return ENOMEM;
      for (i = 0; i < params->mr_argc; i++)
	{
	  argl[i] = strlen(params->mr_argv[i]) + 1;
	  length += 8 + argl[i];
	}
    }

  buf = malloc(length);
  if (!buf)
    {
      if (!params->mr_argl)
	free(argl);
      return ENOMEM;
    }
  memset(buf, 0, length);

  putlong(buf + 4, MR_VERSION_2);
  putlong(buf + 8, params->u.mr_procno);
  putlong(buf + 12, params->mr_argc);

  for (i = 0, p = buf + 16; i < params->mr_argc; i++)
    {
      putlong(p, argl[i]);
      memcpy(p += 4, params->mr_argv[i], argl[i]);
      p += argl[i] + (4 - argl[i] % 4) % 4;
    }
  length = p - buf;
  putlong(buf, length);

  written = send(fd, buf, length, 0);
  free(buf);
  if (!params->mr_argl)
    free(argl);

  if (written != (int)length)
    return MR_ABORTED;
  else
    return MR_SUCCESS;
}
Example #4
0
void addentry(int *pos, int length, const char *name, FILE *wadfile)
{
  static char entryname8[8];

  putlong(*pos, wadfile);
  putlong(length, wadfile);
  *pos += length;
  strncpy(entryname8, name, 8);
  fwrite(entryname8, 8, 1, wadfile);
  if (!quiet)
    printf("Adding entry %s (%d bytes)\n", name, length);
}
Example #5
0
void
sendwindowsize(Conn *c, int nrow, int ncol, int width, int height)
{
	Msg *m;

	m = allocmsg(c, SSH_CMSG_WINDOW_SIZE, 4*4);
	putlong(m, nrow);
	putlong(m, ncol);
	putlong(m, width);
	putlong(m, height);
	sendmsg(m);
}
Example #6
0
File: sgilib.c Project: Minoos/gimp
int
sgiClose(sgi_t *sgip)	/* I - SGI image */
{
  int	i;		/* Return status */
  long	*offset;	/* Looping var for offset table */


  if (sgip == NULL)
    return (-1);

  if (sgip->mode == SGI_WRITE && sgip->comp != SGI_COMP_NONE)
  {
   /*
    * Write the scanline offset table to the file...
    */

    fseek(sgip->file, 512, SEEK_SET);

    for (i = sgip->ysize * sgip->zsize, offset = sgip->table[0];
         i > 0;
         i --, offset ++)
      if (putlong(offset[0], sgip) < 0)
        return (-1);

    for (i = sgip->ysize * sgip->zsize, offset = sgip->length[0];
         i > 0;
         i --, offset ++)
      if (putlong(offset[0], sgip) < 0)
        return (-1);
  };

  if (sgip->table != NULL)
  {
    free(sgip->table[0]);
    free(sgip->table);
  };

  if (sgip->length != NULL)
  {
    free(sgip->length[0]);
    free(sgip->length);
  };

  if (sgip->comp == SGI_COMP_ARLE)
    free(sgip->arle_row);

  i = fclose(sgip->file);
  free(sgip);

  return (i);
}
Example #7
0
END_TEST

START_TEST(test_read_putlong)
{
	uint32_t k;
	uint32_t l;
	char* p;
	int i;
	int j;

	for (i = 0; i < 32; i++) {
		p = (char*)&k;
		j = 0xf << i;

		putlong(&p, j);

		fail_unless(ntohl(k) == j,
				"Bad value on putlong for %d: %d != %d", i, ntohl(j), j);

		p = (char*)&k;
		readlong(NULL, &p, &l);

		fail_unless(l == j,
				"Bad value on readlong for %d: %d != %d", i, l, j);
	}
}
Example #8
0
void
copyout(Conn *c, int fd, int mtype)
{
	char buf[8192];
	int n, max, pid;
	Msg *m;

	max = sizeof buf;
	if(max > maxmsg - 32)	/* 32 is an overestimate of packet overhead */
		max = maxmsg - 32;
	if(max <= 0)
		sysfatal("maximum message size too small");
	
	switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
	case -1:
		sysfatal("fork: %r");
	case 0:
		break;
	default:
		atexitkill(pid);
		return;
	}

	while((n = read(fd, buf, max)) > 0){
		m = allocmsg(c, mtype, 4+n);
		putlong(m, n);
		putbytes(m, buf, n);
		sendmsg(m);
	}
	exits(nil);
}
Example #9
0
void
evermore80(Place pl, int baud)
{
	char buf[32], *s;
	long now, seconds, week;

	fprint(2, "Evermore80");

	time(&now);
	seconds = now - 315964800;
	week = (seconds / (7*24*3600));
	seconds = seconds %  (7*24*3600);
	s = buf;

	s = putbyte(s, 0x80);		/* message ID */
	s = putshort(s, week);		/* week number */
	s = putlong(s, seconds*100);	/* seconds */
	s = putshort(s, pl.lat*10.0);	/* latitude tenths degree */
	s = putshort(s, pl.lon*10.0);	/* longitude tenths degree */
	s = putshort(s, 100);		/* altitude meters */
	s = putshort(s, 0);		/* datumn ID */
	s = putbyte(s, 2);		/* warm start */
	s = putbyte(s, GGAon|GSAon|GSVon|RMCon|CRCon);
	switch(baud){
	case 4800:	s = putbyte(s, 0);	break;
	case 9600:	s = putbyte(s, 1);	break;
	case 19200:	s = putbyte(s, 2);	break;
	case 38400:	s = putbyte(s, 3);	break;
	default:
		sysfatal("Illegal baud rate");
	}

	evermoresend(buf, s - buf);
	fprint(2, "\n");
}
Example #10
0
File: dns.c Project: frekky/iodine
int
dns_encode_a_response(char *buf, size_t buflen, struct query *q)
/* Only used when iodined gets an A type query for ns.topdomain or www.topdomain */
/* Mostly same as dns_encode_ns_response() above */
{
	HEADER *header;
	int len;
	short name;
	char *ipp;
	char *p;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = 1;
	header->opcode = 0;
	header->aa = 1;
	header->tc = 0;
	header->rd = 0;
	header->ra = 0;

	p = buf + sizeof(HEADER);

	header->qdcount = htons(1);
	header->ancount = htons(1);

	/* pointer to start of name */
	name = 0xc000 | ((p - buf) & 0x3fff);

	/* Query section */
	putname(&p, buflen - (p - buf), q->name);	/* Name */
	CHECKLEN(4);
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */

	/* Answer section */
	CHECKLEN(12);
	putshort(&p, name);			/* Name */
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 4);			/* Data length */

	/* ugly hack to output IP address */
	ipp = (char *) &q->destination;
	CHECKLEN(4);
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *ipp);

	len = p - buf;
	return len;
}
Example #11
0
void main()
{
	unsigned long n;
	printf("Enter an unsigned long number. ");
	scanf("%lu",&n);
	putlong(n);
	putchar('\n');
}
Example #12
0
void putlong(unsigned long n)
{
	if(n<10)
	{
		putchar(n+'0');
		return;
	}
	putlong(n/10);
	putchar(n%10+'0');
}
Example #13
0
static void
send_ssh_smsg_public_key(Conn *c)
{
	int i;
	Msg *m;

	m = allocmsg(c, SSH_SMSG_PUBLIC_KEY, 2048);
	putbytes(m, c->cookie, COOKIELEN);
	putRSApub(m, c->serverkey);
	putRSApub(m, c->hostkey);
	putlong(m, c->flags);

	for(i=0; i<c->nokcipher; i++)
		c->ciphermask |= 1<<c->okcipher[i]->id;
	putlong(m, c->ciphermask);
	for(i=0; i<c->nokauthsrv; i++)
		c->authmask |= 1<<c->okauthsrv[i]->id;
	putlong(m, c->authmask);

	sendmsg(m);
}
Example #14
0
static void
send_ssh_cmsg_session_key(Conn *c)
{
	int i, n, buflen, serverkeylen, hostkeylen;
	mpint *b;
	uchar *buf;
	Msg *m;
	RSApub *ksmall, *kbig;

	m = allocmsg(c, SSH_CMSG_SESSION_KEY, 2048);
	putbyte(m, c->cipher->id);
	putbytes(m, c->cookie, COOKIELEN);

	serverkeylen = mpsignif(c->serverkey->n);
	hostkeylen = mpsignif(c->hostkey->n);
	ksmall = kbig = nil;
	if(serverkeylen+128 <= hostkeylen){
		ksmall = c->serverkey;
		kbig = c->hostkey;
	}else if(hostkeylen+128 <= serverkeylen){
		ksmall = c->hostkey;
		kbig = c->serverkey;
	}else
		error("server session and host keys do not differ by at least 128 bits");
	
	buflen = (mpsignif(kbig->n)+7)/8;
	buf = emalloc(buflen);

	debug(DBG_CRYPTO, "session key is %.*H\n", SESSKEYLEN, c->sesskey);
	memmove(buf, c->sesskey, SESSKEYLEN);
	for(i = 0; i < SESSIDLEN; i++)
		buf[i] ^= c->sessid[i];
	debug(DBG_CRYPTO, "munged session key is %.*H\n", SESSKEYLEN, buf);

	b = rsaencryptbuf(ksmall, buf, SESSKEYLEN);
	n = (mpsignif(ksmall->n)+7) / 8;
	mptoberjust(b, buf, n);
	mpfree(b);
	debug(DBG_CRYPTO, "encrypted with ksmall is %.*H\n", n, buf);

	b = rsaencryptbuf(kbig, buf, n);
	putmpint(m, b);
	debug(DBG_CRYPTO, "encrypted with kbig is %B\n", b);
	mpfree(b);

	memset(buf, 0, buflen);
	free(buf);

	putlong(m, c->flags);
	sendmsg(m);
}
Example #15
0
void
requestpty(Conn *c)
{
	char *term;
	int nrow, ncol, width, height;
	Msg *m;

	m = allocmsg(c, SSH_CMSG_REQUEST_PTY, 1024);
	if((term = getenv("TERM")) == nil)
		term = "9term";
	putstring(m, term);

	readgeom(&nrow, &ncol, &width, &height);
	putlong(m, nrow);	/* characters */
	putlong(m, ncol);
	putlong(m, width);	/* pixels */
	putlong(m, height);

	if(rawhack)
		putbytes(m, rawptyopt, sizeof rawptyopt);
	else
		putbytes(m, ptyopt, sizeof ptyopt);

	sendmsg(m);

	m = recvmsg(c, 0);
	switch(m->type){
	case SSH_SMSG_SUCCESS:
		debug(DBG_IO, "PTY allocated\n");
		break;
	case SSH_SMSG_FAILURE:
		debug(DBG_IO, "PTY allocation failed\n");
		break;
	default:
		badmsg(m, 0);
	}
	free(m);
}
Example #16
0
File: ssh1.c Project: aahud/harvey
static void
sendwritemsg(Conn *c, char *buf, int n)
{
	Msg *m;

	if(n==0)
		m = allocmsg(c, SSH_CMSG_EOF, 0);
	else{
		m = allocmsg(c, SSH_CMSG_STDIN_DATA, 4+n);
		putlong(m, n);
		putbytes(m, buf, n);
	}
	sendmsg(m);
}
Example #17
0
static void ttf_dumpcvar(struct alltabs *at,MMSet *mm) {
    int16_t **deltas;
    int ptcnt, cnt, pcnt;
    int i, j, rj, big;
    int tuple_size;
    uint32_t start, end;
    uint16_t *pts;

    deltas=CvtFindDeltas(mm, &ptcnt);
    for (i=cnt=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL)
            ++cnt;
    if (cnt==0) {
        free(deltas);
        return;
    }

    tuple_size=4 + 2 * mm->axis_count;
    at->cvar=atmpfile();
    putlong(at->cvar, 0x00010000);	/* Format */
    putshort(at->cvar, cnt);	/* Number of instances with cvt tables (tuple count of interesting tuples) */
    putshort(at->cvar, 8 + cnt * tuple_size);	/* Offset to data */

    for (i=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL) {
            putshort(at->cvar, 0);	/* tuple data size, figure out later */
            putshort(at->cvar, 0xa000);	/* tuple coords follow, private points in data */
            for (j=0; j < mm->axis_count; ++j)
                putshort(at->cvar,
                         rint(16384 * mm->positions[i * mm->axis_count + j]));
        }
    if (aftell(at->cvar) != 8 + cnt * tuple_size)
        ErrorMsg(2,"Data offset wrong\n");

    for (i=cnt=0; i < mm->instance_count; ++i)
        if (deltas[i] != NULL) {
            start=aftell(at->cvar);
            for (j=pcnt=0; j < ptcnt; ++j)
                if (deltas[i][j] != 0)
                    ++pcnt;
            pts=malloc(pcnt * sizeof(uint16_t));
            for (j=pcnt=0; j < ptcnt; ++j)
                if (deltas[i][j] != 0)
                    pts[pcnt++]=j;

            if (pcnt > 0x7f) {
                aputc(0x80 | (pcnt >> 8), at->cvar);
                aputc(pcnt & 0xff, at->cvar);
            } else
Example #18
0
/* printSegment (startingAddr)
**
** This routine reads raw bytes from the input file and prints them
** out in a formatted way like this:
**
** 00002000: 0000 0000 0000 0000 0000 0000 0000 0000   ................
** 00002010: 6162 6364 6566 6768 3400 0000 696A 6B6C   abcdefgh4...ijkl
** 00002020: 6D6E 6F70 7172 7374 7576 7778 797A 0000   mnopqrstuvwxyz..
**
** It is passed the starting address; in this example it was 0x0000200.
*/
void printSegment (int startingAddr) {
   int addr;

   /* Each execution of this loop prints a single output line. */
   addr = startingAddr;
   readline ();
   while (size > 0) {
     putlong (addr);
     printf (":  ");
     printline ();
     addr = addr + 16;
     readline ();
   }

}
Example #19
0
void
handleagentoclose(Msg *m)
{
	uint32_t local;

	assert(m->type == SSH_MSG_CHANNEL_OUTPUT_CLOSED);
	local = getlong(m);
	debug(DBG_AUTH, "agent close %d\n", local);
	if(local < nelem(achan)){
		debug(DBG_AUTH, "\tlocal %d is remote %d\n", local, achan[local].chan);
		if(achan[local].needclosed){
			achan[local].needclosed = 0;
			m = allocmsg(m->c, SSH_MSG_CHANNEL_OUTPUT_CLOSED, 4);
			putlong(m, achan[local].chan);
			sendmsg(m);
		}
	}
}
Example #20
0
void
handleagentieof(Msg *m)
{
	uint32_t local;

	assert(m->type == SSH_MSG_CHANNEL_INPUT_EOF);
	local = getlong(m);
	debug(DBG_AUTH, "agent close %d\n", local);
	if(local < nelem(achan)){
		debug(DBG_AUTH, "\tlocal %d is remote %d\n", local, achan[local].chan);
		achan[local].open = 0;
/*
		m = allocmsg(m->c, SSH_MSG_CHANNEL_OUTPUT_CLOSED, 4);
		putlong(m, achan[local].chan);
		sendmsg(m);
*/
		if(achan[local].needeof){
			achan[local].needeof = 0;
			m = allocmsg(m->c, SSH_MSG_CHANNEL_INPUT_EOF, 4);
			putlong(m, achan[local].chan);
			sendmsg(m);
		}
	}
}
Example #21
0
void NetCity::Packetize(uint8* buf, uint16& size)
{
	buf[0] = 'C';
	buf[1] = 'D';
	uint16 unitSize;

	putlong(&buf[2], m_unitData->m_id);
	NetUnit::PacketizeUnit(&buf[6], unitSize, m_unitData);

	size = 6 + unitSize;
	CityData* cityData = m_unitData->m_city_data;

	PUSHBYTE(m_isInitialPacket);

	PUSHLONG(cityData->m_slaveBits);
	PUSHLONG(cityData->m_shieldstore);
	PUSHLONG(cityData->m_shieldstore_at_begin_turn);
	PUSHLONG(cityData->m_net_gold);
	PUSHLONG(cityData->m_science);
	PUSHLONG(cityData->m_luxury);
	PUSHLONG(cityData->m_city_attitude);
	PUSHLONG64((uint64)cityData->m_built_improvements);

	
	

#ifdef CTP1_TRADE
	uint8 numNonZeroResources = 0;
	
	sint32 resourceCountPosition = size++;

	sint32 i;

	for(i = 0; i < g_theResourceDB->NumRecords(); i++) {
		if(cityData->m_resources.GetResourceCount(i) > 0) {
			numNonZeroResources++;
			PUSHBYTE((uint8)i);
			PUSHSHORT((uint16)cityData->m_resources.GetResourceCount(i));
		}
	}
	buf[resourceCountPosition] = numNonZeroResources;
#endif

	sint32 i;
	PUSHLONG(cityData->m_population);
	for(i = 0; i < (sint32)POP_MAX; i++) {
		PUSHSHORT(cityData->m_numSpecialists[i]);
	}
	PUSHLONG(cityData->m_partialPopulation);
	PUSHLONG(cityData->m_sizeIndex);
	PUSHLONG(cityData->m_workerFullUtilizationIndex);
	PUSHLONG(cityData->m_workerPartialUtilizationIndex);

	PUSHDOUBLE(cityData->m_defensiveBonus);
	PUSHBYTE((uint8)cityData->m_founder);

	PacketizeResources(cityData->m_collectingResources, buf, size);
	PacketizeResources(cityData->m_sellingResources, buf, size);
	PacketizeResources(cityData->m_buyingResources, buf, size);

	sint32 r;
	for(r = 0; r < g_theResourceDB->NumRecords(); r++) {
		PUSHLONG(cityData->m_distanceToGood[r]);
	}
	
}
Example #22
0
int main(int argc, char *argv[]) {
  FILE      *infile, *wadfile;
  char      keyword[20];
  char      vertexname[20], sectorname[20];
  char      fromvertexname[20], tovertexname[20];
  int      ox, oy, x, y, z, xf, yf;
  int      numentries, reslength;
  int      i;
  int      episode, level, maptype = 0;
  char      gametype[20];
  int      lastitem;
  int      floorh, ceilingh, brightness, special, tag, type, attrs, angle, flags;
  int      arg1, arg2, arg3, arg4, arg5;
  int       floort, ceilingt;
  int      uppert, lowert, normalt;
  char      thingname[20];
  int      tid;
  int      fromvertexindex, tovertexindex;
  int      rightsidedefindex, leftsidedefindex;
  int      sectorindex;
  char      behavior[16] = { 'A', 'C', 'S', '\0', 0x08, '\0', '\0', '\0',
             '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
                };

  w_vertex64_t   vertices[32768];
  w_sector64_t   sectors[32768];
  sidedef64_t  sidedefs[32768];
  linedef64_t  linedefs[32768];
  things64_t   things[32768];
  char lights[100000], macros[100000];
  int lightbytes = 0, macrobytes = 0;
  int      numvertices = 0, numsectors = 0, numsidedefs = 0,
			  numlinedefs = 0, numthings = 0;
  int      vertexshortcut = 1;
  int      sectorshortcut = 1;

  char      entryname[9];
  int      entrypos;

  if (argc != 3) {
    fprintf(stderr,"Usage: %s infile outfile\n", argv[0]);
    exit(-1);
  }
  if (!strcmp(argv[1], "-"))
    infile = stdin;
  else
    infile = fopen(argv[1], "r");
  if (infile == NULL) {
    fprintf(stderr,"Unable to open input file %s\n", argv[1]);
    exit(-1);
  }
  if (!strcmp(argv[2], "-"))
    wadfile = stdout;
  else
    wadfile = fopen(argv[2], "wb");
  if (wadfile == NULL) {
    fprintf(stderr,"Unable to create output file %s\n", argv[2]);
    exit(-1);
  }
  if (wadfile == stdout)
    quiet = 1;
  allowcomment(infile);
  fscanf(infile, " %s ", keyword);

  if (!strcmp(keyword, "LEVEL_START")) {
    fscanf(infile, " %d %d %d %s ", &episode, &level, &maptype, &gametype[0]);
    if(maptype != 64) {
       fprintf(stderr, "map file is not for Doom64EX\n");
       exit(-1);
    }
    allowcomment(infile);
    fscanf(infile, " %s ", keyword);
  } else {
    fprintf(stderr,"LEVEL_START marker missing!\n");
    exit(-1);
  }

  if (!strcmp(keyword, "VERTEXES_START") |
		  !strcmp(keyword, "VERTICES_START"))
  {
	  /* Process vertices until VERTEXES_END is found */
	  lastitem = 0;
	  numvertices = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", vertexname);
		  if (!strcmp(vertexname, "VERTEXES_END") |
				  !strcmp(vertexname, "VERTICES_END"))
			  lastitem = 1;
		  else {
			  /* KLUDGE to speed things up, notice when vertex names contain
				* the vertex number and avoid the long scan of vertices[].
				*/
			  {
				  const char *p;
				  long num = 0;
				  int digits = 0;

				  /* Skip over the non-numeric prefix */
				  for (p = vertexname; *p != '\0' && (*p < '0' || *p > '9'); p++)
					  ;
				  for (; *p >= '0' && *p <= '9'; p++) {
					  num = 10 * num + *p - '0';
					  digits++;
				  }
				  if (digits == 0 || num != numvertices)
					  vertexshortcut = 0;
			  }
			  fscanf(infile, " : %d %d %d %d ", &x, &xf, &y, &yf);
			  allowcomment(infile);
			  vertices[numvertices].x = x;
			  vertices[numvertices].y = y;
			  vertices[numvertices].x_frac = xf;
			  vertices[numvertices].y_frac = yf;
			  strncpy(vertices[numvertices].name, vertexname, 8);
			  numvertices++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"VERTEXES_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "SECTORS_START")) {
	  int i;
	  uint32_t flags;
	  uint16_t sflags;
	  /* Process sectors until SECTORS_END is found */
	  lastitem = 0;
	  numsectors = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", sectorname);
		  if (!strcmp(sectorname, "SECTORS_END"))
			  lastitem = 1;
		  else {
			  fscanf(infile, " : %d %d %d %d %d %d %d ", &floorh, &ceilingh,
					  &floort, &ceilingt, &type, &tag, &flags);
			  /* KLUDGE to speed things up, notice when sector names contain
				* the sector number and avoid the long scan of sectors[].
				*/
			  {
				  const char *p;
				  long num = 0;
				  int digits = 0;

				  /* Skip over the non-numeric prefix */
				  for (p = sectorname; *p != '\0' && (*p < '0' || *p > '9'); p++)
					  ;
				  for (; *p >= '0' && *p <= '9'; p++)
				  {
					  num = 10 * num + *p - '0';
					  digits++;
				  }
				  if (digits == 0 || num != numsectors)
					  sectorshortcut = 0;
			  }
			  allowcomment(infile);
			  sectors[numsectors].s.f_height = floorh;
			  sectors[numsectors].s.c_height = ceilingh;
			  sectors[numsectors].s.type = type;
			  sectors[numsectors].s.flags = flags;
			  sectors[numsectors].s.tag = tag;
			  strncpy(sectors[numsectors].name, sectorname, 8);
			  sectors[numsectors].s.f_index = floort;
			  sectors[numsectors].s.c_index = ceilingt;

			  for(i=0; i<10; i++) {
				  int got;
				  fscanf(infile, " %x ", &got);
				  sectors[numsectors].s.color_index[i] = got & 0xff;
			  }

			  numsectors++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"SECTORS_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "LINEDEFS_START")) {
	  uint32_t flags;
	  uint16_t sflags;

	  /* Process lines until LINEDEFS_END is found */
	  lastitem = 0;
	  numlinedefs = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", fromvertexname);
		  if (!strcmp(fromvertexname, "LINEDEFS_END"))
			  lastitem = 1;
		  else {
			  fscanf(infile, " %s : %u %hu %d ", tovertexname, &flags, &type, &tag);
			  allowcomment(infile);
			  fromvertexindex = findmatch(fromvertexname, (char*)vertices,
					  vertexshortcut, numvertices,
					  sizeof(w_vertex64_t));
			  tovertexindex = findmatch(tovertexname, (char*)vertices,
					  vertexshortcut, numvertices,
					  sizeof(w_vertex64_t));

			  /* Read one or two SIDEDEFS */
			  fscanf(infile, " %s %d %d %d %d %d ", sectorname, &ox, &oy,
					  &uppert, &lowert, &normalt);
			  allowcomment(infile);
			  sectorindex = findmatch(sectorname, (char*)sectors, sectorshortcut,
					  numsectors, sizeof(w_sector64_t));
			  sidedefs[numsidedefs].sector = sectorindex;
			  sidedefs[numsidedefs].x = ox;
			  sidedefs[numsidedefs].y = oy;
			  sidedefs[numsidedefs].u_texture = uppert;
			  sidedefs[numsidedefs].l_texture = lowert;
			  sidedefs[numsidedefs].m_texture = normalt;
			  rightsidedefindex = numsidedefs;
			  numsidedefs++;
			  fscanf(infile, "%s", sectorname);
			  if (!strcmp(sectorname, "-")) {
				  allowcomment(infile);
				  leftsidedefindex = -1;
			  } else {
				  fscanf(infile, " %d %d %u %u %u ", &ox, &oy, &uppert, &lowert, &normalt);
				  allowcomment(infile);
				  sectorindex = findmatch(sectorname, (char*)sectors, sectorshortcut,
						  numsectors, sizeof(w_sector64_t));
				  sidedefs[numsidedefs].sector = sectorindex;
				  sidedefs[numsidedefs].x = ox;
				  sidedefs[numsidedefs].y = oy;
				  sidedefs[numsidedefs].u_texture = uppert;
				  sidedefs[numsidedefs].l_texture = lowert;
				  sidedefs[numsidedefs].m_texture = normalt;
				  leftsidedefindex = numsidedefs;
				  numsidedefs++;
			  }
			  linedefs[numlinedefs].v_from = fromvertexindex;
			  linedefs[numlinedefs].v_to = tovertexindex;
			  linedefs[numlinedefs].flags = flags;
			  linedefs[numlinedefs].type = sflags;
			  linedefs[numlinedefs].tag = tag;
			  linedefs[numlinedefs].r_side = rightsidedefindex;
			  linedefs[numlinedefs].l_side = leftsidedefindex;
			  numlinedefs++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
    fprintf(stderr,"LINEDEFS_START marker missing!\n");
    exit(-1);
  }

  if (!strcmp(keyword, "THINGS_START")) {
	  /* Process things until THINGS_END is found */
	  lastitem = 0;
	  numthings = 0;
	  while (!lastitem) {
		  allowcomment(infile);
		  fscanf(infile, " %s ", thingname);
		  if (!strcmp(thingname, "THINGS_END"))
			  lastitem = 1;
		  else {
			  sscanf(thingname, "%hd", &(things[numthings].type));
			  fscanf(infile, " : %hd %hd %hd %hd %hd %hd",
					  &(things[numthings].x),
					  &(things[numthings].y),
					  &(things[numthings].z),
					  &(things[numthings].angle),
					  &(things[numthings].flags),
					  &(things[numthings].tid));
			  allowcomment(infile);
			  numthings++;
		  }
	  }
	  fscanf(infile, " %s ", keyword);
  } else {
	  fprintf(stderr,"THINGS_START marker missing!\n");
	  exit(-1);
  }

  if (!strcmp(keyword, "LIGHTS_START")) {
	  lightbytes = 0;
	  fscanf(infile, " %s ", keyword);
	  while(strcmp(keyword, "LIGHTS_END")) {
		  unsigned int got;
		  sscanf(keyword, " %d ", &got);
		  lights[lightbytes++] = (unsigned char)(got & 0xff);
		  fscanf(infile, " %s ", keyword);
	  }
  } else {
	  fprintf(stderr,"LIGHTS_START marker missing!\n");
	  exit(-1);
  }

  fscanf(infile, " %s ", keyword);
  if (!strcmp(keyword, "MACROS_START")) {
	  macrobytes = 0;
	  fscanf(infile, " %s ", keyword);
	  while(strcmp(keyword, "MACROS_END")) {
		  unsigned int got;
		  sscanf(keyword, " %d ", &got);
		  macros[macrobytes++] = (unsigned char)(got & 0xff);
		  fscanf(infile, " %s ", keyword);
	  }
  } else {
	  fprintf(stderr,"MACROS_START marker missing!\n");
	  exit(-1);
  }


  if (infile != stdin)
	  fclose(infile);

  /* Concatenate all the data into the final PWAD file */

  /* Header */
  fprintf(wadfile,"PWAD");

  /* Doom64 map type */
  numentries = 14;
  putlong(numentries, wadfile);
  reslength = numthings * sizeof(things64_t) +
	  numlinedefs * sizeof(linedef64_t) +
	  numsidedefs * sizeof(sidedef64_t) +
	  numvertices * sizeof(vertex64_t) +
	  numsectors * sizeof(sector64_t) +
	  lightbytes +
	  macrobytes +
	  sizeof(wadinfo_t);
  putlong(reslength, wadfile);

  /* ExMy - start at position 12 (zero length entry) */

  /* THINGS - start at position 12 */
  for (i = 0; i < numthings; i++) {
      /* Doom map type */
      putshort(things[i].x, wadfile);
      putshort(things[i].y, wadfile);
      putshort(things[i].z, wadfile);
      putshort(things[i].angle, wadfile);
      putshort(things[i].type, wadfile);
      putshort(things[i].flags, wadfile);
      putshort(things[i].tid, wadfile);
  }

  /* LINEDEFS - start at position 12 + numthings * sizeof(things_t) */
  for (i = 0; i < numlinedefs; i++) {
	  /* Doom map type */
	  putshort(linedefs[i].v_from, wadfile);
	  putshort(linedefs[i].v_to, wadfile);
	  putlong(linedefs[i].flags, wadfile);
	  putshort(linedefs[i].type, wadfile);
	  putshort(linedefs[i].tag, wadfile);
	  putshort(linedefs[i].r_side, wadfile);
	  putshort(linedefs[i].l_side, wadfile);
  }

  /* SIDEDEFS - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t)
   */
  for (i = 0; i < numsidedefs; i++) {
    putshort(sidedefs[i].x, wadfile);
    putshort(sidedefs[i].y, wadfile);
    putshort(sidedefs[i].u_texture, wadfile);
    putshort(sidedefs[i].l_texture, wadfile);
    putshort(sidedefs[i].m_texture, wadfile);
    putshort(sidedefs[i].sector, wadfile);
  }

  /* VERTEXES - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t) + numsidedefs * sizeof(sidedef_t)
   */
  for (i = 0; i < numvertices; i++) {
    putshort(vertices[i].x_frac, wadfile);
    putshort(vertices[i].x, wadfile);
    putshort(vertices[i].y_frac, wadfile);
    putshort(vertices[i].y, wadfile);
  }

  /* SECTORS - start at position 12 + numthings * sizeof(things_t) +
   * numlinedefs * sizeof(linedef_t) + numsidedefs * sizeof(sidedef_t) +
   * numvertices * sizeof(vertex_t)
   */
  for (i = 0; i < numsectors; i++) {
    putshort(sectors[i].s.f_height, wadfile);
    putshort(sectors[i].s.c_height, wadfile);
    putshort(sectors[i].s.f_index, wadfile);
    putshort(sectors[i].s.c_index, wadfile);
	 fwrite(sectors[i].s.color_index, 10, 1, wadfile);
    putshort(sectors[i].s.type, wadfile);
    putshort(sectors[i].s.tag, wadfile);
    putshort(sectors[i].s.flags, wadfile);
  }

  fwrite(lights, lightbytes, 1, wadfile);
  fwrite(macros, macrobytes, 1, wadfile);

  /* Now, write the main directory */
  entrypos = 12;
  if (episode > 0)
      sprintf(entryname, "E%1dM%1d", episode, level);
  else
      sprintf(entryname, "MAP%02d", level);
  addentry(&entrypos, 0,            entryname,  wadfile);
    addentry(&entrypos, numthings*sizeof(things64_t),   "THINGS",   wadfile);
    addentry(&entrypos, numlinedefs*sizeof(linedef64_t),   "LINEDEFS", wadfile);
  addentry(&entrypos, numsidedefs*sizeof(sidedef64_t),   "SIDEDEFS", wadfile);
  addentry(&entrypos, numvertices*sizeof(vertex64_t),   "VERTEXES", wadfile);
  addentry(&entrypos, 0,                       "SEGS",     wadfile);
  addentry(&entrypos, 0,                       "SSECTORS", wadfile);
  addentry(&entrypos, 0,                       "NODES",    wadfile);
  addentry(&entrypos, numsectors*sizeof(sector64_t),   "SECTORS",  wadfile);
  addentry(&entrypos, 0,                       "REJECT",   wadfile);
  addentry(&entrypos, 0,                       "BLOCKMAP", wadfile);
  addentry(&entrypos, 0,                       "LEAFS", wadfile);
  addentry(&entrypos, lightbytes,                       "LIGHTS", wadfile);
  addentry(&entrypos, macrobytes,                       "MACROS", wadfile);

  /* DONE! */
  if (wadfile != stdout)
    fclose(wadfile);
  return 0;
}
Example #23
0
File: dns.c Project: frekky/iodine
int
dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_t datalen)
{
	HEADER *header;
	short name;
	char *p;
	int len;
	int ancnt;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = (qr == QR_ANSWER);
	header->opcode = 0;
	header->aa = (qr == QR_ANSWER);
	header->tc = 0;
	header->rd = (qr == QR_QUERY);
	header->ra = 0;

	p = buf + sizeof(HEADER);

	switch (qr) {
	case QR_ANSWER:
		header->qdcount = htons(1);

		name = 0xc000 | ((p - buf) & 0x3fff);

		/* Question section */
		putname(&p, buflen - (p - buf), q->name);

		CHECKLEN(4);
		putshort(&p, q->type);
		putshort(&p, C_IN);

		/* Answer section */

		if (q->type == T_CNAME || q->type == T_A ||
			q->type == T_PTR || q->type == T_AAAA ||
			q->type == T_A6 || q->type == T_DNAME) {
			/* data is expected to be like "Hblabla.host.name.com\0" */

			char *startp;
			int namelen;

			CHECKLEN(10);
			putshort(&p, name);
			if (q->type == T_A || q->type == T_AAAA)
				/* answer CNAME to A question */
				putshort(&p, T_CNAME);
			else
				putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			startp = p;
			p += 2;			/* skip 2 bytes length */
			if (q->type == T_A6) {
				CHECKLEN(1);
				putbyte(&p, 128);
			}
			putname(&p, buflen - (p - buf), data);
			CHECKLEN(0);
			namelen = p - startp;
			namelen -= 2;
			putshort(&startp, namelen);
			ancnt = 1;
		} else if (q->type == T_MX || q->type == T_SRV) {
			/* Data is expected to be like
			   "Hblabla.host.name.com\0Hanother.com\0\0"
			   For SRV, see RFC2782.
			 */

			char *mxdata = data;
			char *startp;
			int namelen;

			ancnt = 1;
			while (1) {
				CHECKLEN(10);
				putshort(&p, name);
				putshort(&p, q->type);
				putshort(&p, C_IN);
				putlong(&p, 0);		/* TTL */

				startp = p;
				p += 2;			/* skip 2 bytes length */
				CHECKLEN(2);
				putshort(&p, 10 * ancnt);	/* preference */

				if (q->type == T_SRV) {
					/* weight, port (5060 = SIP) */
					CHECKLEN(4);
					putshort(&p, 10);
					putshort(&p, 5060);
				}

				putname(&p, buflen - (p - buf), mxdata);
				CHECKLEN(0);
				namelen = p - startp;
				namelen -= 2;
				putshort(&startp, namelen);

				mxdata = mxdata + strlen(mxdata) + 1;
				if (*mxdata == '\0')
					break;

				ancnt++;
			}
		} else if (q->type == T_TXT) {
			/* TXT has binary or base-X data */
			char *startp;
			int txtlen;

			CHECKLEN(10);
			putshort(&p, name);
			putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			startp = p;
			p += 2;			/* skip 2 bytes length */
			puttxtbin(&p, buflen - (p - buf), data, datalen);
			CHECKLEN(0);
			txtlen = p - startp;
			txtlen -= 2;
			putshort(&startp, txtlen);
			ancnt = 1;
		} else {
			/* NULL has raw binary data */

			CHECKLEN(10);
			putshort(&p, name);
			putshort(&p, q->type);
			putshort(&p, C_IN);
			putlong(&p, 0);		/* TTL */

			datalen = MIN(datalen, buflen - (p - buf));
			CHECKLEN(2);
			putshort(&p, datalen);
			CHECKLEN(datalen);
			putdata(&p, data, datalen);
			CHECKLEN(0);
			ancnt = 1;
		}
		header->ancount = htons(ancnt);
		break;
	case QR_QUERY:
		/* Note that iodined also uses this for forward queries */

		header->qdcount = htons(1);

		datalen = MIN(datalen, buflen - (p - buf));
		putname(&p, datalen, data);

		CHECKLEN(4);
		putshort(&p, q->type);
		putshort(&p, C_IN);

		/* EDNS0 to advertise maximum response length
		   (even CNAME/A/MX, 255+255+header would be >512) */
		if (dnsc_use_edns0) {
			header->arcount = htons(1);
			CHECKLEN(11);
			putbyte(&p, 0x00);    /* Root */
			putshort(&p, 0x0029); /* OPT */
			putshort(&p, 0x1000); /* Payload size: 4096 */
			putshort(&p, 0x0000); /* Higher bits/edns version */
			putshort(&p, 0x8000); /* Z */
			putshort(&p, 0x0000); /* Data length */
		}

		break;
	}

	len = p - buf;

	return len;
}
Example #24
0
File: dns.c Project: frekky/iodine
int
dns_encode_ns_response(char *buf, size_t buflen, struct query *q, char *topdomain)
/* Only used when iodined gets an NS type query */
/* Mostly same as dns_encode_a_response() below */
{
	HEADER *header;
	int len;
	short name;
	short topname;
	short nsname;
	char *ipp;
	int domain_len;
	char *p;

	if (buflen < sizeof(HEADER))
		return 0;

	memset(buf, 0, buflen);

	header = (HEADER*)buf;

	header->id = htons(q->id);
	header->qr = 1;
	header->opcode = 0;
	header->aa = 1;
	header->tc = 0;
	header->rd = 0;
	header->ra = 0;

	p = buf + sizeof(HEADER);

	header->qdcount = htons(1);
	header->ancount = htons(1);
	header->arcount = htons(1);

	/* pointer to start of name */
	name = 0xc000 | ((p - buf) & 0x3fff);

	domain_len = strlen(q->name) - strlen(topdomain);
	if (domain_len < 0 || domain_len == 1)
		return -1;
	if (strcasecmp(q->name + domain_len, topdomain))
		return -1;
	if (domain_len >= 1 && q->name[domain_len - 1] != '.')
		return -1;

	/* pointer to start of topdomain; instead of dots at the end
	   we have length-bytes in front, so total length is the same */
	topname = 0xc000 | ((p - buf + domain_len) & 0x3fff);

	/* Query section */
	putname(&p, buflen - (p - buf), q->name);	/* Name */
	CHECKLEN(4);
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */

	/* Answer section */
	CHECKLEN(12);
	putshort(&p, name);			/* Name */
	putshort(&p, q->type);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 5);			/* Data length */

	/* pointer to ns.topdomain */
	nsname = 0xc000 | ((p - buf) & 0x3fff);
	CHECKLEN(5);
	putbyte(&p, 2);
	putbyte(&p, 'n');
	putbyte(&p, 's');
	putshort(&p, topname);			/* Name Server */

	/* Additional data (A-record of NS server) */
	CHECKLEN(12);
	putshort(&p, nsname);			/* Name Server */
	putshort(&p, T_A);			/* Type */
	putshort(&p, C_IN);			/* Class */
	putlong(&p, 3600);			/* TTL */
	putshort(&p, 4);			/* Data length */

	/* ugly hack to output IP address */
	ipp = (char *) &q->destination;
	CHECKLEN(4);
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *(ipp++));
	putbyte(&p, *ipp);

	len = p - buf;
	return len;
}
Example #25
0
int main() {
    unsigned long num = 65536;
    putlong(num);
}
Example #26
0
File: sgilib.c Project: Minoos/gimp
sgi_t *
sgiOpenFile(FILE *file,	/* I - File to open */
            int  mode,	/* I - Open mode (SGI_READ or SGI_WRITE) */
            int  comp,	/* I - Type of compression */
            int  bpp,	/* I - Bytes per pixel */
            int  xsize,	/* I - Width of image in pixels */
            int  ysize,	/* I - Height of image in pixels */
            int  zsize)	/* I - Number of channels */
{
  int	i, j;		/* Looping var */
  char	name[80];	/* Name of file in image header */
  short	magic;		/* Magic number */
  sgi_t	*sgip;		/* New image pointer */


  if ((sgip = calloc(sizeof(sgi_t), 1)) == NULL)
    return (NULL);

  sgip->file = file;
  sgip->swapBytes = 0;

  switch (mode)
  {
    case SGI_READ :
        sgip->mode = SGI_READ;

        magic = getshort(sgip);
        if (magic != SGI_MAGIC)
        {
	  /* try little endian format */
	  magic = ((magic >> 8) & 0x00ff) | ((magic << 8) & 0xff00);
	  if(magic != SGI_MAGIC) {
            free(sgip);
            return (NULL);
          } else {
	    sgip->swapBytes = 1;
	  }
	}

        sgip->comp  = getc(sgip->file);
        sgip->bpp   = getc(sgip->file);
        getshort(sgip);		/* Dimensions */
        sgip->xsize = getshort(sgip);
        sgip->ysize = getshort(sgip);
        sgip->zsize = getshort(sgip);
        getlong(sgip);		/* Minimum pixel */
        getlong(sgip);		/* Maximum pixel */

        if (sgip->comp)
        {
         /*
          * This file is compressed; read the scanline tables...
          */

          fseek(sgip->file, 512, SEEK_SET);

          sgip->table    = calloc(sgip->zsize, sizeof(long *));
          sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
          for (i = 1; i < sgip->zsize; i ++)
            sgip->table[i] = sgip->table[0] + i * sgip->ysize;

          for (i = 0; i < sgip->zsize; i ++)
            for (j = 0; j < sgip->ysize; j ++)
              sgip->table[i][j] = getlong(sgip);
        };
        break;

    case SGI_WRITE :
	if (xsize < 1 ||
	    ysize < 1 ||
	    zsize < 1 ||
	    bpp < 1 || bpp > 2 ||
	    comp < SGI_COMP_NONE || comp > SGI_COMP_ARLE)
        {
          free(sgip);
          return (NULL);
        };

        sgip->mode = SGI_WRITE;

        putshort(SGI_MAGIC, sgip);
        putc((sgip->comp = comp) != 0, sgip->file);
        putc(sgip->bpp = bpp, sgip->file);
        putshort(3, sgip);		/* Dimensions */
        putshort(sgip->xsize = xsize, sgip);
        putshort(sgip->ysize = ysize, sgip);
        putshort(sgip->zsize = zsize, sgip);
        if (bpp == 1)
        {
          putlong(0, sgip);	/* Minimum pixel */
          putlong(255, sgip);	/* Maximum pixel */
        }
        else
        {
          putlong(-32768, sgip);	/* Minimum pixel */
          putlong(32767, sgip);	/* Maximum pixel */
        };
        putlong(0, sgip);		/* Reserved */

        memset(name, 0, sizeof(name));
        fwrite(name, sizeof(name), 1, sgip->file);

        for (i = 0; i < 102; i ++)
          putlong(0, sgip);

        switch (comp)
        {
          case SGI_COMP_NONE : /* No compression */
             /*
              * This file is uncompressed.  To avoid problems with sparse files,
              * we need to write blank pixels for the entire image...
              */

              if (bpp == 1)
              {
        	for (i = xsize * ysize * zsize; i > 0; i --)
        	  putc(0, sgip->file);
              }
              else
              {
        	for (i = xsize * ysize * zsize; i > 0; i --)
        	  putshort(0, sgip);
              };
              break;

          case SGI_COMP_ARLE : /* Aggressive RLE */
              sgip->arle_row    = (unsigned short *)calloc(xsize, sizeof(unsigned short));
              sgip->arle_offset = 0;

          case SGI_COMP_RLE : /* Run-Length Encoding */
             /*
              * This file is compressed; write the (blank) scanline tables...
              */

              for (i = 2 * ysize * zsize; i > 0; i --)
        	putlong(0, sgip);

              sgip->firstrow = ftell(sgip->file);
              sgip->nextrow  = ftell(sgip->file);
              sgip->table    = calloc(sgip->zsize, sizeof(long *));
              sgip->table[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
              for (i = 1; i < sgip->zsize; i ++)
        	sgip->table[i] = sgip->table[0] + i * sgip->ysize;
              sgip->length    = calloc(sgip->zsize, sizeof(long *));
              sgip->length[0] = calloc(sgip->ysize * sgip->zsize, sizeof(long));
              for (i = 1; i < sgip->zsize; i ++)
        	sgip->length[i] = sgip->length[0] + i * sgip->ysize;
              break;
        };
        break;

    default :
        free(sgip);
        return (NULL);
  };
Example #27
0
void
startcmd(Conn *c, char *cmd, int *kidpid, int *kidin)
{
	int i, pid, kpid;
	int pfd[3][2];
	char *dir;
	char *sysname, *tz;
	Msg *m;
	Waitmsg *w;

	for(i=0; i<3; i++)
		if(pipe(pfd[i]) < 0)
			sysfatal("pipe: %r");

	sysname = getenv("sysname");
	tz = getenv("timezone");

	switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
	case -1:
		sysfatal("fork: %r");
	case 0:
		switch(kpid = rfork(RFPROC|RFNOTEG|RFENVG|RFFDG)){
		case -1:
			sysfatal("fork: %r");
		case 0:
			for(i=0; i<3; i++){
				if(dup(pfd[i][1], i) < 0)
					sysfatal("dup: %r");
				close(pfd[i][0]);
				close(pfd[i][1]);
			}
			putenv("user", c->user);
			if(sysname)
				putenv("sysname", sysname);
			if(tz)
				putenv("tz", tz);
	
			dir = smprint("/usr/%s", c->user);
			if(dir == nil || chdir(dir) < 0)
				chdir("/");
			if(cmd){
				putenv("service", "rx");
				execl("/bin/rc", "rc", "-lc", cmd, nil);
				sysfatal("cannot exec /bin/rc: %r");
			}else{
				putenv("service", "con");
				execl("/bin/ip/telnetd", "telnetd", "-tn", nil);
				sysfatal("cannot exec /bin/ip/telnetd: %r");
			}
		default:
			*kidpid = kpid;
			rendezvous(kidpid, 0);
			for(;;){
				if((w = wait()) == nil)
					sysfatal("wait: %r");
				if(w->pid == kpid)
					break;
				free(w);
			}
			if(w->msg[0]){
				m = allocmsg(c, SSH_MSG_DISCONNECT, 4+strlen(w->msg));
				putstring(m, w->msg);
				sendmsg(m);
			}else{
				m = allocmsg(c, SSH_SMSG_EXITSTATUS, 4);
				putlong(m, 0);
				sendmsg(m);
			}
			for(i=0; i<3; i++)
				close(pfd[i][0]);
			free(w);
			exits(nil);	
			break;
		}
	default:
		atexitkill(pid);
		rendezvous(kidpid, 0);
		break;
	}

	for(i=0; i<3; i++)
		close(pfd[i][1]);

	copyout(c, pfd[1][0], SSH_SMSG_STDOUT_DATA);
	copyout(c, pfd[2][0], SSH_SMSG_STDERR_DATA);
	*kidin = pfd[0][0];
}
Example #28
0
void
handlefullmsg(Conn *c, Achan *a)
{
	int i;
	uint32_t chan, len, n, rt;
	uint8_t type;
	Msg *m, mm;
	Msg *r;
	Key *k;
	int nk;
	mpint *mod, *ek, *chal;
	uint8_t sessid[16];
	uint8_t chalbuf[32];
	uint8_t digest[16];
	DigestState *s;
	static int first;

	assert(a->len == a->ndata);

	chan = a->chan;
	mm.rp = a->data;
	mm.ep = a->data+a->ndata;
	mm.c = c;
	m = &mm;

	type = getbyte(m);

	if(first == 0){
		first++;
		fmtinstall('H', encodefmt);
	}

	switch(type){
	default:
		debug(DBG_AUTH, "unknown msg type\n");
	Failure:
		debug(DBG_AUTH, "agent sending failure\n");
		r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 13);
		putlong(r, chan);
		putlong(r, 5);
		putlong(r, 1);
		putbyte(r, SSH_AGENT_FAILURE);
		sendmsg(r);
		return;

	case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
		debug(DBG_AUTH, "agent request identities\n");
		nk = listkeys(&k);
		if(nk < 0)
			goto Failure;
		len = 1+4;	/* type, nk */
		for(i=0; i<nk; i++){
			len += 4;
			len += 2+(mpsignif(k[i].ek)+7)/8;
			len += 2+(mpsignif(k[i].mod)+7)/8;
			len += 4+strlen(k[i].comment);
		}
		r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 12+len);
		putlong(r, chan);
		putlong(r, len+4);
		putlong(r, len);
		putbyte(r, SSH_AGENT_RSA_IDENTITIES_ANSWER);
		putlong(r, nk);
		for(i=0; i<nk; i++){
			debug(DBG_AUTH, "\t%B %B %s\n", k[i].ek, k[i].mod, k[i].comment);
			putlong(r, mpsignif(k[i].mod));
			putmpint(r, k[i].ek);
			putmpint(r, k[i].mod);
			putstring(r, k[i].comment);
			mpfree(k[i].ek);
			mpfree(k[i].mod);
			free(k[i].comment);
		}
		free(k);
		sendmsg(r);
		break;

	case SSH_AGENTC_RSA_CHALLENGE:
		n = getlong(m);
		USED(n);	/* number of bits in key; who cares? */
		ek = getmpint(m);
		mod = getmpint(m);
		chal = getmpint(m);
		memmove(sessid, getbytes(m, 16), 16);
		rt = getlong(m);
		debug(DBG_AUTH, "agent challenge %B %B %B %ud (%p %p)\n",
			ek, mod, chal, rt, m->rp, m->ep);
		if(rt != 1 || dorsa(mod, ek, chal, chalbuf) < 0){
			mpfree(ek);
			mpfree(mod);
			mpfree(chal);
			goto Failure;
		}
		s = md5(chalbuf, 32, nil, nil);
		md5(sessid, 16, digest, s);
		r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 12+1+16);
		putlong(r, chan);
		putlong(r, 4+16+1);
		putlong(r, 16+1);
		putbyte(r, SSH_AGENT_RSA_RESPONSE);
		putbytes(r, digest, 16);
		debug(DBG_AUTH, "digest %.16H\n", digest);
		sendmsg(r);
		mpfree(ek);
		mpfree(mod);
		mpfree(chal);
		return;

	case SSH_AGENTC_ADD_RSA_IDENTITY:
		goto Failure;
/*
		n = getlong(m);
		pubmod = getmpint(m);
		pubexp = getmpint(m);
		privexp = getmpint(m);
		pinversemodq = getmpint(m);
		p = getmpint(m);
		q = getmpint(m);
		comment = getstring(m);
		add to factotum;
		send SSH_AGENT_SUCCESS or SSH_AGENT_FAILURE;
*/

	case SSH_AGENTC_REMOVE_RSA_IDENTITY:
		goto Failure;
/*
		n = getlong(m);
		pubmod = getmpint(m);
		pubexp = getmpint(m);
		tell factotum to del key
		send SSH_AGENT_SUCCESS or SSH_AGENT_FAILURE;
*/
	}
}
Example #29
0
void
CUTServerRTPUDPTransportTestDriver::testSendPacketNonReflectPacket ()
{
    const UINT16 streamNumber = 0;
    m_spTransport->m_ulPayloadWirePacket = 0;
    m_spTransport->m_pStreamHandler = new TransportStreamHandler(FALSE);
    m_spTransport->m_pStreamHandler->AddRef();
    m_spTransport->m_pStreamHandler->initStreamData(streamNumber //UINT16 streamNumber,
                                     , 0                        //UINT16 streamGroupNumber,
                                     , FALSE                    //HXBOOL needReliable,
                                     , TRUE                     //HXBOOL bIsSource,
                                     , 0                        //INT16 rtpPayloadType,
                                     , FALSE                    //HXBOOL bPushData,
                                     , 0                        //UINT32 wrapSequenceNumber,
                                     , 0                        //UINT32 ulBufferDepth,
                                     , FALSE                    //HXBOOL bHasOutOfOrderTS = FALSE,
                                     , NULL                     //CHXTimestampConverter* pTSConverter = NULL,
                                     , RTSPMEDIA_TYPE_UNKNOWN   //RTSPMediaType eMediaType = RTSPMEDIA_TYPE_UNKNOWN
                                     );
    UINT32 ulTicks = random32((int)HX_GET_TICKCOUNT());
    m_spTransport->m_pReportHandler->SetSSRC (ulTicks);

    m_spTransport->m_pFirstPlayTime = new Timeval (0, 0);

    SPCUTMockSocket spRTCPSocket = new CUTMockSocket();
    ServerRTCPUDPTransport* pRTCPTran = new ServerRTCPUDPTransport(TRUE);
    pRTCPTran->init(m_spContext.Ptr()
            , spRTCPSocket.Ptr()
            , m_spTransport.Ptr()
            , m_spTransportResponse.Ptr()
            , streamNumber
            );
    m_spTransport->setRTCPTransport(pRTCPTran);

    const UINT32 extraHeaderSize = 0x0c;
    const UINT32 dataLen = 0x1000 + extraHeaderSize;
    char *buffer = new char[dataLen];
    memset (buffer, 'j', dataLen);
    buffer[0] = 0x80;
    buffer[1] = 0x7f;
    for (int ii = 2; ii < extraHeaderSize; ++ii)
    {
        buffer[ii] = 0x00;
    }
    putlong(reinterpret_cast<UINT8*>(buffer+8), ulTicks);

    SPIHXBuffer spFullMontyBuffer;
    m_pContFact->CreateBuffer (spFullMontyBuffer, buffer, dataLen);

    SPIHXBuffer spJustRightBuffer;
    m_pContFact->CreateBuffer (spJustRightBuffer, buffer + extraHeaderSize, dataLen - extraHeaderSize);

    m_spSocket->setExpectedMethod(CUTMockSocket::FTC_WRITETO);
    m_spSocket->setExpectedArg(spFullMontyBuffer->GetBuffer());
    m_spSocket->setExpectedArg(spFullMontyBuffer->GetSize());
    m_spSocket->setExpectedArg(static_cast<IHXSockAddr*>(0));

    SPCUTBasePacket spPacket;
    m_pContFact->CreateBasePacket (spPacket, spJustRightBuffer);

    CPPUNIT_ASSERT( SUCCEEDED( m_spTransport->sendPacket(spPacket.Ptr()) ) );

    CPPUNIT_ASSERT( m_spSocket->Validate() );

    delete [] buffer;
    pRTCPTran->m_bSendBye = FALSE;
    pRTCPTran->Done();

    //The transport will delete the RTCP transport.
    //delete pRTCPTran;

    // The RTCP transport deletes this handler, so don't do it in teardown!
    m_spTransport->m_pReportHandler = NULL;
}