Skip to content

Commit

Permalink
can resolve hostname now
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 26, 2017
1 parent 0e670f1 commit 27d2936
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 86 deletions.
22 changes: 16 additions & 6 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@
/* */
/* ************************************************************************** */


#ifndef FT_P_H
# define FT_P_H

# include <libftp.h>

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
33 changes: 27 additions & 6 deletions close_port.sh → kill_port.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
7 changes: 7 additions & 0 deletions libftp/inc/libftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# include <sys/stat.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netdb.h>
# include <arpa/inet.h>
# include <stdlib.h>
# include <stdio.h>
Expand Down Expand Up @@ -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
*/
Expand Down
51 changes: 51 additions & 0 deletions libftp/src/host.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <libftp.h>

/*
** 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);
}
2 changes: 1 addition & 1 deletion libftp/src/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 2 additions & 9 deletions libftp/src/readn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 32 additions & 34 deletions src/client/main.c
Original file line number Diff line number Diff line change
@@ -1,95 +1,93 @@
# include <ft_p.h>

/*
** 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 <IPaddress> <port>");

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 < <IPaddress>|<DNS> > <port>");
/*
** 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
{
Expand Down
Loading

0 comments on commit 27d2936

Please sign in to comment.