Pages

Showing posts with label Lab 1 -- late submission. Show all posts
Showing posts with label Lab 1 -- late submission. Show all posts

Tuesday, February 4, 2014

Lab1

#2
Both of my traceroutes were done from the school network. My first domain is york.cuny.edu and its ip address is [198.61.16.7]. The hop maximum was 30 and i got 4 results. My second domain was cunyportal.york.edu and its ip address is [128.288.38.160]. the hop maximum was 30 and i got 18 dollars.

#3
The dotcom i used was bing.com

[192******]                queens village, ny
[71.167.34.1]               queens village, ny
[130.81.185.218]        ashburn, va
[130.81.151.224]         ashburn, va
[152.63.17.21]             ashburn, va
[152.63.21.73]             ashburn, va
[204.79.197.200]         redmond, wa


Lab #1

2)

Here I decided to trace www.bing.com with ip address of (204.79.197.200) which is located in the U.S from school.

  • The first hop was sending packets to the router with an ip address of 172.16.20.1. The first response time was 1.506 ms, the second was 1.122 ms and lastly 0.925 ms. 
  • The second hop sent packets to 172.16.20.1 with the response times of 2.263 ms, 2.335 ms, and 2.317 ms, respectively. 

The rest of the hops (all to the way to 64-which is the max hops) were all timed out which means that it found a server that wasn't responding and couldn't send the packets there or another kind of error has occured.




Here I decided to trace www.dominos.com with ip address of (205.218.22.11), which is located in the Chesterfield, Missouri, from school.

  • The first hop was sending packets to the router with an ip address of 172.20.0.1. The first response time was 40.063 ms, the second was 1.045 ms and lastly 41.996 ms. 
  • The second hop sent packets to 172.16.20.1 with the response times of 3.312 ms, 41.236 ms, and 42.852 ms, respectively. 
The rest of the hops (all to the way to 64-which is the max hops) were all timed out just like the first trace.

3)

Here I decided to trace www.bing.com with ip address of (205.218.22.11), which is located in the U.S, from home.

  • The first hop was from my home router with an ip address of 192.168.1.1. The first time was 7.940 ms, the second was 8.375 ms, and the third and last was 9.075 ms. 
  • The second hop was timed out. 
  • The third hop was from the router with an ip of 67.59.228.189(Bethpage, NY). The first response time was 22.740 ms, the second was 13.867 ms and lastly 19.789 ms. 
  • The fourth hop was from the router rtr4-ge1-1.mhe.hcvlny.cv.net with an ip of 67.83.222.5(Leland, North Carolina) with the response times of 30.277 ms, 24.088 ms, and 15.639 ms, respectively. 
  • The fifth hop was from the router 451be07d.cst.lightpath.net with an ip of 65.19.99.125(Bethpage, NY). The time it took for the first round trip of packets being sent from my pc to the router and back was 26.550 ms. The second trip was from a different ip of 65.19.107.117(Arlington Heights, Illinois) with a time of 22.786 ms, and lastly 27.409 ms.
  • The sixth hop was from the router with an ip of 64.15.0.218(Bethpage, NY). The first time was 28.074 ms, the second was 28.738 ms, and the third and last was 28.393 ms. 
  • The seventh hop was time out.
  • The eight hop was from the router tor.nyc.msedge.net with an ip of 131.253.32.50(Redmond, WA). The response times were: 49.535 ms, 98.748 ms, and the last one timed out.

The rest of the hops (all to the way to 64-which is the max hops) were all timed out.


