Crossfire Server, Trunk  R20513
Functions
lowlevel.c File Reference

Low-level socket-related functions. More...

#include "global.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "shared/newclient.h"
#include "sproto.h"
Include dependency graph for lowlevel.c:

Go to the source code of this file.

Functions

static void add_to_buffer (socket_struct *ns, const unsigned char *buf, int len)
 Adds data to a socket buffer for whatever reason. More...
 
int GetInt_String (const unsigned char *data)
 Basically does the reverse of SockList_AddInt, but on strings instead. More...
 
short GetShort_String (const unsigned char *data)
 
void Send_With_Handling (socket_struct *ns, SockList *sl)
 Calls Write_To_Socket to send data to the client. More...
 
void SockList_AddChar (SockList *sl, char data)
 Adds an 8 bit value. More...
 
void SockList_AddData (SockList *sl, const void *data, size_t len)
 Adds a data block. More...
 
void SockList_AddInt (SockList *sl, uint32_t data)
 Adds a 32 bit value. More...
 
void SockList_AddInt64 (SockList *sl, uint64_t data)
 Adds a 64 bit value. More...
 
void SockList_AddLen16Data (SockList *sl, const void *data, size_t len)
 Adds a data block prepended with an 16 bit length field. More...
 
void SockList_AddLen8Data (SockList *sl, const void *data, size_t len)
 Adds a data block prepended with an 8 bit length field. More...
 
void SockList_AddPrintf (SockList *sl, const char *format,...)
 Adds a printf like formatted string. More...
 
void SockList_AddShort (SockList *sl, uint16_t data)
 Adds a 16 bit value. More...
 
void SockList_AddString (SockList *sl, const char *data)
 Adds a string without length. More...
 
void SockList_AddStringBuffer (SockList *sl, StringBuffer *sb)
 Deallocates string buffer instance and appends its contents. More...
 
size_t SockList_Avail (const SockList *sl)
 Returns the available bytes in a SockList instance. More...
 
static void SockList_Ensure (const SockList *sl, size_t size)
 Checks that at least a given number of bytes is available in a SockList instance. More...
 
void SockList_Init (SockList *sl)
 Initializes the SockList instance. More...
 
void SockList_NullTerminate (SockList *sl)
 Adds a NUL byte without changing the length. More...
 
int SockList_ReadPacket (int fd, SockList *sl, int len)
 This reads from fd and puts the data in sl. More...
 
void SockList_Reset (SockList *sl)
 Resets the length of the stored data for writing. More...
 
void SockList_ResetRead (SockList *sl)
 Resets the length of the stored data for reading. More...
 
void SockList_Term (SockList *sl)
 Frees all resources allocated by a SockList instance. More...
 
void write_socket_buffer (socket_struct *ns)
 Writes data to socket. More...
 
static void Write_To_Socket (socket_struct *ns, const unsigned char *buf, int len)
 This writes data to the socket. More...
 

Detailed Description

Low-level socket-related functions.

Date
2003-12-02

Contains some base functions that both the client and server can use. As such, depending what we are being compiled for will determine what we can include. the client is designed have CFCLIENT defined as part of its compile flags.

Definition in file lowlevel.c.

Function Documentation

static void add_to_buffer ( socket_struct ns,
const unsigned char *  buf,
int  len 
)
static

Adds data to a socket buffer for whatever reason.

ns is the socket we are adding the data to, buf is the start of the data, and len is the number of bytes to add.

Definition at line 389 of file lowlevel.c.

References buffer_struct::data, socket_struct::fd, buffer_struct::len, llevDebug, LOG(), Ns_Dead, socket_struct::outputbuffer, SOCKETBUFSIZE, buffer_struct::start, and socket_struct::status.

Referenced by Write_To_Socket().

Here is the call graph for this function:

Here is the caller graph for this function:

int GetInt_String ( const unsigned char *  data)

Basically does the reverse of SockList_AddInt, but on strings instead.

Same for the GetShort, but for 16 bits.

Definition at line 246 of file lowlevel.c.

Referenced by inscribe_scroll_cmd(), lock_item_cmd(), mark_item_cmd(), and new_player_cmd().

Here is the caller graph for this function:

short GetShort_String ( const unsigned char *  data)

Definition at line 250 of file lowlevel.c.

Referenced by new_player_cmd().

Here is the caller graph for this function:

void Send_With_Handling ( socket_struct ns,
SockList sl 
)

Calls Write_To_Socket to send data to the client.

The only difference in this function is that we take a SockList , and we prepend the length information.

Definition at line 542 of file lowlevel.c.

