Example #1
0
int echoServer(int argc, char** argv) {
    int listenfd, connfd, port, clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char* haddrp;
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);

    listenfd = Open_listenfd(port);
    while (1) {
        clientlen = sizeof(clientaddr);
        connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *)&clientlen);

        hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
                sizeof(clientaddr.sin_addr.s_addr), AF_INET);
        haddrp = inet_ntoa(clientaddr.sin_addr);
        printf("server connected to %s (%s)\n", hp->h_name, haddrp);
        echo(connfd);
        Close(connfd);
    }
    exit(0);
}
int main(int argc, char **argv)
{
    char **pp;
    struct in_addr addr;
    struct hostent *hostp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <domain name or dotted-decimal>\n", argv[0]);
        exit(0);
    }

    if (inet_aton(argv[1], &addr) != 0)
        hostp = Gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);  //AF_INET等于0,指的是使用IPV4
    else
        hostp = Gethostbyname(argv[1]);

    printf("official hostname: %s\n", hostp->h_name);

    for (pp = hostp->h_aliases; *pp != NULL; pp++) {  //aliase->别名
        printf("alias: %s\n", *pp);
    }

    for (pp = hostp->h_addr_list; *pp != NULL; pp++) {
        //printf("%s\n", *pp);
        addr.s_addr = ((struct in_addr *)*pp)->s_addr;   //*pp 是 char *类型
        /* expected ‘struct in_addr’ but argument is of type ‘struct in_addr *’ */
        printf("address: %s\n", inet_ntoa(addr));
    }
    exit(0);
}
Example #3
0
// call(2, localhost)
int retrievesAndPrintsDNSHostEntry(int argc, char** argv) {
    char** pp;
    struct in_addr addr;
    struct hostent *hostp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <domain name or dotted-decimal>\n", argv[0]);
        exit(0);
    }

    if (inet_aton(argv[1], &addr) != 0) {
        hostp = Gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
    } else {
        hostp = Gethostbyname(argv[1]);
    }

    printf("official hostname: %s\n", hostp->h_name);

    for (pp = hostp->h_aliases; *pp != NULL; pp++) {
        printf("alias: %s\n", *pp);
    }

    for (pp = hostp->h_addr_list; *pp != NULL; pp++) {
        addr.s_addr = ((struct in_addr *) *pp)->s_addr;
        printf("address: %s\n", inet_ntoa(addr));
    }
}
Example #4
0
int main(int argc, char **argv)
{
    int listenfd, connfd, port;
    socklen_t clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(0);
    }
    port = atoi(argv[1]);

    Signal(SIGCHLD, sigchld_handler);
    listenfd = Open_listenfd(port);
    while (1) {
        connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

        if (Fork() == 0) {
            Close(listenfd);
            echo(connfd);
            hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
            haddrp = inet_ntoa(clientaddr.sin_addr);
            printf("server connected to %s (%s)\n", hp->h_name, haddrp);
            Close(connfd);
            exit(0);
        }
        Close(connfd);
    }
    exit(0);
}
Example #5
0
int main(int argc, char **argv) 
{
    int listenfd, connfd, port, clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    short client_port;
    if (argc != 2) {
	fprintf(stderr, "usage: %s <port>\n", argv[0]);
	exit(0);
    }
    port = atoi(argv[1]);

    listenfd = Open_listenfd(port);
    while (1) {
	clientlen = sizeof(clientaddr);
	connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

	/* determine the domain name and IP address of the client */
	hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
			   sizeof(clientaddr.sin_addr.s_addr), AF_INET);
	haddrp = inet_ntoa(clientaddr.sin_addr);
	client_port = ntohs(clientaddr.sin_port);
	printf("server connected to %s (%s), port %d\n",
	       hp->h_name, haddrp, client_port);
	echo(connfd, "");
	printf("Connection closed\n");
	Close(connfd);
    }
    exit(0);
}
Example #6
0
int main(int argc, char *argv[]) {
	char **pp; /* cursor */
	struct in_addr addr; /* holds the IP in integer form */
	struct hostent *hostp; /* holds various info about the server */

	if (argc != 2) {
		fprintf(stderr, "usage: %s <domain name or dotted-decimal>\n", argv[0]);
		return 0;
	}

	/*inet_aton will convert a dotted decimal into an in_addr. If the user has
	  given a named form, this will be 0, and we'll use Gethostbyname. */
	if (inet_aton(argv[1], &addr) != 0)
		hostp = Gethostbyaddr((const char*) &addr, sizeof(addr), AF_INET);
	else
		hostp = Gethostbyname(argv[1]);

	printf("Official hostname: %s\n", hostp->h_name);

	for (pp=hostp->h_aliases; *pp != NULL; ++pp)
		printf("alias: %s\n", *pp);

	for (pp = hostp->h_addr_list; *pp != NULL; ++pp) {
		addr.s_addr = ((struct in_addr*)*pp)->s_addr;
		printf("address: %s\n", inet_ntoa(addr));
	}

	return 0;
}
int main(int argc, char **argv) 
{
    int port = atoi(argv[1]);
    struct sockaddr_in clientaddr;
    int clientlen=sizeof(clientaddr);
    pthread_t tid; 

    int listenfd = Open_listenfd(port);
    while (1) {
	int *connfdp = Malloc(sizeof(int));
	struct hostent *hp;
	char *haddrp;
	int client_port;

	*connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen);
	/* determine the domain name and IP address of the client */
	hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
			   sizeof(clientaddr.sin_addr.s_addr), AF_INET);
	haddrp = inet_ntoa(clientaddr.sin_addr);
	client_port = ntohs(clientaddr.sin_port);
	printf("Server connected to %s (%s), port %d\n",
	   hp->h_name, haddrp, client_port);
	Pthread_create(&tid, NULL, echo_thread, connfdp);
    }
}
Example #8
0
int main(int argc, char **argv) {
    int listenfd, connfd;
    socklen_t clientlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    user* u;

    /* Init node and set port accoriding to configue file */
    init_node(argc, argv);

    /* Init the linked list of channel and the table of user */
    init_channel();
    init_user();
    
    /* Init server socket and set it as unblocked */
    listenfd = init_unblocking_server_socket(curr_node_config_entry->irc_port);

    /* connect to local daemon */
    local_client_fd = socket_connect(curr_node_config_entry->ipaddr,curr_node_config_entry->local_port);

    /* Init struct pool */
    init_pool();

    add_listen_fd(listenfd);

    while (1) {
        /* Wait for listening/connected descriptor(s) to become ready */
        p.ready_set = p.read_set;
        p.nready = Select(p.maxfd+1, &p.ready_set, NULL, NULL, NULL);

        /* If listening descriptor ready, add new client to pool */
        if (FD_ISSET(listenfd, &p.ready_set)) {
            connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

            /* create a user struct for the connect fd */
            u = (user*) Calloc(1,sizeof(user));
            user_table[connfd] = u;

            /*set server_name and host_name for user */
            u->host_name = strdup(Gethostbyaddr((const char*)&clientaddr.sin_addr,sizeof(clientaddr.sin_addr),AF_INET)->h_name);
            u->server_name = strdup(Gethostbyname("localhost")->h_name);

            add_client(connfd);
        }

        /* Echo a text line from each ready connected descriptor */
        check_clients();
    }
}
Example #9
0
int main(int argc, char* argv[])
{
    int listenfd, connfd;
    socklen_t clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    fd_set read_set, ready_set;

    if (argc != 2) {
        more_error("usage: %s <port>", argv[0]);
    }

    Signal(SIGCHLD, sigchld_handler);
    listenfd = Open_listenfd(atoi(argv[1]));

    FD_ZERO(&read_set);
    FD_SET(STDIN_FILENO, &read_set);
    FD_SET(listenfd, &read_set);

    while (1)
    {
        ready_set = read_set;
        Select(listenfd + 1, &ready_set, NULL, NULL, NULL);
        if (FD_ISSET(STDIN_FILENO, &ready_set)) {
            command();
        } else if (FD_ISSET(listenfd, &ready_set)) {
            clientlen = sizeof(clientaddr);
            connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
            /* printf("%#x\n", ntohl(clientaddr.sin_addr.s_addr)); */
            hp = Gethostbyaddr((const void *)&clientaddr.sin_addr.s_addr, 
                    sizeof(clientaddr.sin_addr.s_addr), AF_INET);
            haddrp = inet_ntoa(clientaddr.sin_addr);
            printf("Server connected to %s [%s]\n", hp->h_name, haddrp);

            if (Fork() == 0) {
                Close(listenfd);
                echo(connfd);
                Close(connfd);
                exit(0);
            }
            Close(connfd);
        }
    }
    return 0;
}
Example #10
0
void servertest(char *port) {
    char *request, *hostname, *response, *client_ip_address;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    int clientlen, connfd;

    // start listening and enter infinite listening loop
    int listenfd = Open_listenfd(port);
    while (1) {
        // accept a connection
        clientlen = sizeof(clientaddr);
        connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
        // get some information from the client
        hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
            sizeof(clientaddr.sin_addr.s_addr), AF_INET);
        client_ip_address = inet_ntoa(clientaddr.sin_addr);
        // read the request from the end-user
        request = read_until(connfd, "\r\n\r\n\0");
        // Store the hostname from the request
        hostname = parseRequest(request);
        // printf("S-Parse result:%s", hostname);
        if (hostname) {
            // Pass on the client's request
            response = clienttest(hostname, request);
            // printf("S-Response result:\n%s\n", response);
            //check that the response isn't empty (end-server responded)

            // respond to the end-user
            Rio_writen(connfd, response, strlen(response));
            // writeLogEntry (only if there was a response)
            if (strcmp(response, "") != 0) {
               writeLogEntry(client_ip_address, hostname, strlen(response));
               // printf("S-Wrote log.\n");
            }
        }
        // Finished, close connection
        Close(connfd);
        // Free the buffers except
        // client_ip_address is statically managed by inet_ntoa
        free(request);
        free(hostname);
        free(response);
    }
}
Example #11
0
/* 
 * main - Main routine for the proxy program runs the proxy server
 */
