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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include "socket.h"
#include "vsprintf.h"
Socket::Socket(const char *_hostname, int _port)
{
sock = 0;
hostname = strdup(_hostname);
port = _port;
status = connect_socket();
}
Socket::~Socket()
{
if (sock) close(sock);
if (hostname) free(hostname);
hostname = NULL;
}
int Socket::Ok()
{
struct stat stat_buf;
return (status && !fstat(sock, &stat_buf));
}
int Socket::connect_socket()
{
struct hostent *he;
struct sockaddr_in sa;
if (!(he = gethostbyname(hostname))) return 0;
if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) < 0) return 0;
memset(&sa, 0, sizeof(sa));
memcpy(&sa.sin_addr, he->h_addr, he->h_length);
sa.sin_family = he->h_addrtype;
sa.sin_port = htons(port);
return connect(sock, (struct sockaddr*)&sa, sizeof(sa)) >= 0;
}
int Socket::bind_socket()
{
struct hostent *he;
struct sockaddr_in sa;
if (!(he = gethostbyname(hostname))) return 0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) return 0;
memset(&sa, 0, sizeof(sa));
sa.sin_family = he->h_addrtype;
sa.sin_port = htons(port);
if (bind(sock, (sockaddr*)&sa, sizeof(sa))) return 0;
if (listen(sock, 5)) return 0;
return 1;
}
int Socket::MsgAvail(AvailType mode)
{
fd_set fd;
struct timeval tv = {0,0};
FD_ZERO(&fd);
FD_SET(sock, &fd);
switch (mode)
{
case SCK_READ:
return select(sock+1, &fd, NULL, NULL, &tv) > 0;
break;
case SCK_WRITE:
return select(sock+1, NULL, &fd, NULL, &tv) > 0;
break;
case SCK_EXC:
return select(sock+1, NULL, NULL, &fd, &tv) > 0;
default:
return 0;
}
return 0;
}
int Socket::WriteMsg(const char *fmt, ...)
{
char buf[MAX_MSG_LEN];
va_list args;
va_start(args, fmt);
VSPRINTF(buf, sizeof(buf)-1, fmt, args);
va_end(args);
return WriteMsg((void*)buf, strlen(buf));
}
int Socket::WriteMsg(const void *buf, int buf_len)
{
return write(sock, buf, buf_len);
}
char *Socket::ReadMsg(int max_len)
{
char *buf = (char*)ReadMsg(NULL, max_len), *ret;
if (max_len > 0)
{
char *eol = strstr(buf, "\n");
if (eol) *eol = 0;
ret = strdup(buf);
}
if (buf) delete buf;
return ret;
}
void *Socket::ReadMsg(void *buf, int &buf_len)
{
void *alloc_buf;
int bytes_read = 0, flag;
if (buf_len <= 0) buf_len = MAX_MSG_LEN;
alloc_buf = (buf) ? buf : (void*)malloc(buf_len);
if (!alloc_buf)
{
buf_len = -1;
return NULL;
}
if (!buf) memset(alloc_buf, 0, buf_len);
while (bytes_read < buf_len) {
flag = read(sock, (char*)alloc_buf + bytes_read, buf_len);
if (!flag || flag == -1) break;
bytes_read += flag;
}
// if ((buf_len = read(sock, alloc_buf, buf_len)) <= 0)
// {
// if (!buf) delete alloc_buf;
// return NULL;
// }
return alloc_buf;
}
char *Socket::ReadAll(int &buf_len)
{
char *tmp_buf, *return_buf = NULL, *buf2;
int bytes_read = 0, flag, finish = 0, tmp_buf_size = MAX_MSG_LEN;
// pomocniczny bufor tylko do czytania z socketu
tmp_buf = (char*)malloc(tmp_buf_size);
while (!finish) {
flag = read(sock, (char*)tmp_buf, tmp_buf_size);
if (!flag || flag == -1) {
finish = 1;
break;
}
bytes_read += flag;
// teraz zaalokujemy pamiec na pobrane dane
return_buf = (char*)realloc(return_buf, bytes_read);
memmove(return_buf + bytes_read - flag, tmp_buf, flag);
}
// usuwamy tymczasowy bufor
free(tmp_buf);
buf_len = bytes_read;
buf2 = (char*)malloc(buf_len+1);
memmove(buf2, return_buf, buf_len);
buf2[buf_len] = 0;
free(return_buf);
return buf2;
}
int Socket::ReadMsg(int buf_len, void *buf)
{
if (!buf || buf_len<=0) return -1;
if ((buf_len = read(sock, buf, buf_len)) <= 0) return -1;
return buf_len;
}
#ifndef NO_ANYIP
int AnyIPSocket::connect_socket() {
struct addrinfo * ai_cptr = NULL, * ai_bptr = NULL,* ai_ptr = NULL,ai_hints;
char ai_port[65]; /* XXX: tymczasowa proteza */
int err = 0;
memset (ai_port,0,sizeof (char)*65);
memset (&ai_hints,0,sizeof (ai_hints));
ai_hints.ai_family = PF_INET;
ai_hints.ai_socktype = SOCK_STREAM;
snprintf (ai_port,64,"%u",port);
if ((err = getaddrinfo (hostname,ai_port,&ai_hints,&ai_cptr))){
#ifdef DEBUG
fprintf (stderr,"getaddrinfo: connect: %s:%s: %s !\n",hostname,
ai_port,gai_strerror (err));
#endif /* DEBUG */
return (0);
}
if (srcip){
memset (&ai_hints,0,sizeof (ai_hints));
ai_hints.ai_family = PF_INET;
ai_hints.ai_socktype = SOCK_STREAM;
if ((err = getaddrinfo (srcip,NULL,&ai_hints,&ai_bptr))){
#ifdef DEBUG
fprintf (stderr,"getaddrinfo: bind: %s: %s !\n",srcip,
gai_strerror (err));
#endif /* DEBUG */
return (0);
}
}
for (ai_ptr = ai_cptr;ai_ptr;ai_ptr = ai_ptr->ai_next){
if ((sock = socket (ai_ptr->ai_family,ai_ptr->ai_socktype,
ai_ptr->ai_protocol)) < 0){
#ifdef DEBUG
fprintf (stderr,"socket: %s !\n",strerror (errno));
#endif /* NS88_DEBUG */
errno = 0;
continue;
}
if (srcip){
if (!bindaddr_socket (ai_bptr)){
#ifdef DEBUG
fprintf (stderr,"bindaddr_socket: %s !\n",strerror (errno));
#endif /* DEBUG */
if (close (sock) < 0){
#ifdef DEBUG
fprintf (stderr,"close: %s !\n",strerror (errno));
#endif /* DEBUG */
}
sock = -1;
errno = 0;
continue;
}
}
if (connect (sock,ai_ptr->ai_addr,ai_ptr->ai_addrlen) < 0){
#ifdef DEBUG
fprintf (stderr,"bind: %s !\n",strerror (errno));
#endif /* DEBUG */
if (close (sock) < 0){
#ifdef DEBUG
fprintf (stderr,"close: %s !\n",strerror (errno));
#endif /* DEBUG */
}
sock = -1;
errno = 0;
continue;
}
break;
}
if (ai_ptr)
err = 1;
else
err = 0;
freeaddrinfo (ai_bptr);
freeaddrinfo (ai_cptr);
return (err);
}
int AnyIPSocket::bindaddr_socket (struct addrinfo * ai_bptr) {
for (;ai_bptr;ai_bptr = ai_bptr->ai_next){
if (bind (sock,ai_bptr->ai_addr,ai_bptr->ai_addrlen) < 0)
continue;
break;
}
return (ai_bptr ? 1 : 0);
}
AnyIPSocket::AnyIPSocket(const char *_hostname, int _port,char * _srcip)
{
sock = -1;
hostname = strdup(_hostname);
port = _port;
srcip = _srcip;
status = connect_socket();
}
#endif // NO_ANYIP
|