References Socket_Info::allocated_sockets, SockList::buf, cst_lst, cst_tot, CS_Stats::ibytes, SockList::len, llevInfo, LOG(), CS_Stats::max_conn, Ns_Dead, CS_Stats::obytes, socket_info, socket_struct::status, CS_Stats::time_start, write_cs_stats(), and Write_To_Socket().

Referenced by account_add_player_cmd(), account_login_cmd(), account_new_cmd(), account_password(), account_play_cmd(), add_me_cmd(), create_player_cmd(), draw_client_map2(), draw_magic_map(), esrv_add_spells(), esrv_del_item(), esrv_draw_look(), esrv_new_player(), esrv_remove_spell(), esrv_send_animation(), esrv_send_face(), esrv_send_inventory(), esrv_send_item(), esrv_send_pickup(), esrv_update_item(), esrv_update_spells(), esrv_update_stats(), final_free_player(), init_connection(), knowledge_process_incremental(), knowledge_send_info(), map_newmap_cmd(), new_player_cmd(), play_again(), play_sound_player_only(), print_ext_msg(), quest_send_initial_states(), quest_set_state(), request_info_cmd(), send_account_players(), send_background_music(), send_class_info(), send_class_list(), send_exp_table(), send_file(), send_image_info(), send_image_sums(), send_map_info(), send_new_char_info(), send_plugin_custom_message(), send_query(), send_race_info(), send_race_list(), send_skill_info(), send_smooth(), send_spell_paths(), send_tick(), and set_up_cmd().

Here is the call graph for this function:

void SockList_AddChar ( SockList sl,
char  data 
)
void SockList_AddData ( SockList sl,
const void *  data,
size_t  len 
)

Adds a data block.

Parameters
slthe SockList instance to add to
datathe value to add
lenthe length in byte

Definition at line 159 of file lowlevel.c.

References SockList::buf, SockList::len, and SockList_Ensure().

Referenced by append_spell(), esrv_send_face(), send_arch_info(), SockList_AddLen16Data(), SockList_AddLen8Data(), and SockList_AddString().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddInt ( SockList sl,
uint32_t  data 
)

Adds a 32 bit value.

Parameters
slthe SockList instance to add to
datathe value to add

Definition at line 119 of file lowlevel.c.

References SockList::buf, SockList::len, and SockList_Ensure().

Referenced by add_object_to_socklist(), append_spell(), esrv_del_item(), esrv_draw_look(), esrv_new_player(), esrv_remove_spell(), esrv_send_face(), esrv_send_inventory(), esrv_send_item(), esrv_send_pickup(), esrv_update_item(), esrv_update_spells(), knowledge_process_incremental(), new_player_cmd(), play_again(), quest_send_initial_states(), quest_set_state(), send_image_sums(), and send_tick().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddInt64 ( SockList sl,
uint64_t  data 
)

Adds a 64 bit value.

Parameters
slthe SockList instance to add to
datathe value to add

Definition at line 132 of file lowlevel.c.

References SockList::buf, SockList::len, and SockList_Ensure().

Referenced by esrv_update_stats(), and send_exp_table().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddLen16Data ( SockList sl,
const void *  data,
size_t  len 
)

Adds a data block prepended with an 16 bit length field.

Parameters
slthe SockList instance to add to
datathe value to add
lenthe length in byte; must not exceed 65535

Definition at line 183 of file lowlevel.c.

References SockList_AddData(), and SockList_AddShort().

Referenced by knowledge_process_incremental(), quest_send_initial_states(), quest_set_state(), and send_map_info().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddLen8Data ( SockList sl,
const void *  data,
size_t  len 
)

Adds a data block prepended with an 8 bit length field.

Parameters
slthe SockList instance to add to
datathe value to add
lenthe length in byte; must not exceed 255

Definition at line 171 of file lowlevel.c.

References SockList_AddChar(), and SockList_AddData().

Referenced by add_object_to_socklist(), append_spell(), esrv_draw_look(), esrv_new_player(), esrv_send_inventory(), esrv_update_item(), play_sound_player_only(), send_arch_info(), send_image_sums(), and send_new_char_info().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddPrintf ( SockList sl,
const char *  format,
  ... 
)

Adds a printf like formatted string.

Parameters
slthe SockList instance to add to
formatthe format specifier

Definition at line 194 of file lowlevel.c.

References SockList::buf, fatal(), SockList::len, OUT_OF_MEMORY, and vsnprintf.

Referenced by account_play_cmd(), build_class_list_reply(), build_race_list_reply(), create_player_cmd(), draw_magic_map(), esrv_draw_look(), esrv_send_inventory(), init_connection(), knowledge_send_info(), print_ext_msg(), send_class_info(), send_image_info(), send_image_sums(), send_map_info(), send_query(), send_race_info(), send_skill_info(), send_spell_paths(), and set_up_cmd().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_AddShort ( SockList sl,
uint16_t  data 
)
void SockList_AddString ( SockList sl,
const char *  data 
)
void SockList_AddStringBuffer ( SockList sl,
StringBuffer sb 
)

