noirchat/net.h

131 lines
2.7 KiB
C

/*
===============================================================================
Net
===============================================================================
*/
#ifndef NET_H
#define NET_H
#include <sys/types.h>
#if defined (WIN32)
#include <Windows.h>
#include <Ws2tcpip.h>
#include <Winsock2.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#if defined (sun)
/* For Solaris 2.6, as it lacks SIOCGIFCONF */
#include <sys/sockio.h>
#endif
#endif
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h> //
#include <net/if.h> // for ifreq
#include "users.h" // for Users functions
#include "common.h" // for config
#include "con/console.h"
#include <inttypes.h>
#include <stdint.h>
#define MESSAGE_OPEN 0
struct in_addr local_interface;
struct sockaddr_in group_socket;
struct ip_mreq group;
int listen_socket;
int send_socket;
/* address of last received message, set via recvfrom */
struct sockaddr_in from_address;
socklen_t from_address_length;
char from_address_string[INET_ADDRSTRLEN];
/* list of known users/hosts */
UserList user_list;
/* "bools" */
int is_listening;
int is_sending;
int is_group_socket_set;
/* ifreq */
struct ifconf ifconf;
struct ifreq ifreq[31];
int interfaces;
char *ip_list[16];
char *dev_list[16];
/* listen */
struct timeval listen_tv;
fd_set master_fds;
fd_set read_fds;
int max_fd;
int i;
char data_buffer[512];
int data_buffer_length;
char ip_address[15];
char port[15];
int initNetwork();
void handleNetInput(const char *data_buffer);
void getInterfaces(); // same v
void freeInterfaces(); // change to int
void startSockets(const char *value);
void stopSockets(const char *value);
void restartSockets(const char *value);
int startSendSocket();
int stopSendSocket();
int openSendSocket();
int closeSendSocket();
int startListenSocket();
int stopListenSocket();
int openListenSocket();
int closeListenSocket();
int receiveSocket(int send_socket);
int checkSocket();
int sendMulticast(int send_socket, struct sockaddr_in *group_socket, char data_buffer[], int data_length);
void sendOpen();
void receiveOpen(const char packet[], size_t packet_size);
int handleOpenMessage(const char packet[]);
int setString(char *destination, const char source[]);
char *stripString(const char string[]);
typedef struct {
uint8_t type;
uint8_t nick_size;
uint8_t id;
uint8_t payload_type;
uint8_t payload_size;
struct in_addr address; // IPv4
char *nick;
char payload[255];
} Message;
Message buildMessage(const char *data_buffer, struct sockaddr_in *socket);
/* frees the malloc'd nick */
int freeMessage(Message *message);
Message current_message; // global message struct, contains last packet
#endif