Example #1
0
void mloop()
{
  fd_set rfds;
  fd_set wfds;
  while(loop_flag){
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    rfdset(moption.mcsocket, &rfds);
    wfdset(moption.mcsocket, &wfds);
    cfdset(moption.comm, &rfds, &wfds);
    if(do_select(&rfds, &wfds)){
      do_pong();
      do_free();
    }else{
      do_pong();
      do_recv();
      do_send();
      do_accept(moption.comm, &rfds);
      do_comexe(moption.comm, &rfds);
      do_exechk(moption.comm);
    }
    if(log_level != moption.loglevel){
      lprintf(0, "%s: loglevel change %d to %d\n", __func__, moption.loglevel, log_level);
      moption.loglevel = log_level;
    }
  }
}
Example #2
0
int main(int argc, char** argv)
{
  struct configuration cfg;
  int parent, sock, got;
  unsigned int pkt_num = 0;

  parse_options(argc, argv, &cfg);

  /* Initialise */
  parent = add_socket(&cfg);
  do_ioctl(&cfg, parent);
  sock = parent;
  if( cfg.cfg_protocol == IPPROTO_TCP )
    sock = accept_child(parent);
  do_ts_sockopt(sock);

  /* Run forever */
  while( 1 ) {
    pkt_num ++;
    got = do_recv(sock, pkt_num);
    /* TCP can detect an exit; for UDP, zero payload packets are valid */
    if ( got == 0 && cfg.cfg_protocol == IPPROTO_TCP ) {
      printf( "recvmsg returned 0 - end of stream\n" );
      break;
    }
  }

  close(sock);
  if( cfg.cfg_protocol == IPPROTO_TCP )
    close(parent);
  return 0;
}
Example #3
0
static int oshmem_mkey_recv_cb(void)
{
    MPI_Status status;
    int flag;
    int n;
    int rc;
    opal_buffer_t *msg;
    int32_t size;
    void *tmp_buf;
    oob_comm_request_t *r;

    n = 0;
    r = (oob_comm_request_t *)opal_list_get_first(&memheap_oob.req_list);
    assert(r);
    while (1) {
        my_MPI_Test(&r->recv_req, &flag, &status);
        if (OPAL_LIKELY(0 == flag)) {
            return n;
        }
        MPI_Get_count(&status, MPI_BYTE, &size);
        MEMHEAP_VERBOSE(5, "OOB request from PE: %d, size %d", status.MPI_SOURCE, size);
        n++;
        opal_list_remove_first(&memheap_oob.req_list);

        /* to avoid deadlock we must start request
         * before processing it. Data are copied to
         * the tmp buffer
         */
        tmp_buf = malloc(size);
        if (NULL == tmp_buf) {
            MEMHEAP_ERROR("not enough memory");
            ORTE_ERROR_LOG(0);
            return n;
        }
        memcpy(tmp_buf, (void*)&r->buf, size);
        msg = OBJ_NEW(opal_buffer_t);
        if (NULL == msg) {
            MEMHEAP_ERROR("not enough memory");
            ORTE_ERROR_LOG(0);
            return n;
        }
        opal_dss.load(msg, (void*)tmp_buf, size);

        rc = MPI_Start(&r->recv_req);
        if (MPI_SUCCESS != rc) {
            MEMHEAP_ERROR("Failed to post recv request %d", rc);
            ORTE_ERROR_LOG(rc);
            return n;
        }
        opal_list_append(&memheap_oob.req_list, &r->super);

        do_recv(status.MPI_SOURCE, msg);
        OBJ_RELEASE(msg);

        r = (oob_comm_request_t *)opal_list_get_first(&memheap_oob.req_list);
        assert(r);
    }
    return 1;  
}
Example #4
0
void do_client_run(ctx_t *ctx)
{
	int full = ctx->size;
	int half = ctx->size >> 1;
	int cnt = 0;

	/* 1 */
	do_recv(ctx, full, cnt++);

	/* 3 */
	do_recv(ctx, full, cnt);

	/* 4 */
	do_recv(ctx, half, cnt++);
	do_recv(ctx, half, cnt++);

	do_sleep(50);
}
void file_sync_service(int fd, void *cookie)
{
    syncmsg msg;
    char name[1025];
    unsigned namelen;

    char *buffer = malloc(SYNC_DATA_MAX);
    if(buffer == 0) goto fail;

    for(;;) {
        D("sync: waiting for command\n");

        if(readx(fd, &msg.req, sizeof(msg.req))) {
            fail_message(fd, "command read failure");
            break;
        }
        namelen = ltohl(msg.req.namelen);
        if(namelen > 1024) {
            fail_message(fd, "invalid namelen");
            break;
        }
        if(readx(fd, name, namelen)) {
            fail_message(fd, "filename read failure");
            break;
        }
        name[namelen] = 0;

        msg.req.namelen = 0;
        D("sync: '%s' '%s'\n", (char*) &msg.req, name);

        switch(msg.req.id) {
        case ID_STAT:
            if(do_stat(fd, name)) goto fail;
            break;
        case ID_LIST:
            if(do_list(fd, name)) goto fail;
            break;
        case ID_SEND:
            if(do_send(fd, name, buffer)) goto fail;
            break;
        case ID_RECV:
            if(do_recv(fd, name, buffer)) goto fail;
            break;
        case ID_QUIT:
            goto fail;
        default:
            fail_message(fd, "unknown command");
            goto fail;
        }
    }

fail:
    if(buffer != 0) free(buffer);
    D("sync: done\n");
    adb_close(fd);
}
Example #6
0
int sess_recv(struct rekey_session *sess, mb_t buf) 
{
  int ret;
  if (sess->state != REKEY_SESSION_LISTENING) {
    prtmsg("Cannot read from session while in state %d\n",
           sess->state);
    return -1;
  }
  ret = do_recv(sess->ssl, buf);
  if (ret > 0)
    sess->state = REKEY_SESSION_SENDING;
  return ret;
}
Example #7
0
void dorw(epoll_event& evt, SockData* sd)
{
	
	epoll_event	newEvt = {0};
	newEvt.data.ptr = sd;
	if(evt.events & (EPOLLIN))
	{
		sd->size = do_recv(sd->socket, (char*)&(sd->data), sizeof(sd->data));
		if(sd->size <= 0)
		{
			perror("call of recv error:");
			close(sd->socket);
			close(sd->epfd);
			delete sd;
			exit(1);
		}
		newEvt.events = EPOLLOUT;
	}
	if(evt.events & (EPOLLOUT))
	{
		sd->size = do_send(sd->socket, (char*)&(sd->data), sizeof(sd->data));
		if(sd->size <= 0)
		{
			perror("call of send error:");
			close(sd->socket);
			close(sd->epfd);
			delete sd;
			exit(1);
		}
		newEvt.events = EPOLLIN;
	}

	epoll_ctl(sd->epfd, EPOLL_CTL_MOD, sd->socket, &newEvt);

	if(g_started)
	{
		++g_nMsg;
		if(g_nMsg >= g_maxCount)
		{
			g_stop = GetTickCount();
			g_totalMsg = g_nMsg;
			printf("measure stop: %u.\n", g_stop);

			double time = (double)(g_stop - g_start)/1000;
			printf("max msg: %d; total msg: %d; total time: %fs; msg per second: %f.\n", g_maxCount, g_totalMsg, time, g_totalMsg/time);
			close(sd->epfd);
			exit(0);
		}
	}
}
Example #8
0
/**
 * reads exactly size bytes from the vchan.
 * returns 0 if insufficient data is available, -1 on error, or size on success
 */