Deallocates string buffer instance and appends its contents.

The passed StringBuffer must not be accessed afterwards.

Parameters
slthe SockList instance to add to
sbthe StringBuffer to deallocate

Definition at line 217 of file lowlevel.c.

References SockList_AddString(), and stringbuffer_finish().

Here is the call graph for this function:

size_t SockList_Avail ( const SockList sl)

Returns the available bytes in a SockList instance.

Returns
the available bytes

Definition at line 238 of file lowlevel.c.

References SockList::buf, and SockList::len.

Referenced by esrv_add_spells(), esrv_draw_look(), esrv_send_inventory(), knowledge_process_incremental(), quest_send_initial_states(), send_exp_table(), send_image_sums(), send_skill_info(), and send_spell_paths().

Here is the caller graph for this function:

static void SockList_Ensure ( const SockList sl,
size_t  size 
)
static

Checks that at least a given number of bytes is available in a SockList instance.

Returns normal if the space is available. Otherwise calls fatal(OUT_OF_MEMORY);

Parameters
slthe SockList instance to check
sizethe number of bytes to ensure

Definition at line 87 of file lowlevel.c.

References SockList::buf, fatal(), SockList::len, and OUT_OF_MEMORY.

Referenced by SockList_AddChar(), SockList_AddData(), SockList_AddInt(), SockList_AddInt64(), SockList_AddShort(), and SockList_NullTerminate().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_Init ( SockList sl)
void SockList_NullTerminate ( SockList sl)

Adds a NUL byte without changing the length.

Parameters
slthe SockList instance to add to

Definition at line 229 of file lowlevel.c.

References SockList::buf, SockList::len, and SockList_Ensure().

Referenced by handle_client().

Here is the call graph for this function:

Here is the caller graph for this function:

int SockList_ReadPacket ( int  fd,
SockList sl,
int  len 
)

This reads from fd and puts the data in sl.

We return true if we think we have a full packet, 0 if we have a partial packet, or -1 if an error occurred. The only processing we do is remove the initial size value. len (As passed) is the size of the buffer allocated in the socklist. We make the assumption the buffer is at least 2 bytes long.

Definition at line 267 of file lowlevel.c.

References SockList::buf, cst_lst, cst_tot, CS_Stats::ibytes, SockList::len, llevDebug, llevError, and LOG().

Referenced by handle_client().

Here is the call graph for this function:

Here is the caller graph for this function:

void SockList_Reset ( SockList sl)

Resets the length of the stored data for writing.

Does not free or re-allocate resources.

Parameters
slthe SockList instance to reset

Definition at line 66 of file lowlevel.c.

References SockList::len.

Referenced by build_class_list_reply(), esrv_add_spells(), esrv_draw_look(), esrv_send_inventory(), knowledge_process_incremental(), quest_send_initial_states(), send_image_sums(), and SockList_Init().

Here is the caller graph for this function:

void SockList_ResetRead ( SockList sl)

Resets the length of the stored data for reading.

Does not free or re-allocate resources.

Parameters
slthe SockList instance to reset

Definition at line 75 of file lowlevel.c.

References SockList::len.

Referenced by account_play_cmd(), handle_client(), init_connection(), and set_player_socket().

Here is the caller graph for this function:

void SockList_Term ( SockList sl)
void write_socket_buffer ( socket_struct ns)

Writes data to socket.

When the socket is clear to write, and we have backlogged data, this is called to write it out.

Definition at line 424 of file lowlevel.c.

References socket_struct::can_write, cst_lst, cst_tot, buffer_struct::data, socket_struct::fd, buffer_struct::len, llevDebug, llevError, LOG(), Ns_Dead, CS_Stats::obytes, socket_struct::outputbuffer, SOCKETBUFSIZE, buffer_struct::start, and socket_struct::status.

Referenced by do_server().

Here is the call graph for this function:

Here is the caller graph for this function:

static void Write_To_Socket ( socket_struct ns,
const unsigned char *  buf,
int  len 
)
static

This writes data to the socket.

  • It is very low level - all we try and do is write out the data to the socket provided (ns). buf is the data to write, len is the number of bytes to write. IT doesn't return anything - rather, it updates the ns structure if we get an error.

Definition at line 480 of file lowlevel.c.

References add_to_buffer(), socket_struct::can_write, cst_lst, cst_tot, socket_struct::fd, llevDebug, llevError, LOG(), Ns_Dead, CS_Stats::obytes, and socket_struct::status.

Referenced by Send_With_Handling().

Here is the call graph for this function:

Here is the caller graph for this function: