Beispiel #1
0
/**===========================================
 * @brief main thread, starting up the client and the other threads
 * @param argc:int argv**char
 * ==========================================*/
int main(int argc, char **argv)
{
	char *name   = "Guest";
	char *server = "localhost";
	char *port   = "54321";
	int thread;
	int ret, c, conn=0;
	
	while(optind < argc) {
		int option_index = 0;
		static struct option long_options[] = {
			{"name",    required_argument, 0, 'n'},
			{"port",    optional_argument, 0, 'p'},
			{"help",    no_argument,       0, 'h'},
			{0,0,0,0}
		};
		c = getopt_long(argc, argv, "hp:n:", long_options, &option_index);
		if(c == -1) break;
		
		switch(c) {
			case '?': /* unknown parameter */
			case ':': /* missing argument */
				print_help(argv[0]);
				break;
			case 'n':
				name = strdup(optarg);
				if(!name) return -1;
				break;
			case 'p':
				port = strdup(optarg);
				break;
			case 'h':
				print_help(argv[0]);
				break;
			default:
				break;
		}
	}
	while(optind < argc) {
		server = argv[optind++];
	}
	
	GCI.name = name;
	
	printf("Benutzername: %s\n", GCI.name);
	printf("server: %s", server);
	struct addrinfo *addr_info, *p;
	
	ret = getaddrinfo(server, port, NULL, &addr_info);
	if(ret) {
		printf("getaddrinfo: %s\n", gai_strerror(ret));
		exit(-1);
	}
	
	p = addr_info;
	
	while (p)
	{
		if(p->ai_socktype != SOCK_STREAM)/* we only care about TCP */
		{
			p = p->ai_next;
			continue;
		}
		int sock = socket(p->ai_family, p->ai_socktype, 0);
		if(sock == -1)
		{
			perror("socket");
			exit(-1);
		}
		
		if(connect(sock, p->ai_addr, p->ai_addrlen) == 0)
		{
			signal(SIGINT, sigint_handler);
			// move to better positioon
			sem_V(keymng_local(KEY_GCI_SEM));
			
			
			printf("Socket OK");
			GCI.sock = sock;
			send_login(GCI.name);
			int state = wait_loginOK();
			
			if(state !=0)
			{
				printf("Keine antwort erhalten \n");
				return 0;
			}
			
			printf("juhu ich bin eingeloggt \n");
			GCI.status = preparation;
			conn =1;
			
			guiInit(&argc, &argv);
			printf("GUI init \n");
			
			setClientMode();
			preparation_showWindow();
			guiShowMessageDialog("Willkommen bei You Dont Know Rainer",0);
			
			// start the threads
			thread = pthread_create(&listener_thread_id, NULL, &listener_thread, NULL);
			
			if(thread)
			{
				printf("Failed to start Listener Thread\n");
				exit(0);
			}
			
			thread = pthread_create(&fragen_thread_id, NULL, &fragen_thread, NULL);
			
			if(thread)
			{
				printf("Failed to start Fragewechsel Thread\n");
				exit(0);
			}
			//katalog request
			sendCR();
			
			guiMain();
			guiDestroy();
		}
		
		close(sock);
		p = p->ai_next;
	}
	if(conn==0)
		printf("Could not connect to server :/\n");
	
	freeaddrinfo(addr_info);
	exit(0);
}
Beispiel #2
0
KWBoolean
commandLoadMailbox(SMTPClient *client,
            struct _SMTPVerb* verb,
            char **operands)
{
   char xmitBuf[XMIT_LENGTH];

/*--------------------------------------------------------------------*/
/*                   Create the final mailbox name                    */
/*--------------------------------------------------------------------*/

   strncpy(client->transaction->mailboxName,
            client->clientName,
            sizeof client->transaction->mailboxName);

   client->transaction->mailboxName[8] = '\0';

   if (expand_path(client->transaction->mailboxName,
                    client->transaction->userp->homedir,
                    client->transaction->userp->homedir,
                    E_mailext) == NULL)
   {
      sprintf(xmitBuf,
              "Internal error, cannot determine mailbox location for %s",
              client->clientName);

      SMTPResponse(client, PR_ERROR_GENERIC, xmitBuf);
      setClientMode(client, P3_AUTHORIZATION);
      return KWFalse;
   }

/*--------------------------------------------------------------------*/
/*                Get any new mail from system mailbox                */
/*--------------------------------------------------------------------*/

   if (! IncludeNew(client->transaction->mailboxName,
                      client->clientName))
   {
      sprintf(xmitBuf,
              "Internal error, unable to append new mail to mailbox %s: %s",
              client->transaction->mailboxName,
              strerror(errno));
      SMTPResponse(client, PR_ERROR_GENERIC, xmitBuf);
      setClientMode(client, P3_AUTHORIZATION);
      return KWFalse;
   }

/*--------------------------------------------------------------------*/
/*         Load the mailbox information, with error checking          */
/*--------------------------------------------------------------------*/

   if (! popBoxLoad(client))
   {

      sprintf(xmitBuf,
              "Internal error, unable to load mailbox %s",
              client->transaction->mailboxName);

      SMTPResponse(client, PR_ERROR_GENERIC, xmitBuf);
      setClientMode(client, P3_AUTHORIZATION);
      return KWFalse;

   } /* if (! popBoxLoad(client)) */

/*--------------------------------------------------------------------*/
/*                     Report our mailbox is open                     */
/*--------------------------------------------------------------------*/

   sprintf(xmitBuf,
           "%s has %ld message(s) (%ld octets)",
           client->clientName,
           client->transaction->messageCount,
           getMessageOctetCount(client->transaction->top, NULL));

   SMTPResponse(client,
                verb->successResponse,
                xmitBuf);

   return KWTrue;

} /* commandLoadMailbox */
Beispiel #3
0
KWBoolean
SMTPGetLine(SMTPClient *client)
{
   static const char mName[] = "SMTPGetLine";
   char *lineBreak;
   size_t lineLength;
   KWBoolean bResult = KWTrue;

/*--------------------------------------------------------------------*/
/*                   Handle previously signaled EOF                   */
/*--------------------------------------------------------------------*/

   if (isClientEOF(client) && (client->receive.NetworkUsed == 0))
   {
      printmsg(0, "%s: client %d is out of data (EOF)",
                   mName,
                   getClientSequence(client));

      SetDataLine(client, "", 0);
      setClientMode(client, SM_ABORT);
      return KWTrue;
   }

/*--------------------------------------------------------------------*/
/*       Read more data if we have some and need it,                  */
/*--------------------------------------------------------------------*/

   lineBreak = getLineBreak(&client->receive);

   if (getClientReady(client) && (lineBreak == NULL))
   {
      if (client->stalledReads)     /* Improve response time ...     */
         client->stalledReads--;

      SMTPRead(client);
      lineBreak = getLineBreak(&client->receive);
   }

/*--------------------------------------------------------------------*/
/*              If no data available, return immediately              */
/*--------------------------------------------------------------------*/

   if (lineBreak == NULL)
   {

      if (isClientEOF(client))
      {
         static const char quit[] = "quit";
         static const size_t quitLength = sizeof quit - 1;

         if ((client->receive.NetworkUsed == quitLength) &&
             equalni(client->receive.NetworkBuffer, quit, quitLength))
         {
            printmsg(8,"%s: Applying CR/LF after Netscape %s/EOF",
                     mName,
                     quit);
            SetDataLine(client, client->receive.NetworkBuffer, quitLength);
            SMTPBurpBuffer(client, client->receive.NetworkUsed);
         }
         else {
            printmsg(0, "%s: Client %d Terminated unexpectedly without QUIT",
                       mName,
                       getClientSequence(client));
            SMTPBurpBuffer(client, client->receive.NetworkUsed);

            /* Abort client immediately */
            setClientMode(client, SM_ABORT);
            return KWTrue;
         }
      }
      else if (client->receive.NetworkUsed < MAX_BUFFER_SIZE)
      {
         printmsg(2, "%s: Client %d Input buffer "
                      "(%d bytes) waiting for data.",
                      mName,
                      getClientSequence(client),
                      client->receive.NetworkUsed);

         /* Sleep client for few secs  */
         setClientIgnore(client, (time_t) ++client->stalledReads);

         /* Don't process command yet  */
         return KWFalse;

      } /* END if (client->receive.NetworkUsed < MAX_BUFFER_SIZE) */
      else {

        printmsg(0, "%d <<< %.125s",
                     getClientSequence(client),
                     client->receive.NetworkBuffer);
        printmsg(0, "%s: Client %d Input buffer (%d bytes) full (cannot append more).",
                     mName,
                     getClientSequence(client),
                     client->receive.NetworkUsed);

        SetDataLine(client,
                    client->receive.NetworkBuffer,
                    client->receive.NetworkUsed);
        SMTPBurpBuffer(client, client->receive.NetworkUsed);

        /* Abort client immediately     */
        setClientMode(client, SM_ABORT);
        return KWTrue;

      } /* END else */

   } /* END if (lineBreak == NULL) */

   lineLength = lineBreak - client->receive.NetworkBuffer;
   bResult = SetDataLine(client,
                         client->receive.NetworkBuffer,
                         lineLength);

   SMTPBurpBuffer(client, lineLength + 2);
   return bResult;

} /* END SMTPGetLine */