int main(int argc, char **argv)
{
  int listenfd, connfd, clientlen, ID=0;
  struct sockaddr_in clientaddr;
  struct sockaddr_in * passClientaddr=&clientaddr;
  struct hostent *hp;
  char *haddrp;
  pthread_t tid[MAXTREAD];

 
  /* Check arguments */
  if(argc != 2){
    fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
    exit(0);
  }
  signal(SIGPIPE, SIG_IGN);
  listenfd=Open_listenfd(atoi(argv[1]));
  
  while(1){
    
    clientlen =sizeof(clientaddr);
    connfd = Accept(listenfd, (SA *)&clientaddr,(unsigned int *)&clientlen);
    
    //Determine the domain name and IP address of the client then send print statement
    hp=Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
    haddrp =inet_ntoa(clientaddr.sin_addr);
    printf("Thread %d: Receieved request from %s (%s):\n",ID,hp->h_name,haddrp);

    //make and set known data to thread
    struct treadInfo thread;
    thread.clientfd=connfd;
    rio_readinitb(&thread.rioWithClient,thread.clientfd);
    thread.ID=ID;
    thread.sockaddr=*passClientaddr;
 
    //create new thread
    Pthread_create(&tid[ID], NULL,doit,&(void *)thread); 

    //add one to ID count
    ID++;
  }    
  exit(0);
}
Example #12
0
int main(int argc, char **argv) 
{
    int listenfd, connfd, port;
    socklen_t clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    if (argc != 2) {
	fprintf(stderr, "usage: %s <port>\n", argv[0]);
	exit(0);
    }
    port = atoi(argv[1]);


    // read in the first line from the client
    readconfig();
    readcommand();

    //size_t n;
    char buf[128];
    char user[40];
    char pass[40];
    rio_t rio;

    listenfd = Open_listenfd(port);

    clientlen = sizeof(clientaddr);
    connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

    Rio_readinitb(&rio, connfd);
   
    //read username and password from client. Each individual line.
    Rio_readlineb(&rio, user, 40); //line:netp:echo:eof
    Rio_readlineb(&rio, pass, 40);
	

    int login = 0;
    int ctr = 0;

    strtok(pass, "\n");
    strtok(user, "\n");
    while(ctr < 128)
    {
	 if(strcmp(userpass[0][ctr], user) == 0 && (strcmp(userpass[1][ctr], pass)) == 0)
	 {
			login++;
	 }
	 ctr++;
    }
    
    if (login == 0)
    {
	printf("User %s Failed logging in\n", user);
	Rio_writen(connfd, "Login Failed\n", 14);
	Close(connfd);
	exit(0);
    }
    else
    {
	 // Determine the domain name and IP address of the client 
        hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
                           sizeof(clientaddr.sin_addr.s_addr), AF_INET);
        haddrp = inet_ntoa(clientaddr.sin_addr);
        printf("server connected to %s (%s)\n", hp->h_name, haddrp);
	
	printf("User %s logging in from %s at TCP Port %d\n", user, haddrp, port);
	printf("User %s successfully logged in.\n", user);
	Rio_writen(connfd, "Login Successful\n", 17);//Warning if this isn't 17 it will affect every Rio_writen after this
    
	ctr = 0;
    	while(ctr == 0)
    	{
    		//reads your command from the client
    		Rio_readlineb(&rio, buf, 128);
		strtok(buf, "\n");
		printf("User %s sent the commandline %s to be executed\n", user, buf);

		if(strcmp(buf, "quit") == 0)
		{
			printf("Logged out\n");
			Rio_writen(connfd, "quit\n", 5);// Same warning as above
			ctr++;
		}
		else
		{
			 char *file;
			 char *command;
			 char *thirdinput;
			 char *str = buf;            //buf = /bin/cat echo.c                                     

                         command = strtok(str, " "); //command = /bin/cat
                         file = strtok(NULL, " ");   //file = echo.c
			 thirdinput = strtok(NULL, " ");

			 int usecommand = 0;
			 int ctr2 = 0;
			 while(ctr2 < 128)
		         {
         			if(strcmp(execcom[ctr2], command) == 0)
         		 	{
                        		usecommand++;
         		 	}
         			ctr2++;
    			 }
			

			if(usecommand == 0)
			{
				Rio_writen(connfd, "nocomm\n", 7);// Same warning as above
				printf("Cannot execute '%s' on this server.\n", buf);
			}
			else
			{
				printf("Forking/Execing the command '%s' on behalf of %s\n", buf, user);
				
				char buffer[MAXLINE];
				int pipefd[2];
				pipe(pipefd);				

				pid_t pid = fork();
                                if (!pid)
				{
					close(pipefd[0]);
					
					dup2(pipefd[1], 1);
					dup2(pipefd[1], 2);

					close(pipefd[1]);

                                        char *env[] = {0};
					char *arg[] = {command, file, thirdinput, NULL};			
		
                                        //call your executable in your current directory
					execve(command, arg, env);
                                }
                                else if (pid == -1)
                                {
                                        perror("fork");
                                }
				else
				{
					//parent
					close(pipefd[1]);
					while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
    					{
    					}
				}


                                        int status;
                                        wait(&status);

				//sends out the string results of exec.
				Rio_writen(connfd, buffer, strlen(buffer));
			
				//we use this as a counter to tell the client that we have sent everything over.
				Rio_writen(connfd, "exec\n", 5);// Same warning as above
				
				int ctr2 = 0;
				while(ctr2 < MAXLINE)
				{
					buffer[ctr2] = 0;
					ctr2++;
				}
			}
			

		}
    	}
    }

    Close(connfd);
    exit(0);
}