From: Kevin Day Date: Sun, 17 Jan 2016 20:20:05 +0000 (-0600) Subject: Progress: add socket libraries X-Git-Tag: 0.5.0~536 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=2556f1ede7f007a14a7ab2b21658b6f88184a99c;p=fll Progress: add socket libraries There is likely more work to do with these libraries. I will follow this up after reviewing with any changes, if needed. --- diff --git a/level_0/f_socket/c/socket.c b/level_0/f_socket/c/socket.c new file mode 100644 index 0000000..0e6889c --- /dev/null +++ b/level_0/f_socket/c/socket.c @@ -0,0 +1,18 @@ +/* FLL - Level 0 + * Project: Socket + * Version: 0.4.2 + * Licenses: lgplv2.1 + * Programmers: Kevin Day + * Documentation: + * + * Provide means to connect to and use sockets. + */ +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_socket/c/socket.h b/level_0/f_socket/c/socket.h new file mode 100644 index 0000000..d5ec7ab --- /dev/null +++ b/level_0/f_socket/c/socket.h @@ -0,0 +1,45 @@ +/* FLL - Level 0 + * Project: Socket + * Version: 0.4.2 + * Licenses: lgplv2.1 + * Programmers: Kevin Day + * Documentation: + * + * Provide means to connect to and use sockets. + */ +#ifndef _F_socket_h +#define _F_socket_h + +// libc includes +#include +#include +#include +#include + +// fll includes +#include +#include +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +#ifndef _di_f_socket_types_ + #define f_socket_address struct sockaddr_un + #define f_socket_id f_s_int + #define f_socket_close_id f_u_short + + enum { + f_socket_close_fast, // As in close(); + f_socket_close_read, // As in shutdown(, SHUT_RD); + f_socket_close_write, // As in shutdown(, SHUT_WR); + f_socket_close_read_write, // As in shutdown(, SHUT_RDWR); + }; +#endif // _di_f_socket_types_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _F_socket_h diff --git a/level_0/f_socket/data/build/dependencies b/level_0/f_socket/data/build/dependencies new file mode 100644 index 0000000..85f269d --- /dev/null +++ b/level_0/f_socket/data/build/dependencies @@ -0,0 +1,2 @@ +f_types +f_errors diff --git a/level_0/f_socket/data/build/settings b/level_0/f_socket/data/build/settings new file mode 100644 index 0000000..66ffda4 --- /dev/null +++ b/level_0/f_socket/data/build/settings @@ -0,0 +1,23 @@ +# fss-0000 + +project_name f_socket +project_level 0 + +version_major 0 +version_minor 4 +version_micro 2 + +build_compiler gcc +build_linker ar +build_libraries -lc +build_sources_library socket.c +build_sources_program +build_sources_headers socket.h +build_shared yes +build_static yes + +flags_all -z now +flags_shared +flags_static +flags_library -fPIC +flags_program -fPIE diff --git a/level_1/fl_socket/c/socket.c b/level_1/fl_socket/c/socket.c new file mode 100644 index 0000000..5452804 --- /dev/null +++ b/level_1/fl_socket/c/socket.c @@ -0,0 +1,138 @@ +/* FLL - Level 1 + * Project: Socket + * Version: 0.4.2 + * Licenses: lgplv2.1 + * Programmers: Kevin Day + * Documentation: + * + * Provide means to connect to and use sockets. + */ +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +#ifndef _di_fl_socket_file_bind_ + f_return_status fl_socket_file_bind(const f_string socket_path, f_socket_id *socket_id, f_socket_address *socket_address) { + memset(&socket_address, 0, structure_socket_length); + socket_address.sun_family = SOCKET_FAMILY; + strncpy(socket_address.sun_path, socket_path, sizeof(socket_address.sun_path) - 1); + + if (bind(*socket_id, (f_socket_address *) socket_address, structure_socket_length) < 0) { + if (errno == EACCES) { + return f_access_denied; + } + else if (errno == EADDRINUSE) { + return f_busy_address; + } + else if (errno == EADDRNOTAVAIL) { + return f_unavailable_address; + } + else if (errno == EINVAL) { + return f_busy_socket; + } + else if (errno == ENOTSOCK) { + return f_invalid_socket; + } + else if (errno == ENAMETOOLONG) { + return f_string_too_large; + } + else if (errno == ENOENT) { + return f_file_not_found; + } + else if (errno == EFAULT) { + return f_invalid_address; + } + else if (errno == ENOTDIR) { + return f_directory_not_found; + } + else if (errno == ENOMEM) { + return f_out_of_memory; + } + else if (errno == ELOOP) { + return f_loop; + } + + return f_failure; + } + + return f_none; + } +#endif // _di_fl_socket_file_bind_ + +#ifndef _di_fl_socket_listen_ + f_return_status fl_socket_listen(const f_socket_id socket_id, const f_u_int socket_backlog) { + if (listen(socket_id, socket_backlog) < 0) { + if (errno == EADDRINUSE) { + return f_busy; + } + else if (errno == EBADF) { + return f_file_descriptor_error; + } + else if (errno == ENOTSOCK) { + return f_invalid_socket; + } + else if (errno == EOPNOTSUPP) { + return f_unsupported; + } + + return f_failure; + } + + return f_none; + } +#endif // _di_fl_socket_listen_ + +#ifndef _di_fl_socket_close_client_ + // terminate a socket connection. + f_return_status fl_socket_close_client(const f_socket_id socket_id_client, const f_socket_close_id close_action = f_socket_close_fast) { + f_u_int error_code = 0; + + if (close_action == f_socket_close_fast) { + if (close(socket_id_client) < 0) { + error_code = errno; + } + } + else { + if (close_action == f_socket_close_read || close_action == f_socket_close_write || close_action == f_socket_close_read_write) { + if (shutdown(socket_id_client, close_action) < 0) { + error_code = errno; + } + } + else { + // socket close id is unsupported. + return f_unsupported; + } + } + + if (error_code > 0) { + if (result == EBADF) { + return f_file_descriptor_error; + } + else if (result == EINVAL) { + return f_invalid_value; + } + else if (result == ENOTCONN) { + return f_not_connected; + } + else if (result == ENOTSOCK) { + return f_invalid_socket; + } + else if (result == EINTR) { + return f_interrupted; + } + else if (result == EBADF) { + return f_input_output_error; + } + + return f_failure; + } + + return f_none; + } +#endif // _di_fl_socket_close_client_ + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_1/fl_socket/c/socket.h b/level_1/fl_socket/c/socket.h new file mode 100644 index 0000000..d868424 --- /dev/null +++ b/level_1/fl_socket/c/socket.h @@ -0,0 +1,49 @@ +/* FLL - Level 1 + * Project: Socket + * Version: 0.4.2 + * Licenses: lgplv2.1 + * Programmers: Kevin Day + * Documentation: + * + * Provide means to connect to and use sockets. + */ +#ifndef _F_socket_h +#define _F_socket_h + +// libc includes +#include +#include +#include +#include + +// fll includes +#include +#include +#include + +#ifdef __cplusplus +extern "C"{ +#endif + +#ifndef _di_fl_socket_file_bind_ + // bind a socket. + f_extern f_return_status fl_socket_file_bind(const f_string socket_path, f_socket_id *socket_id, f_socket_address *socket_address); +#endif // _di_fl_socket_file_bind_ + +#ifndef _di_fl_socket_listen_ + // terminate a socket connection + // suggested socket_backlog default setting = 8. + f_extern f_return_status fl_socket_listen(const f_socket_id socket_id, const f_u_int socket_backlog); +#endif // _di_fl_socket_listen_ + +#ifndef _di_fl_socket_close_client_ + // terminate a socket connection + // suggested default close_action = f_socket_close_fast. + f_extern f_return_status fl_socket_close_client(const f_socket_id socket_id_client, const f_socket_close_id close_action); +#endif // _di_fl_socket_close_client_ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _F_socket_h diff --git a/level_1/fl_socket/data/build/dependencies b/level_1/fl_socket/data/build/dependencies new file mode 100644 index 0000000..02ab1d1 --- /dev/null +++ b/level_1/fl_socket/data/build/dependencies @@ -0,0 +1,3 @@ +f_types +f_errors +f_socket diff --git a/level_1/fl_socket/data/build/settings b/level_1/fl_socket/data/build/settings new file mode 100644 index 0000000..c254eef --- /dev/null +++ b/level_1/fl_socket/data/build/settings @@ -0,0 +1,23 @@ +# fss-0000 + +project_name fl_socket +project_level 0 + +version_major 0 +version_minor 4 +version_micro 2 + +build_compiler gcc +build_linker ar +build_libraries -lc +build_sources_library socket.c +build_sources_program +build_sources_headers socket.h +build_shared yes +build_static yes + +flags_all -z now +flags_shared +flags_static +flags_library -fPIC +flags_program -fPIE