From 27d2936ffbde6e5225acd9f9caf8941be9b5251f Mon Sep 17 00:00:00 2001 From: Andre Date: Thu, 27 Jul 2017 00:04:25 +0200 Subject: [PATCH] can resolve hostname now --- inc/ft_p.h | 22 ++++++++---- close_port.sh => kill_port.sh | 33 ++++++++++++++---- libftp/inc/libftp.h | 7 ++++ libftp/src/host.c | 51 +++++++++++++++++++++++++++ libftp/src/ls.c | 2 +- libftp/src/readn.c | 11 ++---- src/client/main.c | 66 +++++++++++++++++------------------ src/server/main.c | 40 ++++++--------------- 8 files changed, 146 insertions(+), 86 deletions(-) rename close_port.sh => kill_port.sh (66%) create mode 100644 libftp/src/host.c diff --git a/inc/ft_p.h b/inc/ft_p.h index a075a4e..06d2b1b 100644 --- a/inc/ft_p.h +++ b/inc/ft_p.h @@ -10,16 +10,26 @@ /* */ /* ************************************************************************** */ - #ifndef FT_P_H # define FT_P_H # include -void ft_cd(char *, char **); -void ft_lcd(char *, char **); -void ft_invalid(char *); -void ft_handle_request(char *, int); -int ft_lhandle_request(char *, int); +/* +** On MacOS environ must not be free'd +*/ +#ifdef __APPLE__ +# define FREE_ENVIRON FALSE +#else +# define FREE_ENVIRON TRUE #endif + + +void ft_cd(char *ptr, char **pptr); +void ft_lcd(char *ptr, char **pptr); +void ft_invalid(char *ptr); +void ft_handle_request(char *ptr, int arg); +int ft_lhandle_request(char *ptr, int arg); + +#endif \ No newline at end of file diff --git a/close_port.sh b/kill_port.sh similarity index 66% rename from close_port.sh rename to kill_port.sh index 87efdcc..54596a6 100755 --- a/close_port.sh +++ b/kill_port.sh @@ -10,13 +10,20 @@ # # # **************************************************************************** # -## number pattern +## target name -re='^[0-9]+$' +target='server' + +## ansi colors + +RED='\033[0;31m' +YELLOW='\033[0;33m' +GREEN='\033[0;32m' +NC='\033[0m' ## ask for port number -printf "PORT #: "; +printf "${YELLOW}Enter port number to be close: ${NC}"; ## assign port number @@ -28,14 +35,28 @@ if [ "$port" -eq "$port" ] 2>/dev/null; then # get output from lsof ( including PID ) - lsof="$(lsof -i :$port)" + printf "${YELLOW}Looking for port :[ ${port} ] PID${NC}\n" + pid="$(lsof -i :$port | grep $target | cut -d ' ' -f 3)" + + # check if pid is a num + + if [ "$pid" -eq "$pid" ] 2>/dev/null; then + + ## kill pid + + printf "${GREEN}PID found, killing process: ${NC}" + kill $pid + + else + + printf "${RED}Unable to find PID, bye${NC}\n" - printf "$lsof"; + fi else # not a valid port number - printf "error: not a valid port number"; + printf "${RED}Not a valid port number, bye\n${NC}"; fi diff --git a/libftp/inc/libftp.h b/libftp/inc/libftp.h index ae00f6a..d6db173 100644 --- a/libftp/inc/libftp.h +++ b/libftp/inc/libftp.h @@ -20,6 +20,7 @@ # include # include # include +# include # include # include # include @@ -79,6 +80,12 @@ char *ft_path(char *); char *ft_lpath(char *name); char **ft_get_argv(char *); +/* +** Host names +*/ + +char *ft_resolve_host(char *argv); + /* ** Error handling */ diff --git a/libftp/src/host.c b/libftp/src/host.c new file mode 100644 index 0000000..1f8e5c6 --- /dev/null +++ b/libftp/src/host.c @@ -0,0 +1,51 @@ +#include + +/* +** return the IPv4 address of a DNS +*/ + +char *ft_resolve_host(char *argv) +{ + struct hostent *hptr; + char *ip_addr; + + /* + ** dynamically allocate memory for the IP addr + ** The size can handle either 32-bit IPv4 or 128-bit IPv6 + */ + if (!(ip_addr = (char *)malloc(INET_ADDRSTRLEN))) + return (NULL); + /* + ** gethostbyname failed if return is NULL + */ + if (!(hptr = gethostbyname (argv))) + { + ft_err_quit("unable to resolve host name"); + } + /* + ** check host address type is AF_INET (internet) + */ + if (hptr->h_addrtype == AF_INET) + { + /* + ** hostent h_addr_list contains a list of IPv4 addresses + ** that belong to the hostname, I only want 1 + */ + if (*(hptr->h_addr_list)) + { + /* + ** In an effort to create a protocol independant resolver + ** i will use inet_ntop to get the representational value of + ** the IP addressm be it IPv4 or IPv6 + */ + if (inet_ntop(hptr->h_addrtype, *(hptr->h_addr_list), ip_addr, INET_ADDRSTRLEN)) + { + return (ip_addr); + } + } + } + /* + ** unable to find address, quit + */ + return (NULL); +} \ No newline at end of file diff --git a/libftp/src/ls.c b/libftp/src/ls.c index e25f43f..ff7cdcb 100644 --- a/libftp/src/ls.c +++ b/libftp/src/ls.c @@ -69,7 +69,7 @@ char *read_dir(char *dir) name[ft_strlen(drnt->d_name)] = '\n'; /* - ** append the contents to the char * alone with a newline + ** append the contents to the char * along with a newline */ dir_contents = ft_wstrjoin(dir_contents, name); diff --git a/libftp/src/readn.c b/libftp/src/readn.c index d1f3aac..fb70373 100644 --- a/libftp/src/readn.c +++ b/libftp/src/readn.c @@ -38,19 +38,12 @@ ssize_t ft_readn(int fd, void *vptr, size_t n) return (n - nleft); /* return >= 0 */ } -/* -** ft_readn wrapper function -** If the system supports the MSG_WAITALL flag we can omit the ft_readn -** function which keeps calling to read until n bytes are read and use -** recv rather. It tells the kernel not to return from a read operation -** until the requested number of bytes have been read. -*/ - ssize_t ft_wreadn(int fd, void *ptr, size_t nbytes) { ssize_t n; - if ((n = ft_readn(fd, ptr, nbytes)) < 0) { + if ((n = ft_readn(fd, ptr, nbytes)) < 0) + { ft_err_sys("readn error"); } return (n); diff --git a/src/client/main.c b/src/client/main.c index 83beddb..c32eb43 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -1,95 +1,93 @@ # include /* -** Take note, right now. The client are using the constant SERV_PORT -** this must be changed as per PDF +** Connect the client to the server */ -int main(int argc, char **argv) +int connect_to_server(char **argv) { - char *cmd; - char buff[MAXLINE]; - int sockfd; struct sockaddr_in servaddr; + char *ip_addr; + int sockfd; /* - ** User must specify server IP address as second argument + ** resolve host IP */ - - if (argc != 3) - ft_err_quit("usage: client "); - + ip_addr = ft_resolve_host(argv[1]); /* ** open socket for tcp */ - sockfd = ft_wsocket(AF_INET, SOCK_STREAM, 0); - /* ** set server address */ - - ft_set_sockaddr((SA *) &servaddr, AF_INET, ft_atoi(argv[2]), inet_addr(argv[1])); - + ft_set_sockaddr((SA *) &servaddr, AF_INET, ft_atoi(argv[2]), inet_addr(ip_addr)); + /* + ** dont need resolved host IP anymore + */ + ft_strdel(&ip_addr); /* ** establish a connection with a TCP server */ - ft_wconnect(sockfd, (SA *) &servaddr, sizeof(servaddr)); + /* + ** return the socket fd + */ + return (sockfd); +} + +int main(int argc, char **argv) +{ + char *cmd; + char buff[MAXLINE]; + int sockfd; /* - ** display client prompt + ** User must specify server IP address as second argument + */ + if (argc != 3) + ft_err_quit("usage: client < | > "); + /* + ** connect to server + */ + sockfd = connect_to_server(argv); + /* + ** we are now connected to server, display client prompt */ - ft_display_prompt(); - /* ** read from stdin */ - while ((cmd = ft_wreadline())) { - if (ft_strlen(cmd) > 0 && !ft_empty(cmd)) { - /* ** set buff with cmd contents, free cmd for next read */ - ft_fill_buffer(buff, cmd); - /* ** handle local command */ - if (!ft_lhandle_request(buff, STDOUT_FILENO)) { - /* ** write buff to server socket */ - ft_wwriten(sockfd, buff, MAXLINE); - /* ** clean buff */ - ft_bzero(buff, MAXLINE); - /* ** read server response into buff */ - if (ft_wreadn(sockfd, buff, MAXLINE)) { ft_putstr(buff); ft_putstr("\n"); } - } - } else { diff --git a/src/server/main.c b/src/server/main.c index 67b9ee6..1c420e3 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -14,89 +14,69 @@ int main(int argc, char **argv) /* ** create a socket */ - listenfd = ft_wsocket(AF_INET, SOCK_STREAM, 0); - /* ** set socket address ** NB - INADDR_ANY tells the kernel to accept requests ** on any interface for port `SERV_PORTS` */ - ft_set_sockaddr((SA *) &servaddr, AF_INET, ft_atoi(argv[1]), htonl(INADDR_ANY)); - /* ** assign a local protocol address to a socket */ - ft_wbind(listenfd, (SA *) &servaddr, sizeof(servaddr)); - /* ** convert unconnected socket into a passive socket ** (accept incoming connection requests directed to this socket) */ - ft_wlisten(listenfd, BACKLOG); - + /* + ** handle connections + */ while (42) { clilen = sizeof(cliaddr); - /* ** return next completed connection (client request accepted) from the ** que, assign the file descriptor to connfd */ - connfd = ft_waccept(listenfd, (SA *) &cliaddr, &clilen); - /* ** fork a new process, so we can handle multiple clients */ - if ((childpid = ft_wfork()) == 0) { - /* ** initialize environment with variables we want */ - -// ft_init_environ(); - + ft_init_environ(); /* ** close to prevent zombies */ - ft_wclose(listenfd); - /* ** read from client as long as client is writing */ - while (ft_wreadn(connfd, buff, MAXLINE)) { - /* ** use buff to handle client request */ - ft_handle_request(buff, connfd); - } - -#ifndef __APPLE__ -// ft_free_environ(); -#endif + /* + ** free the environment, wont work on mac + */ + if (FREE_ENVIRON) + ft_free_environ(); /* ** kill the process */ - exit(0); } - /* - ** + ** close connection */ - ft_wclose(connfd); } return (0);