Changeset - c318109520cc
[Not reviewed]
default
0 18 0
Nathan Brink (binki) - 15 years ago 2010-08-07 20:08:12
ohnobinki@ohnopublishing.net
User authentication and some access checking.
18 files changed with 610 insertions and 286 deletions:
0 comments (0 inline, 0 general)
doc/architecture.txt
Show inline comments
 
Concepts:
 

	
 
- trust: The first model of distren will assume that is can trust all other servers it associates
 
    with. This removes a great deal of potential complication and yet this should be addressed
 
    in a later revision of this document.
 

	
 
- server: A process that accepts jobs, manages rendering processes, and talks to other servers
 
  - server identification: each server must be given a unique, alphanumeric identifier.
 
      It may make sense to have a server on a machine named after a domain name by which the
 
      server may be accessed. There doesn't need to be such an association, though. This document
 
      assumes that a server's name shall also remain constant (anything else would complicate...).
 
      If a new URL schema would be used, I suppose that distren://<servername>/ might identify a
 
      distren server.
 

	
 
- job: Represents a collection of renderings that must be unbundled, rendered, and bundled together again.
 
  - job identification: A server shall internally assign numeric job identifiers. It must only
 
      make sure that it never issues the same numeric job identifier to multiple jobs. To allow
 
      multiple servers to share jobs with eachother (which is the the whole point of distren),
 
      a job shall be refered to by prefixing the job a server identification. For example,
 
      distren://<servername>/<jobid>
 
  - job packaging: Currently, the most creative way of dealing with jobs I have at the moment is
 
      storing all of the data necessary to render the job in a tarball. This tarball can be
 
      treated as a directory of a normal filesystem. A job's directory would contain at least one
 
      file called ``distrenjob.xml'' which provides information necessary to render the job. This
 
      will be an XML file rather than a rigidly defined binary format because XML supports
 
      arbitrary data storage out of the box. This should allow different rendering backends to
 
      store the extra information that is specific to that backend.
 
  - post-render rebundling: Again, to make things simpler, the server where a job is rendered
 
      shall be responsible for collecting the individual completed frames and collecting them
 
      into a tarball. This tarball may be called for using a client. This tarball is a bundle
 
      of completed frames and will exclude the seed tarball.
 

	
 
- frame: Represents a distinct, and hopefully small, unit of work that a renderer backend can perform.
 
  - frame identification: Like a job numeral is useless without a qualifying servername, a 
 
      frame's identification number would be useless without an accompanying job identifier. The
 
      numeric value of a frame identification value must be unique to that job. There are no
 
      restrictions of ordering of frame numbers except that they are not to be negative. There
 
      need be no sequencing of frame numbers either. Thus, a frame identification URL would look
 
      like distren://<servername>/<jobid>/frame/<frameid>
 
  - size: A frame hopefully represents a smaller unit of work in terms of the rendering
 
      back-end's capabilities. For example, POV-Ray's CLI can initiate and carry out the rendering
 
      of an entire animation. But distren would hopefully be able to provide a clean method for
 
      rendering each individual frame and bringing the resulting set of files back to the user
 
      in a much shorter time than a single computer could on its own. Many smaller and distinct
 
      is key to a project being benefited by distren.
 
  - dependencies: One cannot escape from frame's completion potentially requiring the completion
 
      of another. Thus, each frame's record must explicitly list the URLs of frames that need to
 
      be completed prior to the said frame. For a server to complete a frame dependent on other
 
      frames, those other frames must be transfered to the first server and made available.
 
  - packaging: To render a single frame and move it about somewhere is normally trivial. However,
 
      one frame or rendering unit of a given backend may produce multiple files. For this reason
 
      and for further uniformity and simplification, the data files representing one frame shall
 
      be transferred using the tarball format.
 

	
 
- client: A distren client is able to submit, query state of, and download completed frames of
 
      jobs registered in a server.
 
  - servers: A server, though having many more functions, shall be able to also perform the list
 
      of actions a client may perform.
 

	
 
- user: A user is an entity which is given access to a distren server.
 
  - user identifiction: As users are per-server, a user shall be identified by the name of the
 
      server he has an account on combined with his handle. A tilde prefixes the username to
 
      differentiate this URL from a job's URL.
 
      distren://<servername>/~<username> , like distren://ohnopub.net/~ohnobinki
 

	
 
- file: There are different uses of files above and distributed rendering requires file distribution.
 
  - file identification: Every file mentioned above was at least in the context of a job. Thus,
 
      file identification numbers shall be assigned in the context of a job identification number.
 
      They shall, however, be numeric.
 
      distren://<servername>/<jobid>/file/<fileid>
etc/distrencommon.conf
Show inline comments
 
/*
 
  Configuration file for distren.
 
  Currently, this file is being prepared as the goal for this project. For instance, the ability to support connecting and communicating with servers is suggested by the server sections.
 
*/
 

	
 
/*
 
  currently, server's are only supported rudimentarily. If a job has n frames and there are s servers, n / (s + 1) frames will be sent to each individual server and also rendered on the machine the job was submitted to. There will be no recursive distribution of jobs, but I want to make that possible in the future. AND, I want job IDs to have significance based on 1: the files, 2: the versions of software used to render just like git, mercurial, or bazaar's commit IDs have significance. This will allow global distribution of renderjobs without requiring central servers to coordinate the jobs - a network only need be distributed. And complex algorithms based on timeouts and completion of jobs should allow slow servers to reassign jobs to fast ones and, possibly, find shorter routes to return the resulting images to the original job submitter. Multiple server declarations are currently included, but an implementation for multiple servers is lacking.
 
*/
 

	
 
server protofusion
 
{
 
  hostname = "protofusion.org"
 
  username = "distrenc"
 
  method = "ssh"
 
  types = {"submit", "distribution"} /* submit means it accepts jobs, distribution means it can host files */
 
}
 

	
 
server ohnopublishing
 
{
 
  hostname = "ohnopublishing.net"
 
  username = "distrenc"
 
  method = "ssh"
 
  types = {"distribution"}
 
}
 

	
 

	
 
server localhost
 
{
 
  hostname = "localhost"
 
  username = "test"
 
  password = "xyzzy123"
 
  method = "tcp"
 
  types = {"submit", "distribution", "master"}
 
}
src/client/libdistren.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matt Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
/*
 
  Implementation of distren_* functions from distren.h excluding distren_job_* functions.
 
 */
 

	
 
#include "common/config.h"
 
#include "common/options.h"
 
#include "common/protocol.h"
 
#include "common/remoteio.h"
 
#include "common/request.h"
 

	
 
#include "libdistren.h"
 

	
 
#include <errno.h>
 
#include <libgen.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 

	
 
/**
 
 * Handle common cleanup actions for distren_init().
 
 */
 
static void distren_init_cleanup(distren_t distren);
 

	
 
int distren_init(distren_t *handle)
 