int libvchan_recv(struct libvchan *ctrl, void *data, size_t size)
{
    while (1) {
        int avail = libvchan_data_ready(ctrl);
        if (size <= avail)
            return do_recv(ctrl, data, size);
        if (!libvchan_is_open(ctrl))
            return -1;
        if (!ctrl->blocking)
            return 0;
        if (size > rd_ring_size(ctrl))
            return -1;
        if (libvchan_wait(ctrl))
            return -1;
    }
}
Example #9
0
File: io.c Project: CPFL/gxen
int libxenvchan_read(struct libxenvchan *ctrl, void *data, size_t size)
{
	while (1) {
		int avail = fast_get_data_ready(ctrl, size);
		if (avail && size > avail)
			size = avail;
		if (avail)
			return do_recv(ctrl, data, size);
		if (!libxenvchan_is_open(ctrl))
			return -1;
		if (!ctrl->blocking)
			return 0;
		if (libxenvchan_wait(ctrl))
			return -1;
	}
}
Example #10
0
static bool handle_sync_command(int fd, std::vector<char>& buffer) {
    D("sync: waiting for request");

    SyncRequest request;
    if (!ReadFdExactly(fd, &request, sizeof(request))) {
        SendSyncFail(fd, "command read failure");
        return false;
    }
    size_t path_length = request.path_length;
    if (path_length > 1024) {
        SendSyncFail(fd, "path too long");
        return false;
    }
    char name[1025];
    if (!ReadFdExactly(fd, name, path_length)) {
        SendSyncFail(fd, "filename read failure");
        return false;
    }
    name[path_length] = 0;

    const char* id = reinterpret_cast<const char*>(&request.id);
    D("sync: '%.4s' '%s'", id, name);

    switch (request.id) {
      case ID_STAT:
        if (!do_stat(fd, name)) return false;
        break;
      case ID_LIST:
        if (!do_list(fd, name)) return false;
        break;
      case ID_SEND:
        if (!do_send(fd, name, buffer)) return false;
        break;
      case ID_RECV:
        if (!do_recv(fd, name, buffer)) return false;
        break;
      case ID_QUIT:
        return false;
      default:
        SendSyncFail(fd, android::base::StringPrintf("unknown command '%.4s' (%08x)",
                                                     id, request.id));
        return false;
    }

    return true;
}
Example #11
0
static int bot(char *server, char *port)
{
	char *buf;
	char nickm[42];

	sd = do_connect(server, port);
	if (sd < 0)
		error(1, errno, "Failed connecting to %s:%s", server, port);

	buf = malloc(BUFSZ);
	if (!buf)
		goto exit;

	if (pass)
		do_send("PASS %s\r\n", pass);
	do_send("NICK %s\r\n", nick);
	do_send("USER %s 0 0 :%s\r\n", nick, NAME);

	snprintf(nickm, sizeof(nickm), ":%s:", nick);
	while (loop) {
		if (!do_recv(buf, BUFSZ)) {
			char *pos = strstr(buf, " ") + 1;

			if (!pos)
				continue;

			if (strstr(buf, " 001 "))       /* Authenicated/registered */
				do_send("JOIN #%s\r\n", channel);

			if (!strncmp(buf, "PING ", 5)) /* Keepalive */
				do_send("PONG %s\r\n", pos);

			if (!strncmp(pos, "PRIVMSG ", 8) && strstr(pos, nickm)) {
				pos = strchr(buf, '!');
				if (pos)
					*pos = 0;
				do_send("PRIVMSG #%s %s: :Why hello there, stranger!\r\n", channel, &buf[1]);
			}
		}
	}

	free(buf);
exit:
	return close(sd);
}
Example #12
0
static void do_process(int fd, int events)
{
	if(!(events & (EPOLLIN | EPOLLOUT))) 
	{
		LOG(glogfd, LOG_DEBUG, "error event %d, %d\n", events, fd);
		do_close(fd);
		return;
	}
	if(events & EPOLLIN) 
	{
		LOG(glogfd, LOG_TRACE, "read event %d, %d\n", events, fd);
		do_recv(fd);
	}
	if(events & EPOLLOUT) 
	{
		LOG(glogfd, LOG_TRACE, "send event %d, %d\n", events, fd);
		do_send(fd);
	}
}
Example #13
0
int main(int argc, char *argv[])
{
    if (argc < 2) {
        printf("test <send/recv>\n");
        return 0;
    }
    if (strcmp(argv[1], "send")==0) {
        printf("Testing broadcast sending.\n");
        do_send();
    }
    else if (strcmp(argv[1], "recv")==0) {
        printf("Testing broadcast receiving.\n");
        do_recv();
    }
    else {
        printf("Unknown action.\n");
        return 1;
    }
    return 0;
}
Example #14
0
int main(int argc, char* argv[])
{
    int opt;
    while((opt = getopt(argc, argv, "wc:s:f:")) != -1) {
        switch(opt) {
        case 'w': writer = 1; break;
        case 'c': count = atoi(optarg); break;
        case 's': size = atoi(optarg); break;
        case 'f': capacity = atoi(optarg); break;
        default:
            return -1;
        }
    }

    if(writer)
        do_send(capacity, count, size);
    else
        do_recv(capacity, count, size);

    return 0;
}
Example #15
0
static int oshmem_mkey_recv_cb(void)
{
    MPI_Status status;
    int flag;
    int n;
    int rc;
    opal_buffer_t *msg;
    int32_t size;
    void *tmp_buf;
    oob_comm_request_t *r;

    n = 0;
    r = (oob_comm_request_t *)opal_list_get_first(&memheap_oob.req_list);
    assert(r);
    while(r != (oob_comm_request_t *)opal_list_get_end(&memheap_oob.req_list)) {
        my_MPI_Test(&r->recv_req, &flag, &status);
        if (OPAL_LIKELY(0 == flag)) {
            return n;
        }
        PMPI_Get_count(&status, MPI_BYTE, &size);
        MEMHEAP_VERBOSE(5, "OOB request from PE: %d, size %d", status.MPI_SOURCE, size);
        n++;
        opal_list_remove_first(&memheap_oob.req_list);

        /* to avoid deadlock we must start request
         * before processing it. Data are copied to
         * the tmp buffer
         */
        tmp_buf = malloc(size);
        if (NULL == tmp_buf) {
            MEMHEAP_ERROR("not enough memory");
            ORTE_ERROR_LOG(0);
            return n;
        } else {
		    memcpy(tmp_buf, (void*)&r->buf, size);
		    msg = OBJ_NEW(opal_buffer_t);
		    if (NULL == msg) {
		        MEMHEAP_ERROR("not enough memory");
		        ORTE_ERROR_LOG(0);
		        free(tmp_buf);
		        return n;
		    }
		    opal_dss.load(msg, (void*)tmp_buf, size);

            /*
             * send reply before posting the receive request again to limit the recursion size to
             * number of receive requests.
             * send can call opal_progress which calls this function again. If recv req is started
             * stack size will be proportional to number of job ranks.
             */
            do_recv(status.MPI_SOURCE, msg);
            OBJ_RELEASE(msg);
        }

        rc = PMPI_Start(&r->recv_req);
        if (MPI_SUCCESS != rc) {
            MEMHEAP_ERROR("Failed to post recv request %d", rc);
            ORTE_ERROR_LOG(rc);
            return n;
        }
        opal_list_append(&memheap_oob.req_list, &r->super);


        r = (oob_comm_request_t *)opal_list_get_first(&memheap_oob.req_list);
        assert(r);
    }

    return 1;
}
Example #16
0
File: main.c Project: OPSF/uClinux
static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
{
	int status;
	struct file_list *flist;
	char *local_name = NULL;
	char *dir = NULL;
	int save_verbose = verbose;

	if (filesfrom_fd >= 0) {
		/* We can't mix messages with files-from data on the socket,
		 * so temporarily turn off verbose messages. */
		verbose = 0;
	}

	if (verbose > 2) {
		rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
			argc, (long)getpid());
	}

	if (am_daemon && lp_read_only(module_id)) {
		rprintf(FERROR,"ERROR: module is read only\n");
		exit_cleanup(RERR_SYNTAX);
		return;
	}


	if (argc > 0) {
		dir = argv[0];
		argc--;
		argv++;
		if (!am_daemon && !push_dir(dir)) {
			rsyserr(FERROR, errno, "push_dir#4 %s failed",
				full_fname(dir));
			exit_cleanup(RERR_FILESELECT);
		}
	}

	io_start_buffering_in();
	recv_filter_list(f_in);

	if (filesfrom_fd >= 0) {
		/* We need to send the files-from names to the sender at the
		 * same time that we receive the file-list from them, so we
		 * need the IO routines to automatically write out the names
		 * onto our f_out socket as we read the file-list.  This
		 * avoids both deadlock and extra delays/buffers. */
		io_set_filesfrom_fds(filesfrom_fd, f_out);
		filesfrom_fd = -1;
	}

	flist = recv_file_list(f_in);
	verbose = save_verbose;
	if (!flist) {
		rprintf(FERROR,"server_recv: recv_file_list error\n");
		exit_cleanup(RERR_FILESELECT);
	}
	the_file_list = flist;

	if (argc > 0) {
		if (strcmp(dir,".")) {
			argv[0] += strlen(dir);
			if (argv[0][0] == '/')
				argv[0]++;
		}
		local_name = get_local_name(flist,argv[0]);
	}

	status = do_recv(f_in,f_out,flist,local_name);
	exit_cleanup(status);
}
Example #17
0
File: main.c Project: OPSF/uClinux
/*
 * This is called once the connection has been negotiated.  It is used
 * for rsyncd, remote-shell, and local connections.
 */