Here I decided to trace our 461 blog, www.sjcs461.blogspot.com, with ip address of (74.125.226.234), which is located in the Mountain View, California, from home.

  • The first hop was from my home router with an ip address of 192.168.1.1. The first time was 3.031 ms, the second was 8.985 ms, and the third and last was 5.292 ms. 
  • The second hop was timed out. 
  • The third hop was from the router with an ip of 67.59.228.181(Bethpage, NY). The first response time was 12.551 ms, the second was 19.326 ms and lastly 19.934 ms. 
  • The fourth hop was from the router rtr3-ge2-1.mhe.hcvlny.cv.net with an ip of 67.83.222.9(Sping Valley, NY) with the response times of 30.191 ms, 18.757 ms, and 29.864 ms, respectively. 
  • The fifth hop was from the router 451be07d.cst.lightpath.net with an ip of 65.19.99.125(Bethpage, NY). The time it took for the first round trip of packets being sent from my pc to the router and back was 30.187 ms. The second trip was from 451be06d.cst.lightpath.net with an ip of 65.19.107.109(Bethpage, NY) with a time of 26.556 ms, and lastly it went to 451be07d.cst.lightpath.net with an ip address of 65.19.107.125(Bethpage, NY) with a time of 28.560 ms.
  • The sixth hop was from the router 451be0ce.cst.lightpath.net with an ip of 65.19.120.206(Bethpage NY). The first time was 18.747 ms, the second was from the router 451be0c2.cst.lightpath.net with an ip of 65.19.120.194(Bethpage, NY) and a time of 12.685 ms. The last one was from the router 451be0ce.cst.lightpath.net with an ip of 65.19.120.206(Bethpage, NY) and a time of 28.628 ms.
  • The seventh hop was from a router with an ip of 74.125.51.221(Mountain View, CA) with the first time of 19.452 ms. Then it went to the router with an ip of 72.14.212.17(The Dalles, OR) with the times: 22.340 ms, and 28.745 ms, respectively. 
  • The eighth hop was from the router with an ip of 72.14.239.248(Mountain View, CA). The response times were: 20.381 ms, 18.935 ms, and the last one was 13.891 ms.
  • The ninth hop was from the router with an ip of 72.14.239.252(Mountain View, CA) with the response times of 19.438 ms, 17.639 ms, and 25.237 ms, respectively. 
  • The tenth hop was from the router lga15s29-in-f10.1e100.net with an ip of 74.125.226.234(Mountain View, CA) with the response times of 26.690 ms, 26.282 ms, and 22.896 ms, respectively. 
4)

Header: <sys/socket.h>

int bind(
  SOCKET s,
  const struct sockaddr FAR* name,
  int namelen
); This function associates a local address with a socket.

Takes three parameters.
1)  s - [in] Descriptor identifying an unbound socket.
2)  name - [in] Address to assign to the socket from the sockaddr structure.
3)  namelen - [in] Length of the value in the name parameter.

·      If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR

int listen(
  SOCKET s,
  int backlog
); This function places a socket at a state where it is listening for an incoming connection

Takes two parameters.
1)    s- [in] Descriptor identifying a bound, unconnected socket.
2)    backlog - [in] Maximum length of the queue of pending connections. If backlog is set to SOMAXCONN, the underlying service provider responsible for socket s will set the backlog to a maximum reasonable value. There is no standard provision to obtain the actual backlog value.

·      If no error occurs, this function returns zero. If an error occurs, a value of SOCKET_ERROR is returned

SOCKET accept(
  SOCKET s,
  struct sockaddr FAR* addr,
  int FAR* addrlen
); This function permits an incoming connection attempt on a socket.

Takes three parameters.
1)   s - [in] Descriptor identifying a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by this function.
2)   addr - [out] Optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family established when the socket was created.
3)   addrlen - [out] Optional pointer to an integer that contains the length of addr.

·      If no error occurs, this function returns a value of type SOCKET, but If an error occurs, a value of INVALID_SOCKET is returned

int send(
  SOCKET s,
  const char FAR* buf,
  int len,
  int flags
); This function sends data on a connected socket.

Takes four parameters.
1)    s - [in] Descriptor identifying a connected socket
2)    buf - [in] Buffer containing the data to be transmitted.
3)    len - [in] Length of the data in the buf parameter.
4)    flags - [in] Indicator specifying the way in which the call is made.

·      If no error occurs, this function returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets. If an error occurs, a value of SOCKET_ERROR is returned

int recv(
  SOCKET s,
  char FAR* buf,
  int len,
  int flags
); This function receives data from a connected socket.

Takes four parameters.
1)    s - [in] Descriptor identifying a connected socket.
2)    buf - [in] Buffer for the incoming data.
3)    len - [in] Length of the buf parameter.
4)    flags - [in] Flag specifying the way in which the call is made.

·      If no error occurs, this function returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. If an error occurs, a value of SOCKET_ERROR is returned



