00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __LIBNET_H__
00011 #define __LIBNET_H__
00012
00013 #include <iostream>
00014 #include <string>
00015 #include <errno.h>
00016 #include <netdb.h>
00017 #include <sys/time.h>
00018 #include <netinet/ip.h>
00019 #include <netinet/ip_icmp.h>
00020
00021 #include <sys/socket.h>
00022 #include <sys/unistd.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025
00026 #include <sys/socket.h>
00027 #include <netinet/in.h>
00028 #include <arpa/inet.h>
00029
00030 #include <strstream>
00031
00032 const int SOCKET_CLOSED = -1;
00033 const int ERREUR = -1;
00034
00035 #define ostringstream ostrstream
00036
00037 class EXCEPTION
00038 {
00039 public:
00040 EXCEPTION (string str)
00041 {
00042 ostringstream toto;
00043 toto << "Erreur " << str << " [errno : " << strerror(errno) << "(" << errno << ")] ";
00044 mStr = toto.str();
00045 }
00046 string getString () { return mStr; }
00047 private:
00048 string mStr;
00049 };
00050
00051 class Socket
00052 {
00053 public:
00054 Socket (int port, int type = SOCK_STREAM, int protocole = 0);
00055 Socket (int sd, struct sockaddr_in adr);
00056 Socket (const Socket& s);
00057 Socket ();
00058 string read (int nb);
00059 string readall ();
00060 string readln ();
00061 int write (string str);
00062 int getSocket ();
00063 int getPort ();
00064 string getAdresse ();
00065 int setOption (int optname, void* optval, int optlen);
00066 int setOption (int optname, int option = 1);
00067 int getProto (string name);
00068 int close ();
00069
00070 protected:
00071 int mSocket;
00072 struct sockaddr_in mAdresse;
00073 };
00074
00075 class SocketClient : public Socket
00076 {
00077 public:
00078 SocketClient (string adr, int port, int type = SOCK_STREAM, int protocole = 0);
00079 SocketClient (const SocketClient& sd);
00080 SocketClient ();
00081 int getPortDest ();
00082 string getAdresseDest ();
00083 protected:
00084 struct sockaddr_in mAdresseDest;
00085 };
00086
00087 class SocketServeur : public Socket
00088 {
00089 public:
00090 SocketServeur (int port, bool opt, int type = SOCK_STREAM, int protocole = 0);
00091 SocketServeur (int port, int type = SOCK_STREAM, int protocole = 0);
00092 SocketServeur (Socket sd, bool opt);
00093 SocketServeur ();
00094 Socket accept ();
00095
00096 };
00097
00098 #endif