int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
{
	struct file_list *flist = NULL;
	int status = 0, status2 = 0;
	char *local_name = NULL;

	cleanup_child_pid = pid;
	if (!read_batch) {
		set_nonblocking(f_in);
		set_nonblocking(f_out);
	}

	io_set_sock_fds(f_in, f_out);
	setup_protocol(f_out,f_in);

	if (protocol_version >= 23 && !read_batch)
		io_start_multiplex_in();

	/* We set our stderr file handle to blocking because ssh might have
	 * set it to non-blocking.  This can be particularly troublesome if
	 * stderr is a clone of stdout, because ssh would have set our stdout
	 * to non-blocking at the same time (which can easily cause us to lose
	 * output from our print statements).  This kluge shouldn't cause ssh
	 * any problems for how we use it.  Note also that we delayed setting
	 * this until after the above protocol setup so that we know for sure
	 * that ssh is done twiddling its file descriptors.  */
	set_blocking(STDERR_FILENO);

	if (am_sender) {
		keep_dirlinks = 0; /* Must be disabled on the sender. */
		io_start_buffering_out();
		if (!filesfrom_host)
			set_msg_fd_in(f_in);
		send_filter_list(f_out);
		if (filesfrom_host)
			filesfrom_fd = f_in;

		if (write_batch && !am_server)
			start_write_batch(f_out);
		flist = send_file_list(f_out, argc, argv);
		set_msg_fd_in(-1);
		if (verbose > 3)
			rprintf(FINFO,"file list sent\n");
		the_file_list = flist;

		io_flush(NORMAL_FLUSH);
		send_files(flist,f_out,f_in);
		io_flush(FULL_FLUSH);
		handle_stats(-1);
		if (protocol_version >= 24)
			read_final_goodbye(f_in, f_out);
		if (pid != -1) {
			if (verbose > 3)
				rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
			io_flush(FULL_FLUSH);
			wait_process(pid, &status);
		}
		output_summary();
		io_flush(FULL_FLUSH);
		exit_cleanup(status);
	}

	if (need_messages_from_generator && !read_batch)
		io_start_multiplex_out();

	if (argc == 0)
		list_only |= 1;

	send_filter_list(read_batch ? -1 : f_out);

	if (filesfrom_fd >= 0) {
		io_set_filesfrom_fds(filesfrom_fd, f_out);
		filesfrom_fd = -1;
	}

	if (write_batch && !am_server)
		start_write_batch(f_in);
	flist = recv_file_list(f_in);
	the_file_list = flist;

	if (flist && flist->count > 0) {
		local_name = get_local_name(flist, argv[0]);

		status2 = do_recv(f_in, f_out, flist, local_name);
	} else {
		handle_stats(-1);
		output_summary();
	}

	if (pid != -1) {
		if (verbose > 3)
			rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
		io_flush(FULL_FLUSH);
		wait_process(pid, &status);
	}

	return MAX(status, status2);
}
Example #18
0
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd, int nCmdShow)
{
    WSADATA wsadata;
    WNDCLASS wc;
    RECT window;
    int w, h;
    u_long mode;
    MSG msg;
    int pxformat;
    HDC hdc;
    HGLRC hrc, hrc_tmp;
    PIXELFORMATDESCRIPTOR pfd;
    LARGE_INTEGER freq, now, then;
    struct addrinfo *root, *info;
    const char *version;
    config_t cfg;

    int len;
    uint8_t buf[65536];

    if (__argc != 3) {
        printf("usage: %s [ADDR] [PORT]\n", __argv[0]);
        return 0;
    }

    if (!QueryPerformanceFrequency(&freq))
        return 0;

    if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
        return 0;

    loadconfig(&cfg);

    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0)
        goto EXIT_WSACLEANUP;

    mode = 1;
    if (ioctlsocket(sock, FIONBIO, &mode))
        goto EXIT_CLOSE_SOCK;

    if (getaddrinfo(__argv[1], __argv[2], 0, &root)) {
        printf("getaddrinfo() failed\n");
        goto EXIT_CLOSE_SOCK;
    }

    info = root;
    do {
        if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM)
            continue;

        if (connect(sock, info->ai_addr, info->ai_addrlen))
            break;
    } while ((info = info->ai_next));
    freeaddrinfo(root);

    wc.style = CS_OWNDC;
    wc.lpfnWndProc = wndproc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = instance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = 0;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = NAME;

    if (!RegisterClass(&wc))
        goto EXIT_CLOSE_SOCK;

    memset(&window, 0, sizeof(window));
    if (!AdjustWindowRect(&window, WS_OVERLAPPEDWINDOW, 0))
        goto EXIT_UNREGISTER;

    w = cfg.width + window.right - window.left;
    h = cfg.height + window.bottom - window.top;

    //minSize.x = MIN_WIDTH + window.right - window.left;
    //minSize.y = MIN_HEIGHT + window.bottom - window.top;

    hwnd = CreateWindow(NAME, NAME, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, w, h, 0, 0, instance, 0);
    if (!hwnd)
        goto EXIT_UNREGISTER;

    tme.hwndTrack = hwnd;

    /*if (WSAAsyncSelect(sock, hwnd, WM_SOCKET, FD_READ))
        goto EXIT_DESTROY_WINDOW; */

    hdc = GetDC(hwnd);
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DEPTH_DONTCARE;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;

    pxformat = ChoosePixelFormat(hdc, &pfd);
    if (!SetPixelFormat(hdc, pxformat, &pfd))
        goto EXIT_DESTROY_WINDOW;

    hrc_tmp = wglCreateContext(hdc);
    if (!hrc_tmp)
        goto EXIT_DESTROY_WINDOW;

    if (!wglMakeCurrent(hdc, hrc_tmp)) {
        wglDeleteContext(hrc_tmp);
        goto EXIT_DESTROY_WINDOW;
    }

    if (!linkgl()) {
        wglMakeCurrent(hdc, 0);
        wglDeleteContext(hrc_tmp);
        goto EXIT_DESTROY_WINDOW;
    }

    PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs =
    (void*) wglGetProcAddress("wglCreateContextAttribsARB");
    if (!wglCreateContextAttribs)
        printf("....\n");

    hrc = wglCreateContextAttribs(hdc, 0, ctx_attribs);
    wglMakeCurrent(hdc, 0);
    wglDeleteContext(hrc_tmp);
    if (!hrc)
        goto EXIT_DESTROY_WINDOW;

    if (!wglMakeCurrent(hdc, hrc))
        goto EXIT_DELETE_CONTEXT;

    version = (const char*)glGetString(GL_VERSION);
    printf("GL version: %s\n", version);

    wglSwapIntervalEXT(1);

    if (!do_init(&cfg))
        goto EXIT_DELETE_CONTEXT;

    init_done = 1;

    thread(audio_thread, 0);

    QueryPerformanceCounter(&then);
    while (!done) {
        while ((len = recv(sock, (char*)buf, sizeof(buf), 0)) >= 0)
            do_recv(buf, len);

        QueryPerformanceCounter(&now);
        do_frame((double)(now.QuadPart - then.QuadPart) / freq.QuadPart);
        then = now;
        SwapBuffers(hdc);

        while (PeekMessage(&msg, 0, 0, 0, 1)) {
            if (msg.message == WM_QUIT) {
                done = 1;
                break;
            }

            //TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

EXIT_DELETE_CONTEXT:
    wglMakeCurrent(hdc, 0);
    wglDeleteContext(hrc);
EXIT_DESTROY_WINDOW:
    DestroyWindow(hwnd);
EXIT_UNREGISTER:
    UnregisterClass(NAME, instance);
EXIT_CLOSE_SOCK:
    closesocket(sock);
EXIT_WSACLEANUP:
    WSACleanup();
    return 0;
}
Example #19
0
/* If called without arguments, listen for connections.  Otherwise make a
 * connection to the specified first argument.
 */
int main(int argc, char *argv[]) {
	get_args(argc, argv);
	
	SSL_load_error_strings();
	ERR_load_BIO_strings();
	ERR_load_SSL_strings();
	SSL_library_init();
	OpenSSL_add_all_algorithms();

	int port = DFL_PORT;
	if (prog_args.port != 0) {
		if ((port = atoi(prog_args.port)) <= 0) {
			fprintf(stderr, "%s: port must be positive integer\n",
			        prog_args.name);
			exit(1);
		}
	}

	/* Create and bind the socket.
	 */
	int s;
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = INADDR_ANY;
	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		perror("bind");
		exit(1);
	}
	
	printf("Attempting to create SSL context... ");
	ctx = SSL_CTX_new(SSLv23_server_method());

	if(ctx == NULL) {
		printf("Failed. Aborting.\n");
		return 0;
	}

	printf("\nLoading certificates...\n");
	if(!SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)) {
		ERR_print_errors_fp(stdout);
		SSL_CTX_free(ctx);
		return 0;
	}
	if(!SSL_CTX_use_PrivateKey_file(ctx, PRI_KEY, SSL_FILETYPE_PEM)) {
		ERR_print_errors_fp(stdout);
		SSL_CTX_free(ctx);
		return 0;
	}
	SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
	SSL_CTX_set_timeout(ctx, 6000);
	ssl = SSL_new(ctx);

	do_recv(s);
	
	return 0;
}
int
main(void)
{
	static const kernel_ulong_t bogus_zero =
		(kernel_ulong_t) 0x8765432100000000ULL;
	static const kernel_ulong_t bogus_oflags =
		(kernel_ulong_t) 0xdefaced100000003ULL;
	static const kernel_ulong_t bogus_mode =
		(kernel_ulong_t) 0xdec0deadfacefeedULL;
	static const kernel_ulong_t bogus_fd =
		(kernel_ulong_t) 0xfeedfacedeadba5eULL;
	static const kernel_ulong_t bogus_zero_size =
		(sizeof(kernel_ulong_t) > sizeof(int)) ? (kernel_ulong_t) 0 :
			(kernel_ulong_t) 0xface1e5500000000ULL;
	static const kernel_ulong_t bogus_size =
		(kernel_ulong_t) 0xbadc0dedda7a1057ULL;
	static const kernel_ulong_t bogus_prio =
		(kernel_ulong_t) 0xdec0ded1defaced3ULL;
	static const struct timespec bogus_tmout_data = {
		.tv_sec = (time_t) 0xdeadfacebeeff00dLL,
		.tv_nsec = (long) 0xfacefee1deadfeedLL,
	};
	static const struct timespec future_tmout_data = {
		.tv_sec = (time_t) 0x7ea1fade7e57faceLL,
		.tv_nsec = 999999999,
	};
	struct_sigevent bogus_sev_data = {
		.sigev_notify = 0xdefaced,
		.sigev_signo = 0xfacefeed,
		.sigev_value.sival_ptr = (unsigned long) 0xdeadbeefbadc0dedULL
	};

	const char *errstr;
	long rc;
	kernel_long_t *bogus_attrs = tail_alloc(sizeof(*bogus_attrs) *
		NUM_ATTRS);
	char *msg = tail_alloc(MSG_SIZE);
	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned, bogus_prio_ptr);
	struct timespec *bogus_tmout = tail_memdup(&bogus_tmout_data,
		sizeof(*bogus_tmout));
	struct timespec *future_tmout = tail_memdup(&future_tmout_data,
		sizeof(*future_tmout));
	struct_sigevent *bogus_sev = tail_memdup(&bogus_sev_data,
		sizeof(*bogus_sev));
	int fd = -1;


	fill_memory_ex(msg, MSG_SIZE, MSG_START, MSG_SIZE);
	fill_memory_ex(bogus_attrs, sizeof(*bogus_attrs) * NUM_ATTRS,
		       0xbb, 0x70);


	/* mq_open */

	/* Zero values, non-O_CREAT mode */
	rc = syscall(__NR_mq_open, NULL, bogus_zero, bogus_mode, NULL);
	printf("mq_open(NULL, O_RDONLY) = %s\n", sprintrc(rc));

	/* O_CREAT parsing, other flags, bogs values */
	rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
		     NULL);
	printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, NULL) = %s\n",
	       msg, (unsigned short) bogus_mode, sprintrc(rc));

	/* Partially invalid attributes structure */
	rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
		     bogus_attrs + 1);
	printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, %p) = %s\n",
	       msg, (unsigned short) bogus_mode, bogus_attrs + 1, sprintrc(rc));

	/* Valid attributes structure */
	rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
		     bogus_attrs);
	printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, {mq_flags=%#llx"
	       ", mq_maxmsg=%lld, mq_msgsize=%lld, mq_curmsgs=%lld}) = %s\n",
	       msg, (unsigned short) bogus_mode,
	       (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
	       (long long) bogus_attrs[1],
	       (long long) bogus_attrs[2],
	       (long long) bogus_attrs[3], sprintrc(rc));


	/* mq_timedsend */

	/* Zero values*/
	rc = syscall(__NR_mq_timedsend, bogus_zero, NULL, bogus_zero_size,
		     bogus_zero, NULL);
	printf("mq_timedsend(0, NULL, 0, 0, NULL) = %s\n", sprintrc(rc));

	/* Invalid pointers */
	rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE, bogus_size,
		     bogus_prio, bogus_tmout + 1);
	printf("mq_timedsend(%d, %p, %llu, %u, %p) = %s\n",
	       (int) bogus_fd, msg + MSG_SIZE, (unsigned long long) bogus_size,
	       (unsigned) bogus_prio, bogus_tmout + 1, sprintrc(rc));

	/* Partially invalid message (memory only partially available) */
	rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
		     MSG_SIZE, bogus_prio, bogus_tmout);
	printf("mq_timedsend(%d, %p, %llu, %u, {tv_sec=%lld, tv_nsec=%llu})"
	       " = %s\n",
	       (int) bogus_fd, msg + MSG_SIZE - MSG_CUT,
	       (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
	       (long long) bogus_tmout->tv_sec,
	       zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));

	/* Fully valid message, uncut */
	rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
		     MSG_CUT, bogus_prio, bogus_tmout);
	errstr = sprintrc(rc);
	printf("mq_timedsend(%d, ", (int) bogus_fd);
	printstr(MSG_START + MSG_SIZE - MSG_CUT, MSG_CUT);
	printf(", %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
	       (unsigned long long) MSG_CUT, (unsigned) bogus_prio,
	       (long long) bogus_tmout->tv_sec,
	       zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);

	/* Partially invalid message, cut at maxstrlen */
	rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_CUT, MSG_SIZE,
		     bogus_prio, bogus_tmout);
	errstr = sprintrc(rc);
	printf("mq_timedsend(%d, ", (int) bogus_fd);
	printstr(MSG_START + MSG_CUT, MSG_MAX_UNCUT);
	printf("..., %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
	       (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
	       (long long) bogus_tmout->tv_sec,
	       zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);


	/* mq_timedreceive */

	/* Zero values */
	rc = syscall(__NR_mq_timedreceive, bogus_zero, NULL, bogus_zero_size,
		     NULL, NULL);
	printf("mq_timedreceive(0, NULL, 0, NULL, NULL) = %s\n", sprintrc(rc));

	/* Invalid addresses */
	rc = syscall(__NR_mq_timedreceive, bogus_fd, msg + MSG_SIZE, bogus_size,
		     bogus_prio_ptr + 1, bogus_tmout + 1);
	printf("mq_timedreceive(%d, %p, %llu, %p, %p) = %s\n",
	       (int) bogus_fd, msg + MSG_SIZE, (unsigned long long) bogus_size,
	       bogus_prio_ptr + 1, bogus_tmout + 1, sprintrc(rc));

	/* Invalid fd, valid msg pointer */
	rc = syscall(__NR_mq_timedreceive, bogus_fd, msg, bogus_size,
		     bogus_prio_ptr, bogus_tmout);
	printf("mq_timedreceive(%d, %p, %llu, %p, {tv_sec=%lld, tv_nsec=%llu}) "
	       "= %s\n",
	       (int) bogus_fd, msg, (unsigned long long) bogus_size,
	       bogus_prio_ptr, (long long) bogus_tmout->tv_sec,
	       zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));


	/* mq_notify */

	/* Zero values */
	rc = syscall(__NR_mq_notify, bogus_zero, NULL);
	printf("mq_notify(0, NULL) = %s\n", sprintrc(rc));

	/* Invalid pointer */
	rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev + 1);
	printf("mq_notify(%d, %p) = %s\n",
	       (int) bogus_fd, bogus_sev + 1, sprintrc(rc));

	/* Invalid SIGEV_* */
	rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
	printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
	       ", sigev_signo=%u, sigev_notify=%#x /* SIGEV_??? */}) = %s\n",
	       (int) bogus_fd, bogus_sev->sigev_value.sival_int,
	       bogus_sev->sigev_value.sival_ptr,
	       bogus_sev->sigev_signo, bogus_sev->sigev_notify,
	       sprintrc(rc));

	/* SIGEV_NONE */
	bogus_sev->sigev_notify = SIGEV_NONE;
	rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
	printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
	       ", sigev_signo=%u, sigev_notify=SIGEV_NONE}) = %s\n",
	       (int) bogus_fd, bogus_sev->sigev_value.sival_int,
	       bogus_sev->sigev_value.sival_ptr,
	       bogus_sev->sigev_signo, sprintrc(rc));

	/* SIGEV_SIGNAL */
	bogus_sev->sigev_notify = SIGEV_SIGNAL;
	bogus_sev->sigev_signo = SIGALRM;
	rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
	printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
	       ", sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}) = %s\n",
	       (int) bogus_fd, bogus_sev->sigev_value.sival_int,
	       bogus_sev->sigev_value.sival_ptr, sprintrc(rc));

	/* SIGEV_THREAD */
	bogus_sev->sigev_notify = SIGEV_THREAD;
	bogus_sev->sigev_un.sigev_thread.function =
		(unsigned long) 0xdeadbeefbadc0dedULL;
	bogus_sev->sigev_un.sigev_thread.attribute =
		(unsigned long) 0xcafef00dfacefeedULL;
	rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
	printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
	       ", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD"
	       ", sigev_notify_function=%#lx, sigev_notify_attributes=%#lx})"
	       " = %s\n",
	       (int) bogus_fd, bogus_sev->sigev_value.sival_int,
	       bogus_sev->sigev_value.sival_ptr,
	       bogus_sev->sigev_un.sigev_thread.function,
	       bogus_sev->sigev_un.sigev_thread.attribute, sprintrc(rc));

	/* mq_unlink */

	/* Zero values */
	rc = syscall(__NR_mq_unlink, NULL);
	printf("mq_unlink(NULL) = %s\n", sprintrc(rc));

	/* Invalid ptr */
	rc = syscall(__NR_mq_unlink, msg + MSG_SIZE);
	printf("mq_unlink(%p) = %s\n", msg + MSG_SIZE, sprintrc(rc));

	/* Long unterminated string */
	rc = syscall(__NR_mq_unlink, msg);
	errstr = sprintrc(rc);
	printf("mq_unlink(%p) = %s\n", msg, errstr);


	/* Sending and receiving test */

	if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
		perror_msg_and_fail("asprintf");

