-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.c
250 lines (178 loc) · 5.5 KB
/
web.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define P_LENGHT 6
#define ADDR_LENGHT 15
// Functions
int set_server(char * port, int status, struct addrinfo hints, struct addrinfo * servinfo);
int set_client(char * address, char * port, int status, struct addrinfo hints, struct addrinfo * servinfo);
int show_ip(int argc, char * first_arg);
// Structs
struct server_args{
char port[P_LENGHT];
char *port_p;
};
struct client_args{
char address[ADDR_LENGHT];
char port[P_LENGHT];
char *address_p;
char *port_p;
};
char * usage(int i){
static char usage_s[] = "Usage: web \"argument\"\n"
"Arguments: c client\n"
" s server\n";
static char error_s[] = "First argument must be \"-c\" or \"-s\".\n";
if(i == 1) {
return usage_s;
}
else {
return error_s;
}
}
int parse_cmd(int argc, char *argv[]){
if(argc == 1){
printf("%s",usage(1));
exit(1);
}
if(argv[1][0] != '-')
{
printf("%s", usage(1));
exit(1);
}
if(argv[1][1] != 'c' && argv[1][1] != 's')
{
printf("%s\n", usage(2));
exit(1);
}
return 0;
}
int main(int argc, char *argv[])
{
parse_cmd(argc, argv);
int status = 0;
struct addrinfo hints;
struct addrinfo *servinfo;
// char arg_n1 = (char)argv[1];
//implement string comparision function for parsing cmd command
switch (argv[1][0]) {
case 'c':
printf("character c entered\n");
break;
}
struct client_args new_client_args;
new_client_args.address_p = new_client_args.address;
new_client_args.port_p = new_client_args.port;
//TODO: read adress and port from command line
new_client_args.address_p = (char *)"172.0.0.1";
new_client_args.port_p =(char *) "3490";
set_client(new_client_args.address_p, new_client_args.port_p, status, hints, servinfo);
// Setup a server
struct server_args new_server_args;
new_server_args.port_p = new_server_args.port;
// TODO: read port number from command line ... argv[]...
new_server_args.port_p = (char *)"3490";
set_server(new_server_args.port_p, status, hints, servinfo);
// Setup a client
//
// struct client_args new_client_args;
// new_client_args.address_p = new_client_args.address;
// new_client_args.port_p = new_client_args.port;
//TODO: read adress and port from command line
// new_client_args.address_p = "172.0.0.1";
// new_client_args.port_p = "3490";
//
// set_client(new_client_args.address_p, new_client_args.port_p, status, hints, servinfo);
/* Show IP
int arg_num = argc;
char *first_arg = argv[1];
show_ip(arg_num, first_arg);
*/
return 0;
}
int set_server(char * port, int status, struct addrinfo hints, struct addrinfo * servinfo)
{
struct sockaddr_storage their_addr;
socklen_t addr_size;
int backlog = 10;
int sockfd;
int new_fd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype= SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(NULL, port, &hints, &servinfo)) != 0){
fprintf(stderr, "getddrinfo errot: %s\n", gai_strerror(status));
exit(1);
}
// printf("test\n");
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
printf("socket descriptor %d\n", sockfd);
bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
listen(sockfd, backlog);
addr_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
printf("new socket destriptor %d\n", new_fd);
// printf("new connection from %d\n", their_addr.);
freeaddrinfo(servinfo);
return 0;
}
int set_client(char * address, char * port, int status, struct addrinfo hints, struct addrinfo * servinfo)
{
int sockfd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(address, port, &hints, &servinfo)) != 0){
fprintf(stderr, "getddrinfo errot: %s\n", gai_strerror(status));
exit(1);
}
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
printf("socket number is %d\n", sockfd);
connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
freeaddrinfo(servinfo);
return 0;
}
int show_ip(int argc, char *first_arg)
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(first_arg, NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", first_arg);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = (char *)"IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = (char *)"IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}