{
 
  int tmp;
 

	
 
  struct distren_request *req;
 
  void *data;
 

	
 
  const char *username;
 
  const char *pass;
 

	
 
  if(!handle)
 
    return 1;
 

	
 
  *handle = malloc(sizeof(struct distren));
 
  if(!*handle)
 
    return 1;
 

	
 
  memset(*handle, 0, sizeof(struct distren));
 

	
 
  /* now the environment is ready for general use */
 
  if(_distren_getoptions(*handle))
 
    {
 
      fprintf(stderr, "error getting configuration\n");
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 

	
 
  tmp = remoteio_authinfo_get((*handle)->options->remoteio,
 
			      (*handle)->server,
 
			      &username,
 
			      &pass);
 
  if(tmp
 
     || !username
 
     || !pass)
 
    {
 
      fprintf(stderr, "error: unable to find information necessary to connect to the server named ``%s'', please check your configuration file.\n",
 
	      (*handle)->server);
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 

	
 
  tmp = remoteio_open_server(&(*handle)->rem,
 
			     (*handle)->options->remoteio,
 
			     (remoteio_read_handle_func_t)&libdistren_remoteio_read_handle,
 
			     *handle,
 
			     (remoteio_close_handle_func_t)&libdistren_remoteio_close_handle,
 
			     (*handle)->server);
 
  if(tmp)
 
    {
 
      fprintf(stderr, "error: unable to connect to server\n");
 

	
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 

	
 
  /* send off a DISTREN_REQUEST_VERSION to the server */
 
  tmp = distren_request_version(&req, &data, DISTREN_SERVERTYPE_CLIENT, PACKAGE_STRING);
 
  if(tmp)
 
    {
 
      fprintf(stderr, "error: unable to allocate request");
 

	
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 
  distren_request_send((*handle)->rem, req, data);
 
  distren_request_free_with_data(req, data);
 

	
 
  tmp = distren_request_pass(&req, &data, username, pass);
 
  if(tmp)
 
    {
 
      fprintf(stderr, "error: unable to allocate request");
 

	
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 
  distren_request_send((*handle)->rem, req, data);
 
  distren_request_free_with_data(req, data);
 

	
 
  /*
 
   * There is no response to the DISTREN_REQUEST_PASS packet. However,
 
   * we want to ensure that the username/password we sent are valid
 
   * before returning to the claler. Thus, we here send a PING
 
   * packet. If we get the DISTREN_REQUEST_PONG, we know we're
 
   * authenticated. Otherwise, we'll receive a quit/discconect packet.
 
   */
 
  tmp = distren_request_poing(&req, &data, 1, "auth test", strlen("auth test"));
 
  if(tmp)
 
    {
 
      fprintf(stderr, "error: Unable to allocate a DISTREN_REQUEST_PING\n");
 

	
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 
  distren_request_send((*handle)->rem, req, data);
 
  distren_request_free_with_data(req, data);
 

	
 
  /* flush out the above packets. */
 
  while((*handle)->rem
 
	&& (*handle)->state == DISTREN_STATE_VERSION)
 
	&& ((*handle)->state == DISTREN_STATE_VERSION
 
	    || (*handle)->state == DISTREN_STATE_AUTH))
 
      multiio_poll((*handle)->multiio, 500);
 

	
 
  if(!(*handle)->rem)
 
    {
 
      distren_init_cleanup(*handle);
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 
static void distren_init_cleanup(distren_t distren)
 
{
 
  if(distren->rem)
 
    remoteio_close(distren->rem);
 
  distren->rem = NULL;
 
  distren_free(distren);
 
}
 

	
 
#ifdef _WIN32
 
#define FOPEN_BINARY "b"
 
#else
 
#define FOPEN_BINARY ""
 
#endif
 
int distren_submit_file(distren_t handle, distren_job_t *job, const char *filename)
 
{
 
  FILE *in;
 
  char buf[DISTREN_REQUEST_FILE_POST_DATA_LEN];
 
  size_t len;
 
  void *data;
 

	
 
  struct distren_request *req;
 
  struct distren_request_file_post data_header;
 
  distren_request_file_post_context_t post_context;
 

	
 
  /* for basename() to play with */
 
  char *my_filename;
 

	
 
  size_t sendq_dots;
 

	
 
  errno = 0;
 
  in = fopen(filename, "r" FOPEN_BINARY);
 
  if(!in)
 
    {
 
      perror("fopen");
 
      return 1;
 
    }
 

	
 
  fprintf(stderr, "info: Starting upload of %s...\n", filename);
 

	
 
  /* prepare the server for a file upload */
 
  my_filename = strdup(filename);
 
  distren_request_file_post_start(&req,
 
				  &data,
 
				  &post_context,
 
				  handle->file_post_id,
 
				  basename(my_filename));
 
  free(my_filename);
 
  distren_request_send(handle->rem, req, data);
 
  distren_request_free_with_data(req, data);
 
  handle->file_post_id ++;
 

	
 
  /* send file body */
 
  while(in
 
	&& !ferror(in)
 
	&& !feof(in)
 
	&& handle->rem)
 
    {
 
      len = fread(buf, 1, DISTREN_REQUEST_FILE_POST_DATA_LEN, in);
 
      if(len == 0)
 
	continue;
 
      distren_request_file_post(&req,
 
				&data_header,
 
				post_context,
 
				buf,
 
				len);
 
      remoteio_write(handle->rem, req, sizeof(struct distren_request));
 
      /* should we check of handle->rem is NULL or not here...? */
 
      remoteio_write(handle->rem, &data_header, sizeof(struct distren_request_file_post));
 
      remoteio_write(handle->rem, buf, len);
 
      distren_request_free(req);
 

	
 
      /* ensure we have no more than a megabyte waiting to be sent. */
 
      while(handle->rem
 
	    && (sendq_dots = remoteio_sendq_len(handle->rem) / 3) > (1024 * 1024 / DISTREN_REQUEST_FILE_POST_DATA_LEN))
 
	{
 
	  multiio_poll(handle->multiio, -1);
 
	  if(handle->rem)
 
	    while(sendq_dots > remoteio_sendq_len(handle->rem) / 3)
 
	      {
 
		sendq_dots --;
 
		fputc('.', stderr);
 
	      }
 
	}
 
    }
 

	
 
  /*
 
   * declare the upload as finished, marking it as cancelled on I/O
 
   * error.
 
  */
 
  distren_request_file_post_finish(&req, &data, post_context, (!in || ferror(in)) ? 1 : 0);
 
  if(handle->rem)
 
    distren_request_send(handle->rem, req, data);
 
    distren_request_free_with_data(req, data);
 

	
 
  if(in)
 
    fclose(in);
 
  if(!handle->rem)
 
    return 1;
 

	
 
  /* let's block until the file's gone. */
 
  while(handle->rem
 
	&& (sendq_dots = remoteio_sendq_len(handle->rem)))
 
    {
 
      multiio_poll(handle->multiio, -1);
 
      if(handle->rem)
 
	while(sendq_dots / 3 > remoteio_sendq_len(handle->rem) / 3)
 
	  {
 
	    sendq_dots -= 3;
 
	    fputc('.', stderr);
 
	  }
 
    }
 
  fputc('\n', stderr);
 
  fprintf(stderr, "info: %s successfully uploaded, as far as we know.\n", filename);
 

	
 
  return 0;
 
}
 

	
 
int distren_free(distren_t handle)
 
{
 
  if(handle->rem)
 
    remoteio_close(handle->rem);
 
  free(handle);
 
  return 0;
 
}
src/client/libdistren.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matt Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef LIBDISTREN_H_
 
#define LIBDISTREN_H_ 1
 

	
 
/*
 
  Private definitions for libdistren.
 
 */
 

	
 
#include "distren.h"
 

	
 
#include "common/multiio.h"
 
#include "common/options.h"
 
#include "common/remoteio.h"
 

	
 
#include <stdint.h>
 

	
 
enum distren_state
 
  {
 
    /**
 
     * client is waiting for a VERSION packet from the server.
 
     */
 
    DISTREN_STATE_VERSION,
 
    /**
 
     * We are waiting to be authenticated.
 
     */
 
    DISTREN_STATE_AUTH,
 
    DISTREN_STATE_NORMAL,
 
    DISTREN_STATE_UPLOADING,
 
  };
 

	
 
struct distren
 
{
 
  struct options_common *options;
 
  char *server;
 

	
 
  enum distren_state state;
 

	
 
  /*
 
   * If rem is NULL, we're not connected to the server. This is the
 
   * way to detect a communication error.
 
   */
 
  struct remoteio *rem;
 
  /*
 
   * for libdistren_remoteio_read_handle(): determine whether or not
 
   * we've passed through the server's hacky MOTD
 
   */
 
  short done_ad;
 

	
 
  /*
 
   * The servertype bitmask of the remote server.
 
   */
 
  uint32_t servertype;
 

	
 
  /* something on which to call multiio_poll() ;-) */
 
  multiio_context_t multiio;
 

	
 
  /*
 
   * The last-used per-connection DISTREN_REQUEST_FILE_POST
 
   * identifier.
 
   */
 
  uint32_t file_post_id;
 
};
 

	
 
struct distren_job
 
{
 
  char *joburi;
 
};
 

	
 
/*
 
  functions
 
*/
 

	
 
/**
 
   Avoid poluting the public namespace until we fix visibility.
 
 */
 
#define _malloc _distren_malloc
 
/**
 
   All of libdistren should use this rather than malloc.h's malloc.
 
 */
 
void *_malloc(distren_t distren, size_t size);
 

	
 
/**
 
   Avoid poluting the public namespace until we fix visibility.
 
 */
 
#define _free _distren_free
 
/**
 
   All of libdisren should use this instead of malloc.h's free()
 
*/
 
void _free(distren_t distren, void *tofree);
 

	
 
/**
 
 * Sets up the distren handle with information garnered from
 
 * configuration files, etc. Uses the environment variable
 
 * DISTREN_CONFIG or the built-in default config file location.
 
 *
 
 * Also initializes multiio.
 
 */
 
int _distren_getoptions(distren_t handle);
 

	
 
/**
 
 * Unsets-up the distren handle with options loadable from a config file.
 
 *
 
 * Also unloads multiio.
 
 */
 
int _distren_loseoptions(distren_t handle);
 

	
 
/**
 
 * Handle newly read data.
 
 *
 
 * Matches remoteio_read_handle_func_t
 
 */
 
size_t libdistren_remoteio_read_handle(struct remoteio *rem, void *garbage, void *buf, size_t len, distren_t distren);
 

	
 
/**
 
 * React to a remoteio-based connection closing.
 
 *
 
 * Matches remoteio_close_handle_func_t
 
 */
 
void libdistren_remoteio_close_handle(void *garbage, distren_t distren);
 

	
 
#endif
 

	
src/client/libdistren_request.c
Show inline comments
 
/*
 
 * Copyright 2010 Nathan Phillip Brink
 
 *
 
 * This file is a part of DistRen.
 
 *
 
 * DistRen is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * DistRen is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
#include "common/config.h"
 

	
 
#include "libdistren.h"
 

	
 
#include "common/protocol.h"
 
#include "common/remoteio.h"
 
#include "common/request.h"
 

	
 
#include <string.h>
 

	
 
static void handle_ping(struct remoteio *rem, struct distren_request *req, void *req_data);
 
static void handle_pong(distren_t distren, struct remoteio *rem, struct distren_request *req, void *req_data);
 
static void handle_version(distren_t distren, struct distren_request *req, void *req_data);
 
static void handle_disconnect(distren_t distren, struct remoteio *rem, struct distren_request *req, void *req_data);
 

	
 
size_t libdistren_remoteio_read_handle(struct remoteio *rem, void *garbage, void *buf, size_t len, distren_t distren)
 
{
 
  size_t to_return;
 
  size_t last_len;
 
  short err;
 

	
 
  struct distren_request *req;
 
  void *req_data;
 

	
 
  to_return = 0;
 
  while(!distren->done_ad)
 
    {
 
      if(!len)
 
	return to_return;
 

	
 
      putchar(*(char *)buf);
 
      if(*(char *)buf == '\n')
 
	distren->done_ad = 1;
 

	
 
      len --;
 
      to_return ++;
 
      buf ++;
 
    }
 

	
 
  /* hack to get into the loop at all ;-) */
 
  last_len = 1; 
 
  while(last_len)
 
    {
 
      last_len = distren_request_handle(NULL, buf, len, &req, &req_data, &err);
 
      if(err)
 
	{
 
	  remoteio_close(rem);
 
	  return to_return;
 
	}
 
      if(!last_len)
 
	return to_return;
 

	
 
      switch((enum distren_request_type)req->type)
 
	{
 
	case DISTREN_REQUEST_PING:
 
	  handle_ping(rem, req, req_data);
 
	  break;
 

	
 
	case DISTREN_REQUEST_PONG:
 
	  handle_pong(distren, rem, req, req_data);
 
	  break;
 

	
 
	case DISTREN_REQUEST_VERSION:
 
	  handle_version(distren, req, req_data);
 
	  break;
 

	
 
	case DISTREN_REQUEST_DISCONNECT:
 
	  handle_disconnect(distren, rem, req, req_data);
 
	  break;
 

	
 
	default:
 
	  /*
 
	   * we don't implement everything because we don't need to do
 
	   * so. But, we should complain when we get something we
 
	   * don't recognize because... server protocols change
 
	   * ;-). Oh, and when I'm first writing this, this
 
	   * block will be hit a lot ;-).
 
	   */
 
	  fprintf(stderr, "Unrecognized request type: %lu\n", (unsigned long)req->type);
 
	  break;
 
	}
 

	
 
      distren_request_free(req);
 
      buf += last_len;
 
      len -= last_len;
 
    }
 

	
 
  return to_return;
 
}
 

	
 
void libdistren_remoteio_close_handle(void *garbage, distren_t distren)
 
{
 
  distren->rem = NULL;
 
}
 

	
 

	
 
/* handlers */
 

	
 
static void handle_ping(struct remoteio *rem, struct distren_request *req, void *req_data)
 
{
 
  struct distren_request *pong_req;
 
  void *pong_req_data;
 

	
 
  distren_request_poing(&pong_req, &pong_req_data, 0, req_data, req->len);
 
  distren_request_send(rem, pong_req, pong_req_data);
 
  distren_request_free_with_data(pong_req, pong_req_data);
 
}
 

	
 
static void handle_pong(distren_t distren, struct remoteio *rem, struct distren_request *req, void *req_data)
 
{
 
  const char *auth_test_str = "auth test";
 

	
 
  if(req->len == strlen(auth_test_str)
 
     && !strncmp(req_data, auth_test_str, req->len))
 
    distren->state = DISTREN_STATE_NORMAL;
 
}
 

	
 
static void handle_version(distren_t distren, struct distren_request *req, void *req_data)
 
{
 
  struct distren_request_version version;
 
  int tmp;
 

	
 
  tmp = distren_request_parse_version(req, req_data, &version);
 
  if(tmp)
 
    {
 
      fprintf(stderr, "error: Invalid DISTREN_REQUEST_VERSION, disconnecting from server\n");
 
      /*
 
       * our remoteio_close handler sets distren->rem to NULL, thus we
 
       * don't need to return an error code.
 
       */
 
      remoteio_close(distren->rem);
 
      return;
 
    }
 
  distren->servertype = version.servertype;
 
  distren->state = DISTREN_STATE_AUTH;
 

	
 
  fprintf(stderr, "info: connected to a server running %s\n", version.package_string);
 
  if(version.servertype & DISTREN_SERVERTYPE_CLIENT)
 
    fprintf(stderr, "\tis a client\n");
 
  if(version.servertype & DISTREN_SERVERTYPE_SUBMIT)
 
    fprintf(stderr, "\taccepts frame submissions\n");
 
  if(version.servertype & DISTREN_SERVERTYPE_RENDER)
 
    fprintf(stderr, "\trenders frames\n");
 
  if(version.servertype & DISTREN_SERVERTYPE_DISTRIBUTE)
 
    fprintf(stderr, "\thandles file distribution\n");
 
}
 

	
 
static void handle_disconnect(distren_t distren, struct remoteio *rem, struct distren_request *req, void *req_data)
 
{
 
  size_t tmp;
 

	
 
  fputs("warning: The server has disconnected us because ``", stderr);
 
  tmp = fwrite(req_data, 1, req->len, stderr);
 
  fputs("''\n", stderr);
 

	
 
  remoteio_close(distren->rem);
 
}
src/common/libremoteio.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef _DISTREN_LIBREMOTEIO_H
 
#define _DISTREN_LIBREMOTEIO_H
 

	
 
#include "common/multiio.h"
 
#include "common/remoteio.h"
 

	
 
#include <confuse.h>
 
#include <list.h>
 
#include <queue.h>
 

	
 
/**
 
  private declarations for remoteio, to be included by options.c and remoteio.c
 
 */
 

	
 
enum remoteio_method
 
  {
 
    REMOTEIO_METHOD_SSH = 0,
 
#ifndef WINDOWS
 
    REMOTEIO_METHOD_UNIX = 1,
 
#endif
 
    REMOTEIO_METHOD_TCP = 2,
 
    /*    REMOETIO_METHOD_XMLRPC */ /*< maybe someday */
 
    /** method for the remoteio_open_socket() function, where we don't call open() ourselves: */
 
    REMOTEIO_METHOD_SOCKET = 3,
 
    REMOTEIO_METHOD_MAX = 4 /*< This is a number used to check the consitency of remoteio_server structs */
 
  };
 

	
 
struct remoteio_server
 
{
 
  char *name; /*< The friendly named passed to remoteio_open() */
 
  char *hostname;
 
  char *username;
 
  char *password;
 
  enum remoteio_method method;
 
  unsigned int types; /*< See ``Server types'' in protocol.h */
 
};
 

	
 
struct remoteio_opts
 
{
 
  char *ssh_command;
 
  list_t servers;  /* type: (struct remoteio_server *) */
 
  /* store the multiio context for general use */
 
  multiio_context_t multiio;
 
  /* store remoteio's socket_type */
 
  multiio_socket_type_t socket_type;
 

	
 
  /* an argument for the remoteio_read_handle_func_t */
 
  void *generic_handler_data;
 
};
 

	
 
/**
 
 * Used to describe the nature of the data stored in the
 
 * outbound and message queues that power remoteio_write()
 
 * and remoteio_read() (?).
 
 */
 
struct remoteio_packet
 
{
 
  size_t len;
 
  char *data;
 
};
 

	
 

	
 
struct remoteio
 
{
 
  enum remoteio_method method;
 
  struct remoteio_opts *opts;
 
  struct execio *execio;
 
#ifndef WINDOWS
 
  int sock;
 
#endif
 

	
 
  remoteio_read_handle_func_t read_handler;
 
  /* for the read_handler */
 
  void *read_handler_data;
 
  /* so that read_handler_data can be cleaned up */
 
  remoteio_close_handle_func_t close_handler;
 

	
 
  /**
 
   * Store a buffer of data waiting to be processed.
 
   */
 
  struct remoteio_packet inbuf;
 

	
 
  /**
 
   * Provide the asynchronosity abstraction by queuing outgoing messages.
 
   */
 
  queue_t outmsgs;
 

	
 
  /**
 
   * This is disappointingly hacky. If this variable is 0, then
 
   * remoteio_close() will act normal. If set to 1, then
 
   * remoteio_close() will not actually free this struct but instead
 
   * increment this variable to 2. This is so that read_handler can
 
   * call remoteio_close() without segfaulting us.
 
   */
 
  short careful_free;
 
};
 

	
 

	
 
/* lookup table */
 
typedef int remoteio_open_func_t(struct remoteio *rem, struct remoteio_server *server);
 
typedef int remoteio_read_func_t(struct remoteio *rem, void *buf, size_t len, size_t *bytesread);
 
typedef int remoteio_write_func_t(struct remoteio *rem, const void *buf, size_t len, size_t *bytesread);
 
typedef int remoteio_close_func_t(struct remoteio *rem);
 

	
 
struct remoteio_method_funcmap
 
{
 
  enum remoteio_method method;
 

	
 
  remoteio_open_func_t *open_func;
 
  remoteio_read_func_t *read_func;
 
  remoteio_write_func_t *write_func;
 
  remoteio_close_func_t *close_func;
 

	
 
  char *name; /*< used for error messages */
 
};
 

	
 
int remoteio_config(cfg_t *cfg, struct remoteio_opts *opts);
 

	
 
#endif
src/common/options.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "common/config.h"
 

	
 
#include "common/options.h"
 

	
 
#include "common/asprintf.h"
 
#include "common/misc.h"
 
#include "common/multiio.h"
 
#include "common/libremoteio.h"
 
#include "common/protocol.h"
 

	
 
#include <confuse.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <unistd.h>
 

	
 
struct options_common_data
 
{
 
  cfg_t *cfg;
 
};
 

	
 

	
 
/**
 
   Not reentrant because of call to getenv()
 
   @todo replace abort()s with something useful
 
 */
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts, multiio_context_t multiio)
 
{
 
  char *configfileprefix;
 
  char *configfile;
 

	
 
  size_t i;
 

	
 
  char *optstring = "hc:";
 
  char curopt;
 

	
 
  char *workingdir;
 
  char *tmp;
 

	
 
  configfileprefix = NULL;
 
  while((curopt = getopt(argc, argv, optstring)) != (char)-1)
 
    switch(curopt)
 
      {
 
      case 'h':
 
	fprintf(stderr, "libdistren common options\n\
 
\n\
 
\t-h\tShow this help.\n\
 
\t-c <path>\tBasename for configuration files. For instance, if distrencommon.conf is located at /etc/distren/distrencommon.conf, set this option /etc/distren/distren . Default value: %s\n\
 
\n", SYSCONFDIR "/distren");
 
	return 2;
 
	break;
 
      case 'c':
 
	configfileprefix = strdup(optarg);
 
	if(!configfileprefix)
 
	  /* return is unnecessary here */
 
	  fprintf(stderr, "OOM\n");
 
	break;
 
      case ':':
 
	fprintf(stderr, "Option -%c requires an argument\n", optopt);
 
	return 1;
 
      }
 
  /* restore optind for other people who use getopt */
 
  optind = 1;
 
  
 
  if(!configfileprefix
 
     && (configfileprefix = getenv("DISTREN_CONFIG_PREFIX")) )
 
    {
 
      configfileprefix = strdup(configfileprefix);
 
      if(!configfileprefix)
 
	{
 
	  fprintf(stderr, "OOM\n");
 
	  return 1;
 
	}
 
    }
 
  /* For those of you who don't know, the following is an example of concatenation of constant strings by the C compiler. Now, that doesn't mean you can do run-time string concatenation ;-)
 
     strdup is because someday configfile will be customizable via argv[]
 
  */
 
  if(!configfileprefix)
 
      configfileprefix = strdup(SYSCONFDIR);
 
  if(!configfileprefix)
 
    {
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 

	
 
  workingdir = distren_getcwd();
 
  if(!workingdir)
 
    fprintf(stderr, "Error finding working directory, I will not be able to return to it after reading the configuration files\n");
 
  if(chdir(configfileprefix))
 
    fprintf(stderr, "Unable to chdir(\"%s\") where I expected to find config files, expect failure\n", configfileprefix);
 

	
 
  free(configfileprefix);
 

	
 
  _distren_asprintf(&configfile, "%s%s.conf", PACKAGE, myname);
 
  if(!configfile)
 
    {
 
      perror("blah");
 
      free(workingdir);
 
      return 1;
 
    }
 

	
 
  tmp = distren_getcwd();
 
  fprintf(stderr, "using configuration file: ``%s'' (CWD=``%s'')\n", configfile, tmp ? tmp : "");
 
  free(tmp);
 

	
 
  /* initialize structs */
 
  *allopts = malloc(sizeof(struct options_common));
 
  if(!*allopts)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      return 1;
 
    }
 
  memset(*allopts, '\0', sizeof(struct options_common));
 

	
 
  (*allopts)->data = malloc(sizeof(struct options_common_data));
 
  if(!(*allopts)->data)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      free(*allopts);
 
      return 1;
 
    }
 
  memset((*allopts)->data, '\0', sizeof(struct options_common_data));
 

	
 
  (*allopts)->remoteio = malloc(sizeof(struct remoteio_opts));
 
  if(!(*allopts)->data)
 
    {
 
      perror("malloc");
 
      free(workingdir);
 
      free(configfile);
 
      free((*allopts)->data);
 
      free(*allopts);
 
      return 1;
 
    }
 
  memset((*allopts)->remoteio, '\0', sizeof(struct remoteio_opts));
 
  (*allopts)->remoteio->ssh_command = strdup("ssh");
 
  /** @TODO: check for NULL return above -- a segfault dereferencing NULL is more fun than such a check ;-) */
 
  (*allopts)->remoteio->multiio = multiio;
 
  multiio_socket_type_register(multiio, &(*allopts)->remoteio->socket_type);
 

	
 
  cfg_opt_t common_opts[] =
 
    {
 
      CFG_SIMPLE_STR("ssh-command", &(*allopts)->remoteio->ssh_command),
 
      CFG_END()
 
    };
 

	
 
  /**
 
    In these arrays, I should replace NULL with a pointer to a string in the struct which should hold the result of confuse processing an option. This is partially because confuse will not free the values it parses.
 
    the server sections in the distrencommon.conf describe different servers that the client may connect to
 
   */
 
  cfg_opt_t server_opts[] =
 
    {
 
      CFG_STR("username", NULL, CFGF_NONE),
 
      CFG_STR("password", NULL, CFGF_NONE),
 
      CFG_STR("hostname", NULL, CFGF_NONE),
 
      CFG_STR("method", "ssh", CFGF_NONE),
 
      CFG_STR_LIST("types", NULL, CFGF_NONE),
 
      CFG_END()
 
    };
 

	
 
  cfg_opt_t opts[] =
 
    {
 
      CFG_SEC("common",
 
	      common_opts,
 
	      CFGF_NONE),
 
      CFG_SEC("server",
 
	      server_opts,
 
	      CFGF_MULTI | CFGF_TITLE),
 
      CFG_SEC(myname,
 
	      myopts,
 
	      CFGF_NONE),
 
      CFG_FUNC("include",
 
	       &cfg_include),
 
      CFG_END()
 
    };
 

	
 

	
 

	
 
  (*allopts)->data->cfg = cfg_init(opts, 0);
 
  switch(cfg_parse((*allopts)->data->cfg, configfile))
 
    {
 
    default:
 
      fprintf(stderr, "cfg_parse returned an unknown error code\n");
 
    case CFG_FILE_ERROR:
 
      cfg_error((*allopts)->data->cfg, "Couldn't open file ``%s''\n", configfile);
 
      /* no break; on purpose */
 

	
 
    case CFG_PARSE_ERROR:
 

	
 
      fprintf(stderr, "error parsing configuration file, exiting\n");
 

	
 
      free((*allopts)->data);
 
      free(*allopts);
 
      free(configfile);
 

	
 
      return 1;
 

	
 
    case CFG_SUCCESS:
 
      break;
 
    }
 

	
 
  free(configfile);
 

	
 
  *mycfg = cfg_getsec((*allopts)->data->cfg, myname);
 

	
 
  if(chdir(workingdir))
 
    fprintf(stderr, "Unable to return to ``%s'', my old working directory. If you passed relative pathnames on the commandline, those pathnames will likely be disrespected\n", workingdir);
 
  free(workingdir);
 

	
 
  /*
 
   * libdistrencommon's config options:
 
   */
 

	
 
  /* remoteio -- iterate through servers */
 
  i = remoteio_config((*allopts)->data->cfg, (*allopts)->remoteio);
 
  if(i)
 
    {
 
      fprintf(stderr, "cleanup\n");
 
      abort();
 
      return 1;
 
    };
 

	
 
  return 0;
 
}
 

	
 
/**
 
   @todo free remoteio_servers
 
*/
 
int options_free(struct options_common *opts)
 
{
 
  /* free the other common_options struct's members */
 

	
 
  cfg_free(opts->data->cfg);
 

	
 
  free(opts->remoteio);
 
  free(opts->data);
 
  free(opts);
 

	
 
  return 0;
 
}
src/common/protocol.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef DISTREN_PROTOCOL_H
 
#define DISTREN_PROTOCOL_H
 

	
 
#include <openssl/evp.h>
 
#include <stddef.h>
 
#include <stdint.h>
 

	
 
/**
 
 * Server types:
 
 *
 
 * It is assumed that any client has at least the functionality of a
 
 * normal client (i.e., accepts all of the REQUIRED requests). These
 
 * bitmask values add to the types of requests one may make to a
 
 * server. After first connecting to a server or receiving a
 
 * connection, a client shall send a DISTREN_REQUEST_VERSION packet
 
 * describing its version.
 
 */
 
#define DISTREN_SERVERTYPE_SUBMIT (0x1)
 
#define DISTREN_SERVERTYPE_DISTRIBUTE (0x2)
 
#define DISTREN_SERVERTYPE_RENDER (0x4)
 
#define DISTREN_SERVERTYPE_CLIENT (0x8)
 

	
 
/**
 
 * This file defines the constants and structs that distren clients
 
 * and server use to converse.
 
 */
 

	
 

	
 
/**
 
 * List of request types and metadata.
 
 */
 
enum distren_request_type
 
  {
 
    /**
 
     * identifies the version of software being used by the sender and
 
     * tells if it is a client or server. Sends PACKAGE_STRING as well
 
     * as informing the other server what type of server this is.
 
     *
 
     * DATA: struct distren_request followed by PACKAGE_STRING. The length of PACKAGE_STRING must be no longer than 32 bytes.
 
     *
 
     * REQUIRED: ALL
 
    */
 
    DISTREN_REQUEST_VERSION = 1,
 
    /**
 
     * Test if the end has a live server.
 
     *
 
     * Only authenticated clients may use this request. Thus, if a
 
     * client wants to confirm that a DISTREN_REQUEST_PASS request was
 
     * successful, that client may send a DISTREN_REQUEST_PING
 
     * immediately after the DISTREN_REQUEST_PASS and then wait for
 
     * the PONG.
 
     *
 
     * DATA: up to 32 bytes of a PING cookie
 
     *
 
     * REQUIRED: ALL
 
     */
 
    DISTREN_REQUEST_PING = 2,
 
    /**
 
     * Response to DISTREN_REQUEST_PING.
 
     *
 
     * Unauthenticated clients may be penalized for responding to PING
 
     * requests. This is because a newly connecting client should
 
     * queue a DISTREN_REQUEST_VERSION and DISTREN_REQUEST_PASS
 
     * back-to-back before checking for and processing data from the
 
     * remote server.
 
     *
 
     * DATA: up to 32 bytes copied from a received PING cookie
 
     *
 
     * REQUIRED: ALL
 
     */
 
    DISTREN_REQUEST_PONG = 3,
 
    /**
 
     * The data is the a reason describing why the one end is
 
     * disconnecting. The other end should respectfully close()
 
     * the socket or the sender will timeout shortly.
 
     *
 
     * DATA: A string describing the reason for the connection's
 
     * termination.
 
     *
 
     * REQUIRED: ALL
 
     */
 
    DISTREN_REQUEST_DISCONNECT = 4,
 

	
 

	
 
    /**
 
     * Allow a client to identify itself using simple password
 
     * authentication.
 
     *
 
     * As there is no distren request which affirms a
 
     * DISTREN_REQUEST_PASS went through, clients may send a
 
     * DISTREN_REQUEST_PING and wait for the DISTREN_REQUETS_PONG they
 
     * want to block until they're authenticated.
 
     *
 
     * DATA: struct distren_request_pass
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT (for now, since
 
     * server2server links are only protected using password
 
     * authentication, all server types have to support this except
 
     * for the client.)
 
     */
 
    DISTREN_REQUEST_PASS = 5,
 

	
 
    /**
 
     * DATA: struct distren_request_submit
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT
 
     */
 
    DISTREN_REQUEST_SUBMIT = 5,
 
    DISTREN_REQUEST_SUBMIT = 6,
 

	
 
    /**
 
     * Inform the other party about a job.
 
     *
 
     * DATA: struct distren_request_jobinfo
 
     *
 
     * REQUIRED: ALL
 
     */
 
    DISTREN_REQUEST_JOBINFO = 6,
 
    DISTREN_REQUEST_JOBINFO = 7,
 

	
 
    /**
 
     * Request a DISTREN_REQUEST_JOBINFO
 
     *
 
     * DATA: struct distren_request_jobinfo_get
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT, DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_JOBINFO_GET = 7,
 
    DISTREN_REQUEST_JOBINFO_GET = 8,
 

	
 
    /**
 
     * Command the other party to render a frame
 
     *
 
     * DATA: struct distren_request_frame_render
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_RENDER
 
     */
 
    DISTREN_REQUEST_FRAME_RENDER = 8,
 
    DISTREN_REQUEST_FRAME_RENDER = 9,
 
    /**
 
     * Inform the receiver of the sender's state, such as frames being
 
     * rendered or jobs that need to be completed.
 
     *
 
     * DATA: struct distren_request_status
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_RENDER, DISTREN_SERVERTYPE_DISTRIBUTE, DISTREN_SERVERTYPE_CLIENT
 
     */
 
    DISTREN_REQUEST_STATUS = 9,
 
    DISTREN_REQUEST_STATUS = 10,
 

	
 
    /**
 
     * Request that the receiver send a DISTREN_REQUEST_FRAME_STATUS
 
     * packet. No data because the sending party might not beforehand
 
     * know what frames/jobs the receiving server is managing.
 
     *
 
     * DATA: (none)
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_RENDER
 
     */
 
    DISTREN_REQUEST_STATUS_GET = 10,
 
    DISTREN_REQUEST_STATUS_GET = 11,
 

	
 
    /**
 
     * Declare that a client is preparing to post a file using a
 
     * particular post-id and with a user-friendly filename.
 
     *
 
     * Yes, this is evil. Yes, this tries to replicate
 
     * well-established protocols like FTP and HTTP (which is
 
     * sometimes itself mistreated as FTP). Yes, this will be
 
     * buggy. Why is it needed? For my personal coding experience and
 
     * because I don't know off-hand of any libraries that allow a
 
     * file upload to be interleaved over an existing connection with
 
     * its own protocol. Over TCP? We aren't doing real-time streaming
 
     * or anything :-p.
 
     *
 
     * Possible future expansions: ``transparent'' zlib compression.
 
     *
 
     * DATA: struct distren_request_file_post_start
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT, DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_FILE_POST_START = 11,
 
    DISTREN_REQUEST_FILE_POST_START = 12,
 

	
 
    /**
 
     * Allow a client to upload a job tarball over a remoteio line.  A
 
     * client that wants to do this must take care not to overbuffer
 
     * his sendqueue so as to be able to respond to PING packets in
 
     * time. A server receiving such a message will want to write the
 
     * file as directly to disk as possible to avoid bogging the
 
     * server down in swap. This packet may only be sent if
 
     * DISTREN_REQUEST_FILE_POST_START has previously been sent.
 
     *
 
     * It is potential that if a server is DISTREN_SERVERTYPE_SUBMIT
 
     * but not also DISTREN_SERVERTYPE_DISTRIBUTE, that this request
 
     * would be relayed by the DISTREN_SERVERTYPE_SUBMIT server to a
 
     * DISTREN_SERVERTYPE_DISTRIBUTE server so that other clients can
 
     * obtain the file from the distribution server. In this case, the
 
     * file's URL is already known.
 
     *
 
     * Of course, having this sort of functionality at all is where
 
     * the nasty security issues start coming into play :-D.
 
     *
 
     * DATA: struct distren_request_file_post followed by a maximum of 131072 bytes (128kB).
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT, DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_FILE_POST = 12,
 
    DISTREN_REQUEST_FILE_POST = 13,
 

	
 
    /**
 
     * Marks a post-id's file as having completely uploaded. Provides
 
     * verification information so that the file's integrity may be
 
     * verified.
 
     *
 
     * DATA: struct distren_request_file_post_finish
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_SUBMIT, DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_FILE_POST_FINISH = 13,
 
    DISTREN_REQUEST_FILE_POST_FINISH = 14,
 

	
 
    /**
 
     * Request information about obtaining a file (such as a
 
     * cURL-compatible URL) based on a distren file URL.
 
     *
 
     * DATA: struct distren_request_file_find
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_FILE_FIND = 14,
 
    DISTREN_REQUEST_FILE_FIND = 15,
 

	
 
    /**
 
     * Provide information about obtaining a file (such as a URL).
 
     *
 
     * DATA: struct distren_request_file
 
     *
 
     * REQUIRED: DISTREN_SERVERTYPE_DISTRIBUTE
 
     */
 
    DISTREN_REQUEST_FILE = 15,
 
    DISTREN_REQUEST_FILE = 16,
 
  };
 

	
 
struct distren_request
 
{
 
  uint32_t magic;
 
  /* the length of the data associated with the packet excluding the header */
 
  uint32_t len;
 
  /** treat type as an enum distren_request_type using casting */
 
  uint32_t /* enum distren_request_type */ type;
 
};
 

	
 
#define DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN (32)
 
/**
 
 * A DISTREN_REQUEST_VERSION is started with a bitmask specification
 
 * of the DISTREN_SERVERTYPE_* values.
 
 */
 
struct distren_request_version
 
{
 
  uint32_t servertype;
 
  /* + 1 is for terminating NULL */
 
  char package_string[DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN + 1];
 
};
 

	
 
#define DISTREN_REQUEST_PASS_USERNAME_LEN (16)
 
#define DISTREN_REQUEST_PASS_PASS_LEN (32)
 
struct distren_request_pass
 
{
 
  char username[DISTREN_REQUEST_PASS_USERNAME_LEN];
 
  char pass[DISTREN_REQUEST_PASS_PASS_LEN];
 
};
 

	
 
#define DISTREN_REQUEST_FILE_POST_NAME_LEN (64)
 
struct distren_request_file_post_start
 
{
 
  /**
 
   * Uniquely identify this file upload (per connection).
 
   */
 
  uint32_t post_id;
 
  /**
 
   * The user-friendly filename
 
   */
 
  char filename[DISTREN_REQUEST_FILE_POST_NAME_LEN];
 
};
 

	
 
#define DISTREN_REQUEST_FILE_POST_DATA_LEN (1024*128)
 
struct distren_request_file_post
 
{
 
  /**
 
   * Uniquely identify this file upload (per connection).
 
   */
 
  uint32_t post_id;
 
};
 

	
 
struct distren_request_file_post_finish
 
{
 
  /**
 
   * Uniquely identify this file upload (per connection).
 
   */
 
  uint32_t post_id;
 
  /**
 
   * Marks this upload as failed/cancelled.
 
   */
 
  uint32_t cancel;
 
  /**
 
   * An SHA1sum of the entire file. Only set if last_packet is
 
   * nonzero. Should be otherwise ignored.
 
   */
 
  unsigned char sha[EVP_MAX_MD_SIZE];
 
};
 

	
 
/**
 
 * initializes and allocates request
 
 */
 
int distren_request_new(struct distren_request **req, uint32_t len, enum distren_request_type type);
 

	
 
struct remoteio;
 
/**
 
   Takes a struct distren_request and its associated data, allocates
 
   a new block of data to hold the whole packet, and packets the req
 
   header and data together.
 

	
 
   @param rem A remoteio handle to ship this packet off to
 
   @param req Something you initialized with distren_request_new(). You are responsible for distren_request_free()ing this yourself.
 
   @param data A chunk of data the size of req->len. You are responsible for free()ing this yourself.
 
   @return 0 on success and 1 on failure.
 
 */
 
int distren_request_send(struct remoteio *rem, struct distren_request *req, void *data);
 

	
 
/**
 
 * initializes and allocates request based on raw input data
 
 * which includes the headers of the request.
 
 *
 
 * @return 0 on success, 1 on failure
 
 */
 
int distren_request_new_fromdata(struct distren_request **req, void *data, size_t len);
 

	
 
/**
 
 * Parses requests in a way suitable to be called from a
 
 * remoteio_read_handle_func_t.
 
 *
 
 * If this function returns non-zero, you should handle the request
 
 * and then call this function again (after shortening len and
 
 * incrementing buf, of course).
 
 *
 
 * @param expectlen A pointer to a state variable that, if not NULL, can speed up this function. If you want to use this, you must set the variable to 0 before the first call and preserve the variable.
 
 * @param buf the input buffer.
 
 * @param len the length of buf.
 
 * @param req a pointer to where we should set NULL if we don't have a full request or where we store the address of a newly allocated request. You should call distren_request_free() on this.
 
 * @param req_data a pointer to where the request data pointer should be stored. This will just be set to a pointer in buf, so don't free() this.
 
 * @param err this will be set to 0 if there is no error and to 1 if there is an error and the connection should be closed. You must check this.
 
 * @return number of bytes that we have used from buf and that should be marked used.
 
 */
 
size_t distren_request_handle(size_t *expectlen,
 
			      void *buf,
 
			      size_t len,
 
			      struct distren_request **req,
 
			      void **req_data,
 
			      short *err);
 

	
 
/**
 
 * frees request
 
 */
 
int distren_request_free(struct distren_request *req);
 

	
 
/**
 
 * An implementation of remoteio_read_handle_func_t for use with remoteio.
 
 *
 
 * To use this handler, first initialize a struct distren_request_remoteio_data.
 
 * (to be continued... or not...? ;-) )
 
 */
 
/* size_t distren_request_remoteio_handle(struct remoteio *rem, void *generic_data, void *buf, size_t len, void *data); */
 

	
 
#endif
src/common/remoteio.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
*/
 

	
 
#include "common/config.h"
 

	
 
#include "common/libremoteio.h"
 
#include "common/execio.h"
 
#include "common/asprintf.h"
 

	
 
#include <list.h>
 

	
 
#include <errno.h>
 
#ifndef _WIN32
 
#include <netdb.h>
 
#endif
 
#include <poll.h>
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <sys/types.h>
 
#ifdef _WIN32
 
#include <winsock2.h>
 
#include <ws2tcpip.h>
 
#else
 
#include <sys/socket.h>
 
#endif
 
#include <unistd.h>
 
#include <queue.h>
 

	
 
#ifndef _WIN32
 
#include <sys/un.h>
 
#endif
 

	
 
/* local */
 

	
 
#define REMOTEIO_DEFAULT_PORT "4050"
 

	
 
int _remoteio_handle_write(multiio_context_t multiio,
 
			   int fd,
 
			   short revent,
 
			   struct remoteio_opts *opts,
 
			   struct remoteio *rem);
 
int _remoteio_handle_read(multiio_context_t multiio,
 
			  int fd,
 
			  short revent,
 
			  struct remoteio_opts *opts,
 
			  struct remoteio *rem);
 

	
 
int _remoteio_ssh_open(struct remoteio *rem, struct remoteio_server *server);
 
int _remoteio_ssh_read(struct remoteio *rem, void *buf, size_t len, size_t *bytesread);
 
int _remoteio_ssh_write(struct remoteio *rem, const void *buf, size_t len, size_t *byteswritten);
 
int _remoteio_ssh_close(struct remoteio *rem);
 

	
 
/**
 
   ``named pipes''
 
 */
 
#ifndef _WIN32
 
int _remoteio_sock_open(struct remoteio *rem, struct remoteio_server *server);
 
int _remoteio_sock_close(struct remoteio *rem);
 
#endif
 
int _remoteio_sock_read(struct remoteio *rem, void *buf, size_t len, size_t *bytesread);
 
int _remoteio_sock_write(struct remoteio *rem, const void *buf, size_t len, size_t *byteswritten);
 

	
 
/**
 
   These borrow from _remoteio_sock_read() and _remoteio_sock_write().
 
 */
 
int _remoteio_tcp_open(struct remoteio *rem, struct remoteio_server *server);
 
int _remoteio_tcp_close(struct remoteio *rem);
 

	
 
/**
 
  lookup table for different methods of remoteio:
 
  the enum remoteio_method is the index of the entry to use for that method. 
 
  Regardless, a NULL terminator is required because the configuration function
 
  searches through this table for the method specified in the config file.
 
*/
 
struct remoteio_method_funcmap funcmap[] = 
 
  {
 
    /* [REMOTEIO_METHOD_SSH] */
 
    {REMOTEIO_METHOD_SSH, &_remoteio_ssh_open, &_remoteio_ssh_read, &_remoteio_ssh_write, &_remoteio_ssh_close, "ssh"},
 
#ifndef _WIN32
 
    {REMOTEIO_METHOD_UNIX, &_remoteio_sock_open, &_remoteio_sock_read, &_remoteio_sock_write, &_remoteio_sock_close, "unix"},
 
#endif
 
    {REMOTEIO_METHOD_TCP, &_remoteio_tcp_open, &_remoteio_sock_read, &_remoteio_sock_write, &_remoteio_tcp_close, "tcp"},
 
    {REMOTEIO_METHOD_SOCKET, NULL, &_remoteio_sock_read, &_remoteio_sock_write, &_remoteio_sock_close},
 
    {REMOTEIO_METHOD_MAX, NULL, NULL, NULL, NULL, NULL}
 
  };
 

	
 
struct remoteio_server *remoteio_getserver(const struct remoteio_opts *opts, const char *servername);
 

	
 
void remoteio_packet_free(struct remoteio_packet *packet);
 

	
 
int remoteio_config(cfg_t *cfg, struct remoteio_opts *opts)
 
{
 
  size_t numservers;
 
  size_t counter, counter2;
 
  static int haslisted_methods = 0;
 
  
 
  struct remoteio_server aserver;
 

	
 
  multiio_socket_type_register(opts->multiio, &opts->socket_type);
 

	
 
  multiio_event_handler_register(opts->multiio,
 
				 opts->socket_type,
 
				 POLLOUT,
 
				 (multiio_event_handler_func_t)&_remoteio_handle_write,
 
				 opts);
 
  multiio_event_handler_register(opts->multiio,
 
				 opts->socket_type,
 
				 POLLIN,
 
				 (multiio_event_handler_func_t)&_remoteio_handle_read,
 
				 opts);
 

	
 
  opts->servers = list_init();
 
  if(!opts->servers)
 
    {
 
      fprintf(stderr, "@todo cleanup!\n");
 
      abort();
 
    }
 
  
 
  numservers = cfg_size(cfg, "server");
 
  for(counter = 0; counter < numservers; counter ++)
 
    {
 
      cfg_t *cfg_aserver;
 
      char *method;
 
      
 
      cfg_aserver = cfg_getnsec(cfg, "server", counter);
 
      
 
      aserver.name = NULL;
 
      aserver.hostname = NULL;
 
      aserver.username = NULL;
 
      aserver.password = NULL;
 
      
 
      aserver.name = strdup(cfg_title(cfg_aserver));
 
      aserver.hostname = strdup(cfg_getstr(cfg_aserver, "hostname"));
 
      aserver.username = strdup(cfg_getstr(cfg_aserver, "username"));
 
      if(cfg_getstr(cfg_aserver, "password"))
 
	aserver.password = strdup(cfg_getstr(cfg_aserver, "password"));
 

	
 
      aserver.method = REMOTEIO_METHOD_MAX;
 
      method = cfg_getstr(cfg_aserver, "method");
 
      for(counter2 = 0; funcmap[counter2].name; counter2 ++)
 
	if(strcmp(method, funcmap[counter2].name) == 0)
 
	  aserver.method = funcmap[counter2].method;
 
      if(aserver.method == REMOTEIO_METHOD_MAX)
 
	{
 
	  fprintf(stderr, "No such method as %s\n", method);
 
	  if(!haslisted_methods)
 
	    {
 
	      fprintf(stderr, "Available methods:\n");
 
	      for(counter2 = 0; funcmap[counter2].name; counter2 ++)
 
		fprintf(stderr, "\t%s\n", funcmap[counter2].name);
 
	      
 
	      haslisted_methods ++;
 
	    }
 
	  abort();
 
	}
 
      list_insert_after(opts->servers, &aserver, sizeof(struct remoteio_server));
 
    }
 
  
 
  return 0;
 
}
 

	
 
int remoteio_generic_data_set(struct remoteio_opts *opts, void *generic_data)
 
{
 
  opts->generic_handler_data = generic_data;
 

	
 
  return 0;
 
}
 

	
 
int remoteio_open_common(struct remoteio **remoteio,
 
			 enum remoteio_method method,
 
			 struct remoteio_opts *opts,
 
			 remoteio_read_handle_func_t read_handler,
 
			 void *read_handler_data,
 
			 remoteio_close_handle_func_t close_handler)
 
{
 
  struct remoteio *rem;
 

	
 
  rem = malloc(sizeof(struct remoteio));
 
  if(!rem)
 
    {
 
      fprintf(stderr, "OOM\n");
 
      return 2;
 
    }
 

	
 
  *remoteio = rem;
 

	
 
  rem->execio = NULL;
 
  rem->method = method;
 
  rem->opts = opts;
 
  rem->inbuf.data = NULL;
 
  rem->inbuf.len = 0;
 
  rem->outmsgs = q_init();
 
  rem->read_handler = read_handler;
 
  rem->read_handler_data = read_handler_data;
 
  rem->close_handler = close_handler;
 
  /*
 
   * the following initialization is very important... though the
 
   * others are too, I suppose :-p. See remoteio_close()
 
   */
 
  rem->careful_free = 0;
 

	
 
  return 0;
 
}
 

	
 
int remoteio_open_socket(struct remoteio **remoteio,
 
			 struct remoteio_opts *opts,
 
			 remoteio_read_handle_func_t read_handler,
 
			 void *read_handler_data,
 
			 remoteio_close_handle_func_t close_handler,
 
			 int fd)
 
{
 
  struct remoteio *rem;
 
  if(remoteio_open_common(remoteio, REMOTEIO_METHOD_SOCKET, opts, read_handler, read_handler_data, close_handler))
 
    return 1;
 
  rem = *remoteio;
 

	
 
  rem->sock = fd;
 
  multiio_socket_add(opts->multiio, rem->sock, opts->socket_type, rem, POLLIN);
 

	
 
  return 0;
 
}
 

	
 
int remoteio_open_server(struct remoteio **remoteio,
 
			 struct remoteio_opts *opts,
 
			 remoteio_read_handle_func_t read_handler,
 
			 void *read_handler_data,
 
			 remoteio_close_handle_func_t close_handler,
 
			 const char *servername)
 
{
 
  struct remoteio_server *theserver;
 
  struct remoteio *rem;
 

	
 
  int tmp;
 

	
 
  if(!opts)
 
    {
 
      fprintf(stderr, "%s:%d: no null opts!\n\tThis is a bug, please report it (after making sure it isn't already reported)\n", __FILE__, __LINE__);
 
      return 1;
 
    }
 

	
 
  theserver = remoteio_getserver(opts, servername);
 
  if(!theserver)
 
    {
 
      fprintf(stderr, "%s:%d: Could not find server named ``%s''\n", __FILE__, __LINE__, servername);
 
      return 1;
 
    }
 

	
 
  if(theserver->method >= REMOTEIO_METHOD_MAX
 
     || theserver->method < 0)
 
    {
 
      fprintf(stderr, "%s:%d: Unsupported remoteio method %d\n\tThis is a bug, probably indicating memory corruption. This is, of course, probably my fault (not your hardware's) ;-)\n", __FILE__, __LINE__, theserver->method);
 
      return 1;
 
    }
 

	
 
  if(remoteio_open_common(remoteio, theserver->method, opts, read_handler, read_handler_data, close_handler))
 
    return 1;
 
  rem = *remoteio;
 

	
 
  tmp = funcmap[theserver->method].open_func(rem, theserver);
 
  if(tmp)
 
    {
 
      fprintf(stderr, "Error using method %s for server ``%s''", funcmap[theserver->method].name, servername);
 
      free(rem->inbuf.data);
 
      q_free(rem->outmsgs, QUEUE_NODEALLOC);
 
      free(rem);
 
      *remoteio = NULL;
 
      return tmp;
 
    }
 

	
 
  /**
 
   * @todo make this code slightly more generic... able to handle
 
   * execio's multi-sockets by letting execio register itself with
 
   * multiio instead of us registering here perhaps
 
   */
 
  multiio_socket_add(opts->multiio, rem->sock, opts->socket_type, rem, POLLIN);
 
  
 
  return 0;
 
}
 

	
 
int remoteio_authinfo_get(struct remoteio_opts *rem_opts, const char *servername, const char **username, const char **pass)
 
{
 
  struct remoteio_server *server;
 

	
 
  *username = NULL;
 
  *pass = NULL;
 

	
 
  server = remoteio_getserver(rem_opts, servername);
 
  if(!server)
 
    {
 
      fprintf(stderr, "%s:%d: Could not find server named ``%s''\n", __FILE__, __LINE__, servername);
 
      return 1;
 
    }
 

	
 
  *username = server->username;
 
  *pass = server->password;
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * Implementation of multiio_event_handler_func_t
 
 */
 
int _remoteio_handle_read(multiio_context_t multiio,
 
			  int fd,
 
			  short revent,
 
			  struct remoteio_opts *opts,
 
			  struct remoteio *rem)
 
{
 
  struct remoteio_packet packet;
 
  size_t readlen;
 
  char buf[8192];
 

	
 
  int tmp;
 

	
 
  packet.len = 0;
 
  packet.data = NULL;
 

	
 
  if(rem->sock != fd)
 
    fprintf(stderr, "%d != %d\n", rem->sock, fd);
 

	
 
  tmp = funcmap[rem->method].read_func(rem, buf, sizeof(buf), &readlen);
 
  if(tmp)
 
    {
 
      remoteio_close(rem);
 
      return 1;
 
    }
 

	
 
  /* expand the input buffer */
 
  packet.len = rem->inbuf.len + readlen;
 
  packet.data = malloc(rem->inbuf.len + readlen);
 
  if(!packet.data)
 
    {
 
      fprintf(stderr, "OOM!\n");
 

	
 
      return 1;
 
    }
 
  if(rem->inbuf.data)
 
    memcpy(packet.data, rem->inbuf.data, rem->inbuf.len);
 
  memcpy(packet.data + rem->inbuf.len, buf, readlen);
 
  free(rem->inbuf.data);
 
  memcpy(&rem->inbuf, &packet, sizeof(struct remoteio_packet));
 

	
 
  /*
 
   * readlen wil now keeps track of how many bytes the handler
 
   * function has read.
 
   *
 
   * Call the read_handler. Set careful_free, see remoteio_close(), so
 
   * that rem->read_handler() may call remoteio_close() without
 
   * segfaulting us ;-).
 
   */
 
  rem->careful_free = 1;
 
  readlen = (*rem->read_handler)(rem, rem->opts->generic_handler_data, rem->inbuf.data, rem->inbuf.len, rem->read_handler_data);
 
  if(rem->careful_free == 2)
 
    {
 
      rem->careful_free = 0;
 
      remoteio_close(rem);
 

	
 
      return 0;
 
    }
 
  rem->careful_free = 0;
 

	
 
  memmove(rem->inbuf.data, rem->inbuf.data + readlen, rem->inbuf.len - readlen);
 
  rem->inbuf.len -= readlen;
 

	
 
  return 0;
 
}
 

	
 

	
 
int remoteio_write(struct remoteio *rem, const void *buf, size_t len)
 
{
 
  struct remoteio_packet *packet;
 
  struct pollfd pollfd;
 
  size_t bytes_written;
 
  int err;
 

	
 
  /**
 
   * This is probably about the only optimization that exists in
 
   * distren.... :-D
 
   *
 
   * Write to the client immediately if there are no other messages
 
   * waiting and if the client will accept it.
 
   */
 
  if(q_empty(rem->outmsgs))
 
    {
 
      pollfd.fd = rem->sock;
 
      pollfd.revents = POLLOUT;
 
      pollfd.events = 0;
 
      poll(&pollfd, 1, 0);
 
      if(pollfd.events & POLLOUT)
 
	{
 
	  err = funcmap[rem->method].write_func(rem, buf, len, &bytes_written);
 
	  if(bytes_written > 0)
 
	    {
 
	      len -= bytes_written;
 
	      buf += bytes_written;
 
	    }
 
	}
 
    }
 

	
 
  /**
 
   * zero length is easy... and might be possible if the above
 
   * optimization works ;-)
 
   */
 
  if(!len)
 
    return 0;
 

	
 
  packet = malloc(sizeof(struct remoteio_packet));
 
  if(!packet)
 
    {
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 

	
 
  packet->len = len;
 
  packet->data = malloc(len);
 
  if(!packet->data)
 
    {
 
      free(packet);
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 

	
 
  memcpy(packet->data, buf, len);
 

	
 
  q_enqueue(rem->outmsgs, packet, 0);
 
  multiio_socket_event_enable(rem->opts->multiio, rem->sock, POLLOUT);
 

	
 
  return 0;
 
}
 

	
 
int _remoteio_handle_write(multiio_context_t multiio,
 
			   int fd,
 
			   short revent,
 
			   struct remoteio_opts *opts,
 
			   struct remoteio *rem)
 
{
 
  struct remoteio_packet *packet;
 
  size_t written_amount;
 

	
 
  int tmp;
 

	
 
  /*
 
   * check if we're out of stuff to write.
 
   */
 
  if(q_empty(rem->outmsgs))
 
    {
 
      multiio_socket_event_disable(multiio, fd, POLLOUT);
 
      return 0;
 
    }
 

	
 
  packet = q_front(rem->outmsgs);
 
  tmp = funcmap[rem->method].write_func(rem, packet->data, packet->len, &written_amount);
 

	
 
  /**
 
     Disconnect in case of write error.
 
  */
 
  if(tmp)
 
    {
 
      fprintf(stderr, __FILE__ ":%d: error handling for write() needs to be inserted into remoteio.... perhaps.. ;-)\n", __LINE__);
 
    }
 
  if(packet->len == written_amount)
 
    {
 
      q_dequeue(rem->outmsgs);
 
      remoteio_packet_free(packet);
 

	
 
      if(q_empty(rem->outmsgs))
 
	multiio_socket_event_disable(multiio, fd, POLLOUT);
 
    }
 
  else
 
    {
 
      /**
 
       * shifting seems the simplest solution.
 
       */
 
      packet->len -= written_amount;
 
      memmove(packet->data, packet->data + written_amount, packet->len);
 
    }
 

	
 
  return 0;
 
}
 

	
 

	
 
int remoteio_close(struct remoteio *rem)
 
{
 
  int rtn;
 
  
 
  /**
 
   * See careful_free's and _remoteio_handle_read()'s docs.  If
 
   * careful_free is nonzero, then we shouldn't free it here because
 
   * such a free would cause a segfault. However, whoever set
 
   * rem->careful_free to nonzero will handle resetting
 
   * rem->careful_free to zero and calling remoteio_close() if
 
   * necessary.
 
   */
 
  if(rem->careful_free)
 
    {
 
      rem->careful_free = 2;
 
      return 0;
 
    }
 

	
 
  /* call close handler */
 
  if(rem->close_handler)
 
    (*rem->close_handler)(rem->opts->generic_handler_data, rem->read_handler_data);
 

	
 
  /* cleanup multiiio stuff */
 
  multiio_socket_del(rem->opts->multiio, rem->sock);
 

	
 
  /* backend-specific cleanup */
 
  rtn = funcmap[rem->method].close_func(rem);
 

	
 
  /* this part is normal ;-) */
 
  free(rem->inbuf.data);
 
  q_free(rem->outmsgs, (list_dealloc_func_t)remoteio_packet_free);
 

	
 
  free(rem);
 

	
 
  return rtn;
 
}
 

	
 
/**
 
 * Frees an entire packet, including the passed pointer. If you just
 
 * want the contents of the packet free()ed, just do
 
 * free(packet.data);
 
 */
 
void remoteio_packet_free(struct remoteio_packet *packet)
 
{
 
  free(packet->data);
 
  free(packet); 
 
}
 

	
 

	
 
int _remoteio_getserver_traverse(char *servername, struct remoteio_server *aserver)
 
{
 
  if(!strcmp(aserver->name, servername))
 
    return FALSE; /* stop traversal */
 

	
 
  return TRUE;
 
}
 

	
 
struct remoteio_server *remoteio_getserver(const struct remoteio_opts *opts, const char *servername)
 
{
 
  int traversal_result;
 
  char *dispensible_servername;
 

	
 
  dispensible_servername = strdup(servername); /* for the sake of constness... */
 
  traversal_result = list_traverse(opts->servers, dispensible_servername, (list_traverse_func_t)&_remoteio_getserver_traverse, LIST_FRNT | LIST_ALTR);
 
  free(dispensible_servername);
 

	
 
  if(traversal_result == LIST_OK)
 
    return (struct remoteio_server *)list_curr(opts->servers);
 

	
 
  return (struct remoteio_server *)NULL;
 
}
 

	
 
size_t remoteio_sendq_len(const struct remoteio *rem)
 
{
 
  return (size_t)q_size(rem->outmsgs);
 
}
 

	
 
/**
 
   different remoteio methods' implementations:
 
 */
 

	
 
/*
 
  SSH, via execio
 
*/
 

	
 
int _remoteio_ssh_open(struct remoteio *rem, struct remoteio_server *server)
 
{
 
  char *userhost;
 
  char *sshargs[] = {rem->opts->ssh_command, NULL /* userhost */, "distrend", "-d", (char *)NULL};
 

	
 
  int rtn;
 

	
 
  if(server->username)
 
    _distren_asprintf(&userhost, "%s@%s", server->username, server->hostname);
 
  else
 
    userhost = strdup(server->hostname);
 
  sshargs[1] = userhost;
 

	
 
  rtn = execio_open( &rem->execio, "ssh", sshargs);
 
  if(rtn)
 
    {
 
      fprintf(stderr, "error opening remoteio channel to ssh userhost ``%s''\n" , userhost);
 
      free(userhost);
 
      return 1;
 
    }
 
  free(userhost);
 
  
 
  return 0;
 
}
 

	
 
int _remoteio_ssh_read(struct remoteio *rem, void *buf, size_t len, size_t *bytesread)
 
{
 
  return execio_read(rem->execio, buf, len, bytesread);
 
}
 

	
 
int _remoteio_ssh_write(struct remoteio *rem, const void *buf, size_t len, size_t *byteswritten)
 
{
 
  return execio_write(rem->execio, buf, len, byteswritten);
 
}
 

	
 
int _remoteio_ssh_close(struct remoteio *rem)
 
{
 
  int rtn;
 
  
 
  rtn = execio_close(rem->execio);
 
  if(rtn)
 
    fprintf(stderr, "%s:%d: error closing execio\n", __FILE__, __LINE__);
 
  
 
  return rtn;
 
}
 

	
 
#ifndef _WIN32
 
/*
 
  local sockets implementation (``named pipes''), unix-only
 
 */
 
int _remoteio_sock_open(struct remoteio *rem, struct remoteio_server *server)
 
{
 
  int sock;
 
  struct sockaddr_un sockaddr;
 

	
 
  /*
 
    The POSIX docs pretty much say that I can't depend on sockpath being able to be longer than 
 
    some proprietary length. So, if the compiler specifies a long path for RUNSTATEDIR, it could
 
    cause a buffer overflow.
 
   */
 
  char *sockpath = RUNSTATEDIR "/distrend.sock";
 
  unsigned int sockaddr_len;
 

	
 
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
 
  if(sock == -1)
 
    {
 
      perror("socket");
 
      return 1;
 
    }
 

	
 
  sockaddr.sun_family = AF_UNIX;
 
  /*
 
    The terminating NULL should not be included in what's copied to sun_path,
 
    although it won't hurt as long as strlen(sockpath) < max socket length
 
   */
 
  for(sockaddr_len = 0; sockpath[sockaddr_len]; sockaddr_len ++)
 
    sockaddr.sun_path[sockaddr_len] = sockpath[sockaddr_len];
 

	
 
  if(connect(sock, (struct sockaddr *)&sockaddr, sockaddr_len) == -1)
 
    {
 
      perror("connect");
 
      close(sock);
 
      return 1;
 
    }
 

	
 
  rem->sock = sock;
 

	
 
  return 0;
 
}
 

	
 
#endif /* _WIN32 */
 

	
 
int _remoteio_sock_close(struct remoteio *rem)
 
{
 
  close(rem->sock);
 

	
 
  return 0;
 
}
 

	
 
int _remoteio_sock_read(struct remoteio *rem, void *buf, size_t len, size_t *bytesread)
 
{
 
  ssize_t readrtn;
 

	
 
  *bytesread = 0;
 
  readrtn = read(rem->sock, buf, len);
 
  /*
 
    The following is valid for blocking sockets:
 
   */
 
  if(readrtn == -1)
 
    {
 
      /*
 
	in this case, we may have been interrupted by a signal and errno == EINTR
 
	or the connection was reset and errno = ECONNRESET
 

	
 
	Some of these are not error conditions:
 
       */
 
      perror("read");
src/common/remoteio.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef _DISTREN_REMOTEIO_H
 
#define _DISTREN_REMOTEIO_H
 

	
 
#include <stdlib.h>
 

	
 
/**
 
 * @file RemoteIO provides an abstraction to the method of talking to a remote distrend. It is a layer on top of execio that should provide an equivalent interface.
 
 *
 
 * RemoteIO works on top of multiio, so for it to work you write your
 
 * program in an event-oriented fashion and call multiio_poll().
 
 */
 

	
 
struct remoteio_opts;
 
struct remoteio;
 

	
 
/**
 
 * asynchronous read handler signature.
 
 *
 
 * This is the signature of the callback that is called when data is
 
 * available for reading. The handler may process as much of the
 
 * available data as it wants. When it has processed a chunk of data,
 
 * it must return the length of the data that it processed. If the
 
 * handler returns 0, the handler is essentially signalling ``I can't
 
 * figure out what this means until I see more''.
 
 *
 
 * If you need to close a socket after reading certain data from it,
 
 * you may call remoteio_close() from inside of this function.
 
 *
 
 * @param rem the associated remoteio handle
 
 * @param generic_data a pointer that is stored in remoteio's struct remoteio_opts which isn't client-specific
 
 * @param buf a pointer to the buffer containing data waiting to be processed
 
 * @param len the size of buf that may be accessed
 
 * @param data the pointer passed to remoteio_open(). _NOT_ the data just received on the socket.
 
 * @return the number of bytes that the function accepted and thus should be removed from the rem handle.
 
 */
 
typedef size_t(*remoteio_read_handle_func_t)(struct remoteio *rem, void *generic_data, void *buf, size_t len, void *data);
 

	
 
/**
 
 * asynchronous close handler which is called whenever remoteio_close() is called.
 
 *
 
 * As reading is now event-oriented and as libremoteio may itself call
 
 * remoteio_close(), you need a way to be informed that a socket is
 
 * being closed. This is particularly important if you have to clean
 
 * up your read_handler_data.
 
 *
 
 * @param generic_data a pointer set by a call to remoteio_generic_data_set().
 
 * @param data the same pointer passed to remoteio_open().
 
 */
 
typedef void (*remoteio_close_handle_func_t)(void *generic_data, void *data);
 

	
 
/**
 
 * Determines the value of generic_data which is passed to
 
 * remoteio_read_handle_func_t
 
 *
 
 * @param opts the remoteio runtime options
 
 */
 
int remoteio_generic_data_set(struct remoteio_opts *opts, void *generic_data);
 

	
 
/**
 
   Opens connection with to a remote distrend. Returns 1 on error.
 

	
 
   @todo should this be asynchronous? YES! but optionally, perhaps
 
   @param opts the configuration settings for remoteio gotten from options_init().
 
   @param read_handler the function to call when data has been read from the server.
 
   @param read_handler_data the data to pass to the read_handler function.
 
   @param servername the name of the configuration file entry for the server.
 
   From this, information about how to make the outgoing connection is derived.
 
 */
 
int remoteio_open_server(struct remoteio **rem,
 
			 struct remoteio_opts *opts,
 
			 remoteio_read_handle_func_t read_handler,
 
			 void *read_handler_data,
 
			 remoteio_close_handle_func_t close_handler,
 
			 const char *servername);
 

	
 
/**
 
 * Initializes a remoteio instance for a socket that's already been floating
 
 * around for a while. I.e., this socket probably came from accept().
 
 *
 
 * @param rem a pointer to where the poiner to the newly allocated struct remoteio should be stored.
 
 * @param opts remoteio's options
 
 * @param read_handler the function to call when data has been read from the server.
 
 * @param read_handler_data the data to pass to the read_handler function.
 
 * @param opts self explanatory.
 
 */
 
int remoteio_open_socket(struct remoteio **rem,
 
			 struct remoteio_opts *opts,
 
			 remoteio_read_handle_func_t read_handler,
 
			 void *read_handler_data,
 
			 remoteio_close_handle_func_t close_handler,
 
			 int fd);
 

	
 
/**
 
 * Queue bytes to be written to the remote host.
 
 *
 
 * @param rem the remoteio handle
 
 * @param buf a buffer to be queued for writing. We will copy this, so the caller has to handle its memory (and free() it if necessary).
 
 * @param len number of bytes to grab from buf
 
 * @return 0 on success, 1 on failure
 
 */
 
int remoteio_write(struct remoteio *rem, const void *buf, size_t len);
 

	
 
/**
 
 * \brief Retrieves authentication information associated with a
 
 *   server's configuration entry.
 
 *
 
 * Possibly, the whole idea of remoteio handling server names with the
 
 * remoteio_open() function should be moved somewhere else and
 
 * remoteio should be more general-purpose I/O?
 
 *
 
 * \param rem_opts A remoteio options handle.
 
 * \param servername The name of the server whose information should be retrieved.
 
 * \param username Where to store a pointer to the username. Do not free this.
 
 * \param pass Where to store a pointer to the password. Do not free this.
 
 */
 
int remoteio_authinfo_get(struct remoteio_opts *rem_opts, const char *servername, const char **username, const char **pass);
 

	
 
/**
 
 * Closes a remoteio session.
 
 *
 
 * It is safe to call this function from within
 
 * remoteio_read_handle_func_t.
 
 *
 
 * @return nonzero on error
 
*/
 
int remoteio_close(struct remoteio *rem);
 

	
 
/**
 
 * Returns the number of unfulfilled remoteio_write() calls pending on
 
 * a remoteio handle.
 
 */
 
size_t remoteio_sendq_len(const struct remoteio *rem);
 

	
 
#endif
src/common/request.c
Show inline comments
 
/*
 
 * Copyright 2010 Nathan Phillip Brink
 
 *
 
 * This file is a part of DistRen.
 
 *
 
 * DistRen is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * DistRen is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
#include "common/config.h"
 

	
 
#include "common/request.h"
 

	
 
#include "common/protocol.h"
 

	
 
#include <arpa/inet.h>
 
#include <openssl/evp.h>
 
#include <stdlib.h>
 
#include <string.h>
 

	
 
int distren_request_free_with_data(struct distren_request *req, void *data)
 
{
 
  free(data);
 
  return distren_request_free(req);
 
}
 

	
 
int distren_request_version(struct distren_request **req, void **data, uint32_t servertype, const char *package_string)
 
{
 
  struct distren_request_version *version;
 

	
 
  distren_request_new(req, sizeof(struct distren_request_version), DISTREN_REQUEST_VERSION);
 
  version = malloc(sizeof(struct distren_request_version));
 
  if(!version || !*req)
 
    {
 
      free(version);
 
      if(*req)
 
	distren_request_free(*req);
 
      return 1;
 
    }
 

	
 
  memset(version, 0, sizeof(struct distren_request_version));
 
  version->servertype = servertype;
 
  strncpy(version->package_string, package_string, DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN);
 

	
 
  *data = version;
 

	
 
  return 0;
 
}
 

	
 
int distren_request_parse_version(struct distren_request *req, void *data, struct distren_request_version *version)
 
{
 
  if(req->len < sizeof(struct distren_request_version))
 
    return 1;
 

	
 
  memcpy(version, data, sizeof(struct distren_request_version));
 
  /* there is space for another '\0' */
 
  version->package_string[DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN] = '\0';
 

	
 
  return 0;
 
}
 

	
 
int distren_request_pass(struct distren_request **req, void **data, const char *username, const char *pass)
 
{
 
  struct distren_request_pass *request_pass;
 

	
 
  distren_request_new(req, sizeof(struct distren_request_pass), DISTREN_REQUEST_PASS);
 
  request_pass = malloc(sizeof(struct distren_request_pass));
 
  if(!request_pass || !*req)
 
    {
 
      if(*req)
 
	distren_request_free(*req);
 
      free(request_pass);
 

	
 
      return 1;
 
    }
 

	
 
  /*
 
   * The packet itself doesn't need the string's NULL terminator
 
   * _unless_ if the string is shorter than the maximum length of the
 
   * username or password. Thus, strncpy()'s behavior (not strlcpy()'s
 
   * behavior) is _exactly_ what we want for this case.
 
   */
 
  strncpy(request_pass->username, username, DISTREN_REQUEST_PASS_USERNAME_LEN);
 
  strncpy(request_pass->pass, pass, DISTREN_REQUEST_PASS_PASS_LEN);
 

	
 
  *data = request_pass;
 

	
 
  return 0;
 
}
 

	
 
int distren_request_poing(struct distren_request **req, void **data, short is_ping, const void *poing_cookie, size_t poing_data_len)
 
{
 
  enum distren_request_type type;
 

	
 
  if(is_ping)
 
    type = DISTREN_REQUEST_PING;
 
  else
 
    type = DISTREN_REQUEST_PONG;
 
  distren_request_new(req, poing_data_len, type);
 
  (*data) = malloc(poing_data_len);
 
  memcpy(*data, poing_cookie, poing_data_len);
 

	
 
  return 0;
 
}
 

	
 
/*
 
 * file posting stuffs
 
 */
 
struct distren_request_file_post_context
 
{
 
  EVP_MD_CTX *digest_ctx;
 
  /* stored in network-byte order */
 
  uint32_t post_id;
 
};
 

	
 
/**
 
 * Helper for distren_request_file_post_start() and
 
 * distren_request_parse_file_post_start().
 
 */
 
static int _distren_request_file_post_context_new(distren_request_file_post_context_t *post_context,
 
						  uint32_t post_id)
 
{
 
  *post_context = malloc(sizeof(struct distren_request_file_post_context));
 
  if(!*post_context)
 
    return 1;
 

	
 
  (*post_context)->post_id = htonl(post_id);
 
  (*post_context)->digest_ctx = EVP_MD_CTX_create();
 
  EVP_DigestInit_ex((*post_context)->digest_ctx, EVP_sha1(), NULL);
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * Frees a post_context and sets the pointer to NULL.
 
 */
 
static int _distren_request_file_post_context_free(distren_request_file_post_context_t *post_context)
 
{
 
  if(!*post_context)
 
    return 0;
 

	
 
  EVP_MD_CTX_destroy((*post_context)->digest_ctx);
 
  free(*post_context);
 

	
 
  *post_context = NULL;
 

	
 
  return 0;
 
}
 

	
 
int distren_request_file_post_context_free(distren_request_file_post_context_t post_context)
 
{
 
  /*
 
   * for the ``public'' function, we don't need to have post_context
 
   * set to NULL. Thus, we have this wrapper around
 
   * _distren_request_file_post_context_free().
 
   */
 
  return _distren_request_file_post_context_free(&post_context);
 
}
 

	
 
int distren_request_file_post_start(struct distren_request **req,
 
				    void **data,
 
				    distren_request_file_post_context_t *post_context,
 
				    uint32_t post_id,
 
				    const char *filename)
 
{
 
  struct distren_request_file_post_start *file_post_start;
 

	
 
  distren_request_new(req, sizeof(struct distren_request_file_post_start), DISTREN_REQUEST_FILE_POST_START);
 
  file_post_start = malloc(sizeof(struct distren_request_file_post_start));
 
  _distren_request_file_post_context_new(post_context, post_id);
 
  if(!*req
 
     || !file_post_start)
 
    {
 
      if(*req)
 
	distren_request_free(*req);
 
      free(file_post_start);
 
      _distren_request_file_post_context_free(post_context);
 
      return 1;
 
    }
 

	
 
  file_post_start->post_id = (*post_context)->post_id;
 
  strncpy(file_post_start->filename, filename, DISTREN_REQUEST_FILE_POST_NAME_LEN);
 

	
 
  *data = file_post_start;
 
  return 0;
 
}
 

	
 
int distren_request_parse_file_post_start(struct distren_request *req,
 
					  void *data,
 
					  distren_request_file_post_context_t *post_context,
 
					  uint32_t *post_id,
 
					  char **filename)
 
{
 
  int tmp;
 
  struct distren_request_file_post_start *file_post_start;
 

	
 
  /* keep our promises ;-) */
 
  *post_context = NULL;
 
  *filename = NULL;
 

	
 
  if(req->len < sizeof(struct distren_request_file_post_start))
 
    return 1;
 

	
 
  file_post_start = data;
 

	
 
  tmp = _distren_request_file_post_context_new(post_context, file_post_start->post_id);
 
  *filename = malloc(DISTREN_REQUEST_FILE_POST_NAME_LEN + 1);
 
  if(tmp
 
     || !*filename)
 
    {
 
      _distren_request_file_post_context_free(post_context);
 
      free(*filename);
 
    }
 

	
 
  memcpy(*filename, file_post_start->filename, DISTREN_REQUEST_FILE_POST_NAME_LEN);
 
  (*filename)[DISTREN_REQUEST_FILE_POST_NAME_LEN] = '\0';
 

	
 
  *post_id = ntohl(file_post_start->post_id);
 

	
 
  return 0;
 
}
 

	
 
int distren_request_file_post(struct distren_request **req,
 
			      struct distren_request_file_post *data_header,
 
			      distren_request_file_post_context_t post_context,
 
			      const void *data,
 
			      size_t len)
 
{
 
  distren_request_new(req, sizeof(struct distren_request_file_post) + len, DISTREN_REQUEST_FILE_POST);
 
  if(!*req)
 
    return 1;
 

	
 
  data_header->post_id = post_context->post_id;
 

	
 
  EVP_DigestUpdate(post_context->digest_ctx, data, len);
 

	
 
  return 0;
 
}
 

	
 
int distren_request_parse_file_post(struct distren_request *req,
 
				    void *data,
 
				    uint32_t *post_id,
 
				    distren_request_parse_file_post_find_context_func_t find_context_func,
 
				    void *find_post_context_data,
 
				    void **file_data,
 
				    size_t *file_data_len)
 
{
 
  struct distren_request_file_post *file_post;
 
  distren_request_file_post_context_t post_context;
 

	
 
  if(req->type != DISTREN_REQUEST_FILE_POST
 
     || req->len < sizeof(struct distren_request_file_post))
 
    return 1;
 

	
 
  file_post = data;
 
  *post_id = ntohl(file_post->post_id);
 
  post_context = (*find_context_func)(*post_id, find_post_context_data);
 
  if(!post_context
 
     || *post_id != post_context->post_id)
 
    return 1;
 

	
 
  *file_data = data + sizeof(struct distren_request_file_post);
 
  *file_data_len = req->len - sizeof(struct distren_request_file_post);
 
  EVP_DigestUpdate(post_context->digest_ctx, *file_data, *file_data_len);
 

	
 
  return 0;
 
}
 

	
 
int distren_request_file_post_finish(struct distren_request **req,
 
				     void **data,
 
				     distren_request_file_post_context_t post_context,
 
				     uint32_t cancel)
 
{
 
  struct distren_request_file_post_finish *file_post_finish;
 

	
 
  distren_request_new(req, sizeof(struct distren_request_file_post_finish), DISTREN_REQUEST_FILE_POST_FINISH);
 
  file_post_finish = malloc(sizeof(struct distren_request_file_post_finish));
 
  if(!*req
 
     || !file_post_finish)
 
    {
 
      if(*req)
 
	distren_request_free(*req);
 
      free(file_post_finish);
 

	
 
      _distren_request_file_post_context_free(&post_context);
 
      return 1;
 
    }
 
  file_post_finish->post_id = post_context->post_id;
 
  file_post_finish->cancel = htonl(cancel);
 

	
 
  EVP_DigestFinal(post_context->digest_ctx, file_post_finish->sha, NULL);
 
  _distren_request_file_post_context_free(&post_context);
 

	
 
  *data = file_post_finish;
 
  return 0;
 
}
 

	
 
int distren_request_parse_file_post_finish(struct distren_request *req,
 
				    void *data,
 
				    uint32_t *post_id,
 
				    distren_request_parse_file_post_find_context_func_t find_context_func,
 
				    void *find_post_context_data)
 
{
 
  struct distren_request_file_post_finish *file_post_finish;
 
  distren_request_file_post_context_t post_context;
 

	
 
  unsigned int checksum_len;
 
  unsigned char checksum[EVP_MAX_MD_SIZE];
 

	
 
  if(req->type != DISTREN_REQUEST_FILE_POST_FINISH
 
     || req->len < sizeof(struct distren_request_file_post_finish))
 
    return 1;
 

	
 
  file_post_finish = data;
 
  *post_id = ntohl(file_post_finish->post_id);
 
  if(ntohl(file_post_finish->cancel))
 
    return 2;
 

	
 
  post_context = find_context_func(*post_id, find_post_context_data);
 
  if(!post_context)
 
    return 1;
 

	
 
  /* finish up checksumming */
 
  EVP_DigestFinal_ex(post_context->digest_ctx, checksum, &checksum_len);
 
  if(checksum_len > EVP_MAX_MD_SIZE)
 
    checksum_len = EVP_MAX_MD_SIZE;
 
  if(memcmp(checksum, file_post_finish->sha, checksum_len))
 
    return 3;
 

	
 
  return 0;
 
}
src/common/request.h
Show inline comments
 
/*
 
 * Copyright 2010 Nathan Phillip Brink
 
 *
 
 * This file is a part of DistRen.
 
 *
 
 * DistRen is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * DistRen is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
#ifndef _DISTREN_REQUEST_H
 
#define _DISTREN_REQUEST_H
 

	
 
#include "common/protocol.h"
 

	
 
#include <openssl/sha.h>
 

	
 
/**
 
 * @file functions to initialize various requests that the server and
 
 * client may both use.
 
 */
 

	
 

	
 
/**
 
 * Free any request made with one of the functions below
 
 */
 
int distren_request_free_with_data(struct distren_request *req, void *data);
 

	
 
/**
 
 * Initialize a VERSION request.
 
 *
 
 * @param req pointer to where the poitner to the new req should be stored..
 
 * @param data pointer to where the newly allocated data's address should go.
 
 * @param servertype the ORing of different DISTREN_SERVERTYPE_* constants.
 
 * @param package_string the PACKAGE_STRING constant.
 
 */
 
int distren_request_version(struct distren_request **req, void **data, uint32_t servertype, const char *package_string);
 

	
 
/**
 
 * Parses a DISTREN_REQUEST_VERSION packet.
 
 *
 
 * @param req the request to parse.
 
 * @param data the request's data.
 
 * @param version where the result should be stored.
 
 * @return 0 on success, 1 if the packet is invalid (if the length of package_version is longer than 32-bytes, for example).
 
 */
 
int distren_request_parse_version(struct distren_request *req, void *data, struct distren_request_version *version);
 

	
 
/**
 
 * Initialize a PASS request so as to identify to a server.
 
 *
 
 * \param req Where to store the newly allocated req.
 
 * \param data Where to store the newly allocated data.
 
 * \param user The username to send to the server.
 
 * \param pass The password to send to the server.
 
 */
 
int distren_request_pass(struct distren_request **req, void **data, const char *username, const char *pass);
 

	
 
/**
 
 * Initialize a PING or PONG request.
 
 *
 
 * @param data a place to allocate storage for the data associated with this request
 
 * @param is_ping 1 if this is a DISTREN_REQUEST_PING or 0 if this is a DISTREN_REQUEST_PONG
 
 * @param poing_cookie chocolate chip, chocolate chunk, or oatmeal chocolate chip
 
 * @param poing_data_len bytes in the poing_cookie
 
 * @return the length of the data allocated for this request
 
 */
 
int distren_request_poing(struct distren_request **req, void **data, short is_ping, const void *poing_cookie, size_t poing_data_len);
 

	
 
struct distren_request_file_post_context;
 
typedef struct distren_request_file_post_context *distren_request_file_post_context_t;
 

	
 
/**
 
 * Frees a post_context for if a client is disconnected or an upload
 
 * otherwise interrupted.
 
 */
 
int distren_request_file_post_context_free(distren_request_file_post_context_t post_context);
 

	
 
/**
 
 * Prepares for a file upload and initializes a
 
 * distren_request_file_post_context_t.
 
 *
 
 * If you call this function and you never finish an upload, you
 
 * _must_ call distren_request_file_post_finish() on post_context at
 
 * least to free the post_context.
 
 *
 
 * @param req where to store the new distren_request pointer.
 
 * @param data where to store the new request's data pointer. 
 
 * @param post_context where the newly allocated distren_request_file_post_context_t's addrss should be stored.
 
 * @param post_id a number that uniquely identifies this upload for this connection.
 
 * @param filename a user-friendly filename for the file posting.
 
 */
 
int distren_request_file_post_start(struct distren_request **req,
 
				    void **data,
 
				    distren_request_file_post_context_t *post_context,
 
				    uint32_t post_id,
 
				    const char *filename);
 

	
 
/**
 
 * Parses a DISTREN_REQUEST_FILE_POST_START request.
 
 *
 
 * @param req the recieved request.
 
 * @param data the request's data.
 
 * @param post_context a post_context to be initialized for a continued download. Must be free()d with a call to distren_request_parse_file_post_finish() or distren_request_file_post_context_free(), however.
 
 * @param post_id will be set to the post_id contained in the packet.
 
 * @param filename will be made to point at the filename the client requested. *filename must be free()d.
 
 * @return 0 upon no problem, 1 if there's an error. If 1, then *filename should not be used (it will be set NULL, so no problem calling free() on it though).
 
 */
 
int distren_request_parse_file_post_start(struct distren_request *req,
 
					  void *data,
 
					  distren_request_file_post_context_t *post_context,
 
					  uint32_t *post_id,
 
					  char **filename);
 

	
 
/**
 
 * Initializes a DISTREN_REQUEST_FILE_POST packet's header.
 
 *
 
 * As file posting requires an acceptable chunk of memory, we don't
 
 * malloc() the data portion of the packet here to encourage reading
 
 * files in through static buffers.
 
 *
 
 * As a single file takes multiple DISTREN_REQUEST_FILE_POST packets
 
 * before it's completely uploaded, certain state information has to
 
 * persist between calls to distren_request_file_post(). This is what
 
 * the post_context argument is for. This pointer must be preserved
 
 * between calls to distren_request_file_post().
 
 *
 
 * @param req place to store the new req's pointer
 
 * @param data_header where the data's header should be stored
 
 * @param post_context what this points to must be preserved between calls to distren_request_file_post(), for keeping state.
 
FILE_POST_DATA_LEN.
 
 * @param data a hunk of the file being posted.
 
 * @param len the length of the data argument. This must be less than or equal to DISTREN_REQUEST_ */
 
int distren_request_file_post(struct distren_request **req,
 
			      struct distren_request_file_post *data_header,
 
			      distren_request_file_post_context_t post_context,
 
			      const void *data,
 
			      size_t len);
 

	
 
/**
 
 * A callback to find a distren_request_file_post_context_t for a
 
 * specified post_id.
 
 *
 
 * This exists because it is recognized that two servers may be
 
 * transfering more than one file at a time between eachother. At the
 
 * time that distren_request_parse_file_post() is called, the caller
 
 * doesn't know post_id and thus can't provide the proper
 
 * post_context. Yet distren_request_parse_file_post() needs the
 
 * post_context to get its job done ;-).
 
 *
 
 * @param post_id the post_id for which to find a distren_request_file_post_context_t
 
 * @param find_post_context_data the same find_post_context_data pointer passed to distren_request_parse_file_post() or distren_request_parse_file_post_finish().
 
 * @return NULL if the context could not be found or the context.
 
 */
 
typedef distren_request_file_post_context_t(*distren_request_parse_file_post_find_context_func_t)(uint32_t post_id, void *find_post_context_data);
 

	
 
/**
 
 * Parse a DISTREN_REQUEST_FILE_POST packet and update the
 
 * post_context.
 
 *
 
 * @param req the request header.
 
 * @param data the request data section.
 
 * @param post_id will be filled with the request's post_id.
 
 * @param find_context_func a function that can look up the necessary context_post given a post_id.
 
 * @param find_post_context_data the pointer to pass as context data to find_context_func
 
 * @param file_data will be set to the beginning of the file data portion of the packet. Do not free() this pointer.
 
 * @param file_data_len the number of bytes of file data that may be accessed at *file_data.
 
 * @return 0 on success and 1 on failure, in which case the client should be kicked and you should free the post_context with distren_request_file_post_context_free().
 
 */
 
int distren_request_parse_file_post(struct distren_request *req,
 
				    void *data,
 
				    uint32_t *post_id,
 
				    distren_request_parse_file_post_find_context_func_t find_context_func,
 
				    void *find_post_context_data,
 
				    void **file_data,
 
				    size_t *file_data_len);
 

	
 
/**
 
 * Construct a packet signalling that a file posting is finished.
 
 *
 
 * Frees the memory used to store the post_context. Must be called
 
 * once for every call to distren_request_file_post_start(),
 
 * regardless of whether or not you actually call
 
 * distren_request_send() on the result.
 
 *
 
 * @param req where to store the pointer of the newly allocated request
 
 * @param data where to store the pointer t othe newly allocated request data
 
 * @param post_context the post_context which will be used and then freed.
 
 * @param cancel 0 if the file was uploaded successfully, 1 if the upload is cancelled or failed. 
 
 */
 
int distren_request_file_post_finish(struct distren_request **req,
 
				     void **data,
 
				     distren_request_file_post_context_t post_context,
 
				     uint32_t cancel);
 

	
 
/**
 
 * Parses a DISTREN_REQUEST_FILE_POST_FINISH packet.
 
 *
 
 * You must still call distren_request_file_post_context_free() on the
 
 * post_context which you must find yourself. If you are a server, you
 
 * must keep a list of these contexts per-client so that when a client
 
 * exits, you may reclaim these contexts.
 
 *
 
 * @param req the request header
 
 * @param data the data section of the packet
 
 * @param post_id a place where the post_id will be set.
 
 * @param find_context_func a function pointer to let me find a post_context based on the post_id of the request.
 
 * @param find_post_context_data the pointer to pass to the find_context_func callback.
 
 * @return 0 for a good packet, 1 for a bad packet or if the post_context can't be found (post_id won't be valid), 2 if the request asks for the user to cancel the file upload (post_id will be valid), and 3 if the checksums don't match (post_id will be valid).
 
 */
 
int distren_request_parse_file_post_finish(struct distren_request *req,
 
				    void *data,
 
				    uint32_t *post_id,
 
				    distren_request_parse_file_post_find_context_func_t find_context_func,
 
				    void *find_post_context_data);
 
#endif /* _DISTREN_REQUEST_H */
src/server/distrend.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
*/
 

	
 
/* This file contains the code which both processes (renders) jobs as a slave, and the code which distributes frames to slaves after receiving them from the client portion of the codebase. */
 

	
 
#include "common/config.h"
 

	
 
#include "distrenjob.h"
 
#include "listen.h"
 
#include "mysql.h"
 
#include "slavefuncs.h"
 
#include "mysql.h"
 
#include "user_mgr.h"
 

	
 
#include "common/asprintf.h"
 
#include "common/execio.h"
 
#include "common/options.h"
 
#include "common/protocol.h"
 
#include "common/request.h"
 

	
 
#include <arpa/inet.h>
 
#include <confuse.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <sys/stat.h>
 
#include <sys/types.h>
 
#include <time.h>
 
#include <unistd.h>
 

	
 
#include <libxml/encoding.h>
 
#include <libxml/parser.h>
 
#include <libxml/tree.h>
 
#include <libxml/xmlmemory.h>
 
#include <libxml/xmlreader.h>
 
#include <libxml/xmlwriter.h>
 

	
 
/* ******************* Structs ************************ */
 
struct general_info
 
{
 
  struct distrenjob head;
 
  distrend_mysql_conn_t conn;
 

	
 
  struct distrend_config *config;
 

	
 
  struct
 
  {
 
    /** general_info.xml */
 
    char *geninfo;
 

	
 
    /**
 
     * \brief Where to store the user listing.
 
     */
 
    char *userlist;
 

	
 
    /**
 
     * where to store in-progress uploads or things that should
 
     * otherwise be on the same filesystem as the rest of the datadir
 
     * so that it may be rename()d.
 
     */
 
    char *tmpdir;
 
    
 
  } files;
 

	
 
  int jobs_in_queue;
 
  unsigned int free_clients;
 
  unsigned int rendering_clients;
 
  unsigned int total_finished_jobs;
 
  unsigned int total_frames_rendered;
 
  unsigned int highest_jobnum;
 
  int hibernate;
 
  time_t timestamp;
 
  unsigned long total_render_power;
 
  unsigned long total_priority_pieces;
 

	
 
  user_mgr_t user_mgr;
 
};
 

	
 

	
 
/* *********************************************
 
   Function Prototypes
 
   ********************************************* */
 

	
 
/* ************General Functions************* */
 
int distrend_do();
 
int distrend_do_config(int argc, char *argv[], struct distrend_config **config, multiio_context_t multiio);
 
int distrend_config_free(struct distrend_config *config);
 
int distrend_handle_request(struct distrend_listens *listens, struct distrend_client *client, struct distren_request *req, void *reqdata, struct general_info *geninfo);
 

	
 
/**
 
 * client request handlers
 
 */
 
int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 
int distrend_handle_pass(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 
int distrend_handle_file_post_start(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 
int distrend_handle_file_post(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 
int distrend_handle_file_post_finish(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 

	
 
/* functions of some generic sort ...ish */
 
int distrend_handle_successful_upload(struct distrend_client *client, struct distrend_client_file_post *client_file_post);
 

	
 
/* **************XML Functions**************** */
 
void update_general_info(struct general_info *geninfo);
 
int import_general_info(struct general_info *general_info);
 
int update_xml_joblist(struct general_info *geninfo);
 

	
 
/* **************Test Functions**************** */
 
int interactiveTest(int test, multiio_context_t multiio, struct general_info *general_info);
 

	
 
/* **************** Main ********************* */
 
int main(int argc, char *argv[])
 
{
 
  /* Parse arguments */
 
  int counter;
 
  int test = 0; /*< Interactive mode if 1 */
 
  int tmp;
 
  struct general_info general_info;
 
  multiio_context_t multiio;
 

	
 
  enum clientstatus
 
  {
 
    CLIENTSTATUS_UNINITIALIZED = 0,
 
    CLIENTSTATUS_BUSY = 1,
 
    CLIENTSTATUS_IDLE = 2
 
  } clientstatus;
 

	
 
  fprintf(stderr, PACKAGE_STRING "\n\
 
Nathan Phillip Brink <binki@ohnopub.net>\n\
 
Ethan Zonca <e@ethanzonca.com>\n\
 
\n");
 

	
 
#ifdef HAVE_LIST_BRAG
 
  fprintf(stderr, "Using %s\n", list_brag);
 
#endif
 

	
 
  clientstatus = CLIENTSTATUS_UNINITIALIZED;
 
  // xmlinit();
 

	
 
  for(counter = 0; counter < argc; counter ++)
 
    {
 
      if(strcmp(argv[counter], "-h") == 0)
 
      {
 
    	  fprintf(stderr, "Usage: distrend [option] \nStarts the distrend server\n\t-h\tshow this help\n\t-t\tlaunches queue testing interface \n");
 
	  return 2;
 
      }
 

	
 
      else if(strcmp(argv[counter], "-t") == 0)
 
      {
 
    	  fprintf(stderr, "Entering into test mode...\n\n");
 
    	  test = 1;
 
      }
 
    }
 

	
 

	
 
  multiio = multiio_context_new();
 

	
 
  if(distrend_do_config(argc, argv, &general_info.config, multiio))
 
    return 1;
 

	
 
  /** preset paths */
 
  _distren_asprintf(&general_info.files.geninfo, "%s/general_info.xml",
 
		    general_info.config->datadir);
 

	
 
  _distren_asprintf(&general_info.files.tmpdir, "%s/tmp",
 
		    general_info.config->datadir);
 
  distren_mkdir_recurse(general_info.files.tmpdir);
 

	
 
  _distren_asprintf(&general_info.files.userlist, "%s/users.xml",
 
		    general_info.config->datadir);
 

	
 
  /** configuraton stuff that depends on the paths being calculated, such as loading data */
 
  general_info.user_mgr = user_mgr_init(general_info.files.userlist);
 
  if(!general_info.user_mgr)
 
    {
 
      fprintf(stderr, "Error initializing user_mgr\n");
 
      return 1;
 
    }
 

	
 

	
 
  /** MySQL Connection */
 
  fprintf(stderr,"Connecting to mysql...\n");
 
  if(mysqlConnect(&general_info.conn,
 
		  general_info.config->mysql_user,
 
		  general_info.config->mysql_host,
 
		  general_info.config->mysql_pass,
 
		  general_info.config->mysql_database) )
 
    {
 
      fprintf(stderr, "%s:%d: mysqlConnect() failed\n", __FILE__, __LINE__);
 
      fprintf(stderr, "don't test mysql stuff\n");
 
      interactiveTest(test, multiio, &general_info);
 
      return 1;
 
    }
 
  fprintf(stderr,"Finished connecting!\n");
 

	
 
  /** Execute test function */
 
  interactiveTest(test, multiio, &general_info);
 

	
 
  general_info.config->listens = distrend_listens_new(multiio, &general_info, general_info.config->options);
 
  if(!general_info.config->listens)
 
    {
 
      fprintf(stderr, "error initializing listens\n");
 
      return 1;
 
    }
 
  remoteio_generic_data_set(general_info.config->options->remoteio, general_info.config->listens);
 
  for(counter = 0; general_info.config->listen_ports[counter]; counter ++)
 
    {
 
      tmp = distrend_listen_add(general_info.config->listens, general_info.config->listen_ports[counter]);
 
      if(tmp)
 
	{
 
	  fprintf(stderr, "Error listening on port %d\n", general_info.config->listen_ports[counter]);
 
	  return 1;
 
	}
 
    }
 

	
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_VERSION, &distrend_handle_version);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST_START, &distrend_handle_file_post_start);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST, &distrend_handle_file_post);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST_FINISH, &distrend_handle_file_post_finish);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_VERSION, &distrend_handle_version, (uint8_t)DISTREND_CLIENT_PREVERSION);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_PASS,
 
			      &distrend_handle_pass, (uint8_t)DISTREND_CLIENT_PREAUTH);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST_START, &distrend_handle_file_post_start, (uint8_t)DISTREND_CLIENT_GOOD);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST, &distrend_handle_file_post, (uint8_t)DISTREND_CLIENT_GOOD);
 
  distrend_listen_handler_add(general_info.config->listens, DISTREN_REQUEST_FILE_POST_FINISH, &distrend_handle_file_post_finish, (uint8_t)DISTREND_CLIENT_GOOD);
 

	
 
  /* Main Loop */
 
  general_info.config->die = 0;
 
  while(!general_info.config->die)
 
    {
 
      multiio_poll(multiio, 15000);
 

	
 
      tabletennis_serve(general_info.config->listens->tabletennis);
 

	
 
      /* Run the watchdog, @TODO: like every 10 mins or something */
 
      frame_watchdog(general_info.conn);
 
    }
 

	
 
  distrend_listen_free(general_info.config->listens);
 
  distrend_config_free(general_info.config);
 

	
 
  xmlcleanup();
 

	
 
  /** free() paths */
 
  free(general_info.files.geninfo);
 
  free(general_info.files.tmpdir);
 
  free(general_info.files.userlist);
 
  mysqlDisconnect(general_info.conn);
 

	
 
  return 0;
 
}
 

	
 
/* ********************** Functions ************************* */
 

	
 
int distrend_handle_version(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  char *tmp_str;
 
  struct distren_request_version version;
 

	
 
  if(distren_request_parse_version(req, req_data, &version))
 
    {
 
      distrend_send_disconnect(client, "Invalid DISTREN_REQUEST_VERSION packet.");
 
      return 1;
 
    }
 

	
 
  if(client->state != DISTREND_CLIENT_PREVERSION)
 
    {
 
      distrend_send_disconnect(client, "You have already sent the VERSION command.");
 
      return 1;
 
    }
 
  if(!strncmp(PACKAGE_STRING, version.package_string, DISTREN_REQUEST_VERSION_PACKAGE_STRING_LEN))
 
    {
 
      /**
 
       * The client and I claim to be of the same version of distren :-D
 
       * Now we will mark the client as valid.
 
       *
 
       * We won't increment his time to live, though, because it shouldn't take
 
       * him that long to auth.
 
       */
 
      client->state = DISTREND_CLIENT_PREAUTH;
 
    }
 
  else
 
    {
 
      /**
 
       * The client claims to be of a different version of distren.
 
       * Now we will just send a disconnect packet and disconnect the client.
 
       */
 
      _distren_asprintf(&tmp_str, "You have tried to connect to a %s server when your client claims to be running %s. Bye ;-)\n", PACKAGE_STRING, version.package_string);
 
      if(tmp_str)
 
	{
 
	  distrend_send_disconnect(client, tmp_str);
 
	  free(tmp_str);
 
	}
 
      else
 
	distrend_send_disconnect(client, "Invalid PACKAGE_VERSION :-|.");
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * Handle a DISTREN_REQUEST_PASS request.
 
 */
 
int distrend_handle_pass(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  struct distren_request_pass *pass_req;
 

	
 
  char username[DISTREN_REQUEST_PASS_USERNAME_LEN + 1];
 
  char pass[DISTREN_REQUEST_PASS_PASS_LEN + 1];
 

	
 
  struct user *user;
 

	
 
  if(req->len < sizeof(struct distren_request_pass))
 
    {
 
      distrend_send_disconnect(client, "You tried to send too short of a DISTREN_REQUEST_PASS.");
 
      return 1;
 
    }
 

	
 
  pass_req = req_data;
 
  memcpy(username, pass_req->username, DISTREN_REQUEST_PASS_USERNAME_LEN);
 
  username[DISTREN_REQUEST_PASS_USERNAME_LEN] = '\0';
 

	
 
  memcpy(pass, pass_req->pass, DISTREN_REQUEST_PASS_PASS_LEN);
 
  pass[DISTREN_REQUEST_PASS_PASS_LEN] = '\0';
 

	
 
  user = user_find(geninfo->user_mgr, username);
 
  if(!user
 
     || strcmp(user->username, username)
 
     || strcmp(user->pass, pass))
 
    {
 
      distrend_send_disconnect(client, "Invalid username or password.");
 
      return 1;
 
    }
 

	
 
  client->state = DISTREND_CLIENT_GOOD;
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * Traversal helper for distrend_client_find_post().
 
 */
 
int distrend_client_find_post_traverse(uint32_t *post_id, struct distrend_client_file_post *client_file_post)
 
{
 
  if(*post_id == client_file_post->post_id)
 
    return FALSE;
 

	
 
  return TRUE;
 
}
 

	
 
/**
 
 * Find the record for an in-progress client's file posting.
 
 */
 
struct distrend_client_file_post *distrend_client_find_post(struct distrend_client *client, uint32_t post_id)
 
{
 
  if(list_traverse(client->file_post_list, &post_id, (list_traverse_func_t)&distrend_client_find_post_traverse, LIST_ALTR | LIST_FORW | LIST_FRNT) != LIST_EXTENT)
 
    return list_curr(client->file_post_list);
 
  return NULL;
 
}
 

	
 
/**
 
 * Finds a post_context based on the post_id and client.
 
 *
 
 * Compatible the distren_request_parse_file_post_find_context_func_t.
 
 */
 
static distren_request_file_post_context_t distrend_client_find_file_post_context(uint32_t post_id, void *client)
 
{
 
  struct distrend_client_file_post *client_file_post;
 

	
 
  client_file_post = distrend_client_find_post(client, post_id);
 
  if(client_file_post)
 
    return client_file_post->post_context;
 
  return NULL;
 
}
 

	
 
/**
 
 * Clean up and free a client_file_post
 
 *
 
 * Whenever calling this functino, you almost _always_ have to call
 
 * list_remove_element(client->file_post_list, client_file_post);
 
 * first.
 
 */
 
void distrend_client_file_post_free(struct distrend_client_file_post *client_file_post)
 
{
 
  fclose(client_file_post->fd);
 
  free(client_file_post->filename);
 
  unlink(client_file_post->file_save_path);
 
  free(client_file_post->file_save_path);
 

	
 
  free(client_file_post);
 
}
 

	
 
int distrend_handle_file_post_start(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  struct distrend_client_file_post *client_file_post;
 

	
 
  distren_request_file_post_context_t post_context;
 
  uint32_t post_id;
 
  char *filename;
 

	
 
  char *str_tmp;
 

	
 
  int ret;
 

	
 
  /**
 
   * @todo access check!
 
   */
 
  fprintf(stderr, __FILE__ ":%d:distrend_handle_file_post_start(): You need to check if a client is actually allowed to upload files somehow!\n", __LINE__);
 

	
 
  /*
 
   * other servers should be excluded from this check, but we don't
 
   * store the servertype in client yet.
 
   */
 
  if(list_size(client->file_post_list) > 1)
 
    {
 
      distrend_send_disconnect(client, "You are trying to upload too many files at once!");
 
      return 1;
 
    }
 

	
 
  ret = distren_request_parse_file_post_start(req, req_data, &post_context, &post_id, &filename);
 
  if(ret)
 
    {
 
      distrend_send_disconnect(client, "You sent me an invalid DISTREN_REQUEST_FILE_POST_START packet");
 
      return 1;
 
    }
 

	
 
  if(distrend_client_find_post(client, post_id))
 
    {
 
      _distren_asprintf(&str_tmp, "Err accepting file: You are trying to upload using post_id=%d while you have already started another upload using the same post_id", post_id);
 
      distrend_send_disconnect(client, str_tmp);
 
      free(str_tmp);
 

	
 
      distren_request_file_post_context_free(post_context);
 

	
 
      return 1;
 
    }
 

	
 
  client_file_post = malloc(sizeof(struct distrend_client_file_post));
 
  if(!client_file_post)
 
    {
 
      distrend_send_disconnect(client, "Error accepting file: out of memory");
 

	
 
      distren_request_file_post_context_free(post_context);
 

	
 
      return 1;
 
    }
 

	
 
  client_file_post->post_context = post_context;
 
  client_file_post->post_id = post_id;
 
  client_file_post->filename = filename;
 
  _distren_asprintf(&client_file_post->file_save_path, "%s/conn-%d_file_post-%d",
 
		   geninfo->files.tmpdir, client->connection_id, post_id);
 
  client_file_post->fd = fopen(client_file_post->file_save_path, "w");
 
  if(!client_file_post->fd)
 
    {
 
      perror("fopen");
 

	
 
      fprintf(stderr, "error: Unable to open ``%s''. See above ``fopen'' error for more details.\n", client_file_post->file_save_path);
 
      distrend_send_disconnect(client, "Error accepting file: unable to store the file n disk");
 

	
 
      distren_request_file_post_context_free(post_context);
 

	
 
      list_remove_element(client->file_post_list, client_file_post);
 
      distrend_client_file_post_free(client_file_post);
 
      return 1;
 
    }
 

	
 
  list_insert_after(client->file_post_list, client_file_post, 0);
 

	
 
  return 0;
 
}
 

	
 
int distrend_handle_file_post(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  struct distrend_client_file_post *client_file_post;
 

	
 
  void *file_data;
 
  size_t file_data_len;
 

	
 
  uint32_t post_id;
 
  char *tmp_str;
 

	
 
  size_t written_len;
 

	
 
  int ret;
 

	
 
  ret = distren_request_parse_file_post(req, req_data, &post_id,
 
					&distrend_client_find_file_post_context, client,
 
					&file_data, &file_data_len);
 
  if(ret)
 
    {
 
      distrend_send_disconnect(client, "You sent me an invalid DISTREN_REQUEST_FILE_POST packet");
 
      return 1;
 
    }
 

	
 
  client_file_post = distrend_client_find_post(client, post_id);
 

	
 
  if(!client_file_post)
 
    {
 
      _distren_asprintf(&tmp_str, "You are attempting to upload post_id=%d when you haven't given a DISTREN_REQUEST_FILE_POST_START packet", post_id);
 
      distrend_send_disconnect(client, tmp_str);
 
      free(tmp_str);
 
      return 1;
 
    }
 

	
 
  written_len = fwrite(file_data, 1, file_data_len, client_file_post->fd);
 
  if(written_len < file_data_len)
 
    {
 
      distrend_send_disconnect(client, "Error saving upload: error while writing to the temporary upload file");
 

	
 
      list_remove_element(client->file_post_list, client_file_post);;
 
      /* closes the file being written, and free()s everything, unlinks the file */
 
      distrend_client_file_post_free(client_file_post);
 

	
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * here be magic
 
 */
 
int distrend_handle_file_post_finish(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  struct distrend_client_file_post *client_file_post;
 

	
 
  uint32_t post_id;
 
  int ret;
 

	
 
  ret = distren_request_parse_file_post_finish(req, req_data, &post_id,
 
					       &distrend_client_find_file_post_context, client);
 
  if(ret)
 
    {
 
      switch(ret)
 
	{
 
	case 2:
 
	  /* client asked to cancel a file upload */
 
	  client_file_post = distrend_client_find_post(client, post_id);
 
	  if(client_file_post)
 
	    {
 
	      list_remove_element(client->file_post_list, client_file_post);
 
	      distrend_client_file_post_free(client_file_post);
 
	    }
 
	  return 0;
 

	
 
	case 3:
 
	  /* checksuming failed */
 
	  distrend_send_disconnect(client, "You have uploaded a file that doesn't match its checksum somehow... which should be pretty much impossible");
 
	  client_file_post = distrend_client_find_post(client, post_id);
 
	  if(client_file_post)
 
	    {
 
	      list_remove_element(client->file_post_list, client_file_post);
 
	      distrend_client_file_post_free(client_file_post);
 
	    }
 
	  return 1;
 

	
 
	default:
 
	  distrend_send_disconnect(client, "You sent me an invalid DISTREN_REQUEST_FILE_POST_FINISH packet");
 
	}
 
      return 1;
 
    }
 

	
 
  client_file_post = distrend_client_find_post(client, post_id);
 
  if(!client_file_post)
 
    return 1;
 

	
 
  /*
 
   * Here it is... manage a file being submitted for rendering... or
 
   * for whatever purpose it was uploaded for somehow...
 
   */
 
  distrend_handle_successful_upload(client, client_file_post);
 

	
 
  list_remove_element(client->file_post_list, client_file_post);
 
  distrend_client_file_post_free(client_file_post);
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * Does stuff with file uploads after they've been successfully acquired.
 
 *
 
 * @todo this should probably be genericized to handle 1. file uploads and 2. server-initiated file _downloads_, i.e., using libcurl. In that case, struct distrend_client_file_post shouldn't be passed directly to ourself but some common struct might be passed here.
 
 */
 
int distrend_handle_successful_upload(struct distrend_client *client, struct distrend_client_file_post *client_file_post)
 
{
 
  fprintf(stderr, __FILE__ ":%d: STUB: I don't know what to do with %s[%s] :-/\n", __LINE__,
 
	  client_file_post->filename, client_file_post->file_save_path);
 

	
 
  return 0;
 
}
 

	
 
/**
 
   Performs command stored in a client's request. @TODO: Fill stub
 
*/
 
int distrend_do()
 
{
 
  return 0;
 
}
 

	
 
/* Grabs config info from confs */
 
int distrend_do_config(int argc, char *argv[], struct distrend_config **config, multiio_context_t multiio)
 
{
 
  unsigned int counter;
 

	
 
  cfg_opt_t myopts_listen[] =
 
    {
 
      CFG_SIMPLE_STR("type", NULL),
 
      CFG_SIMPLE_STR("path", NULL),
 
      CFG_SIMPLE_INT("port", NULL),
 
      CFG_END()
 
    };
 
  cfg_opt_t myopts[] =
 
    {
 
      CFG_SEC("listen",  /* this must be imported into struct listens (which must still be declared) */
 
          myopts_listen,
 
          CFGF_MULTI),
 
      CFG_SIMPLE_STR("datadir", NULL),
 
      CFG_STR_LIST("render_types", NULL, CFGF_NONE),
 
      CFG_SIMPLE_STR("mysql_user", NULL),
 
      CFG_SIMPLE_STR("mysql_host", NULL),
 
      CFG_SIMPLE_STR("mysql_pass", NULL),
 
      CFG_SIMPLE_STR("mysql_database", NULL),
 
      CFG_END()
 
    };
 

	
 
  cfg_t *cfg_listen;
 

	
 
  fprintf(stderr, "%s:%d: running config\n", __FILE__, __LINE__);
 
  *config = malloc(sizeof(struct distrend_config));
 
  myopts[1].simple_value = &(*config)->datadir;
 
  myopts[3].simple_value = &(*config)->mysql_user;
 
  myopts[4].simple_value = &(*config)->mysql_host;
 
  myopts[5].simple_value = &(*config)->mysql_pass;
 
  myopts[6].simple_value = &(*config)->mysql_database;
 

	
 
  if(options_init(argc, argv, &(*config)->mycfg, myopts, "daemon", &(*config)->options, multiio))
 
    return 1;
 

	
 
  /**
 
     grab listen blocks:
 
   */
 
  (*config)->listen_ports = malloc(sizeof(int) * (cfg_size((*config)->mycfg, "listen") + 1));
 
  for(counter = 0; counter < cfg_size((*config)->mycfg, "listen"); counter ++)
 
    {
 
      cfg_listen = cfg_getnsec((*config)->mycfg, "listen", counter);
 
      (*config)->listen_ports[counter] = cfg_getint(cfg_listen, "port");
 
    }
 
  (*config)->listen_ports[counter] = 0;
 

	
 
  fprintf(stderr, "using %s as datadir\n", (*config)->datadir);
 

	
 
  return 0;
 
}
 

	
 
int distrend_config_free(struct distrend_config *config)
 
{
 
  distrend_listen_free(config->listens);
 
  options_free(config->options);
 
  free(config->listen_ports);
 
  free(config);
 

	
 
  return 0;
 
}
 

	
 
/* ************************** XML Functions ************************* */
 

	
 
// writes the general_info.xml file which is a copy of the general_info structure
 
// except that it doesn't hold free_clients and rendering_clients
 
void update_general_info(struct general_info *geninfo)
 
{
 
  xmlTextWriterPtr writer;
 
  char *tmp;
 

	
 
  writer = xmlNewTextWriterFilename(geninfo->files.geninfo, 0);
 
  xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
 

	
 
  xmlTextWriterStartElement(writer, (xmlChar*)"general_info");
 

	
 
  _distren_asprintf(&tmp, "%d", geninfo->jobs_in_queue);
 
  xmlTextWriterWriteElement(writer, (xmlChar*)"jobs_in_queue", (xmlChar*)tmp);
 
  free(tmp);
 

	
 
  _distren_asprintf(&tmp, "%d", geninfo->total_finished_jobs);
 
  xmlTextWriterWriteElement(writer, (xmlChar*)"total_finished_jobs", (xmlChar*)tmp);
 
  free(tmp);
 

	
 
  _distren_asprintf(&tmp, "%d", geninfo->total_frames_rendered);
 
  xmlTextWriterWriteElement(writer, (xmlChar*)"total_frames_rendered", (xmlChar*)tmp);
 
  free(tmp);
 

	
 
  _distren_asprintf(&tmp, "%d", geninfo->highest_jobnum);
 
  xmlTextWriterWriteElement(writer, (xmlChar*)"highest_jobnum", (xmlChar*)tmp);
 
  free(tmp);
 

	
 
  xmlTextWriterEndDocument(writer);
 
  xmlFreeTextWriter(writer);
 
}
 

	
 
/**
 
   Reads general state information from general_info.xml
 
   into the general_info structure.
 
*/
 
int import_general_info(struct general_info *general_info)
 
{
 
  xmlDocPtr doc;
 
  xmlNodePtr cur;
 

	
 
  doc = xmlParseFile(general_info->files.geninfo);
 
  cur = xmlDocGetRootElement(doc);
 
  if (xmlStrcmp(cur->name, (xmlChar*)"general_info"))
 
    {
 
      fprintf(stderr, "xml document is wrong type");
 
      xmlFreeDoc(doc);
 
      return 1;
 
    }
 

	
 
  cur = cur->xmlChildrenNode;
 
  general_info->jobs_in_queue = atoi((char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
 
  cur = cur->next;
 
  general_info->total_finished_jobs = atoi((char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
 
  cur = cur->next;
 
  general_info->total_frames_rendered = atoi((char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
 
  cur = cur->next;
src/server/listen.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "common/config.h"
 

	
 
#include "listen.h"
 

	
 
#include "common/protocol.h"
 
#include "common/remoteio.h"
 
#include "common/request.h"
 

	
 
#include <errno.h>
 
#include <list.h>
 
#include <netinet/in.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <sys/types.h>
 
#include <poll.h>
 
#include <sys/socket.h>
 
#include <unistd.h>
 

	
 
/* local */
 

	
 
struct distrend_request_handler_info
 
{
 
  enum distren_request_type request_type;
 
  distrend_handle_request_func_t handler;
 
  /**
 
   * A bitmasking of different enum distren_client_states which are
 
   * allowed to use this particular command.
 
   */
 
  uint8_t client_states;
 
};
 

	
 
struct distrend_client *distrend_client_new(struct distrend_listens *listens,
 
					    enum distrend_client_state state,
 
					    struct remoteio *rem,
 
					    int connection_id);
 
int distrend_client_free(struct distrend_client *client);
 
int distrend_dispatch_request(struct distrend_listens *listens, struct remoteio *rem, struct distrend_client *client, struct distren_request *req, void *reqdata);
 
struct distrend_polled_sock *distrend_polled_sock_get_by_offset(struct distrend_listens *listens, size_t pollfds_offset);
 
static size_t distrend_listen_read_handle(struct remoteio *rem, struct distrend_listens *listens, void *buf, size_t len, struct distrend_client *client);
 
static void distrend_listen_remoteio_handle_close(struct distrend_listens *listens, struct distrend_client *client);
 

	
 
int listen_handle_accept(multiio_context_t multiio,
 
			 int fd,
 
			 short revent,
 
			 struct distrend_listens *listens,
 
			 int *port);
 
int listen_handle_error(multiio_context_t multiio,
 
			int fd,
 
			short revent,
 
			struct distrend_listens *listens,
 
			int *port);
 

	
 
/*** TO BE MOVED TO REMOTEIO */
 
int listen_handle_existence(multiio_context_t multiio,
 
			    int fd,
 
			    short revent,
 
			    struct distrend_listens *listens,
 
			    struct distrend_client *client);
 

	
 
struct distrend_listens *distrend_listens_new(multiio_context_t multiio,
 
					      struct general_info *geninfo,
 
					      struct options_common *opts)
 
{
 
  struct distrend_listens *listens;
 

	
 
  listens = malloc(sizeof(struct distrend_listens));
 
  if(!listens)
 
    return NULL;
 

	
 
  listens->request_handlers = list_init();
 
  if(!listens->request_handlers)
 
    {
 
      free(listens);
 
      return NULL;
 
    }
 

	
 
  listens->options = opts;
 
  listens->geninfo = geninfo;
 

	
 
  /* multiio */
 
  listens->multiio = multiio;
 

	
 
  /* tabletennis */
 
  listens->tabletennis = tabletennis_new(listens, 32, 16);
 

	
 
  /* This type is used for accepting connections with accept() */
 
  multiio_socket_type_register(multiio, &listens->socket_type);
 

	
 
  multiio_event_handler_register(multiio,
 
				 listens->socket_type,
 
				 POLLERR | POLLHUP | POLLNVAL,
 
				 (multiio_event_handler_func_t)&listen_handle_error,
 
				 listens);
 
  multiio_event_handler_register(multiio,
 
				 listens->socket_type,
 
				 POLLIN,
 
				 (multiio_event_handler_func_t)&listen_handle_accept,
 
				 listens);
 

	
 
  return listens;
 
}
 

	
 
int distrend_listen_add(struct distrend_listens *listens, int port)
 
{
 
  int tmp;
 
  int fd;
 
  int *saved_port;
 

	
 
  struct sockaddr_in6 sockaddr =
 
    {
 
      .sin6_family = AF_INET6,
 
      .sin6_port = 0,
 
      .sin6_flowinfo = 0,
 
      .sin6_addr = IN6ADDR_ANY_INIT,
 
      .sin6_scope_id = 0
 
    };
 

	
 
  sockaddr.sin6_port = htons(port);
 

	
 
  fd = socket(AF_INET6, SOCK_STREAM, 0);
 
  tmp = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
 
  if(tmp == -1)
 
    {
 
      perror("bind");
 
      close(fd);
 

	
 
      return 1;
 
    }
 

	
 
  tmp = listen(fd, 1);
 
  if(tmp == -1)
 
    {
 
      perror("listen");
 
      close(fd);
 
      
 
      return 1;
 
    }
 

	
 
  saved_port = malloc(sizeof(int));
 
  if(!saved_port)
 
    {
 
      perror("malloc");
 
      close(fd);
 

	
 
      return 1;
 
    }
 
  *saved_port = port;
 

	
 
  tmp = multiio_socket_add(listens->multiio, fd, listens->socket_type, saved_port, POLLIN);
 
  if(tmp)
 
    {
 
      close(fd);
 

	
 
      return 1;
 
    }
 

	
 
  /**
 
   * @todo perhaps we'll someday want the ability to unlisten()? :-p
 
   * Then we have to store the fd somewheres so that we can call
 
   * multiio_socket_del() on it. So far, however, that functionality
 
   * isn't needed.
 
   */
 

	
 
  return 0;
 
}
 

	
 
int listen_handle_error(multiio_context_t multiio,
 
			int fd,
 
			short revent,
 
			struct distrend_listens *listens,
 
			int *port)
 
{
 
  fprintf(stderr, "Port %d experienced an error or is closed. Closing it.\n", *port);
 
  multiio_socket_del(listens->multiio, fd);
 
  close(fd);
 

	
 
  free(port);
 

	
 
  return 0;
 
}
 

	
 

	
 
/**
 
 * an important thing to handle
 
 *
 
 * @deprecated to be replaced with table_tennis
 
 */
 
int listen_handle_existence(multiio_context_t multiio,
 
			    int fd,
 
			    short revent,
 
			    struct distrend_listens *listens,
 
			    struct distrend_client *client)
 
{
 
  /**
 
   * handle dead/closed sockets here!
 
   */
 
  fprintf(stderr, __FILE__ ":%d: handle dead/closed sockets here!\n", __LINE__);
 

	
 
  /**
 
   * Manage timing-out clients.
 
   */
 
  if(time(NULL) > client->cleanup_time)
 
    switch(client->state)
 
      {
 
      case DISTREND_CLIENT_PREVERSION:
 
	distrend_send_disconnect(client, "You have failed to present version information in a timely manner. Cya :-p");
 
	break;
 
      case DISTREND_CLIENT_PREAUTH:
 
	distrend_send_disconnect(client, "You have failed to present authentication information in a timely manner. Cya ;-)");
 
	break;
 

	
 
      case DISTREND_CLIENT_GOOD:
 
	/*
 
	 * pings should be managed with two queues sharing this struct:
 
	 *   struct pingpong_queue { struct distrend_client *client; time_t ping_time };
 
	 *
 
	 * - queue to_ping: contains queue of clients to send pings to
 
	 *   with the client whose ping time is earliest at the front
 
	 *
 
	 * - queue to_be_ponged: contains a queue of clients to clean up
 
	 *   if they haven't recieved pongs. client with earliest cleanup
 
	 *   time is at the front. If a PONG packet is received in time, the
 
	 *   cleanup_time is bumped but the queue is left alone. When the
 
	 *   queue's element is encountered, cleanup_time is checked and then
 
	 *   the client is readded to the to_ping queue.
 
	 *
 
	 * Each queue shall be eaten as time passes and continue forever
 
	 * in circularity.
 
	 *
 
	 * data structures.
 
	 * fun.
 
	 * impossible to explain in text.
 
	 * easy to think up.
 
	 * impossible to determine the workings of existing ones.
 
	 * and... when you need them, screenshot utilities just aren't available :-/.
 
	 */
 
	distrend_send_disconnect(client, "Ping timeout :-p");
 
	break;
 

	
 
      case DISTREND_CLIENT_BAD:
 
	fprintf(stderr, __FILE__ ":%d: aaarrrrgh!\n :-D\n", __LINE__);
 
	break;
 

	
 
      default:
 
	break;
 
      }
 
  return 0;
 
}
 

	
 
struct distrend_accept_client_proc_data
 
{
 
  struct distrend_listens *listens;
 
  time_t current_time;
 
};
 

	
 

	
 
/**
 
 * Handle new connections.
 
 */
 
int listen_handle_accept(multiio_context_t multiio,
 
			 int fd,
 
			 short revent,
 
			 struct distrend_listens *listens,
 
			 int *port)
 
   //int distrend_accept(struct distrend_listens *listens)//, struct distrend_clientset *clients, distrend_handle_request_t handlereq, void *handlereqdata)
 
 {
 
   struct distrend_client *newclient;
 

	
 
   int newclientsock;
 

	
 
   struct remoteio *rem;
 

	
 
   struct distren_request *req;
 
   void *data;
 

	
 
   newclientsock = accept(fd, (struct sockaddr *)NULL, (socklen_t *)NULL);
 
   /* used to call int distrend_client_add(struct distrend_listens *listens, int sock, DISTREND_CLIENT_PREVERSION)*/
 

	
 
   newclient = distrend_client_new(listens, DISTREND_CLIENT_PREVERSION, NULL, newclientsock);
 

	
 
   if(remoteio_open_socket(&rem, listens->options->remoteio, (remoteio_read_handle_func_t)&distrend_listen_read_handle, newclient, (remoteio_close_handle_func_t)&distrend_listen_remoteio_handle_close, newclientsock))
 
     {
 
       fprintf(stderr, "error allocating/adding client struct\n");
 
       return 1;
 
     }
 
   newclient->rem = rem;
 

	
 
   fprintf(stderr, "accepted new connection; fd=%d\n", newclientsock);
 

	
 
   /* tabletennis */
 
   tabletennis_add_client(listens->tabletennis, newclient);
 

	
 
   /**
 
    * For those using netcat/telnet to debug their internets.
 
    */
 
 #ifndef PACKAGE_URL
 
 #define PACKAGE_URL "http://ohnopub.net/distren/"
 
 #endif
 
 #define DISTREN_GREETING PACKAGE_STRING " " PACKAGE_URL " : Nathan Phillip Brink && Ethan Michael Zonca\n"
 
   /* using sizeof() - 1 because the sizeof() includes a NULL byte we want to ignore. */
 
   remoteio_write(newclient->rem, DISTREN_GREETING, sizeof(DISTREN_GREETING) - 1);
 

	
 
   /* send a DISTREN_REQUEST_VERSION immediately as per protocol */
 
   distren_request_version(&req, &data,
 
			   DISTREN_SERVERTYPE_SUBMIT
 
			   | DISTREN_SERVERTYPE_DISTRIBUTE
 
			   | DISTREN_SERVERTYPE_RENDER,
 
			   PACKAGE_STRING);
 
   distren_request_send(rem, req, data);
 
   distren_request_free_with_data(req, data);
 

	
 
   /*
 
   list_mvfront(listens->clients);
 
   newclient = list_curr(listens->clients);
 
   while(newclient)
 
     {
 
       if(newclient->state == DISTREND_CLIENT_DEAD)
 
	 {
 
	   distrend_listen_poll_deletefd(listens, &newclient->sock);
 
	   distrend_client_free(newclient);
 
	   list_remove_curr(listens->clients);
 
	   fprintf(stderr, "removed dead connection\n");
 
	 }
 
       list_mvnext(listens->clients);
 
   *//* provide for termination of this loop *//*
 
       if(newclient == list_curr(listens->clients))
 
	 newclient = NULL;
 
       else
 
	 newclient = list_curr(listens->clients);
 
     }
 
					       */
 
   return 0;
 
 }
 

	
 
 /**
 
  * Handle read events from remoteio, remoteio_read_handle_func_t.
 
  *
 
  * This func requires that someone called remoteio_generic_data_set(remoteio_opts, listens);
 
  *
 
  * @param client the client associated with this remoteio instance.
 
  */
 
size_t distrend_listen_read_handle(struct remoteio *rem, struct distrend_listens *listens, void *buf, size_t len, struct distrend_client *client)
 
 {
 
   struct distren_request *req;
 
   void *reqdata;
 

	
 
   size_t used_len;
 

	
 
  /*
 
   * ignore packets from a client who's going to be disconnected.
 
   */
 
  if(client->state == DISTREND_CLIENT_BAD)
 
    return len;
 

	
 
   used_len = 0;
 
   /**
 
    * Manage input, etc.
 
    */
 
   if(client->expectlen == 0)
 
     {
 
       /* search out header from input so far */
 
       if(len > sizeof(struct distren_request))
 
	 {
 
	   if(distren_request_new_fromdata(&req, buf, len))
 
	     {
 
	       fprintf(stderr, "Error handling data from client (magic likely did not match), closing connection\n");
 

	
 
	       /*
 
		* yes, this is safe and legal... because of hackishness
 
		* in remoteio_close() ;-)
 
		*/
 
	      remoteio_close(rem);
 
	      return 1;
 
	    }
 
	  client->expectlen = req->len + sizeof(struct distren_request);
 
	  distren_request_free(req);
 
	}
 
    }
 
  if(client->expectlen
 
     && len >= client->expectlen)
 
    {
 
      if(distren_request_new_fromdata(&req, buf, len))
 
	{
 
	  if(client->state == DISTREND_CLIENT_PREAUTH)
 
	    remoteio_close(rem);
 
	  else
 
	    distrend_send_disconnect(client, "Protocol error.");
 

	
 
	  return 1;
 
	}
 

	
 
      /*
 
       * this really shouldn't happen... reparsing the same data with
 
       * distren_request_new_fromdata() a second time shouldn't yeild
 
       * a different req->len than it did before.
 
       */
 
      if(len - sizeof(struct distren_request) < req->len)
 
	{
 
	  fprintf(stderr, "Unexpected error handling some data from client\n");
 
	  distren_request_free(req);
 

	
 
	  /* but we should pay homage to W3C if the impossible happens */
 
	  distrend_send_disconnect(client, "HTTP/1.1 503 Internal Server Error");
 
	  return 1;
 
	}
 

	
 
      reqdata = malloc(req->len);
 
      if(!reqdata)
 
	{
 
	  fprintf(stderr, "OOM\n");
 

	
 
	  distren_request_free(req);
 
	  return 1;
 
	}
 
      memcpy(reqdata, ((void *)buf) + sizeof(struct distren_request), req->len);
 

	
 
      client->expectlen = 0;
 
      used_len = sizeof(struct distren_request) + req->len;
 

	
 
      distrend_dispatch_request(listens, rem, client, req, reqdata);
 
      free(reqdata);
 
      distren_request_free(req);
 

	
 
      /* I actually just used recursion in non-LISP code! :-D */
 
      return used_len + distrend_listen_read_handle(rem, listens, buf + used_len, len - used_len, client);
 
    }
 

	
 
  return used_len;
 
}
 

	
 

	
 
/**
 
 * Handle cleaning up after remoteio_close() has been called. This includes cleaning up the struct distrend_client and stuffs
 
 */
 
void distrend_listen_remoteio_handle_close(struct distrend_listens *listens, struct distrend_client *client)
 
{
 
  /*
 
   * remoteio handles removing itself from multiio for us. We just
 
   * have to clean up tabletennis and the struct itself.
 
   */
 

	
 
  tabletennis_del_client(listens->tabletennis, client);
 
  list_free(client->file_post_list, (list_dealloc_func_t)&distrend_client_file_post_free);
 

	
 
  free(client);
 
}
 

	
 
int distrend_listen_free(struct distrend_listens *listens)
 
{
 
  tabletennis_free(listens->tabletennis);
 
  fprintf(stderr, "%s:%d: I am a stub that needn't be implemented 'til later\n", __FILE__, __LINE__);
 

	
 
  return 1;
 
}
 
/**
 
   This is probably just NOT a placeholder for remotio
 
*/
 
void remotio_send_to_client(struct distrend_client *client, const char *msg, size_t len)
 
{
 
    fprintf(stderr, "%s:%d: STUB I should queue data for writing to a client.... or should I? :-p\n", __FILE__, __LINE__);
 
}
 

	
 
/**
 
 * Allocates and initializes a struct distrend_client.
 
 *
 
 *
 
 */
 
struct distrend_client *distrend_client_new(struct distrend_listens *listens, enum distrend_client_state state, struct remoteio *rem, int connection_id)
 
{
 
  struct distrend_client *client;
 

	
 
  client = malloc(sizeof(struct distrend_client));
 
  if(!client)
 
    {
 
      fprintf(stderr, "OOM\n");
 

	
 
      return NULL;
 
    }
 
  client->state = state;
 
  client->cleanup_time = time(NULL) + DISTREND_LISTEN_AUTHTIME;
 
  client->inlen = 0;
 
  client->expectlen = 0;
 
  client->file_post_list = list_init();
 
  client->rem = rem;
 
  client->connection_id = connection_id;
 

	
 
  return client;
 
}
 

	
 
int distrend_client_write_request(struct distrend_client *client, const struct distren_request *req, const void *data)
 
{
 
  char *towrite;
 
  int ret;
 
  size_t msglen;
 

	
 
  msglen = sizeof(struct distren_request) + req->len;
 

	
 
  towrite = malloc(msglen);
 
  if(!towrite)
 
    {
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 

	
 
  memcpy(towrite, req, sizeof(struct distren_request));
 
  memcpy(towrite + sizeof(struct distren_request), data, req->len);
 

	
 
  ret = remoteio_write(client->rem, towrite, msglen);
 

	
 
  free(towrite);
 
  return ret;
 
}
 

	
 
/*
 
int distrend_client_read(struct distrend_client *client, char **toread, size_t *lenread)
 
{
 
  struct distrend_packet *packet;
 

	
 
  *lenread = 0;
 
  *toread = NULL;
 

	
 
  if(q_empty(client->inmsgs))
 
    return 1;
 

	
 
  packet = q_dequeue(client->inmsgs);
 
  *lenread = packet->len;
 
  *toread = packet->data;
 
  free(packet);
 

	
 
  return 0;
 
}
 
*/
 

	
 

	
 
int distrend_send_disconnect(struct distrend_client *client, const char *quit_msg)
 
{
 
  struct distren_request *req;
 

	
 
  distren_request_new(&req, strlen(quit_msg), DISTREN_REQUEST_DISCONNECT);
 
  distrend_client_write_request(client, req, quit_msg);
 
  distren_request_free(req);
 

	
 
  client->state = DISTREND_CLIENT_BAD;
 
  client->cleanup_time = time(NULL) + DISTREND_LISTEN_DISCONNECT_GRACE;
 

	
 
  return 0;
 
}
 

	
 
int distrend_listen_handler_add(struct distrend_listens *listens, enum distren_request_type type, distrend_handle_request_func_t handler)
 
int distrend_listen_handler_add(struct distrend_listens *listens, enum distren_request_type type, distrend_handle_request_func_t handler, uint8_t client_state_mask)
 
{
 
  struct distrend_request_handler_info *handler_info;
 

	
 
  handler_info = malloc(sizeof(struct distrend_request_handler_info));
 
  if(!handler_info)
 
    return 1;
 

	
 
  handler_info->request_type = type;
 
  handler_info->handler = handler;
 
  handler_info->client_states = client_state_mask;
 
  list_insert_after(listens->request_handlers, handler_info, 0);
 

	
 
  return 0;
 
}
 

	
 
struct distrend_dispatch_request_data
 
{
 
  struct general_info *geninfo;
 
  struct distrend_client *client;
 
  struct distren_request *req;
 
  void *req_data;
 
};
 

	
 
/**
 
   traversal function for distrend_dispatch_request().
 
 */
 
int _distrend_dispatch_request_trav(struct distrend_dispatch_request_data *data, struct distrend_request_handler_info *handler_info)
 
{
 
  if(handler_info->request_type == data->req->type)
 
    {
 
      /* check permissions first */
 
      if(!(data->client->state & handler_info->client_states))
 
	{
 
	  distrend_send_disconnect(data->client, "You attempted to use a command out of context.");
 
	  return FALSE;
 
	}
 

	
 
    (*handler_info->handler)(data->geninfo, data->client, data->req, data->req_data);
 

	
 
      /* shortcut one a hit ;-) */
 
      return FALSE;
 
    }
 

	
 
  return TRUE;
 
}
 

	
 
/**
 
   helper for distrend_listen_read_handle() which looks up the correct
 
   request handler and handles handing the the request to the
 
   handler. :-p
 
*/
 
int distrend_dispatch_request(struct distrend_listens *listens, struct remoteio *rem, struct distrend_client *client, struct distren_request *req, void *reqdata)
 
{
 
  struct distrend_dispatch_request_data data;
 

	
 
  data.geninfo = listens->geninfo;
 
  data.client = client;
 
  data.req = req;
 
  data.req_data = reqdata;
 

	
 
  list_traverse(listens->request_handlers, &data, (list_traverse_func_t)&_distrend_dispatch_request_trav, LIST_FRNT | LIST_SAVE);
 

	
 
  return 0;
 
}
src/server/listen.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
/**
 
   @file listen provides the ability to set up a listening socket
 
   through multiio's poll() interface. This means that if the listen()-ish
 
   libc calls we use are incompatible with windows, we don't have to
 
   throw ifdefs all throughout src/common. We just put that code in the
 
   only place that needs it: in src/server.
 
 */
 

	
 
struct general_info;
 
struct distrend_listens;
 
struct distrend_client;
 

	
 
#ifndef _DISTREN_LISTEN_H
 
#define _DISTREN_LISTEN_H
 

	
 
#include "distrend.h"
 
#include "tabletennis.h"
 

	
 
#include "common/options.h"
 
#include "common/multiio.h"
 
#include "common/protocol.h"
 
#include "common/remoteio.h"
 
#include "common/request.h"
 

	
 
#include <queue.h>
 
#include <time.h>
 

	
 
/**
 
   How long a client has after connecting to send
 
   authentication information before his connection is cleaned
 
   up.
 
 */
 
#define DISTREND_LISTEN_AUTHTIME 32
 

	
 
/**
 
   How long a client has when in DISTREND_CLIENT_BAD before
 
   his connection is dropped. This grace time is intended so that
 
   the client will actually see his disconnect message instead of
 
   just having his connection reset.
 
 */
 
#define DISTREND_LISTEN_DISCONNECT_GRACE 8
 

	
 
/**
 
 * Numbers are explicitly given so that commands can be restricted to
 
 * clients in certain states.
 
 */
 
enum distrend_client_state
 
  {
 
    /**
 
       The client hasn't yet given us its version.
 
     */
 
    DISTREND_CLIENT_PREVERSION,
 
    DISTREND_CLIENT_PREVERSION = 1,
 
    /**
 
       We don't yet know the client. It may only use authentication
 
       commands.
 
     */
 
    DISTREND_CLIENT_PREAUTH,
 
    DISTREND_CLIENT_PREAUTH = 2,
 
    /**
 
       The client is authenticated, etc.
 
     */
 
    DISTREND_CLIENT_GOOD,
 
    DISTREND_CLIENT_GOOD = 4,
 
    /**
 
       The client is queued to be disconnected. (This state exists
 
       so that the client at least has a chance to recieve its
 
       disconnect message/error before being dumped).
 
     */
 
    DISTREND_CLIENT_BAD,
 
    DISTREND_CLIENT_BAD = 8,
 
  };
 

	
 
struct distrend_listens
 
{
 
  /* of type (struct distrend_request_handler_info) */
 
  list_t request_handlers;
 
  /* the data to pass on to all request handlers */
 
  struct general_info *geninfo;
 
  /* the distrend config */
 
  struct options_common *options;
 

	
 
  tabletennis_t tabletennis;
 

	
 
  /* of type (struct distrend_client)  (multiio stores a pointer per socket, we'll store each strut distrend_client as that pointer instead of using this list) */
 
  /* list_t clients; */
 

	
 
  /* the multiio context the listening interface should use/initialize */
 
  multiio_context_t multiio;
 
  /*
 
   * the socket type reserved for us, i.e., the listen()/accept()
 
   * socket type whose events we handle.
 
  */
 
  multiio_socket_type_t socket_type;
 
};
 

	
 
struct distrend_client_file_post
 
{
 
  distren_request_file_post_context_t post_context;
 
  uint32_t post_id;
 
  /** user-friendly filename */
 
  char *filename;
 
  /** on-disk filename */
 
  char *file_save_path;
 
  FILE *fd;
 
};
 

	
 
/**
 
   The information necessary to recieve data from and send data
 
   to a client.
 
 */
 
struct distrend_client
 
{
 
  enum distrend_client_state state;
 

	
 
  /**
 
     The absolute time at which this client's entry in the client list will be
 
     expired, closed, and marked as dead so that it may be cleaned up. This is
 
     used to implement ping timeouts (if state == DISTREND_CLIENT_GOOD) and 
 
     disconnect message grace time (if state == DISTREND_CLIENT_BAD).
 
   */
 
  time_t cleanup_time;
 

	
 
  size_t inlen; /*< number of bytes waiting to be processed */
 
  size_t expectlen; /*< number of bytes that inlen has to be for a complete request to be had, 0 when waiting on header */
 

	
 
  struct tabletennis_client tabletennis_client;
 

	
 
  /**
 
   * File postings (uploads) are per-connection, and thus stored
 
   * here. Of type struct distrend_client_file_post.
 
   */
 
  list_t file_post_list;
 

	
 
  /**
 
   * Yes, in some places we need a unique key for this connection.
 
   */
 
  int connection_id;
 

	
 
  /**
 
   * Yes, even though the remoteio will have a void *pointer to this
 
   * struct, we need a reverse pointer for something like
 
   * distrend_client_write() to work.
 
   */
 
  struct remoteio *rem;
 
};
 

	
 

	
 

	
 
/**
 
   A function signature that may be registered as a client
 
   request handler.
 

	
 
   @param client the client that sent the request.
 
   @param req the distren request header.
 
   @param data the message received from the client.
 
 */
 
typedef int(*distrend_handle_request_func_t)(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 

	
 
/**
 
   Initializes the listens member of struct distrend_config.
 

	
 
   @param multiio the multiio context in which we should register a new socket type and insert records for clients who connect.
 
   @param geninfo general info to apss to the request handler.
 
   @return Must be free()d with distrend_listen_free();
 
*/
 
struct distrend_listens *distrend_listens_new(multiio_context_t multiio, struct general_info *geninfo, struct options_common *opts);
 

	
 
/**
 
   Adds a socket and configures it to listen().
 

	
 

	
 
   @param listens The handle for this set of listens, obtained via distrend_listen_init().
 
 */
 
int distrend_listen_add(struct distrend_listens *listens, int port);
 

	
 
/**
 
 * Register a request handler with the listener.
 
 *
 
 * @param config distrend's configuration
 
 * @param type the request type for which this handler should be called
 
 * @param handler the handler to call when a request of type type is received.
 
 * \param config distrend's configuration.
 
 * \param type The request type for which this handler should be
 
 *   called
 
 * \param handler The handler to call when a request of type type is
 
 *   received.
 
 * \param client_state_mask A mask of enum distrend_client_state
 
 *   values that are allowed to use this command.
 
 */
 
int distrend_listen_handler_add(struct distrend_listens *listens, enum distren_request_type type, distrend_handle_request_func_t handler);
 
int distrend_listen_handler_add(struct distrend_listens *listens, enum distren_request_type type, distrend_handle_request_func_t handler, uint8_t client_state_mask);
 

	
 
/**
 
 * cleans listening sockets/frees main struct. Unnecessary for a working server, currently a stub.
 
 */
 
int distrend_listen_free(struct distrend_listens *listens);
 

	
 
/**
 
   writes request to client.
 
   @param client client to write to
 
   @param req the request struct. caller must free this.
 
   @param data the data of the request which is req->len bytes long. caller must free this.
 
 */
 
int distrend_client_write_request(struct distrend_client *client, const struct distren_request *req, const void *data);
 

	
 
/**
 
   This is probably just NOT a placeholder for remotio
 
*/
 
void remotio_send_to_client();
 

	
 
/**
 
 * Queue a DISTREN_REQUEST_DISCONNECT and prepare a client
 
 * to be disconnected.
 
 */
 
int distrend_send_disconnect(struct distrend_client *client, const char *quit_msg);
 

	
 

	
 
#endif
src/server/tabletennis.c
Show inline comments
 
/*
 
 * Copyright 2010 Nathan Phillip Brink
 
 *
 
 * This file is a part of DistRen.
 
 *
 
 * DistRen is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * DistRen is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
#include "common/config.h"
 

	
 
#include "distrend.h"
 
#include "listen.h"
 
#include "tabletennis.h"
 

	
 
#include "common/request.h"
 
#include "common/protocol.h"
 

	
 
#include <queue.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <time.h>
 

	
 
struct tabletennis
 
{
 
  unsigned int ping_interval;
 
  unsigned int pong_time;
 

	
 
  /* of type (struct distrend_client *) */
 
  queue_t clients_to_ping;
 

	
 
  /* of type (struct distrend_client *) */
 
  queue_t clients_need_pong;
 

	
 
  struct timespec time_last_check;
 
};
 

	
 
static int tabletennis_pong_request_handle(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 
static int tabletennis_ping_request_handle(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data);
 

	
 
tabletennis_t tabletennis_new(struct distrend_listens *listens, unsigned int ping_interval, unsigned int pong_time)
 
{
 
  tabletennis_t tabletennis;
 

	
 
  tabletennis = malloc(sizeof(struct tabletennis));
 

	
 
  tabletennis->ping_interval = ping_interval;
 
  tabletennis->pong_time = pong_time;
 
  tabletennis->clients_to_ping = q_init();
 
  tabletennis->clients_need_pong = q_init();
 
  clock_gettime(CLOCK_MONOTONIC, &tabletennis->time_last_check);
 

	
 
  distrend_listen_handler_add(listens, DISTREN_REQUEST_PING, &tabletennis_ping_request_handle);
 
  distrend_listen_handler_add(listens, DISTREN_REQUEST_PONG, &tabletennis_pong_request_handle);
 
  distrend_listen_handler_add(listens, DISTREN_REQUEST_PING, &tabletennis_ping_request_handle, (uint8_t)DISTREND_CLIENT_GOOD);
 
  distrend_listen_handler_add(listens, DISTREN_REQUEST_PONG, &tabletennis_pong_request_handle, (uint8_t)DISTREND_CLIENT_GOOD);
 

	
 
  return tabletennis;
 
}
 

	
 
int tabletennis_add_client(tabletennis_t tabletennis, struct distrend_client *client)
 
{
 
  client->tabletennis_client.state = TABLETENNIS_NEED_PING;
 
  client->tabletennis_client.time_next_check = tabletennis->time_last_check.tv_sec
 
    + tabletennis->ping_interval;
 

	
 
  q_enqueue(tabletennis->clients_to_ping, client, 0);
 

	
 
  return 0;
 
}
 

	
 
int tabletennis_serve(tabletennis_t tabletennis)
 
{
 
  struct timespec time_now;
 
  struct distrend_client *client;
 
  time_t time_next_check;
 

	
 
  struct distren_request *req;
 
  void *req_data;
 

	
 
  clock_gettime(CLOCK_MONOTONIC, &time_now);
 

	
 
  time_next_check = time_now.tv_sec + tabletennis->pong_time;
 
  for(client = q_front(tabletennis->clients_to_ping);
 
      client && client->tabletennis_client.time_next_check < time_now.tv_sec;
 
      client = q_front(tabletennis->clients_to_ping))
 
    {
 
      q_dequeue(tabletennis->clients_to_ping);
 

	
 
      /* use time_next_check as the ping data */
 
      distren_request_poing(&req, &req_data, 1, &time_next_check, sizeof(time_next_check));
 
      distrend_client_write_request(client, req, req_data);
 
      distren_request_free_with_data(req, req_data);
 

	
 
      client->tabletennis_client.state = TABLETENNIS_NEED_PONG;
 

	
 
      client->tabletennis_client.time_next_check = time_next_check;
 

	
 
      q_enqueue(tabletennis->clients_need_pong, client, 0);
 
    }
 

	
 
  time_next_check = time_now.tv_sec + tabletennis->ping_interval;
 
  for(client = q_front(tabletennis->clients_need_pong);
 
      client && client->tabletennis_client.time_next_check < time_now.tv_sec;
 
      client = q_front(tabletennis->clients_need_pong))
 
    {
 
      q_dequeue(tabletennis->clients_need_pong);
 

	
 
      if(client->tabletennis_client.state == TABLETENNIS_NEED_PONG)
 
	{
 
	  distrend_send_disconnect(client, "You failed to respond to a PING packet after some seconds!");
 
	  client->tabletennis_client.state = TABLETENNIS_DELINQUENT;
 
	}
 
      else /* state must be TABLETENNIS_HAS_PONG */
 
	{
 
	  client->tabletennis_client.time_next_check = time_next_check;
 
	  client->tabletennis_client.state = TABLETENNIS_NEED_PING;
 

	
 
	  q_enqueue(tabletennis->clients_to_ping, client, 0);
 
	}
 
    }
 

	
 
  /* for tabletennis_add_client() */
 
  memcpy(&tabletennis->time_last_check, &time_now, sizeof(struct timespec));
 

	
 
  return 0;
 
}
 

	
 
/**
 
 * helper for tabletennis_del_client() which just looks for a
 
 * particular pointer in the queue (a.k.a., list).
 
 */
 
static int tabletennis_del_client_traverse(void *trav_data, void *client)
 
{
 
  if(trav_data == client)
 
    return FALSE;
 

	
 
  return TRUE;
 
}
 

	
 
int tabletennis_del_client(tabletennis_t tabletennis, struct distrend_client *client)
 
{
 
  list_t thelist;
 

	
 
  thelist = NULL;
 
  switch(client->tabletennis_client.state)
 
    {
 
    case TABLETENNIS_NEED_PING:
 
      thelist = tabletennis->clients_to_ping;
 
      break;
 

	
 
    case TABLETENNIS_NEED_PONG:
 
    case TABLETENNIS_HAS_PONG:
 
      thelist = tabletennis->clients_need_pong;
 
      break;
 

	
 
    case TABLETENNIS_DELINQUENT:
 
      thelist = NULL;
 
      break;
 
    }
 

	
 
  if(!thelist)
 
    return 0;
 

	
 
  list_traverse(thelist, (void *)client, (list_traverse_func_t)&tabletennis_del_client_traverse,
 
		LIST_FORW | LIST_FRNT | LIST_ALTR);
 
  if(list_curr(thelist) == client)
 
    list_remove_curr(thelist);
 

	
 
  return 0;
 
}
 

	
 
void tabletennis_free(tabletennis_t tabletennis)
 
{
 
  q_free(tabletennis->clients_to_ping, LIST_NODEALLOC);
 
  q_free(tabletennis->clients_need_pong, LIST_NODEALLOC);
 

	
 
  free(tabletennis);
 
}
 

	
 
/* implementations of locals */
 

	
 
/**
 
 * Handles a DISTREN_REQUEST_PING from a client.
 
 *
 
 * @todo throttling?
 
 */
 
static int tabletennis_ping_request_handle(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  struct distren_request *pong_req;
 

	
 
  if(req->len > 32)
 
    distrend_send_disconnect(client, "You have tried to send a PING packet with a length longer than 32 bytes.");
 

	
 
  /**
 
      respond to the client using the data he sent in his PONG
 
      command.
 
   */
 
  distren_request_new(&pong_req, req->len, DISTREN_REQUEST_PONG);
 
  distrend_client_write_request(client, pong_req, req_data);
 
  distren_request_free(pong_req);
 

	
 
  return 0;
 
}
 

	
 
static int tabletennis_pong_request_handle(struct general_info *geninfo, struct distrend_client *client, struct distren_request *req, void *req_data)
 
{
 
  fprintf(stderr, "got pong\n");
 

	
 
  /*
 
   * The only place that sends PINGs from distrend is
 
   * tabletennis_serve() and it uses
 
   * client->tabletennis_client.time_next_check as the cookie.
 
   */
 
  if(client->tabletennis_client.state == TABLETENNIS_NEED_PONG
 
     && req->len == sizeof(client->tabletennis_client.time_next_check)
 
     && !memcmp(req_data, &client->tabletennis_client.time_next_check, sizeof(client->tabletennis_client.time_next_check)))
 
    {
 
      /* valid match */
 
      client->tabletennis_client.state = TABLETENNIS_HAS_PONG;
 

	
 
      return 0;
 
    }
 
  /* no match or invalid state */
 
  distrend_send_disconnect(client, "You tried to send a PONG packet without first receiving a PING packet.");
 
  return 0;
 
}
src/server/user_mgr.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
 *  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 
 *
 
 * This file is a part of DistRen.
 
 *
 
 * DistRen is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * DistRen is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "common/config.h"
 

	
 
#include "user_mgr.h"
 

	
 
#include "common/asprintf.h"
 

	
 
#include <list.h>
 

	
 
#include <libxml/xmlmemory.h>
 
#include <libxml/parser.h>
 
#include <libxml/tree.h>
 
#include <libxml/encoding.h>
 
#include <libxml/xmlwriter.h>
 
#include <libxml/xmlreader.h>
 

	
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <unistd.h>
 
#include <sys/stat.h>
 

	
 
struct user_mgr_info
 
struct user_mgr
 
{
 
	struct user *user_array;
 
	int current_users;
 
	int user_array_size;
 
} user_mgr_info;
 

	
 
int resize_user_array()
 
{
 
	int counter;
 
	int counter2;
 

	
 
	// create array twice the size of the current amount of users
 
	user_mgr_info.user_array_size = user_mgr_info.current_users * 2;
 
	struct user *new_user_array = malloc(sizeof(struct user) * user_mgr_info.user_array_size);
 
  /* items are of type user_t */
 
  list_t user_list;
 
  /* where to load/save the userlist */
 
  char *userlist_filename;
 
};
 

	
 
	// this copies the original user_array over to the new one
 
	// using two counters allows the array to be resized at any time
 
	// leaving exactly 1 open space between each user when it is done;
 
	counter2 = 0;
 
	for(counter = 0; counter < user_mgr_info.current_users; counter++)
 
	{
 
		if(user_mgr_info.user_array[counter].username != 0)
 
static int user_find_traverse_forw(char *search_username, user_t user)
 
		{
 
			new_user_array[counter2*2] = user_mgr_info.user_array[counter];
 
			counter2++;
 
		}
 
	}
 

	
 
	// cleanup old array
 
	free(user_mgr_info.user_array);
 

	
 
	// change the pointer to point to the new user array
 
	user_mgr_info.user_array = new_user_array;
 

	
 
	return 1;
 
  if(strcmp(search_username, user->username) >= 0)
 
    return FALSE;
 
  return TRUE;
 
}
 

	
 
struct user *findUser(char *nameOfUser)
 
{
 
	int high;
 
	int low;
 
	int middle;
 
	int result;
 

	
 
	high = user_mgr_info.user_array_size - 1;
 
	low = 0;
 
	result = -1;
 

	
 
	for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
 
	{
 
		// in case middle lands on a part of the array with no user
 
		while(user_mgr_info.user_array[middle].username == 0)
 
static int user_find_traverse_back(char *search_username, user_t user)
 
		{
 
			if(result < 0)
 
				middle --;
 
			else
 
				middle ++;
 
		}
 

	
 
		// this is where the array is cut in half and the half that the nameOfUser is on is kept
 
		result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
 
		if(result == 0)
 
			return &user_mgr_info.user_array[middle];
 
		else if(result < 0)
 
			high = middle;
 
		else
 
			low = middle;
 

	
 
		// in case the user doesn't exist
 
		if(high-low <= 0)
 
			return 0;
 
	}
 
}
 

	
 
int deleteUser(struct user *user_ptr)
 
{
 
	free(user_ptr->username);
 
	memset(user_ptr, '\0', sizeof(struct user));
 

	
 
	user_mgr_info.current_users--;
 

	
 
	return 1;
 

	
 
	backup_list_XML();
 
}
 

	
 
int createUser(struct user *user_ptr, char *nameOfUser)
 
{
 
	// clear old memory
 
	memset(user_ptr, '\0', sizeof(struct user));
 

	
 
	user_ptr->username = nameOfUser;
 
	user_ptr->render_power = 0;
 
	user_ptr->last_job = 0;
 

	
 
	user_mgr_info.current_users++;
 

	
 
	return 1;
 
  if(strcmp(search_username, user->username) <= 0)
 
    return FALSE;
 
  return TRUE;
 
}
 

	
 
// places the new user at position index in the array, moves other users if it needs to
 
int placeUser(int index, char *nameOfUser)
 
user_t user_find(user_mgr_t user_mgr, const char *username)
 
{
 
	int higher;
 
	int lower;
 
	int total_moves;
 
  int list_direction;
 
  list_traverse_func_t traverse_func;
 

	
 
	total_moves = 0;
 
  user_t user;
 
  char *username_copy;
 

	
 
	// I shift data in the array to create an open the space where the user should be added
 
	// but first I figure out which way is the shortest
 
	if(user_mgr_info.user_array[index].username != 0)
 
	{
 
		higher = index + 1;
 
		while(user_mgr_info.user_array[higher].username != 0)
 
			higher++;
 
  if(list_empty(user_mgr->user_list))
 
    return NULL;
 

	
 
		lower = index - 1;
 
		while(user_mgr_info.user_array[lower].username != 0)
 
			lower--;
 
  /* grab the current user for tiny optimizations.. */
 
  user = list_curr(user_mgr->user_list);
 
  if(!user)
 
    return NULL;
 

	
 
		// here the data is shifted to open up a space
 
		if(index - lower < higher - index)
 
		  {
 
		    total_moves = index - lower;
 
		    for(; lower < index; lower++)
 
				memcpy(&user_mgr_info.user_array[lower], &user_mgr_info.user_array[lower + 1], sizeof(struct user));
 
		  }
 
		else
 
  list_direction = LIST_FORW;
 
  traverse_func = (list_traverse_func_t)&user_find_traverse_forw;
 
  if(strcmp(user->username, username) < 0)
 
		  {
 
		    total_moves = higher - index;
 
		    for(; higher > index; higher--)
 
				memcpy(&user_mgr_info.user_array[higher], &user_mgr_info.user_array[higher - 1], sizeof(struct user));
 
		  }
 
	}
 
      list_direction = LIST_BACK;
 
      traverse_func = (list_traverse_func_t)&user_find_traverse_back;
 
    };
 

	
 
	// add the user to the array
 
	createUser(&user_mgr_info.user_array[index], nameOfUser);
 
  username_copy = strdup(username);
 
  list_traverse(user_mgr->user_list, username_copy, traverse_func, list_direction|LIST_CURR|LIST_ALTR);
 
  free(username_copy);
 
  user = list_curr(user_mgr->user_list);
 
  if(!user)
 
    return NULL;
 

	
 
	if(total_moves > 50)
 
	  resize_user_array();
 
  if(!strcmp(username, user->username))
 
    return user;
 

	
 
	return 1;
 
  return NULL;
 
}
 

	
 
int addUser(char *nameOfUser)
 
int user_add(user_mgr_t user_mgr, const char *username, const char *pass)
 
{
 
	int high;
 
	int low;
 
	int middle;
 
	int result;
 
  user_t user;
 
  short insert_after;
 

	
 
  user = user_find(user_mgr, username);
 
  if(user)
 
    return 1;
 

	
 
	high = user_mgr_info.user_array_size - 1;
 
	low = 0;
 
	result = -1;
 

	
 
	for(middle = (low+high)/2; 1 == 1; middle = (low+high)/2)
 
  /*
 
   * The list should be positioned within one element of where we want
 
   * to insert username.
 
   */
 
  insert_after = 1;
 
  user = list_curr(user_mgr->user_list);
 
  if(user)
 
	{
 
		// in case middle lands on a part of the array with no user
 
		while(user_mgr_info.user_array[middle].username == 0)
 
		{
 
			if(result < 0)
 
				middle--;
 
			else
 
				middle++;
 
      if(strcmp(username, user->username) < 0)
 
	insert_after = 0;
 
		}
 

	
 
		// this is where the array is cut in half and the half that the nameOfUser is on is kept
 
		result = strcmp(nameOfUser, user_mgr_info.user_array[middle].username);
 
		if(result == 0)
 
			return 0;
 
		else if(result < 0)
 
			high = middle;
 
		else
 
			low = middle;
 

	
 
		// once there are less than 10 possible places for the user to be placed
 
		// break out of this loop and begin next loop
 
		if(high-low <= 10)
 
			break;
 
	}
 
  /*
 
   * construct the new user.
 
   */
 
  user = malloc(sizeof(struct user));
 
  if(!user)
 
    return 1;
 
  user->username = strdup(username);
 
  user->pass = strdup(pass);
 
  user->render_power = 0;
 
  user->last_job = 0;
 

	
 
	// this function starts at the low index number, and goes up until it finds a
 
	// username that is bigger than it alphabetically, and tells the userPlacer
 
	// that it needs to go 1 before that spot
 
	for(; low <= high; low++)
 
	{
 
		while(user_mgr_info.user_array[low].username == 0)
 
			low++;
 
  /* I admit it... I'm completely crazy --binki */
 
  if(insert_after)
 
    list_insert_after(user_mgr->user_list, user, 0);
 
  else
 
    list_insert_before(user_mgr->user_list, user, 0);
 

	
 
		result = strcmp(nameOfUser, user_mgr_info.user_array[low].username) < 0;
 
		if(result < 0)
 
		{
 
			placeUser(low - 1, nameOfUser);
 
			return 1;
 
		}
 
		if(result == 0)
 
		{
 
			fprintf(stderr, "user already exists");
 
			return 0;
 
		}
 
	}
 

	
 
	backup_list_XML();
 
	return 0;
 
}
 

	
 
int initialize_users()
 
{
 
  struct stat buffer;
 
  // cif user_list.xml exists
 
  if(stat("user_list.xml", &buffer) == 0)
 
  {
 
    restart_From_XML_backup();
 
  }
 
  else
 
/**
 
 * \brief For list_free() et al
 
 */
 
static void user_free(user_t user)
 
  {
 
    user_mgr_info.current_users = 0;
 

	
 
    user_mgr_info.user_array_size = 50;
 
    user_mgr_info.user_array = malloc(sizeof(struct user) * 50);
 
  }
 

	
 
  // if XML file is not found create new array of size 50
 

	
 

	
 
  return 1;
 
  free(user->username);
 
  free(user->pass);
 
  free(user);
 
}
 

	
 
/********************************** XMLness *****************************/
 

	
 
int backup_list_XML()
 
int user_delete(user_mgr_t user_mgr, user_t user)
 
{
 
	xmlTextWriterPtr writer;
 
        char *tmp;
 
	int counter;
 
  user_t user_found;
 
  int ret;
 

	
 

	
 
	writer = xmlNewTextWriterFilename("user_list.xml", 0);
 
	xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
 
  ret = 0;
 

	
 
	// create root element user_list
 
	xmlTextWriterStartElement(writer, (xmlChar*)"user_list");
 
  user_found = user_find(user_mgr, user->username);
 
  if(user_found
 
     && user_found == user
 
     && user == list_curr(user_mgr->user_list))
 
    list_remove_curr(user_mgr->user_list);
 
  else
 
    {
 
      fprintf(stderr, __FILE__ ":%d:user_delete(): List is inconsistent :-/\n", __LINE__);
 
      ret = 1;
 
    }
 

	
 
	_distren_asprintf(&tmp, "%d", user_mgr_info.current_users);
 
	xmlTextWriterWriteAttribute(writer, (xmlChar*)"amount_of_users", (xmlChar*)tmp);
 
	free(tmp);
 
  user_free(user);
 

	
 
  return ret;
 
}
 

	
 
	for(counter = 0; counter < user_mgr_info.user_array_size; counter++)
 
static int user_mgr_save_traverse(xmlTextWriterPtr writer, user_t user)
 
	{
 
		if(user_mgr_info.user_array[counter].username != 0)
 
		{
 
  char *tmp;
 

	
 
			xmlTextWriterStartElement(writer, (xmlChar*)"user");
 

	
 
			xmlTextWriterWriteAttribute(writer, (xmlChar*)"name", (xmlChar*)user_mgr_info.user_array[counter].username);
 
  xmlTextWriterWriteAttribute(writer, (xmlChar *)"name", (xmlChar*)user->username);
 
  xmlTextWriterWriteAttribute(writer, (xmlChar *)"pass", (xmlChar*)user->pass);
 

	
 
			_distren_asprintf(&tmp, "%d", user_mgr_info.user_array[counter].last_job);
 
  _distren_asprintf(&tmp, "%d", user->last_job);
 
			xmlTextWriterWriteAttribute(writer, (xmlChar*)"last_job", (xmlChar*)tmp);
 
			free(tmp);
 

	
 
			_distren_asprintf(&tmp, "%d", user_mgr_info.user_array[counter].render_power);
 
  _distren_asprintf(&tmp, "%d", user->render_power);
 
			xmlTextWriterWriteAttribute(writer, (xmlChar*)"render_power", (xmlChar*)tmp);
 
			free(tmp);
 

	
 
			xmlTextWriterEndElement(writer);
 
		}
 
	}
 

	
 
	return 0;
 

	
 
  return TRUE;
 
}
 

	
 
int restart_From_XML_backup(){
 
int user_mgr_save(user_mgr_t user_mgr, const char *filename)
 
{
 
  xmlTextWriterPtr writer;
 

	
 
  if(!filename)
 
    filename = user_mgr->userlist_filename;
 

	
 
  writer = xmlNewTextWriterFilename(filename, 0);
 
  xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
 

	
 
  /*
 
   * create root element user_list
 
   */
 
  xmlTextWriterStartElement(writer, (xmlChar*)"user_list");
 

	
 
  /**
 
   * \todo error checking/handling?
 
   */
 
  list_traverse(user_mgr->user_list, writer, (list_traverse_func_t)&user_mgr_save_traverse, LIST_FRNT|LIST_FORW|LIST_SAVE);
 

	
 
  xmlTextWriterEndElement(writer);
 
  xmlTextWriterEndDocument(writer);
 
  xmlTextWriterFlush(writer);
 

	
 
  return 0;
 
}
 

	
 
user_mgr_t user_mgr_init(const char *userfile)
 
{
 
  xmlDocPtr doc;
 
  xmlNodePtr cur;
 
  int counter;
 

	
 
  user_mgr_t user_mgr;
 

	
 
  user_t user;
 
  xmlChar *username;
 
  xmlChar *pass;
 
  xmlChar *tmp;
 
  int render_power;
 
  int last_job;
 

	
 
  doc = xmlParseFile("user_list.xml");
 
  user_mgr = malloc(sizeof(struct user_mgr));
 
  user_mgr->user_list = list_init();
 
  user_mgr->userlist_filename = strdup(userfile);
 

	
 
  doc = xmlParseFile(userfile);
 
  if (!doc)
 
    {
 
      fprintf(stderr, "user_mgr: Error opening userlist, assuming we should start with an empty userlist\n");
 
      return user_mgr;
 
    }
 

	
 
  cur = xmlDocGetRootElement(doc);
 
  if (xmlStrcmp(cur->name, (xmlChar*)"user_list"))
 
    {
 
      fprintf(stderr, "xml document is wrong type");
 
      fprintf(stderr, "user_mgr: xml document is wrong type. Using empty userlist, which will overwrite the old userlist soon probably...");
 
      xmlFreeDoc(doc);
 
      return 1;
 
      return user_mgr;
 
    }
 

	
 
  for(cur = cur->xmlChildrenNode; cur; cur = cur->next)
 
    {
 
      if (cur->type != XML_ELEMENT_NODE)
 
	/* skip the implicit XML_TEXT_NODEs */
 
	continue;
 

	
 
      if (xmlStrcmp(cur->name, (xmlChar *)"user"))
 
	{
 
	  fprintf(stderr, "user_mgr: Unrecognized XML element: <%s />\n",
 
		 cur->name);
 

	
 
	  continue;
 
	}
 

	
 
      username = xmlGetProp(cur, (xmlChar *)"name");
 
      pass = xmlGetProp(cur, (xmlChar *)"pass");
 

	
 
      if(!username)
 
	{
 
	  fprintf(stderr, "<user /> is missing a name attribute! (skipping)\n");
 
	  continue;
 
	}
 
      if(!pass)
 
	{
 
	  fprintf(stderr, "<user name=\"%s\"/> is missing a pass attribute! (skipping)\n",
 
		  username ? (char *)username : "");
 
	  continue;
 
    }
 

	
 
  user_mgr_info.current_users = atoi((char*)xmlGetProp(cur, (xmlChar*)"amount_of_users"));
 

	
 
  user_mgr_info.user_array_size = user_mgr_info.current_users * 2;
 
  user_mgr_info.user_array = malloc(sizeof(struct user) * user_mgr_info.user_array_size);
 
      last_job = 0;
 
      tmp = xmlGetProp(cur, (xmlChar *)"last_job");
 
      if(tmp)
 
	{
 
	  last_job = atoi((char *)tmp);
 
	  xmlFree(tmp);
 
	}
 

	
 
  cur = cur->xmlChildrenNode;
 
  for(counter = 0; cur->next; counter++){
 
    user_mgr_info.user_array[counter*2].username = (char*)xmlGetProp(cur, (xmlChar*)"amount_of_users");
 
    user_mgr_info.user_array[counter*2].last_job = atoi((char*)xmlGetProp(cur, (xmlChar*)"last_job"));
 
    user_mgr_info.user_array[counter*2].render_power = atoi((char*)xmlGetProp(cur, (xmlChar*)"render_power"));
 
    cur = cur->next;
 
      render_power = 0;
 
      if(tmp)
 
	{
 
	  tmp = xmlGetProp(cur, (xmlChar *)"render_power");
 
	  render_power = atoi((char *)tmp);
 
	  xmlFree(tmp);
 
  }
 

	
 
  return 0;
 
      user_add(user_mgr, (char *)username, (char *)pass);
 
      xmlFree(username);
 
      xmlFree(pass);
 

	
 
      /*
 
       * user_find should be a very inexpensive operation immediately
 
       * after the user_add() above. I just don't want to trust to
 
       * list_curr() right in this function ;-).
 
       *
 
       * Also, we set this information without passing the following
 
       * as arguments to user_add() because user_add() is an external
 
       * API used for when a user is initially added to the
 
       * userlist. Thus, everybody'd be calling it with
 
       * user_add("username", "pass", 0, 0);
 
       */
 
      user = user_find(user_mgr, (char *)username);
 
      if(user)
 
	{
 
	  user->render_power = render_power;
 
	  user->last_job = last_job;
 
}
 
    }
 

	
 
  return user_mgr;
 
}
src/server/user_mgr.h
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
/*
 
 * Stores, keeps track of, retrieves information concerning,
 
 * and backs up to XML DistRen's userdb system
 
 */
 

	
 
#ifndef _DISTREN_USER_MGR_H
 
#define _DISTREN_USER_MGR_H
 

	
 
struct user
 
{
 
	char *username;
 
  char *pass;
 
	int render_power;
 
	int last_job;
 
};
 
typedef struct user *user_t;
 

	
 
struct user_mgr;
 
typedef struct user_mgr *user_mgr_t;
 

	
 
/**
 
 * \brief Allocate and initialize a user_mgr.
 
 *
 
 * \param userfile The path to where and XML file with user
 
 *   information may be found and where the users file should be save
 
 *   to when new users are added.
 
 * \return The new user_mgr or NULL on error.
 
 */
 
user_mgr_t user_mgr_init(const char *userfile);
 

	
 
/**
 
 * \brief Find a user by username.
 
 *
 
 * \param user_mgr The user_mgr...
 
 * \param username The username to search for.
 
 * \return The user if it is in user_mgr or NULL.
 
 */
 
user_t user_find(user_mgr_t user_mgr, const char *username);
 

	
 
/**
 
 * \brief Add a user to the user_mgr.
 
 *
 
 * \param username The user's username (which is local in scope to this server).
 
 * \param pass The user's plaintext password (insecurity is sooo much easier ;-) ).
 
 * \return 0 on success, 1 on failure (attempt to insert duplicate user, etc.).
 
 */
 
int user_add(user_mgr_t user_mgr, const char *username, const char *pass);
 

	
 
/**
 
 * \brief Delete a user.
 
 *
 
 * The user handle passed to this function is no longer valid after
 
 * this function is called.
 
 *
 
 * \param user The user's handle as retrieved via user_find().
 
 * \return 0 on success, 1 on failure (user does not exist, inconsistency, etc.)
 
 */
 
int user_delete(user_mgr_t user_mgr, user_t user);
 

	
 
/**
 
 * \brief Write out the XML file listing all of the users local to this server.
 
 *
 
 * \param filename The file to write the userlist to or NULL if you
 
 *   want to write to the same filename you loaded the usre_mgr from.
 
 */
 
int user_mgr_save(user_mgr_t user_mgr, const char *filename);
 

	
 

	
 
int restart_From_XML_backup();
 
int backup_list_XML();
 

	
 
#endif
0 comments (0 inline, 0 general)