Recovered from microbel
This commit is contained in:
195
common/lib/srvcli/socket.C
Normal file
195
common/lib/srvcli/socket.C
Normal file
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* PVVMUD a 3D MUD
|
||||
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
#include "pvvmud.H"
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include "socket.H"
|
||||
#include "manager.H"
|
||||
|
||||
#include <sys/utsname.h>
|
||||
|
||||
// TODO:
|
||||
// address and localAddress are not set systematically
|
||||
//
|
||||
|
||||
CSocketException::CSocketException():CException("Socket exception"){
|
||||
}
|
||||
|
||||
CSocket::CSocket(){
|
||||
|
||||
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
|
||||
throw new CSocketException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifndef socklen_t
|
||||
#define socklen_t int
|
||||
#endif
|
||||
|
||||
CSocket::CSocket(int fd){
|
||||
struct sockaddr_in sin;
|
||||
socklen_t addrlen = sizeof(sin);
|
||||
|
||||
this->fd = fd;
|
||||
|
||||
addrlen = sizeof(sin);
|
||||
getpeername(fd,(struct sockaddr*)&sin,&addrlen);
|
||||
address = CInetAddress(ntohl(sin.sin_addr.s_addr),ntohs(sin.sin_port));
|
||||
|
||||
addrlen = sizeof(sin);
|
||||
getsockname(fd,(struct sockaddr*)&sin,&addrlen);
|
||||
localAddress = CInetAddress(ntohl(sin.sin_addr.s_addr),ntohs(sin.sin_port));
|
||||
|
||||
}
|
||||
|
||||
void CSocket::bind( char *addr, int port ) {
|
||||
int opt = 1; /* For setting reuse_addr */
|
||||
struct sockaddr_in sin; /* Address for socket */
|
||||
struct linger ld; /* To set linger option */
|
||||
|
||||
localPort = port;
|
||||
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt) );
|
||||
ld.l_onoff = 0;
|
||||
ld.l_linger = 0;
|
||||
setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&ld, sizeof(ld));
|
||||
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(port);
|
||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
// Try to bind the address to the socket.
|
||||
if(::bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
|
||||
throw new CSocketException();
|
||||
}
|
||||
socklen_t length = sizeof(sin);
|
||||
|
||||
getsockname(fd, (struct sockaddr*)&sin, &length);
|
||||
|
||||
if (strcmp(addr,"any")) {
|
||||
if (!strcmp(addr,"localhost")) {
|
||||
struct hostent *hptr;
|
||||
struct utsname myname;
|
||||
|
||||
if (uname(&myname)<0) return;
|
||||
if ((hptr=gethostbyname(myname.nodename))==NULL) return;
|
||||
|
||||
sin.sin_addr.s_addr = (*((DWORD*)hptr->h_addr_list[0]));
|
||||
} else {
|
||||
CInetAddress address(addr);
|
||||
sin.sin_addr.s_addr = address.getAddress();
|
||||
}
|
||||
}
|
||||
|
||||
localAddress.setAddress(ntohl(sin.sin_addr.s_addr),ntohs(sin.sin_port));
|
||||
}
|
||||
|
||||
void CSocket::listen(int number){
|
||||
if (::listen(fd,number) < 0)
|
||||
throw new CSocketException();
|
||||
}
|
||||
|
||||
void CSocket::connect(unsigned long addr, int port){
|
||||
struct sockaddr_in sin;
|
||||
|
||||
// address = CInetAddress(addr,port);
|
||||
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(port);
|
||||
sin.sin_addr.s_addr = htonl(addr);
|
||||
|
||||
if (::connect(fd, (struct sockaddr*)&sin, sizeof sin ) == -1) {
|
||||
cdebug << "ERROR: Connect: " << strerror(errno) << "\n";
|
||||
throw new CSocketException();
|
||||
}
|
||||
|
||||
socklen_t length = sizeof(sin);
|
||||
getpeername(fd,(struct sockaddr*)&sin,&length);
|
||||
address = CInetAddress(ntohl(sin.sin_addr.s_addr),ntohs(sin.sin_port));
|
||||
|
||||
length = sizeof(sin);
|
||||
getsockname(fd, (struct sockaddr*)&sin, &length);
|
||||
localAddress.setAddress(ntohl(sin.sin_addr.s_addr),ntohs(sin.sin_port));
|
||||
|
||||
}
|
||||
|
||||
CStream * CSocket::getStream(){
|
||||
return new CStream(fd);
|
||||
}
|
||||
|
||||
void CSocket::close(){
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
CClientSocket::CClientSocket(unsigned long addr, int port):CSocket(){
|
||||
connect(addr,port);
|
||||
}
|
||||
|
||||
CServerSocket::CServerSocket(int port):CSocket(){
|
||||
bind("any",port);
|
||||
listen(5);
|
||||
}
|
||||
|
||||
CServerSocket::CServerSocket(char *addr, int port):CSocket(){
|
||||
bind(addr,port);
|
||||
listen(5);
|
||||
}
|
||||
|
||||
CSocket * CServerSocket::accept(){
|
||||
int n_sock; /* For accepting connection */
|
||||
struct sockaddr_in sin; /* Address for socket */
|
||||
socklen_t len; /* For use with accept() */
|
||||
|
||||
len = sizeof(sin);
|
||||
if ((n_sock=::accept(getFileDescriptor(), (struct sockaddr*)&sin, &len)) < 0){
|
||||
cdebug << "CServerSocket::accept Error: " << strerror(errno) << "\n";
|
||||
throw new CSocketException();
|
||||
} else {
|
||||
return new CSocket(n_sock);
|
||||
}
|
||||
}
|
||||
|
||||
void CSocket::set_fl(int flags) /* flags are file status flags to turn o
|
||||
n */
|
||||
{
|
||||
int val;
|
||||
|
||||
if ((val = fcntl(fd, F_GETFL, 0)) < 0)
|
||||
perror("Error Socket: fcntl");
|
||||
|
||||
val |= flags;
|
||||
|
||||
if (fcntl(fd, F_SETFL, val) < 0)
|
||||
perror("Error Socket: fcntl");
|
||||
|
||||
}
|
||||
|
||||
void CSocket::setNonBlocking(){
|
||||
set_fl(O_NONBLOCK);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user