Exemple #1
0
/**
 *  Establish a tcp connection with the server.
 *  @address: server to contact
 *  @return: an error code
 */
const char *operator_connect (ENetAddress address)
{
        const char *error = NULL;

        if (host_init_client())
                error = "Failed to create host.";
        else if (connected)
                error = "You're already connected!";
        else {
                if (host_connect(&address))
                        error = "Connection attempt failed.";
        }

        return error;
}
Exemple #2
0
static bool reissue_connection(struct config_info *conf, struct host **h, char *host_name)
{
  bool ret = false;
  int buf_len;

  assert(h != NULL);

  /* We might be passed an existing socket. If we have been, close
   * and destroy the associated host, and create a new one.
   */
  if(*h)
    host_destroy(*h);

  *h = host_create(HOST_TYPE_TCP, HOST_FAMILY_IPV4);
  if(!*h)
  {
    error("Failed to create TCP client socket.", 1, 8, 0);
    goto err_out;
  }

  m_hide();

  buf_len = snprintf(widget_buf, WIDGET_BUF_LEN,
   "Connecting to \"%s\". Please wait..", host_name);
  widget_buf[WIDGET_BUF_LEN - 1] = 0;

  draw_window_box(3, 11, 76, 13, DI_MAIN, DI_DARK, DI_CORNER, 1, 1);
  write_string(widget_buf, (WIDGET_BUF_LEN - buf_len) >> 1, 12, DI_TEXT, 0);
  update_screen();

  if(!host_connect(*h, host_name, OUTBOUND_PORT))
  {
    buf_len = snprintf(widget_buf, WIDGET_BUF_LEN,
     "Connection to \"%s\" failed.", host_name);
    widget_buf[WIDGET_BUF_LEN - 1] = 0;
    error(widget_buf, 1, 8, 0);
  }
  else
    ret = true;

  clear_screen(32, 7);
  m_show();
  update_screen();

err_out:
  return ret;
}
void *thread_do_get_read(void *pv)
{
    int fd, wfd, n;
    char line[MAXLINE], *filename;
    struct file *pf;

    pf = (struct file *)pv;

    fd = host_connect(pf->f_pchHost, WEBSERVPORT);
    if (fd < 0)
        err_sys("host_connect error");
    pf->f_iFd = fd;
    pf->f_thrId = pthread_self();

    printf("thread_do_get_read for %s, fd %d, thread %u\n",
            pf->f_pchName, pf->f_iFd, pf->f_thrId);

    thread_write_get_cmd(pf);

    filename = strrchr(pf->f_pchName, '/');
    if (filename == NULL)
        filename = pf->f_pchName;
    else
        filename++;
    if ((wfd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
        err_sys("open error");

    /* read server's reply */
    for (;;) {
        if ((n = Read(pf->f_iFd, line, sizeof(line))) == 0)
            break; /* server closed connection */
        Writen(wfd, line, n);
        printf("read %d bytes from %s\n", n, pf->f_pchName);
    }
    printf("end-of-file on %s\n", pf->f_pchName);
    Close(pf->f_iFd);
    Close(wfd);
    pf->f_iFlags = F_DONE;

    Pthread_mutex_lock(&ndone_mutex);
    ndone++;
    Pthread_cond_signal(&ndone_cond);
    Pthread_mutex_unlock(&ndone_mutex);
    return pf;
}
Exemple #4
0
void gui_host_connect(GtkWidget *w, gpointer dialog)
{
gchar *text;
gpointer host;
GtkEntry *user_entry, *host_entry;

user_entry = dialog_child_get(dialog, "user");
host_entry = dialog_child_get(dialog, "host");

text = g_strdup_printf("%s@%s", gtk_entry_get_text(user_entry), gtk_entry_get_text(host_entry));
host = host_new(text);
g_free(text);

if (host_connect(host))
  printf("host_connect(): success!\n");
else
  printf("host_connect(): failed!\n");

gui_host_update(host, dialog);
}
void thread_home_page(const char *webName, const char *homepage, char **ppchReturn)
{
    struct hostent *ph;
    if ((ph = gethostbyname(webName)) == NULL)
        err_sys("gethostbyname error for host: %s", webName);

    char **ppch, *pchMalloc, line[MAXLINE];
    int n, fd;
    if (ph->h_addrtype != AF_INET)
        err_sys("unknown address type");
    for (ppch = ph->h_addr_list; *ppch != NULL; ppch++) {
        if((fd = host_connect(*ppch, WEBSERVPORT)) < 0)
            continue;
        break;
    }
    if (*ppch == NULL)
        err_quit("can't connect to web server");

    pchMalloc = malloc(ph->h_length); /* 32-bit IPv4 address */
    if (pchMalloc == NULL)
        err_sys("malloc error");
    memcpy(pchMalloc, *ppch, ph->h_length);
    *ppchReturn = pchMalloc;

    n = snprintf(line, sizeof(line), GET_CMD, homepage);
    Writen(fd, line, n);
    for (;;) {
        if ((n = Read(fd, line, sizeof(line))) == 0)
            break;
        printf("read %d bytes of home page\n", n);
        /* get the ip and filename with the data 
         * for thread work */
    }
    printf("end-of-file on home page\n");
    Close(fd);
}
Exemple #6
0
pnm_t *pnm_connect(xine_stream_t *stream, const char *mrl) {
  
  char *mrl_ptr=strdup(mrl);
  char *slash, *colon;
  int pathbegin, hostend;
  pnm_t *p;
  int fd;
  int need_response=0;
  
  if (strncmp(mrl,"pnm://",6))
  {
    return NULL;
  }
  
  mrl_ptr+=6;

  p=xine_xmalloc(sizeof(pnm_t));
  p->stream = stream;
  p->port=7070;
  p->url=strdup(mrl);
  p->packet=0;

  slash=strchr(mrl_ptr,'/');
  colon=strchr(mrl_ptr,':');

  if(!slash) slash=mrl_ptr+strlen(mrl_ptr)+1;
  if(!colon) colon=slash; 
  if(colon > slash) colon=slash;

  pathbegin=slash-mrl_ptr;
  hostend=colon-mrl_ptr;

  p->host=malloc(sizeof(char)*hostend+1);
  strncpy(p->host, mrl_ptr, hostend);
  p->host[hostend]=0;

  if (pathbegin < strlen(mrl_ptr)) p->path=strdup(mrl_ptr+pathbegin+1);
  if (colon != slash) {
    strncpy(p->buffer,mrl_ptr+hostend+1, pathbegin-hostend-1);
    p->buffer[pathbegin-hostend-1]=0;
    p->port=atoi(p->buffer);
  }

  free(mrl_ptr-6);

#ifdef LOG
  printf("input_pnm: got mrl: %s %i %s\n",p->host,p->port,p->path);
#endif
  
  fd = host_connect (p->host, p->port);

  if (fd == -1) {
    printf ("input_pnm: failed to connect '%s'\n", p->host);
    free(p->path);
    free(p->host);
    free(p->url);
    free(p);
    return NULL;
  }
  p->s=fd;

  pnm_send_request(p,pnm_available_bandwidths[10]);
  if (!pnm_get_headers(p, &need_response)) {
    printf ("input_pnm: failed to set up stream\n");
    free(p->path);
    free(p->host);
    free(p->url);
    free(p);
    return NULL;
  }
  if (need_response)
    pnm_send_response(p, pnm_response);
  p->ts_last[0]=0;
  p->ts_last[1]=0;
  
  /* copy header to recv */

  memcpy(p->recv, p->header, p->header_len);
  p->recv_size = p->header_len;
  p->recv_read = 0;

  return p;
}
Exemple #7
0
int main (int argc, char *argv[]) {
    Host host = host_new();
    host_connect(&host, "localhost", 7101, 7102, "localhost:7103");

    printf("Testing command...");
    assert(command_test(&host));
    printf("done\n");

    // printf("Testing directory...");
    // assert(directory_test(&host));
    // printf("done\n");

    // print("Testing file...");
    // assert(file_test(&host));
    // print("done\n");
    //
    // print("Testing package...");
    // assert(package_test(&host));
    // print("done\n");
    //
    // print("Testing service...");
    // assert(service_test(&host));
    // print("done\n");
    //
    // print("Testing telemetry...");
    // assert(telemetry_test(&host));
    // print("done\n");

    printf("ALL TESTS PASSED. HI-DIDDLY-HO NEIGHBOUR-EENO!\n\n");
    printf("    .sS$$$$$$$$$$$$$$Ss."
"   .$$$$$$$$$$$$$$$$$$$$$$s."
"   $$$$$$$$$$$$$$$$$$$$$$$$S."
"   $$$$$$$$$$$$$$$$$$$$$$$$$$s."
"   S$$$$'        `$$$$$$$$$$$$$"
"   `$$'            `$$$$$$$$$$$."
"    :               `$$$$$$$$$$$"
"   :                 `$$$$$$$$$$"
".====.  ,=====.       $$$$$$$$$$"
".'      ~'       \".    s$$$$$$$$$$"
":       :         :=_  $$$$$$$$$$$"
"`.  ()  :   ()    ' ~=$$$$$$$$$$$'"
"~====~`.      .'    $$$$$$$$$$$"
" .'     ~====~     sS$$$$$$$$$'"
" :      .         $$$$$' $$$$"
".sS$$$$$$$$Ss.     `$$'   $$$'"
"$$$$$$$$$$$$$$$s         s$$$$"
"$SSSSSSSSSSSSSSS$        $$$$$"
"   :                   $$$$'"
"    `.                 $$$'"
"      `.               :"
"       :               :"
"       :              .'`."
"      .'.           .'   :"
"     : .$s.       .'    .'"
"     :.S$$$S.   .'    .'"
"     : $$$$$$`.'    .'"
"        $$$$   `. .'"
"                 `");

    return 0;
}