$Revision: 1.9 $
Example code: A socket module.
Status: NOT REVUED, NOT TESTED
- Online References:
-
Included Files
- #include "modules/sk/sk.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <netdb.h>
- #include <signal.h>
- #include <stdio.h>
- #include <include/iproutines.h>
Preprocessor definitions
String sizes
#define STR_S 63
#define STR_M 255
#define STR_L 1023
#define STR_XL 4095
#define STR_XXL 16383
#define SKBUFLEN 2047
External Variables
h_errno
extern int h_errno
Local Variables
sk_watch_tsd
static pthread_key_t sk_watch_tsd
sk_init_once
static pthread_once_t sk_init_once
Wait for an incoming connection on the specified socket
int SK_accept_connection The socket for communicating to the client
More:
Authors:
joao
int SK_accept_connection ( int listening_socket )
- int listening_socket
- The socket that the server is bound to
Take a service name, and a service type, and return a port number. If the
service name is not found, it tries it as a decimal number. The number
returned is byte ordered for the network.
char *service Service name (or port number).
char *proto Protocol (eg "tcp").
More:
Authors:
ottrey
- Online References:
int SK_atoport ( const char* service, const char* proto )
int SK_cd_close ( sk_conn_st* condat )
Wrapper around SK_gets.
More:
if the connection structure has bad status for this connection
from previous calls, no write will be attempted.
Authors:
marek
Side Effects:
broken connections get registered
in the connection structure within the query environment
int SK_cd_gets ( sk_conn_st* condat, char* str, size_t count )
- int SK_cd_gets
- The total_count of bytes read,
or errors (represented as negative numbers)
- sk_conn_st* condat
- connection data
- char* str
- The buffer to be written from the socket.
- size_t count
- &nbs;
int SK_cd_printf ( sk_conn_st* condat, char* txt, ... )
This function writes a character string out to a socket.
int SK_qe_puts The total_count of bytes written,
or errors (represented as negative numbers)
char *str The buffer to be written from the socket.
More:
if the connection structure has bad status for this connection
from previous calls, no write will be attempted.
Authors:
marek
Side Effects:
broken connections get registered
in the connection structure within the query environment
int SK_cd_puts ( sk_conn_st* condat, const char* str )
- sk_conn_st* condat
- connection data
- const char* str
- &nbs;
More:
Authors:
ottrey
- Online References:
int SK_close ( int socket )
This function reads a single character from a socket.
returns EOF when no character can be read.
int SK_getc ( int sockfd )
int SK_getpeerip ( int sockfd, ip_addr_t* ip )
This function will tell you who is at the other end of a connected stream socket.
XXX It's not working.
XXX ? MB it is...
More:
Authors:
ottrey
char* SK_getpeername ( int sockfd )
- int sockfd
- The socket file descriptor.
This function reads from a socket, until it recieves a linefeed
character. It fills the buffer "str" up to the maximum size "count".
More:
Authors:
ottrey
Side Effects:
This function will return -1 if the socket is closed during the read operation.
Note that if a single line exceeds the length of count, the extra data
will be read and discarded! You have been warned.
To Do:
Capture the control-c properly!
int SK_gets ( int sockfd, char* str, size_t count )
- int SK_gets
- The total_count of bytes read.
- int sockfd
- The socket file descriptor.
- char* str
- The buffer to be written from the socket.
- size_t count
- The number of bytes in the buffer.
This function creates a socket and binds to it
- Online References:
int SK_getsock ( int socket_type, u_short port, uint32_t bind_address )
- int SK_getsock
- The new socket
- int socket_type
- SOCK_STREAM or SOCK_DGRAM (TCP or UDP sockets)
- u_short port
- The port to listen on. Remember that ports < 1024 are
reserved for the root user. Must be passed in network byte
order (see "man htons").
- uint32_t bind_address
- Address to bind to, in network order.
More:
Authors:
ottrey
joao
Prototyped in:
| modules/sk/sk.h
|
Calls:
| bind(), close(), exit(), listen(), memset(), perror(), setsockopt(), socket()
|
void SK_init ( void )
return number of chars written
int SK_putc ( int sockfd, char ch )
- int SK_putc
- This function writes a single character out to a socket.
- int sockfd
- socket
char ch character
- char ch
- &nbs;
This function writes a character string out to a socket.
char *str The buffer to be written from the socket.
More:
Authors:
ottrey
Side Effects:
This function will return -1 if the socket is closed during the write operation.
Note that if a single line exceeds the length of count, the extra data
will be read and discarded! You have been warned.
int SK_puts ( int sockfd, const char* str )
- int SK_puts
- The total_count of bytes written,
or errors (represented as negative numbers)
- int sockfd
- The socket file descriptor.
- const char* str
- &nbs;
This is just like the read() system call, except that it will make
sure that all your data goes through the socket.
More:
Authors:
ottrey
int SK_read ( int sockfd, char* buf, size_t count )
- int SK_read
- The number of bytes read.
- int sockfd
- The socket file descriptor.
- char* buf
- The buffer to be read from the socket.
- size_t count
- The number of bytes in the buffer.
void SK_watch_setclear ( sk_conn_st* condat )
void SK_watch_setexec ( sk_conn_st* condat, void* (*function)(void*), void* args )
void SK_watch_setkill ( sk_conn_st* condat, pthread_t killthis )
void SK_watchexec ( sk_conn_st* condat )
void SK_watchkill ( sk_conn_st* condat )
er_ret_t SK_watchstart ( sk_conn_st* condat )
er_ret_t SK_watchstop ( sk_conn_st* condat )
void SK_watchtrigger ( sk_conn_st* condat )
This is just like the write() system call, accept that it will
make sure that all data is transmitted.
char *buf The buffer to be written to the socket.
More:
Authors:
ottrey
- Online References:
int SK_write ( int sockfd, const char* buf, size_t count )
- int sockfd
- The socket file descriptor.
- const char* buf
- &nbs;
- size_t count
- The number of bytes in the buffer.
static void func_sigusr ( int n )
static void sk_real_init ( void )
static void* sk_watchdog ( void* arg )
Calls:
| ER_dbg_va() | modules/er/er.c
|
| SK_watchtrigger() | modules/sk/sk_socket.c
|
| fprintf(), memset(), pthread_exit(), pthread_getspecific(), pthread_mutex_unlock(), pthread_setspecific(), pthread_sigmask(), read(), select(), sigaction(), sigaddset(), sigemptyset()
|
Used in:
| SK_watchstart() | modules/sk/sk_socket.c
|
References Functions:
| func_sigusr() | modules/sk/sk_socket.c
|
References Variables:
| sk_watch_tsd | modules/sk/sk_socket.c
|