-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks.c
98 lines (88 loc) · 3.08 KB
/
socks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <strings.h>
#include "linkedList.h"
#define HOSTLEN 256
int createSocket(int portnum, int maxClient, int mode);
int connectToServer(char *host, int portnum);
struct node_t *recv_args(int socket);
//for mode if 0=client if 1=server
int createSocket(int portnum, int maxClient, int mode)
{
struct sockaddr_in saddr; /* build our address here */
struct hostent *hostpointer; /* this is part of our */
char hostname[HOSTLEN]; /* address */
int sock_id; /* the socket which will be returned*/
sock_id = socket(PF_INET, SOCK_STREAM, 0);//establish the socket
if ( sock_id == -1 )
return -1;
bzero((void *)&saddr, sizeof(saddr));//null contensts of struct
//you only need to create a empty socket if you are the server.
//if you are the client you need to look up the server in the
//file table by host name.
if(mode != 1)
{
gethostname(hostname, HOSTLEN);//look up host name and store
hostpointer = gethostbyname(hostname);
bcopy( (void *)hostpointer->h_addr, (void *)&saddr.sin_addr, hostpointer->h_length);
}
else
{
saddr.sin_addr.s_addr = INADDR_ANY;
}
//give the socket good stuff
saddr.sin_port = htons(portnum);
saddr.sin_family = AF_INET;
//verify that everything worked and we are all good.
if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )return -1;
if ( listen(sock_id, maxClient) != 0 )
return -1;
return sock_id;
}
/**
*used to create a new socket that is connected to the desired
*host and port number.
*Input: char *host -> pointer to host
*Input: int portnum -> port number to connect to
*Output: int -> return socket opened and connected to server
*Error: return -1 on error.
**/
int connectToServer(char *host, int portnum)
{
int sock;
struct sockaddr_in servadd; /* the number to call */
struct hostent *hostpointer; /* used to get number */
/** Step 1: Get a socket **/
sock = socket( AF_INET, SOCK_STREAM, 0 ); /* create client sock*/
if ( sock == -1 )return -1;
/** Step 2: connect to server **/
bzero( &servadd, sizeof(servadd) ); /* zero the address */
hostpointer = gethostbyname( host ); /* lookup host's ip # */
if (hostpointer == NULL)
return -1;//invalid host
bcopy(hostpointer->h_addr, (struct sockaddr *)&servadd.sin_addr, hostpointer->h_length);
servadd.sin_port = htons(portnum); /* fill in port number */
servadd.sin_family = AF_INET; /* fill in socket type */
if ( connect(sock,(struct sockaddr *)&servadd, sizeof(servadd)) !=0)
return -1;
//conection is established return connected pipe
//free(hostpointer);
return sock;
}
/*
*used for all communication over the socket
*/
struct node_t *recv_args(int socket)
{
struct node_t *recvd_args = malloc(sizeof(struct node_t));
bzero(recvd_args, sizeof(struct node_t));
recv(socket, recvd_args, sizeof(struct node_t),0);
printf("CMD: %d\n", recvd_args->cmd_mode);
return recvd_args;
}