00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef MN_OS_NET_SOCKET_H
00025
#define MN_OS_NET_SOCKET_H
00026
00027
#include <config.h>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
#include <sys/types.h>
00040
#include <sys/socket.h>
00041
#include <net/if.h>
00042
#include <sys/ioctl.h>
00043
#include <netinet/in.h>
00044
00045
#include "mnstream.h"
00046
#include "MNSocketBase.h"
00047
00048
class MNSelector;
00049
00050
#define IFI_NAME 16
00051
#define IFI_HADDR 8
00052
#define IFI_ALIAS 1
00053
00054 ostream&
operator<<( ostream& ostr,
const struct sockaddr_in& addr );
00055
00056
00057
00058
00059
00060 struct ifi_info
00061 {
00062 char ifi_name[IFI_NAME];
00063 u_char
ifi_haddr[IFI_HADDR];
00064 u_short
ifi_hlen;
00065 short ifi_flags;
00066 short ifi_myflags;
00067 struct sockaddr *
ifi_addr;
00068 struct sockaddr *
ifi_brdaddr;
00069 struct sockaddr *
ifi_dstaddr;
00070 struct ifi_info *
ifi_next;
00071 };
00072
00073
00074
00075
00076
00077 class MNSocket
00078 :
public MNSocketBase
00079 {
00080
int my_socket;
00081
int my_ok;
00082
int my_port;
00083
struct in_addr my_address;
00084
bool _master;
00085
bool _is_mc;
00086
00087
public:
00088
00089
00090
00091 enum SocketFlags
00092 {
00093
TakeoverFlag = 0x1
00094 };
00095
00096
public:
00112
bool mcast_join_new(
struct sockaddr *sa,
char *ifname );
00113
int mcast_leave(
int sockfd,
const struct sockaddr *sa,
char *ifname);
00114
int mcast_set_if(
int sockfd,
const char *ifname);
00115
int mcast_set_loop(
int sockfd,
int onoff);
00116
int mcast_set_ttl(
int sockfd,
int val);
00117
int mcast_get_if(
int sockfd);
00118
int mcast_get_loop(
int sockfd);
00119
int mcast_get_ttl(
int sockfd);
00120
00147
static bool set_addr (
int port,
00148
const char* hostname,
00149
struct sockaddr_in& server_addr );
00150
00151
void make_socket (
int type );
00152
void make_socket (
int domain,
int type );
00153
void remake_socket (
int type );
00154
void remake_socket (
int domain,
int type );
00155
void listen ( );
00156
00160
int bind (
int port );
00161
00168
int bind (
int port,
const char* hostname );
00169
00170
void broadcast ( );
00171
00194
const char*
get_ipaddr(
char* buffer, size_t len );
00195
00205
bool get_sock_name(
struct sockaddr_in& addr )
const;
00206
00208 inline bool get_addr(
struct sockaddr_in& addr )
const {
00209
return get_sock_name( addr );
00210 }
00211
00213
int get_socktype();
00214
00215
private:
00217
MNSocket(
const MNSocket& orig );
00218
00220
MNSocket&
operator=(
const MNSocket& orig );
00221
00222
public:
00228
MNSocket (
MNSocket& orig,
int socketflags = 0 );
00229
MNSocket ( );
00230
MNSocket (
int sock );
00231
virtual ~MNSocket ( );
00232
00243
static bool isMulticast(
const char* hname );
00244
00260
static const char*
getLocalNameForAddr(
struct in_addr* addr,
char* buffer );
00261
00262
int ok ( );
00263
int port ( );
00264
void master (
bool controlSocket );
00265 virtual int get_sockdesc ( )
const {
return my_socket; }
00266 virtual void set_sockdesc (
int socket ) { my_socket = socket; }
00267
virtual void update_ports ( );
00273
inline virtual bool isUdp() const = 0;
00274
00275
int l1_receive ( struct msghdr* hdr );
00276
int l1_receive ( struct iovec* iov,
00277
int iovlen,
00278 struct sockaddr_in* dest );
00279
int l1_receive (
char* buffer,
int max_len );
00280
00292
int send ( struct iovec* iov,
00293
int iov_count,
00294 const struct sockaddr_in* dest );
00295
00296
void reuseaddress ( );
00297
void linger ( );
00298
void nolinger ( );
00299
void nodelay ( );
00300
void noblock ( );
00301
00302
00303 virtual
void connection_closed ( );
00304
00305 virtual const
char* get_title ( );
00306 };
00307
00308 #endif
00309