int closesocket(
  SOCKET s
); This function closes an existing socket.

Takes one parameter.
1)    s - [in] Descriptor identifying the socket to close.

·      Header: <unistd.h>
·      If no error occurs, this function returns zero. If an error occurs, a value of SOCKET_ERROR is returned


The socket API Functions with C Source

Creating a socket
The first thing to do is to create a socket which you can then manipulate in many ways. To create a socket, you can use the socket() function.
HEADER FILE:    #include <sys/socket.h>
Syntax:
int socket(int af, int type, int protocol);
   int socket_desc;
   socket_desc=socket(AF_INET,SOCK_STREAM,0);
   if (socket_desc==-1)
    perror("Create socket");

socket() returns a socket descriptor which can be used in other network commands. This will create a socket which uses DARPA Internet addresses, and the method of connection is a byte stream which is similar to a pipe. An alternative to byte streams is a datagram but this tutorial does not cover them. 
With servers, the first socket created is often known as a " master socket". Before the socket can send or receive data, it must be connected to another socket. If acting as a master socket, it must be bound to a port number so that clients can know where to "find" the socket and connect to it. 
If successful, socket() returns a valid socket descriptor; otherwise it returns -1 and sets errno to indicate the error. perror() or strerror() can be used to turn the errno value into a human readable string.

Binding a socket to a port
To set up a master socket, you need to bind the socket descriptor to a in many ways. To create a socket, you can use the socket() function as described above in Creating a socket.
HEADER:  
#include <sys/socket.h>
#include <netinet/in.h>

Syntax:
int bind(int s, struct sockaddr *addr, int addrlen);

struct sockaddr_in address;

/* type of socket created in socket() */
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;

/* 7000 is the port to use for connections */
  address.sin_port = htons(7000);

/* bind the socket to the port specified above */
  bind(socket_desc,(struct sockaddr *)&address,sizeof(address));

If successful, bind() returns 0; otherwise it returns -1 and sets errno to indicate the error. 
The port specified in the source code above (port 7000) is where the server can be connected to.

Listening for connections
Before any connections can be accepted, the socket must be told to listen for connections and also the maximum number of pending connections using listen()
HEADER:
#include <sys/socket.h>
Syntax:
int listen(int s, int backlog);
listen(socket_desc,3);

the above line specifies that there can be up to 3 connections pending. If a connection request arrives when there are already 3 connections pending, the client receives a timeout error. 
If successful, listen() returns 0; otherwise it returns -1 and sets errno to indicate the error.

Accepting a connection
To actually tell the server to accept a connection, you have to use the function accept()
HEADER:
#include <sys/socket.h>

Syntax:
int accept(int s, struct sockaddr *addr, int *addrlen);

int addrlen;
struct sockaddr_in address;

  addrlen = sizeof(struct sockaddr_in);
  new_socket = accept(socket_desc, (struct sockaddr *)&address, &addrlen);
  if (new_socket<0)
    perror("Accept connection");

accept() is used with connection based sockets such as streams. The parameters are the socket descriptor of the master socket followed by a sockaddr_in structure and the size of the structure. If successful, accept() returns a positive integer which is the socket descriptor for the accepted socket. If an error occurs, -1 is returned and errno is set to indicate the cause.
Closing connections
Probably one of the easiest things to do with a socket, is close it. This is done using close()
HEADER:
#include <unistd.h>

Syntax:
int close(int sockdes);
close(new_socket);

close() closes the socket descriptor indicated by sockdes. 
Upon successful completion, close() returns a value of 0; otherwise, it returns -1 and sets errno to indicate the error.

Sending data to a connection
Accepting a connection would not be any use without the means to send or receive data. Send without receive could be used for an information server which always returns a fixed message.
HEADER:
#include <sys/socket.h>
#include <string.h>
Syntax:
int send(int s, const void *msg, int len, int flags);

char *message="This is a message to send\n\r";

send(new_socket,message,strlen(message),0);

The message should have \n\r instead of just \n or \r because otherwise the text which appears on some clients may seem strange. e.g the text with just a \n would appear as follows:
This is a message
                 and this is the second line
                                            and the third.
instead of:
This is a message
and this is the second line
and the third.

