Ejemplo n.º 1
0
int main() {
  int fd[4], i;
  byte key_pub [2][pub_len],
       key_priv[2][priv_len];

  /* open files */
  for (i=0; i<4; i++) {
    if ((fd[i] = open(name[i], O_WRONLY|O_CREAT|O_EXCL,
		      (i%2) ? 0400 : 0444)) == -1) {
      fprintf(stderr, 
	      (errno == EEXIST)
	      ? "%s already exists\n" : "could not create file %s\n",
	      name[i]);
      error_cleanup(i);
    }
  }

  /* generate keys */
  for (i=0; i<2; i++) {
    crypto_box_keypair(key_pub[i], key_priv[i]);
    if ((write(fd[2*i  ], key_pub[i],  pub_len)  != pub_len)
     || (write(fd[2*i+1], key_priv[i], priv_len) != priv_len)
     || (close(fd[2*i  ]) == -1)
     || (close(fd[2*i+1]) == -1)) {
      error_cleanup(4);
    }
  }

  return 0;
}
bool CTCPIPSystemSrvr::do_write(char* buffer, int bufcount, short operation_id, long timeout)
{
	int count = bufcount;
	char* bp = 0;
	ssize_t wcount;

	if (m_nSocketFnum < 0)
	{
		SET_ERROR((long)this, NSK, TCPIP, operation_id, E_TCPIPROCESS, "do_write", O_DO_WRITE_READ, F_CHECKSOCKET, m_nSocketFnum, 0);
		return false;
	}

	for (bp = &buffer[0]; count > 0; count -= wcount)
	{
		int retries = 0;
		do
		{
			wcount = send(m_nSocketFnum, bp, count, 0);
		} while ((wcount < 0) && (errno == EINTR) && (retries++ < 3));

		if (wcount <= 0)
		{
			SET_ERROR((long)this, NSK, TCPIP, operation_id, E_TCPIPROCESS, "do_write", O_DO_WRITE_READ, F_SEND, errno, wcount);

            error_cleanup();

            return false;

		}
		bp += wcount;
	}
	return true;
}
Ejemplo n.º 3
0
void randombytes(byte *buf, unsigned long long len) {
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1) goto error;

  while (len > 0) {
    int sz = read(fd, buf, len);
    if (sz < 1) goto error;
    buf += sz;
    len -= sz;
  }
  return;

 error:
  perror("failed accessing /dev/urandom");
  error_cleanup(4);
}
Ejemplo n.º 4
0
void
ERROR (const char *fmt, ...)
{
  va_list argp;

  if (!really_quiet) {
    if (WANT_NEWLINE())
      fprintf(stderr, "\n");
    fprintf(stderr, "** ERROR ** ");
    va_start(argp, fmt);
    vfprintf(stderr, fmt, argp);
    va_end(argp);
    fprintf(stderr, "\n");
  }
  error_cleanup();
  exit( 1 );
}
void CTCPIPSystemSrvr::send_error(short error, short error_detail, const CEE_handle_def *call_id_)
{
	if (m_nSocketFnum >= 0)
    {
        m_rhdr.error = error;
        m_rhdr.error_detail = error_detail;
        m_rhdr.hdr_type = SRVR_TRANSPORT_ERROR;
        memcpy(m_IObuffer,&m_rhdr,sizeof(m_rhdr));
	    if (m_rhdr.swap == SWAP_YES)
		   HEADER_swap((HEADER*)m_IObuffer);
        if (send(m_nSocketFnum, m_IObuffer, sizeof(HEADER), 0) <= 0)
        {   // Error in send
            error_cleanup();
        }
    }

	if (GTransport.m_TempMemory_list.m_list_length)
		DEALLOCATE_TEMP_MEMORY(&m_call_id);
}
bool CTCPIPSystemSrvr::do_read(bool bfirstblock, HEADER*& hdr, char*& buffer, int& bufcount, long timeout)
{
	ssize_t wcount;
	long wtimeout = (timeout == 0)? -1: timeout * 100;
	wtimeout = -1;
	int sizeof_header = sizeof(HEADER);

	if (m_nSocketFnum < 0)
	{
		SET_ERROR((long)this, NSK, TCPIP, UNKNOWN_API, E_TCPIPROCESS, "do_read", O_DO_WRITE_READ, F_CHECKSOCKET, m_nSocketFnum, 0);
		goto bailout;
	}

	if (bfirstblock)
	{
		if (bufcount < sizeof_header)
		{
			bufcount = getMsgHeader(m_IObuffer, bufcount, MAX_TCP_BUFFER_LENGTH, sizeof_header, wtimeout);
			if (bufcount == -1)
				goto bailout;
		}
		bufcount = bufcount - sizeof(HEADER);

		buffer = m_IObuffer + sizeof(HEADER);
		hdr = (HEADER*)m_IObuffer;
	}
	else
	{
		if ((wcount = recv(m_nSocketFnum, m_IObuffer, MAX_TCP_BUFFER_LENGTH, 0)) <= 0)
        {
			SET_ERROR((long)this, NSK, TCPIP, hdr->operation_id, E_TCPIPROCESS, "do_read - TCP/IP connection closed be the user", O_DO_WRITE_READ, F_AWAITIOX, 0, 0);
			goto bailout;
		}
		bufcount = wcount;
		buffer = m_IObuffer; 
	}
	return true;
bailout:
    error_cleanup();
	return false;

}
short int CTCPIPSystemSrvr::getMsgHeader(char* buf, int cread, int bufSize, int headerSize, long wtimeout)
{
	ssize_t wcount;
	char* pbuf;
	int inp_size;

	int already_read = cread;

	while (already_read < headerSize )
	{
		pbuf = buf + already_read;
		inp_size = bufSize - already_read;

		if ((wcount = recv(m_nSocketFnum, pbuf, inp_size, 0)) <= 0)
		{
			SET_ERROR((long)this, NSK, TCPIP, UNKNOWN_API, E_TCPIPROCESS, "getMsgHeader - TCP/IP connection closed be the user", O_DO_WRITE_READ, F_AWAITIOX, 0, 0);
            error_cleanup();
            return -1;
		}
		already_read += wcount;
	}
	return (short) already_read;
}
Ejemplo n.º 8
0
int main(int argc,char **argv)
{
    int binary = 0,hex = 0;
    const char *output = NULL;
    const char *symbols = NULL;
    const char *flash_security = NULL;
    int c,i;

    error_init();
    id_init();
    code_init();
    cpp_option = alloc_type_n(char *,argc*2);
    while ((c = getopt(argc,argv,"bef:hD:I:m:o:U:V")) != EOF) {
	char opt[] = "-?";

    	switch (c) {

	    case 'b':
		binary = 1;
		break;
	    case 'e':
		allow_extensions = 1;
		break;
	    case 'f':
		if (flash_security)
		    usage(*argv);
		flash_security = optarg;
		break;
	    case 'h':
		hex = 1;
		break;
	    case 'o':
		if (output)
		    usage(*argv);
		output = optarg;
		break;
	    case 'm':
		symbols = optarg;
		break;
	    case 'D':
	    case 'I':
	    case 'U':
		opt[1] = c;
		cpp_option[cpp_options*2] = stralloc(opt);
		cpp_option[cpp_options*2+1] = stralloc(optarg);
		cpp_options++;
		break;
	    case 'V':
		printf("m8cas from m8cutils version %s\n",VERSION);
		exit(0);
	    default:
		usage(*argv);
	}
    }
    if (binary && hex)
	usage(*argv);

    if (cpp_options && !allow_extensions) {
	fprintf(stderr,"CPP options are only supported if using CPP (-e)\n");
	return 1;
    }

    read_protection(flash_security);

    /* move stdin to a safe place, because we may open other files before */
    fd0 = dup(0);
    if (fd0 < 0) {
	perror("dup");
	exit(1);
    }
    (void) close(0);

    if (optind == argc)
	do_file(NULL);
    else {
	for (i = optind; i != argc; i++)
	    do_file(argv[i]);
    }

    resolve();
    if (symbols)
	write_symbols(symbols);
    program_size = text->highest_pc;
    id_cleanup();
    code_cleanup();
    error_cleanup();
    if (!hex)
	for (i = 0; i != security_size; i++)
	    if (security[i]) {
		fprintf(stderr,
		  "output must be Intel HEX for non-zero flash protection\n");
		exit(1);
	    }
    write_file(output ? output : "-",binary,hex);
    return 0;
}