forked from MagerValp/CGTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
154 lines (133 loc) · 2.99 KB
/
net.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
#include <sys/types.h>
#ifdef __WIN32__
# include <winsock.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
# include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include "net.h"
#ifdef __WIN32__
/* Windows */
#define WOULDBLOCK() (WSAGetLastError() == WSAEWOULDBLOCK)
#define CLOSESOCKET(s) closesocket(s)
#else
/* Unix */
typedef int SOCKET;
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#define WOULDBLOCK() (errno == EWOULDBLOCK)
#define CLOSESOCKET(s) close(s)
#endif
#define ISCONNECTED() (conn != INVALID_SOCKET)
#define BUFSIZE 1024
SOCKET conn = INVALID_SOCKET;
#ifdef __WIN32__
int winsockstarted = 0;
#endif
unsigned char buffer[BUFSIZE];
unsigned char *bufptr;
signed int buflen;
void (*net_status)(int, char *);
int net_connect(unsigned char *host, int port, void (*status)(int, char *)) {
struct sockaddr_in address;
struct hostent *hostent;
#ifdef __WIN32__
WSADATA wsaData;
u_long nonblock = 1;
if (!winsockstarted) {
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
printf("WinSock startup failed\n");
return(1);
}
atexit((void (*)(void))WSACleanup);
winsockstarted = 1;
}
#endif
net_status = status;
if ((conn = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
if (net_status) net_status(2, "Socket call failed");
return(1);
}
address.sin_family = AF_INET;
address.sin_port = htons(port);
if (isdigit((int) host[strlen(host) - 1])) {
address.sin_addr.s_addr = inet_addr(host);
} else {
hostent = gethostbyname(host);
if (hostent) {
address.sin_addr = *((struct in_addr *) hostent->h_addr);
}
else {
if (net_status) net_status(2, "Unknown host");
return(1);
}
}
buflen = 0;
bufptr = buffer;
if (net_status) net_status(0, "Connecting...");
if (connect(conn, (struct sockaddr *)&address, sizeof(address)) == 0) {
#ifdef WIN32
ioctlsocket(conn, FIONBIO, &nonblock);
#else
fcntl(conn, F_SETFL, O_NONBLOCK);
#endif
if (net_status) net_status(1, "Connected");
return(0);
} else {
if (net_status) net_status(2, strerror(errno));
return(1);
}
}
signed int net_receive(void) {
if (!ISCONNECTED()) {
return(-2);
}
if (!buflen) {
buflen = recv(conn, buffer, BUFSIZE, 0);
if (buflen == 0) {
net_disconnect();
return(-2);
} else if (buflen < 0) {
buflen = 0;
if (WOULDBLOCK()) {
return(-1);
} else {
if (net_status) net_status(2, strerror(errno));
net_disconnect();
return(-2);
}
}
bufptr = buffer;
}
--buflen;
return(*bufptr++);
}
void net_send(unsigned char c) {
if (ISCONNECTED()) {
send(conn, &c, 1, 0);
}
}
void net_send_string(unsigned char *s) {
while (*s) {
net_send(*s++);
}
}
void net_disconnect(void) {
if (ISCONNECTED()) {
CLOSESOCKET(conn);
conn = INVALID_SOCKET;
buflen = 0;
}
}
int net_connected(void) {
return(ISCONNECTED());
}