コード例 #1
0
void mdsip_open_cb( globus_xio_handle_t xio_handle,
		    globus_result_t result,
		    void *user_arg)
{
  globus_result_t res;
  mdsip_client_t *ctx;
  mdsip_options_t *options = (mdsip_options_t *)user_arg;
  gss_cred_id_t                           credential;
  ctx = mdsip_new_client(options);
  mdsip_test_result(xio_handle,result,ctx,"mdsip_open_cb");
  if (options->port_name == NULL)
  {
    if (options->security_level > 0)
    {
      gss_buffer_desc buffer_desc = GSS_C_EMPTY_BUFFER;
      OM_uint32 minor_status;
      OM_uint32 major_status;
      res = globus_xio_handle_cntl(xio_handle,options->gsi_driver,
				   GLOBUS_XIO_GSI_GET_DELEGATED_CRED,&credential);
      mdsip_test_result(xio_handle,res,ctx,"mdsip_open_cb,GET_DELEGATED_CRED");
      major_status = gss_export_cred(&minor_status,credential,GSS_C_NO_OID,0,&buffer_desc);
      if(major_status != GSS_S_COMPLETE)
      {
        char *error_str;
        globus_gss_assist_display_status_str(&error_str,
                                             NULL,
                                             major_status,
                                             minor_status,
                                             0);
        printf("\nLINE %d ERROR: %s\n\n", __LINE__, error_str);
      }
      else
      {
        char cred_file_name[]="/tmp/x509pp_pXXXXXX";
        int fd=mkstemp(cred_file_name);
        if (fd != -1)
	{
          fchmod(fd,00600);
          write(fd,buffer_desc.value,buffer_desc.length);
          fchmod(fd,00400);
          close(fd);
          setenv("X509_USER_PROXY",cred_file_name,1);
        }
        else
          perror("Error creating proxy credential file");
        major_status = gss_release_buffer(&minor_status,&buffer_desc);
      }
    }
  }
  ctx->mdsip_read_cb=mdsip_authenticate_cb;
  res = globus_xio_register_read(xio_handle,(globus_byte_t *)&ctx->header, sizeof(mdsip_message_header_t), 
				 sizeof(mdsip_message_header_t),NULL,mdsip_read_header_cb,ctx);
  mdsip_test_result(xio_handle,res,ctx,"mdsip_open_cb,globus_xio_register_read");
}
コード例 #2
0
static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size)
{
    globus_byte_t *                     ptr = message;
    globus_size_t                       left = message_size;
    globus_size_t                       to_write;
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    GlobusXIOName(globus_l_xio_test_write_buffer);

    if (buffer_size == 0)
    {
        buffer_size = 1024;
    }
    while ((left > 0) && (result == GLOBUS_SUCCESS))
    {
        to_write = (left > buffer_size) ? buffer_size : left;
        result = globus_xio_write(
                handle,
                ptr,
                to_write,
                to_write,
                &nbytes,
                NULL);

        if (result == GLOBUS_SUCCESS)
        {
            if (nbytes != to_write)
            {
                fprintf(stderr, "Didn't write all I expected.\n");
                result = GlobusXIOErrorEOF();
            }
            left -= nbytes;
            ptr += nbytes;
        }
        else
        {
            fprintf(stderr, "Error writing data: %s\n",
                    globus_object_printable_to_string(globus_error_peek(result)));
        }
    }

    globus_xio_handle_cntl(
            handle,
            http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);

    return result;
}
コード例 #3
0
static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     msg,
    globus_size_t                       msg_size,
    globus_xio_driver_t			http_driver)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    GlobusXIOName(globus_l_xio_test_write_buffer);

    result = globus_xio_write(
	    handle,
	    msg,
	    msg_size,
	    msg_size,
	    &nbytes,
	    NULL);

    if (result == GLOBUS_SUCCESS)
    {
	if (nbytes != msg_size)
	{
	    fprintf(stderr, "Didn't write all I expected.\n");
	    result = GlobusXIOErrorEOF();
	}
    }
    else
    {
	fprintf(stderr, "Error writing data: %s\n",
		globus_object_printable_to_string(globus_error_peek(result)));
    }

    globus_xio_handle_cntl(
            handle,
            http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);

    return result;
}
コード例 #4
0
int
client_test(
    http_test_info_t *			info,
    int					timer)	
{
    int                                 rc = 0;
    globus_result_t                     result;
    int                                 header_cnt = 0;
    char                                content_length_buffer[64];
    globus_xio_http_header_t            headers[2];
    globus_xio_handle_t                 handle;
    int                                 i;
    size_t                              nbytes;
    globus_xio_data_descriptor_t        descriptor;
    int                                 status_code;
    char *                              reason_phrase;


    globus_utp_start_timer(timer);
    if (info->transfer_encoding != NULL)
    {
        headers[header_cnt].name = "Transfer-Encoding";
        headers[header_cnt].value = info->transfer_encoding;

        header_cnt++;

    }

    if ((info->version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
            ((info->transfer_encoding != NULL)
                && strcmp(info->transfer_encoding, IDENTITY) == 0))
    {
        sprintf(content_length_buffer, "%lu", (unsigned long) info->size);

        headers[header_cnt].name = "Content-Length";
        headers[header_cnt].value = &content_length_buffer[0];

        header_cnt++;
    }

    handle = NULL;

    result = http_test_client_request(
	    &handle,
	    info->tcp_driver,
	    info->http_driver,
	    info->stack,
	    info->contact,
	    "%2fpost-test",
	    "POST",
	    info->version,
	    headers,
	    header_cnt);

    if (result != GLOBUS_SUCCESS)
    {
	fprintf(stderr, "Error making request: %s\n",
		globus_object_printable_to_string(
		globus_error_get(result)));
	rc = 50;
	goto error_exit;
    }

    for (i = 0; i < info->iterations; i++)
    {
	result = globus_xio_write(
		handle,
		info->buffer,
		info->size,
		info->size,
		&nbytes,
		NULL);

	if (result == GLOBUS_SUCCESS)
	{
	    if (nbytes != info->size)
	    {
		fprintf(stderr, "Didn't write all I expected.\n");
	    }
	}
	else
	{
	    fprintf(stderr, "Error writing data: %s\n",
		globus_object_printable_to_string(globus_error_peek(result)));
	}
    }
    globus_xio_handle_cntl(
	    handle,
	    info->http_driver,
	    GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
    /* READ RESPONSE */
    result = globus_xio_data_descriptor_init(&descriptor, handle);
    if (result != GLOBUS_SUCCESS)
    {
	rc = 51;

        goto close_exit;
    }
    result = globus_xio_read(
            handle,
            info->buffer,
            0,
            0,
            NULL,
            descriptor);
    if (result != GLOBUS_SUCCESS)
    {
        rc = 51;
        goto close_exit;
    }

    result = globus_xio_data_descriptor_cntl(
            descriptor,
            info->http_driver,
            GLOBUS_XIO_HTTP_GET_RESPONSE,
            &status_code,
            &reason_phrase,
            NULL,
            NULL);
    if (result != GLOBUS_SUCCESS || status_code < 200 || status_code > 299)
    {
        fprintf(stderr, "Get failed with \"%03d %s\"\n",
                status_code,
                reason_phrase);

        rc = 51;
        goto close_exit;
    }

    result = globus_xio_read(
            handle,
            info->buffer,
            info->size,
            1,
            &nbytes,
            NULL);
    if (result && !http_is_eof(result))
    {       
        fprintf(stderr, "Error reading eof from http: %s\n",
                globus_error_print_friendly(globus_error_get(result)));
    }

close_exit:
    globus_xio_close(handle, NULL);
    globus_utp_stop_timer(timer);

error_exit:

    return rc;
}
コード例 #5
0
static
void
globus_l_xio_test_server_request_callback(
    void *                              user_arg,
    globus_result_t                     result,
    const char *                        method,
    const char *                        uri,
    globus_xio_http_version_t           http_version,
    globus_hashtable_t                  headers)
{
    http_test_server_t *                test_server;
    http_test_info_t *			info;
    globus_xio_http_header_t            response_headers[2];
    globus_size_t                       header_cnt=0;
    char                                content_length_buffer[64];
    int                                 rc=0;
    int					i;
    size_t				nbytes;

    test_server = (http_test_server_t*) user_arg;
    info = test_server->info;
    if (result == GLOBUS_SUCCESS &&
            method != NULL && uri != NULL &&
            (strcmp(method, "POST") == 0) &&
            (strcmp(uri, "/post-test") == 0))
    {
	
	for (i = 0; i < info->iterations; i++)
	{
	    result = globus_xio_read(
		    test_server->handle,
		    info->buffer,
		    info->size,
		    info->size,
		    &nbytes,
		    NULL);

	    if (result != GLOBUS_SUCCESS || nbytes != info->size)
	    {
		fprintf(stderr, "Error reading from http: %s\n",
		    globus_object_printable_to_string(
			globus_error_peek(result)));
	    }
	}
    }
    else
    {
        rc = 404;
        goto error_respond_exit;
    } 
    result = globus_xio_read(
	    test_server->handle,
	    info->buffer,
	    info->size,
	    1,
	    &nbytes,
	    NULL);

    if (result && !http_is_eof(result))
    {
        fprintf(stderr, "Error reading eof from http: %s\n",
                globus_error_print_friendly(globus_error_get(result)));
    }

    if (info->transfer_encoding != NULL)
    {
	response_headers[header_cnt].name = "Transfer-Encoding";
	response_headers[header_cnt].value = info->transfer_encoding;

	header_cnt++;
    }

    if ((http_version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
	    ((info->transfer_encoding != NULL)
		&& strcmp(info->transfer_encoding, IDENTITY) == 0))
    {
	    sprintf(content_length_buffer, "%lu", (unsigned long) info->size);

	    response_headers[header_cnt].name = "Content-Length";
	    response_headers[header_cnt].value = &content_length_buffer[0];

	    header_cnt++;
    }

    result = http_test_server_respond(
	    test_server,
	    rc,
	    NULL,
	    response_headers,
	    header_cnt);

    if (result != GLOBUS_SUCCESS)
    {
	goto error_exit;
    }
    result = globus_xio_write(
            test_server->handle,
            info->buffer,
            1,
            1,
            &nbytes,
            NULL);
    globus_xio_handle_cntl(
            test_server->handle,
            info->http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
    info->size = throughput_next_size(info->size);
    if (info->size == -1)
    {
        http_test_server_close_handle(test_server);
        http_test_server_shutdown(test_server);
    }

    return;

error_respond_exit:
    http_test_server_respond(
            test_server,
            rc,
            NULL,
            NULL,
            0);

error_exit:
    http_test_server_close_handle(test_server);
    http_test_server_shutdown(test_server);

}
コード例 #6
0
void mdsip_authenticate_cb(
		globus_xio_handle_t xio_handle, 
		globus_result_t result,
                globus_byte_t *buffer,
		globus_size_t len,
		globus_size_t nbytes,
		globus_xio_data_descriptor_t data_desc,
		void *user_arg)
{
  mdsip_client_t *ctx = (mdsip_client_t *)user_arg;
  char *remote_user = strncpy(malloc(nbytes+1),buffer,nbytes);
  remote_user[nbytes]=0;

  mdsip_test_result(xio_handle,result,ctx,"mdsip_authenticate_cb");
  if (result == GLOBUS_SUCCESS)
  {
    int status;
    globus_result_t res;
    short dummy;
    mdsip_message_t *reply = (mdsip_message_t *)memset(malloc(sizeof(mdsip_message_t)),0,sizeof(mdsip_message_t));
    char *contact;
    res = globus_xio_handle_cntl(xio_handle, 
				 (globus_xio_driver_t)ctx->options->tcp_driver, 
				 GLOBUS_XIO_TCP_GET_REMOTE_CONTACT, 
				 &contact);
    mdsip_test_result(xio_handle,res,ctx,"mdsip_authenticate_cb,GET_REMOTE_CONTACT");
    ctx->host = strcpy(malloc(strlen(contact)+1),contact);
    ctx->host[strcspn(ctx->host,":")]=0;
    res = globus_xio_handle_cntl(xio_handle, 
				 (globus_xio_driver_t)ctx->options->tcp_driver, 
				 GLOBUS_XIO_TCP_GET_REMOTE_NUMERIC_CONTACT, 
				 &contact);
    mdsip_test_result(xio_handle,res,ctx,"mdsip_authenticate_cb,GETREMOTE_NUMERIC_CONTACT");
    ctx->ipaddr = strcpy(malloc(strlen(contact)+1),contact);
    ctx->ipaddr[strcspn(ctx->ipaddr,":")]=0;
    mdsip_host_to_ipaddr(ctx->host,&ctx->addr,&dummy);
    if (ctx->options->security_level > 0)
    {
      gss_buffer_desc                         buffer_desc = GSS_C_EMPTY_BUFFER;
      gss_buffer_t                            buffer = &buffer_desc;
      OM_uint32                               status;
      globus_xio_driver_t driver = (globus_xio_driver_t)ctx->options->gsi_driver;
      globus_result_t res;
      gss_name_t name;
      res = globus_xio_handle_cntl((globus_xio_handle_t)xio_handle, 
				   driver, 
				   GLOBUS_XIO_GSI_GET_PEER_NAME, 
				   &name);
      gss_export_name(&status, name, buffer);
      ctx->remote_user=strcpy(malloc(strlen((char *)buffer->value)+1),(char *)buffer->value);
    }
    else
    {
      ctx->remote_user=remote_user;
    }
    status = mdsip_find_user_mapping(ctx->options->hostfile, ctx->ipaddr,ctx->host,ctx->remote_user,&ctx->local_user);
    if (status & 1 && ctx->options->port_name == 0)
      status = mdsip_become_user(ctx->local_user,ctx->remote_user);
    reply->h.msglen = sizeof(mdsip_message_header_t);
    reply->h.status = status;
    if (status & 1)
    {
      if (ctx->options->security_level > 0)
      {
        fprintf(stdout,"%s, CONNECT - %s/%s@%s(%s)\n",mdsip_current_time(),ctx->remote_user,remote_user,ctx->host,ctx->ipaddr);
        free(remote_user);
      }
      else
        fprintf(stdout,"%s, CONNECT - %s@%s(%s)\n",mdsip_current_time(),ctx->remote_user,ctx->host,ctx->ipaddr);
    }
    else
    {
      fprintf(stdout,"%s, REJECT - %s@%s(%s)\n",mdsip_current_time(),ctx->remote_user,ctx->host,ctx->ipaddr);
    }
    free(ctx->message);
    mdsip_write((void *)xio_handle,ctx,reply,0);
    if (status & 1)
    {
      ctx->mdsip_read_cb = mdsip_do_message_cb;
      res = globus_xio_register_read(xio_handle,(globus_byte_t *)&ctx->header, sizeof(mdsip_message_header_t), sizeof(mdsip_message_header_t),
			       NULL,mdsip_read_header_cb,ctx);
      mdsip_test_result(xio_handle,res,ctx,"mdsip_authenticate_cb,globus_xio_register_read");
    }
    else
      globus_xio_register_close(xio_handle,NULL,mdsip_close_cb,ctx);
  }
  return;
}
int
main(
    int                                     argc,
    char **                                 argv)
{
    globus_xio_driver_t                     tcp_driver;
    globus_xio_stack_t                      stack;
    globus_xio_handle_t                     xio_handle;
    globus_xio_server_t                     xio_server;
    globus_result_t                         res;
    char *                                  cs;
    globus_gridftp_server_control_attr_t    ftp_attr;
    globus_gridftp_server_control_t         ftp_server;
    globus_xio_system_socket_t              system_handle;

    globus_module_activate(GLOBUS_XIO_MODULE);
    globus_module_activate(GLOBUS_GRIDFTP_SERVER_CONTROL_MODULE);

    globus_mutex_init(&gs_l_mutex, NULL);
    globus_cond_init(&gs_l_cond, NULL);

    /*
     *  set up the xio handle
     */
    res = globus_xio_driver_load("tcp", &tcp_driver);
    test_res(res, __LINE__);

    res = globus_xio_stack_init(&stack, NULL);
    test_res(res, __LINE__);

    res = globus_xio_stack_push_driver(stack, tcp_driver);
    test_res(res, __LINE__);

    res = globus_xio_server_create(&xio_server, NULL, stack);
    test_res(res, __LINE__);

    globus_xio_stack_destroy(stack);

    res = globus_xio_server_get_contact_string(xio_server, &cs);
    test_res(res, __LINE__);
    fprintf(stdout, "%s\n", cs);
    globus_free(cs);

    res = globus_xio_server_accept(&xio_handle, xio_server);
    test_res(res, __LINE__);

    fprintf(stdout, "xio connection esstablished.\n");
    /*
     *  server connection is all set up, hand it to server_lib
     */
    res = globus_gridftp_server_control_init(&ftp_server);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_init(&ftp_attr);
    test_res(res, __LINE__);

    globus_xio_server_close(xio_server);

    if(argc > 1)
    {
        globus_gridftp_server_control_attr_set_security(
            ftp_attr, GLOBUS_GRIDFTP_SERVER_LIBRARY_GSSAPI | 
                GLOBUS_GRIDFTP_SERVER_LIBRARY_NONE);
    }
    else
    {
        globus_gridftp_server_control_attr_set_security(
            ftp_attr, GLOBUS_GRIDFTP_SERVER_LIBRARY_NONE);
    }

    res = globus_gridftp_server_control_attr_set_auth(
        ftp_attr, auth_func, NULL);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_resource(
        ftp_attr, globus_l_resource_cb, NULL);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_list(
        ftp_attr, list_cb, NULL);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_idle_time(
        ftp_attr, 900, 60);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_banner(
        ftp_attr, "This is 1 line of banner\nthis is line 2\nline 3");
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_message(
        ftp_attr, "Setting the message after login, 1 line\n");
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_set_log(
        ftp_attr, logging_func, GLOBUS_GRIDFTP_SERVER_CONTROL_LOG_ALL, NULL);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_data_functions(
        ftp_attr, active_connect, NULL, 
        passive_connect, NULL, data_destroy_cb, NULL);

    res = globus_gridftp_server_control_attr_add_send(
        ftp_attr, NULL, transfer, NULL);
    test_res(res, __LINE__);

    res = globus_gridftp_server_control_attr_add_recv(
        ftp_attr, NULL, transfer, NULL);
    test_res(res, __LINE__);

    res = globus_xio_handle_cntl(xio_handle, tcp_driver,
            GLOBUS_XIO_TCP_GET_HANDLE, &system_handle);
    test_res(res, __LINE__);

    globus_gsc_959_command_add(
        ftp_server,
        "SITE MINE",
        globus_gs_cmd_site,
        GLOBUS_GSC_COMMAND_POST_AUTH,
        2,
        2,
        "SITE <sp> MINE!!!!",
        NULL);

    globus_mutex_lock(&globus_l_mutex);
    {
        res = globus_gridftp_server_control_start(
            ftp_server, ftp_attr, system_handle, 
            globus_l_done_cb, FTP_USER_ARG);
        test_res(res, __LINE__);

        while(!globus_l_done)
        {
            globus_cond_wait(&globus_l_cond, &globus_l_mutex);
        }
    }
    globus_mutex_unlock(&globus_l_mutex);

    globus_xio_close(xio_handle, NULL);
    fprintf(stdout, "Ending...\n");
    res = globus_gridftp_server_control_attr_destroy(ftp_attr);
    test_res(res, __LINE__);
    res = globus_gridftp_server_control_destroy(ftp_server);
    test_res(res, __LINE__);

    globus_module_deactivate(GLOBUS_GRIDFTP_SERVER_CONTROL_MODULE);
    globus_module_deactivate(GLOBUS_XIO_MODULE);

    return 0;
}
コード例 #8
0
static
void
gfs_l_xio_cp_read_cb(
    globus_xio_handle_t                 handle,
    globus_result_t                     result,
    globus_byte_t *                     buffer,
    globus_size_t                       len,
    globus_size_t                       nbytes,
    globus_xio_data_descriptor_t        data_desc,
    void *                              user_arg)
{
    gfs_l_xio_read_buffer_t *           read_buf;
    gfs_i_xio_cp_handle_t *             cp_h;

    read_buf = (gfs_l_xio_read_buffer_t *) user_arg;
    cp_h = read_buf->whos_my_daddy;

    globus_mutex_lock(&cp_h->mutex);
    {
        read_buf->nbytes = nbytes;
        if(result != GLOBUS_SUCCESS)
        {
            if(eof)
            {
                read_buf->eof = GLOBUS_TRUE;
            }
            else
            {
                /* what if this is just EOF */
                goto error;
            }
        }
        /* it is possible to get here in the CLOSING state without an error */
        if(cp_h->state == GFS_CIO_CP_STATE_ERROR)
        {
            goto error;
        }

        /* XXX need to get an offset for this buffer */
        read_buf->nbytes = nbytes;
        result = globus_xio_data_descriptor_cntl(
            data_desc,
            NULL,
            GLOBUS_XIO_DD_GET_OFFSET,
            &offset);
        if(result != GLOBUS_SUCCESS)
        {
            goto error;
        }
        read_buf->offset = offset;

        if(!globus_fifo_empty(cp_h->write_q))
        {
            read_buf->write_xio = 
                (globus_xio_handle_t) globus_fifo_dequeue(cp_h->write_q);
            result = globus_xio_handle_cntl(
                read_buf->write_xio,
                GLOBUS_XIO_QUERY,
                GLOBUS_XIO_SEEK,
                read_buf->offset);
            if(result != GLOBUS_SUCCESS)
            {
                goto error;
            }

            result = globus_xio_register_write(
                read_buf->write_xio,
                read_buf->buffer,
                read_buf->nbytes,
                read_buf->nbytes,
                NULL,
                gfs_l_xio_cp_write_cb,
                read_buf);
            if(result != GLOBUS_SUCCESS)
            {
                goto error;
            }
        }
        else
        {
            /* stick this one in the queue */
            globus_fifo_enqueue(&cp_h->read_buffer_q, read_buf);
        }

        if(!eof)
        {
            /* make and post a new one */
            read_buf = (gfs_l_xio_read_buffer_t *)
                globus_calloc(sizeof(gfs_l_xio_read_buffer_t)+block_size, 1);
            read_buf->block_size = cp_h->block_size;
            read_buf->whos_my_daddy = cp_h;
            /* do this last since it can inspire the CLOSING state */
            gfs_l_xio_cp_post_read(xio_h, read_buf);
        }
        else
        {
            /* remove it from close q and close */
            globus_fifo_remove(cp_h->read_all_q, read_buf->read_xio);
            result = globus_xio_register_close(
                read_buf->read_xio,
                NULL,
                gfs_l_xio_cp_close_cb,
                cp_h);
            if(result != GLOBUS_SUCCESS)
            {
                cp_h->read_handle_count--;
            }
            if(cp_h->read_handle_count <= 0)
            {
                gfs_l_xio_close_write_handles(cp_h);
            }
        }
    }
    globus_mutex_unlock(&cp_h->mutex);

    return;

error:
    globus_free(read_buf);
    gfs_l_xio_cp_error(cp_h, result);
    globus_mutex_unlock(&cp_h->mutex);
}
コード例 #9
0
static
void
gfs_l_xio_cp_write_cb(
    globus_xio_handle_t                 handle,
    globus_result_t                     result,
    globus_byte_t *                     buffer,
    globus_size_t                       len,
    globus_size_t                       nbytes,
    globus_xio_data_descriptor_t        data_desc,
    void *                              user_arg)
{
    gfs_l_xio_read_buffer_t *           read_buf;
    gfs_i_xio_cp_handle_t *             cp_h;

    read_buf = (gfs_l_xio_read_buffer_t *) user_arg;
    cp_h = read_buf->whos_my_daddy;
    globus_free(read_buf);

    globus_mutex_lock(&cp_h->mutex);
    {
        if(result != GLOBUS_SUCCESS)
        {
            goto error;
        }
        if(cp_h->state == GFS_CIO_CP_STATE_ERROR)
        {
            goto error;
        }

        /* if there are outstanding read buffers left use this handle
            to write one */
        if(!globus_fifo_empty(cp_h->read_buffer_q))
        {
            read_buf = (gfs_l_xio_read_buffer_t *)
                globus_fifo_dequeue(&cp_h->write_q);

            globus_xio_handle_cntl(
                read_buf->write_xio,
                NULL, /* QUERY MAYBE? */
                GLOBUS_XIO_SEEK,
                read_buf->offset);

            result = globus_xio_register_write(
                read_buf->write_xio,
                read_buf->buffer,
                read_buf->nbytes,
                read_buf->nbytes,
                NULL,
                gfs_l_xio_cp_write_cb,
                read_buf);
            if(result != GLOBUS_SUCCESS)
            {
                goto error;
            }
        }
        /* if read buffers are gone and all read handles are gone
            then we are at eof and can start cloising the writes */
        else if(globus_fifo_empty(cp_h->read_all_q))
        {
            gfs_l_xio_close_write_handles(cp_h);
        }
        /* if still going but nothing to write just put this back in the
            queue */
        else
        {
            globus_fifo_enqueue(&cp_h->write_q, read_buf->write_xio);
        }
    }
    globus_mutex_unlock(&cp_h->mutex);

    if(cp_h->update_cb)
    {
        cp_h->update_cb(offset, nbytes, cp_h->user_arg);
    }

    return;

error:

    globus_free(read_buf);
    gfs_l_xio_cp_error(cp_h, result);
    globus_mutex_unlock(&cp_h->mutex);
}
コード例 #10
0
ファイル: test-dsi.c プロジェクト: ellert/globus-dsi-rest
static
void *
globus_l_dsi_rest_thread(
    void                               *arg)
{
    globus_l_dsi_rest_handle_t         *dsi_rest_handle = arg;
    globus_xio_data_descriptor_t        descriptor;
    size_t                              buf_size = 256;
    unsigned char                      *buf = malloc(buf_size);
    globus_size_t                       nbytes;
    char                               *encoded_uri;

    globus_mutex_lock(&dsi_rest_handle->mutex);
    while (!dsi_rest_handle->terminate)
    {
        char                       *method;
        char                       *uri;
        globus_xio_http_version_t   http_version;
        globus_hashtable_t          headers;
        globus_result_t             result;

        globus_mutex_unlock(&dsi_rest_handle->mutex);
        result = globus_xio_server_accept(
                &dsi_rest_handle->xio_handle,
                dsi_rest_handle->xio_server);
        globus_mutex_lock(&dsi_rest_handle->mutex);

        if (result != GLOBUS_SUCCESS)
        {
            continue;
        }
        result = globus_xio_open(
                dsi_rest_handle->xio_handle,
                NULL,
                NULL);
        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_data_descriptor_init(&descriptor, dsi_rest_handle->xio_handle);
        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_read(
                dsi_rest_handle->xio_handle,
                buf,
                0,
                0,
                &nbytes,
                descriptor);

        if (result != GLOBUS_SUCCESS)
        {
            goto end_this_socket;
        }

        result = globus_xio_data_descriptor_cntl(
                descriptor,
                dsi_rest_handle->http_driver,
                GLOBUS_XIO_HTTP_GET_REQUEST,
                &method,
                &uri,
                &http_version,
                &headers);

        globus_dsi_rest_uri_escape(uri, &encoded_uri);

        char *uripath = globus_common_create_string("%s/%s", dsi_rest_handle->root, encoded_uri);
        if (strcmp(method, "GET") == 0)
        {
            int fd;
            fd = open(uripath, O_RDONLY);
            if (fd < 0)
            {
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        404);
            }
            else
            {
                globus_size_t read_amt = 0;
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        200);

                do
                {
                    read_amt = read(fd, buf, buf_size);
                    if (read_amt > 0)
                    {
                        globus_size_t written_amt = 0;

                        while (written_amt < read_amt)
                        {
                            globus_size_t this_write;

                            result = globus_xio_write(
                                dsi_rest_handle->xio_handle,
                                buf+written_amt,
                                read_amt-written_amt,
                                read_amt-written_amt,
                                &this_write,
                                NULL);
                            if (this_write > 0)
                            {
                                written_amt += this_write;
                            }
                            else if (result != GLOBUS_SUCCESS)
                            {
                                break;
                            }
                        }
                    }
                }
                while (read_amt > 0);
                close(fd);
            }
        }
        else
        {
            int fd = open(uripath, O_WRONLY|O_CREAT, 0700);

            if (fd < 0)
            {
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        500);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
            }
            else
            {
                off_t total_written=0;

                if (nbytes > 0)
                {
                    globus_size_t written_amt = 0;

                    while (written_amt < nbytes)
                    {
                        globus_size_t this_write;

                        this_write = write(fd, buf+written_amt, nbytes-written_amt);
                        if (this_write > 0)
                        {
                            written_amt += this_write;
                            total_written += this_write;
                        }
                    }
                }
                do
                {

                    result = globus_xio_read(
                        dsi_rest_handle->xio_handle,
                        buf,
                        buf_size,
                        1,
                        &nbytes,
                        NULL);
                    if (nbytes > 0)
                    {
                        globus_size_t written_amt = 0;

                        while (written_amt < nbytes)
                        {
                            globus_size_t this_write;

                            this_write = write(fd, buf+written_amt, nbytes-written_amt);
                            if (this_write > 0)
                            {
                                written_amt += this_write;
                                total_written += this_write;
                            }
                        }
                    }
                    if (result != GLOBUS_SUCCESS)
                    {
                        if (globus_error_match(
                                globus_error_peek(result),
                                GLOBUS_XIO_MODULE,
                                GLOBUS_XIO_ERROR_EOF)
                            || globus_xio_driver_error_match(
                                    dsi_rest_handle->http_driver,
                                    globus_error_peek(result),
                                    GLOBUS_XIO_HTTP_ERROR_EOF))
                        {
                            result = GLOBUS_SUCCESS;
                            break;
                        }
                        else
                        {
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                                    500);
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                                    "Content-Length",
                                    "0");
                            globus_xio_handle_cntl(
                                    dsi_rest_handle->xio_handle,
                                    dsi_rest_handle->http_driver,
                                    GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                                    "Connection",
                                    "Close");
                            goto xio_error;
                        }
                    }
                }
                while (nbytes > 0);
                close(fd);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_STATUS_CODE,
                        204);
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Content-Length",
                        "0");
                globus_xio_handle_cntl(
                        dsi_rest_handle->xio_handle,
                        dsi_rest_handle->http_driver,
                        GLOBUS_XIO_HTTP_HANDLE_SET_RESPONSE_HEADER,
                        "Connection",
                        "Close");
            }
        }
xio_error:
        free(uripath);
        result = globus_xio_handle_cntl(
                dsi_rest_handle->xio_handle,
                dsi_rest_handle->http_driver,
                GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);
end_this_socket:
        result = globus_xio_close(
                dsi_rest_handle->xio_handle,
                NULL);
        dsi_rest_handle->xio_handle = NULL;
    }
    dsi_rest_handle->terminate_complete = true;
    globus_cond_signal(&dsi_rest_handle->cond);
    globus_mutex_unlock(&dsi_rest_handle->mutex);

    return NULL;
}