# if DUMPIO_READ || DUMPIO_WRITE
	close(0);
# endif
	bogus_attrs[1] = 2;
	bogus_attrs[2] = MSG_SIZE;
	fd = rc = syscall(__NR_mq_open, mq_name,
			  O_CREAT|O_RDWR|O_NONBLOCK, S_IRWXU, bogus_attrs);
	errstr = sprintrc(rc);
	if (rc < 0)
		perror_msg_and_skip("mq_open");
	else
		atexit(cleanup);
# if DUMPIO_READ || DUMPIO_WRITE
	if (fd != 0)
		error_msg_and_skip("mq_open returned fd other than 0");
# endif
	fill_memory_ex(bogus_attrs, sizeof(*bogus_attrs) * NUM_ATTRS,
		       0xbb, 0x70);
	printf("mq_open(\"%s\", O_RDWR|O_CREAT|O_NONBLOCK, 0700"
	       ", {mq_flags=%#llx, mq_maxmsg=2, mq_msgsize=%u"
	       ", mq_curmsgs=%lld}) = %s\n",
	       mq_name, (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
	       MSG_SIZE, (long long) bogus_attrs[3], errstr);

	rc = syscall(__NR_mq_getsetattr, fd, NULL, bogus_attrs);
	if (rc < 0)
		perror_msg_and_skip("mq_getsetattr");
	if ((bogus_attrs[1] < 2) || (bogus_attrs[2] < MSG_SIZE))
		error_msg_and_skip("mq too small");

	do_send(fd, msg, MSG_CUT, future_tmout, false);
	do_send(fd, msg, MSG_SIZE, future_tmout, true);

	memset(msg, '\0', MSG_SIZE);
	do_recv(fd, msg, MSG_CUT, future_tmout, false);

	memset(msg, '\0', MSG_SIZE);
	do_recv(fd, msg, MSG_SIZE, future_tmout, true);

	return 0;
}

#else

SKIP_MAIN_UNDEFINED("__NR_mq_open && __NR_mq_timedsend && "
	"__NR_mq_timedreceive && __NR_mq_notify && __NR_mq_unlink");