send() is used to transmit a message to another socket and can be used only when the socket is in a connected state. The socket descriptor that specifies the socket on which the message will be sent is 's' in the syntax above. 'msg' points to the buffer containing the message and the length of the message is given by len, in bytes. 
The supported values for flags are zero, or MSG_OOB (to send out-of-band data) - a write() call made to a socket behaves in exactly the same way as send() with flags set to zero. 
Upon successful completion, send() returns the number of bytes sent. Otherwise, it returns -1 and sets errno to indicate a locally-detected error.
Receiving data from a connection
Accepting a connection would not be any use without the means to send or receive data. Receive only could be used as a data collection method.
HEADER:
#include <sys/socket.h>
Syntax:
int recv(int s, void *msg, int len, int flags);

int bufsize=1024;        /* a 1K buffer */
char *buffer=malloc(bufsize);

  recv(new_socket,buffer,bufsize,0);
The flags parameter can be set to MSG_PEEK, MSG_OOB, both, or zero. If it is set to MSG_PEEK, any data returned to the user still is treated as if it had not been read, i.e the next recv() re-reads the same data. 
A read() call made to a socket behaves in exactly the same way as a recv() with flags set to zero. 
If successful, recv() returns the number of bytes received, otherwise, it returns -1 and sets errno to indicate the error. recv() returns 0 if the socket is blocking and the connection to the remote node failed.
Setting socket options
To allow certain socket operations requires manipulation of socket options using setsockopt()
HEADER:
#include <sys/socket.h>

Syntax:
  int setsockopt(int s, int level, int optname,
                 const void *optval, int optlen);
#define TRUE   1
#define FALSE  0

int socket_desc;     /* master socket returned by socket() */
int opt=TRUE;        /* option is to be on/TRUE or off/FALSE */

  setsockopt(socket_desc,SOL_SOCKET,SO_REUSEADDR,
              (char *)&opt,sizeof(opt));
SOL_SOCKET specifies the option is a `socket level' option, these are defined in <sys/socket.h> 
The socket is identified by the socket descriptor s. 
The option SO_REUSEADDR is only valid for AF_INET sockets. 
There are two kinds of options: boolean and non-boolean. Boolean options are either set or not set and also can use optval and optlen to pass information. Non-boolean options always use optval and optlen to pass information.

Handling more than one connection
To enable a socket to be read without waiting if there is no input, the socket must be set non-blocking using the following snippet of code.
fcntl(mastersocket, F_SETFL, FNDELAY);
or
fcntl(mastersocket, F_SETFL, O_NONBLOCK);
If the above returns a non-zero result, the operation failed and errno should be set to an appropriate value.
Using select to monitor a number of sockets (or just one) is fairly straightforward and is shown in the code below. Please note this is incomplete code as the creation of the master socket is not included (see previous details).
fd_set readfds;

/* create a list of sockets to check for activity */
FD_ZERO(&readfds);

/* specify mastersocket - ie listen for new connections */
FD_SET(mastersocket, &readfds);

/* wait for connection, forever if have to */
new_conns=select(max_conns, readfds, NULL, NULL, NULL);

if ((new_conns<0) && (errno!=EINTR)) {
  /* there was an error with select() */
}
if (FD_ISSET(mastersocket,&readfds)) {
  /* Open the new socket */
}
Of course, the above will only wait for activity on the master socket. What you need to do is run it inside a loop which repeats until the server is shut down.
Any newly created sockets will need to be monitored as well (unless the connections accepted are closed after outputing a message).

Converting a hostname into a network address
HEADER:
#include <netdb.h>

Syntax:
  struct hostent *gethostbyname(const char *name);

    struct hostent *hent;

  hent = gethostbyname("www.foobar.net");

A hostent structure:
struct hostent {
    char    *h_name;        /* official name of host */
    char    **h_aliases;    /* alias list */
    int     h_addrtype;     /* host address type */
    int     h_length;       /* length of address */
    char    **h_addr_list;  /* list of addresses */
}
Some of the network functions require a structure containing the network address and sometimes the port number to connect to or from. The easiest way to convert a hostname to a network address is to use the gethostbyname() function.
gethostbyname() returns a structure of type hostent - this structure contains the name of the host, an array of alternative names and also an array of network addresses (in network byte order).