Example #1
0
void * handleConnection(void * arg) {
    char buffer[1024];
    int cd = *((int *)arg);

    // Detach the thread. The main thread doesn't need to wait for this thread
    // to finish or for the return value.
    pthread_detach(pthread_self());

    ssize_t rc = sendGreeting(cd);
    if (rc == -1) {
        close(cd);
        return NULL;
    }

    ///////
    // Receive USER command from client. TODO: move to function
    int buff_len = recvMessage(cd, buffer);

    if (buff_len == -1) {
        close(cd);
        return NULL;
    }

    // TODO: Make sure it's a USER command.

    // TODO: 5 Should be constant.
    int username_len = buff_len - 5;
    char username[username_len + 1];

    memcpy(username, buffer + 5, username_len);
    username[username_len] = '\0';

    printf("User %s logged in\n", username);
    ///////

    rc = sendValidLoginConfirmation(cd);
    if (rc == -1) {
        close(cd);
        return NULL;
    }

    close(cd);
    return NULL;
}
///<summary> Completely initializes a new POLY connection that has been recieved. Uses synchronous reads and sends. </summary>
///<param name='connection'> Connection socket for the incoming connection. </param>
///<param name='connection_info'> The connection info object associated with this connection. </param>
///<param name='out_connectionPointer'> (OUT) The connection object for the newly initialized connection. </param>
///<returns> Error code on failure, connection realm on success. <returns>
int initializeIncomingConnection(void *connection,  void** out_connectionPointer)
{

	// send greeting
	if (sendGreeting(connection) == POLY_REALM_FAILED) return POLY_REALM_FAILED;

	uint8_t buffer[8]; // create a re-usable buffer for operations

	// attempt to recieve encryption setting
	if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

	// handle encryption negotiation
	if (getShortFromBuffer(buffer) != 0) return POLY_REALM_FAILED; // TODO: encryption not yet implemented

	// attempt to recieve realm code
	if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

	POLYM_CONNECTION_INFO *connection_info;

	// connection realm
	switch (getShortFromBuffer(buffer))
	{

	case POLY_REALM_PEER:

	{
		uint16_t connectionID = allocateNewPeer(&connection_info);

		// initalize the peer connection
		initializeNewPeerInfo(connection_info, connection, connectionID);

		//construct the return buffer
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2)) //send the success code and peer ID
		{
			freePeer(connectionID);
			return POLY_REALM_FAILED;
		}
		return POLY_REALM_PEER;
	}

	case POLY_REALM_SERVICE:
	{
		// this connection is a service
		

		//attempt to recieve service string size
		if (2 != sockRecv(connection, buffer, 2)) return POLY_REALM_FAILED;

		// check validity of service string size, initialize string
		int serviceStringSize = getShortFromBuffer(buffer);
		if (serviceStringSize == 0) return POLY_REALM_FAILED;

		// initialize new service connection
		uint16_t connectionID = allocateNewService(&connection_info);
		connection_info->realm_info.service.serviceString = allocateServiceString(serviceStringSize);

		//get the service string
		if (serviceStringSize != sockRecv(connection, connection_info->realm_info.service.serviceString->buf, serviceStringSize))
		{
			freeService(connectionID);
			return POLY_REALM_FAILED;
		}

		// TODO: require user approval

		// construct the last send buffer to send
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2))
		{
			// if for some reason this fails, we need to remove the connection from the service list
			freeService(connectionID);
			return POLY_REALM_SERVICE; // do not return fail so that the connection isn't closed twice
		}
		connection_info->connectionID = connectionID;
		connection_info->realm = POLY_REALM_SERVICE;
		return POLY_REALM_SERVICE;
	}

	case POLY_REALM_CLIENT:

	{
		uint16_t connectionID = allocateNewClient(&connection_info);

		// initalize the client connection
		connection_info->connectionID = connectionID;
		connection_info->realm = POLY_REALM_CLIENT;

		//construct the return buffer
		buffer[0] = 0;
		buffer[1] = 0; // set the first two bytes (result code) to 0 to indicate success

		if (2 != sockSend(connection, buffer, 2)) //send the success code and peer ID
		{
			freeClient(connectionID);
			return POLY_REALM_FAILED;
		}
		
		return POLY_REALM_CLIENT;

	default:
		return POLY_REALM_FAILED;

	}
	}
}
Example #3
0
void Visualight::processServer() {
  if (wifly.available() > 0) { // check for data from wifly


    if (wifly.gets(buf, sizeof(buf))) { /* See if there is a request */

      if (strstr_P(buf, PSTR("GET /mac")) > 0) { /* GET request */
        VPRINTLN(F("Got GET MAC request"));
        while (wifly.gets(buf, sizeof(buf)) > 0) { /* Skip rest of request */
        }
        //int numNetworks = wifly.getNumNetworks();
        sendMac();
        wifly.flushRx();    // discard rest of input
        VPRINTLN(F("Sent Mac address"));
        wifly.close();
      } 
      
      else if (strstr_P(buf, PSTR("GET / ")) > 0) { // GET request
        VPRINTLN(F("Got GET request"));
        while (wifly.gets(buf, sizeof(buf)) > 0) { /* Skip rest of request */
        }
        //int numNetworks = wifly.getNumNetworks();
        wifly.flushRx();    // discard rest of input
        sendIndex();
        VPRINTLN(F("Sent index page"));
        wifly.close();
      }
      
      else if (strstr_P(buf, PSTR("POST")) > 0) { /* Form POST */
         
        VPRINTLN(F("Got POST"));

        if (wifly.match(F("net="))) { /* Get posted field value */
          //Serial.println(buf);
          memset(decodeBuffer, 0, 65);

          wifly.getsTerm(decodeBuffer, sizeof(decodeBuffer),'&');

          urldecode(network, decodeBuffer);

          replaceAll(network,"+","$");
          VPRINTLN(F("Got network: "));
          VPRINTLN(network);

            if (wifly.match(F("pas="******"+","$");
              //wifly.gets(security, 1); //no need for sizeof() -- it's a 1 or 2
              VPRINTLN(F("Got password: "******"sec="))) {
              wifly.gets(security, sizeof(security));
              
              VPRINTLN(F("Got security: "));
              VPRINTLN(security);

            }
          }
          
          wifly.flushRx();    // discard rest of input
          VPRINT(F("Sending greeting page: "));
          sendGreeting(network); //send greeting back
          delay(500);
          sendGreeting(network); //send a second time *just in case*
          delay(500);
          wifly.flushRx();
          joinWifi();
        }
      } 
      else { /* Unexpected request */
        delay(100);
        wifly.flushRx();    // discard rest of input
        VPRINTLN(F("Sending 404"));
        send404();
      }
    }
  }
}