Exemplo n.º 1
0
void serve(int client_fd)
{
  char buffer[100];
  bzero(buffer, 100);
  int bytes = read(client_fd, buffer, 100);
  if(bytes < 0) { std::cerr << "ERROR: read!" << std::endl; close(client_fd); return; }
  std::cout << buffer;

  //I really wanted to use regex for this, but gcc 4.8.4 doesn't support [0-9]
  std::string receive_msg(buffer);
  std::string ip_address;
  std::string port;
  struct sockaddr_in throwaway;
  if(receive_msg.substr(0,8) == "register" && 
      inet_pton(AF_INET, receive_msg.substr(9,receive_msg.find(' ', 10) - 9).c_str(),
        &(throwaway.sin_addr)))
  {
    //get ip/port pairs from register message and place into register list
    ip_address = receive_msg.substr(9,receive_msg.find(' ', 10) - 9);
    port = receive_msg.substr(receive_msg.find(' ', 10) + 1,
                              receive_msg.find('\r') - receive_msg.find(' ', 10) - 1);
    register_list.emplace_back(ipport(ip_address,port));
    strcpy(buffer,"success\r\n");
    send(client_fd, buffer, 100, 0);
  }
  else if(strcmp(receive_msg.c_str(), "list-servers\r\n") == 0)
  {
    //send list of ip/port pairs to client
    bzero(buffer, 100);
    strcpy(buffer, "success\r");
    bytes = send(client_fd, buffer, 100, 0);
    if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; }

    for(auto i : register_list)
    {
      bzero(buffer, 100);
      strcpy(buffer, i.ip_address.c_str());
      strcat(buffer, " ");
      strcat(buffer, i.port.c_str());
      strcat(buffer, "\r");
      bytes = send(client_fd, buffer, 100, 0);
      if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; }
    }
    strcpy(buffer, "\n");
    bytes = send(client_fd, buffer, 1, 0);
    if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; }
  }
  else
  {
    bzero(buffer, 100);
    strcpy(buffer, "failure\r\n");
    bytes = send(client_fd, buffer, 100, 0);
    if(bytes < 0) { std::cerr << "ERROR: send!" << std::endl; return; }
  }
}
Exemplo n.º 2
0
int tcplisten(const ipaddr *addr, int backlog) {
    int err;
    if(dill_slow(backlog < 0)) {errno = EINVAL; return -1;}
    /* Open listening socket. */
    int s = socket(ipfamily(addr), SOCK_STREAM, 0);
    if(dill_slow(s < 0)) return -1;
    tcptune(s);
    /* Start listening. */
    int rc = bind(s, ipsockaddr(addr), iplen(addr));
    if(dill_slow(rc != 0)) return -1;
    rc = listen(s, backlog);
    if(dill_slow(rc != 0)) return -1;
    /* If the user requested an ephemeral port,
       retrieve the port number assigned by the OS now. */
    int port = ipport(addr);
    if(port == 0) {
        ipaddr baddr;
        socklen_t len = sizeof(ipaddr);
        rc = getsockname(s, (struct sockaddr*)&baddr, &len);
        if(dill_slow(rc < 0)) {err = errno; goto error1;}
        port = ipport(&baddr);
    }
    /* Create the object. */
    struct tcplistener *lst = malloc(sizeof(struct tcplistener));
    if(dill_slow(!lst)) {errno = ENOMEM; goto error1;}
    lst->fd = s;
    lst->port = port;
    /* Bind the object to a handle. */
    int h = handle(tcplistener_type, lst, &tcplistener_vfptrs);
    if(dill_slow(h < 0)) {err = errno; goto error2;}
    return h;
error2:
    free(lst);
error1:
    rc = dsclose(s);
    dill_assert(rc == 0);
    errno = err;
    return -1;
}
Exemplo n.º 3
0
	/*************** Compatibility function	*******************************/
	const ipport_addr_t &	get_ipport()	const throw() { return ipport();	}