# Network functions
[LibTDS API]

Functions for reading or writing from network. More...

## Defines

#define | TEMP_FREE ;
#define | TEMP_INIT(s) char temp[s]
#define | TEMP_SIZE sizeof(temp)

## Typedefs

typedef unsigned int | ioctl_nonblocking_t

## Functions

static int | goodread (TDSSOCKET *tds, unsigned char *buf, int buflen)
static int | read_and_convert (TDSSOCKET *tds, const TDSICONV *char_conv, size_t *wire_size, char **outbuf, size_t *outbytesleft)
int | tds7_get_instance_port (const char *ip_addr, const char *instance)
| Get port of given instance.
int | tds_close_socket (TDSSOCKET *tds)
int | tds_flush_packet (TDSSOCKET *tds)
| Flush packet to server.
unsigned char | tds_get_byte (TDSSOCKET *tds)
int | tds_get_char_data (TDSSOCKET *tds, char *row_buffer, size_t wire_size, TDSCOLUMN *curcol)
| Fetch character data the wire.
TDS_INT | tds_get_int (TDSSOCKET *tds)
| Get an int32 from the server.
TDS_INT8 | tds_get_int8 (TDSSOCKET *tds)
void * | tds_get_n (TDSSOCKET *tds, void *dest, int need)
| Get N bytes from the buffer and return them in the already allocated space given to us.
int | tds_get_size_by_type (int servertype)
| Return the number of bytes needed by specified type.
TDS_SMALLINT | tds_get_smallint (TDSSOCKET *tds)
| Get an int16 from the server.
int | tds_get_string (TDSSOCKET *tds, int string_len, char *dest, size_t dest_size)
| Fetch a string from the wire.
static int | tds_goodread (TDSSOCKET *tds, unsigned char *buf, int buflen, unsigned char unfinished)
| Loops until we have received buflen characters return -1 on failure This function does not close the socket.
static int | tds_goodwrite (TDSSOCKET *tds, const unsigned char *p, int len, unsigned char last)
int | tds_init_write_buf (TDSSOCKET *tds)
int | tds_open_socket (TDSSOCKET *tds, const char *ip_addr, unsigned int port, int timeout)
unsigned char | tds_peek (TDSSOCKET *tds)
int | tds_put_buf (TDSSOCKET *tds, const unsigned char *buf, int dsize, int ssize)
int | tds_put_byte (TDSSOCKET *tds, unsigned char c)
int | tds_put_int (TDSSOCKET *tds, TDS_INT i)
int | tds_put_int8 (TDSSOCKET *tds, TDS_INT8 i)
int | tds_put_n (TDSSOCKET *tds, const void *buf, int n)
int | tds_put_smallint (TDSSOCKET *tds, TDS_SMALLINT si)
int | tds_put_string (TDSSOCKET *tds, const char *s, int len)
| Output a string to wire automatic translate string to unicode if needed.
int | tds_read_packet (TDSSOCKET *tds)
| Read in one 'packet' from the server.
static int | tds_select (TDSSOCKET *tds, unsigned tds_sel, int timeout_seconds)
| Select on a socket until it's available or the timeout expires.
void | tds_unget_byte (TDSSOCKET *tds)
| Unget will always work as long as you don't call it twice in a row.
int | tds_write_packet (TDSSOCKET *tds, unsigned char final)

## Detailed Description

Functions for reading or writing from network.

## Function Documentation

int tds7_get_instance_port | ( | const char * | ip_addr,
|  | const char * | instance
| ) |

Get port of given instance.

Returns:
  port number or 0 if error

int tds_flush_packet | ( | TDSSOCKET * | tds | ) |

Flush packet to server.

Returns:
  TDS_FAIL or TDS_SUCCEED

int tds_get_char_data | ( | TDSSOCKET * | tds,
|  | char * | row_buffer,
|  | size_t | wire_size,
|  | TDSCOLUMN * | curcol
| ) |

Fetch character data the wire.

Output is NOT null terminated. If char_conv is not NULL, convert data accordingly.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | row_buffer | destination buffer in current_row. Can't be NULL
  | wire_size | size to read from wire (in bytes)
  | curcol | column information

Returns:
  TDS_SUCCEED or TDS_FAIL (probably memory error on text data)

Todo:
  put a TDSICONV structure in every TDSCOLUMN

void* tds_get_n | ( | TDSSOCKET * | tds,
|  | void * | dest,
|  | int | need
| ) |

| Get N bytes from the buffer and return them in the already allocated space given to us. We ASSUME that the person calling this function has done the bounds checking for us since they know how many bytes they want here. dest of NULL means we just want to eat the bytes. (tetherow@nol.org)

int tds_get_string | ( | TDSSOCKET * | tds,
|  | int | string_len,
|  | char * | dest,
|  | size_t | dest_size
| ) |

Fetch a string from the wire.

Output string is NOT null terminated. If TDS version is 7 or 8 read unicode string and convert it. This function should be use to read server default encoding strings like columns name, table names, etc, not for data (use tds_get_char_data instead)

Returns:
  bytes written to dest

Parameters:

  | tds | connection information
  | string_len | length of string to read from wire (in server characters, bytes for tds4-tds5, ucs2 for tds7+)
  | dest | destination buffer, if NULL string is read and discarded
  | dest_size | destination buffer size, in bytes

static int tds_goodread | ( | TDSSOCKET * | tds,
|  | unsigned char * | buf,
|  | int | buflen,
|  | unsigned char | unfinished
| ) | `[static]`

| Loops until we have received buflen characters return -1 on failure This function does not close the socket. Maybe it should.

static int tds_goodwrite | ( | TDSSOCKET * | tds,
|  | const unsigned char * | p,
|  | int | len,
|  | unsigned char | last
| ) | `[static]`

Parameters:

  | tds | the famous socket
  | p | pointer to buffer
  | len | bytes in buffer
  | last | 1 if this is the last packet, else 0

Returns:
  len on success, <0 on failure

int tds_put_string | ( | TDSSOCKET * | tds,
|  | const char * | s,
|  | int | len
| ) |

Output a string to wire automatic translate string to unicode if needed.

Returns:
  bytes written to wire

Parameters:

  | tds | state information for the socket and the TDS protocol
  | s | string to write
  | len | length of string in characters, or -1 for null terminated

int tds_read_packet | ( | TDSSOCKET * | tds | ) |

Read in one 'packet' from the server.

This is a wrapped outer packet of the protocol (they bundle result packets into chunks and wrap them at what appears to be 512 bytes regardless of how that breaks internal packet up. (tetherow@nol.org)

Returns:
  bytes read or -1 on failure

static int tds_select | ( | TDSSOCKET * | tds,
|  | unsigned | tds_sel,
|  | int | timeout_seconds
| ) | `[static]`

Select on a socket until it's available or the timeout expires.

Meanwhile, call the interrupt function.

Returns:
  >0 ready descriptors 0 timeout <0 error (cf. errno). Caller should close socket and return failure. This function does not call tdserror or close the socket because it can't know the context in which it's being called.

void tds_unget_byte | ( | TDSSOCKET * | tds | ) |

| Unget will always work as long as you don't call it twice in a row. It it may work if you call it multiple times as long as you don't backup over the beginning of network packet boundary which can occur anywhere in the token stream.