-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestIRCServer.c
129 lines (96 loc) · 2.31 KB
/
TestIRCServer.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
#include <time.h>
//#include <curses.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
char * user;
char * password;
char * host;
char * sport;
int port;
int open_client_socket(char * host, int port) {
// Initialize socket address structure
struct sockaddr_in socketAddress;
// Clear sockaddr structure
memset((char *)&socketAddress,0,sizeof(socketAddress));
// Set family to Internet
socketAddress.sin_family = AF_INET;
// Set port
socketAddress.sin_port = htons((u_short)port);
// Get host table entry for this host
struct hostent *ptrh = gethostbyname(host);
if ( ptrh == NULL ) {
perror("gethostbyname");
exit(1);
}
// Copy the host ip address to socket address structure
memcpy(&socketAddress.sin_addr, ptrh->h_addr, ptrh->h_length);
// Get TCP transport protocol entry
struct protoent *ptrp = getprotobyname("tcp");
if ( ptrp == NULL ) {
perror("getprotobyname");
exit(1);
}
// Create a tcp socket
int sock = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sock < 0) {
perror("socket");
exit(1);
}
// Connect the socket to the specified server
if (connect(sock, (struct sockaddr *)&socketAddress,
sizeof(socketAddress)) < 0) {
perror("connect");
exit(1);
}
return sock;
}
#define MAX_RESPONSE (10 * 1024)
int sendCommand(char * host, int port, char * command, char * response) {
int sock = open_client_socket( host, port);
if (sock<0) {
return 0;
}
// Send command
write(sock, command, strlen(command));
write(sock, "\r\n",2);
//Print copy to stdout
write(1, command, strlen(command));
write(1, "\r\n",2);
// Keep reading until connection is closed or MAX_REPONSE
int n = 0;
int len = 0;
while ((n=read(sock, response+len, MAX_RESPONSE - len))>0) {
len += n;
}
response[len]=0;
printf("response:\n%s\n", response);
close(sock);
return 1;
}
void
printUsage()
{
printf("Usage: test-talk-server host port command\n");
exit(1);
}
int
main(int argc, char **argv) {
char * command;
if (argc < 4) {
printUsage();
}
host = argv[1];
sport = argv[2];
command = argv[3];
sscanf(sport, "%d", &port);
char response[MAX_RESPONSE];
sendCommand(host, port, command, response);
return 0;
}