Compare commits

..

39 Commits

Author SHA1 Message Date
5df38a4e97 Updated Makefile 2023-04-12 22:49:08 -05:00
862fb9f944 Extended 'install' target to install man pages as well 2023-04-05 00:09:02 -05:00
8c05336c73 Added man pages for library 2023-04-05 00:08:34 -05:00
94e9ba3b57 Updated return values to better conform with the other functions 2023-04-04 23:40:35 -05:00
ad0966f749 Updated error codes and replaced 'exit' with 'return' 2023-04-04 23:18:10 -05:00
07e991223f Fixed bug with return statement 2023-04-04 19:31:56 -05:00
14418078ff Changed exit statements with return statements 2023-04-04 19:31:16 -05:00
19281f8ca3 Updated README 2023-03-27 17:32:57 -05:00
b8a9f77e13 Updated README 2023-03-27 17:32:42 -05:00
643c8abb67 Updated README 2023-03-27 17:31:40 -05:00
424e95c603 Updated README 2023-03-27 17:31:11 -05:00
b64268f323 Updated README 2023-03-27 17:30:03 -05:00
bb00c52479 Updated README 2023-03-27 17:29:12 -05:00
32f9974c19 Updated README 2023-03-27 17:28:16 -05:00
1c63ea60c2 No changes (added new-line because git doesn't allow commits with no changes), added file to project 2023-03-25 11:36:44 -05:00
9d164cf7b8 No changes to files, added to new project 2023-03-25 11:32:03 -05:00
9c94eb001b Updated README 2023-03-25 11:30:07 -05:00
ddb1b16c93 Added inet_to_int function 2023-03-08 15:45:47 -06:00
75a9c7f6c3 Added 'char_to_socktype' function 2023-03-08 11:29:04 -06:00
f5415cd1bc Added int_to_inet function 2023-03-08 11:11:08 -06:00
192e5b7af9 Moved 'check_ip_ver' function to easysock.c 2023-03-08 11:08:33 -06:00
97b6ec58fd Replaced exit codes with values that aren't taken by errno 2023-03-03 09:21:13 -06:00
ae433af26f Fixed comment accuracy regarding error codes 2023-02-25 21:30:39 -06:00
0647b2570f Updated definition for 'create_remote' function 2023-02-24 22:06:54 -06:00
f26e83b835 Fixed spelling mistake 2023-02-24 19:57:48 -06:00
6b10465f61 Added include guards 2023-02-22 22:40:35 -06:00
2d640466f7 Added separate file for helper functions. 2023-02-22 08:15:11 -06:00
d8e78a5be7 Removed testing code, fixed bug where domain didn't translate correctly to address 2023-03-08 19:19:35 -06:00
4ea9a18bcf Added code for testing 2023-03-08 19:10:25 -06:00
1660d5cfed Added 'inet_to_int' function, and 'char_to_socktype' function; started working on domain name support for remote address 2023-03-08 16:07:06 -06:00
5f108cf2ff Added int_to_inet function 2023-03-08 11:11:08 -06:00
8e4d3d89de Moved 'check_ip_ver' function to easysock.c 2023-03-08 11:08:33 -06:00
ca83bd693b Replaced exit codes with values that aren't taken by errno 2023-03-03 09:21:13 -06:00
74c5025370 Replaced negative exit codes with positive ones or 'errno' 2023-02-25 21:27:48 -06:00
cf4db500c1 Added new paramter to 'create_remote' function, fixed minor bugs 2023-02-24 22:09:41 -06:00
3e97969d93 Replaced 'printf' with 'exit(errno)' to make the functions portable 2023-02-24 12:40:59 -06:00
06cc3fe2d1 Fixed IPv6 bug (sockaddr size too small) when binding socket to address 2023-02-24 11:01:23 -06:00
9c2c915ca7 Added separate file for helper functions. 2023-02-22 08:15:11 -06:00
72fb153c3a Temporary deletion of easysock files 2023-03-25 11:23:03 -05:00
13 changed files with 43 additions and 33 deletions

View File

@@ -40,9 +40,11 @@ install: $(LIB_FILE)
install -d $(DESTDIR)$(PREFIX)/include install -d $(DESTDIR)$(PREFIX)/include
install -m 644 easysock.h $(DESTDIR)$(PREFIX)/include install -m 644 easysock.h $(DESTDIR)$(PREFIX)/include
install -m 644 docs/man3/*.gz /usr/share/man/man3/
.PHONY: clean .PHONY: clean
clean: clean:
rm *.o rm easysock.o
rm $(LIB_FILE) rm $(LIB_FILE)
@@ -50,3 +52,4 @@ clean:
uninstall: uninstall:
rm $(DESTDIR)$(PREFIX)/lib/$(LIB_FILE) rm $(DESTDIR)$(PREFIX)/lib/$(LIB_FILE)
rm $(DESTDIR)$(PREFIX)/include/easysock.h rm $(DESTDIR)$(PREFIX)/include/easysock.h

View File

@@ -1,4 +1,3 @@
# Easysock
## An easy-to-use C socket library ## An easy-to-use C socket library
*Easysock* is a single-file, easy-to-use socket library, that can be used for network programming in C. It has no dependencies other than the C standard library. *Easysock* is a single-file, easy-to-use socket library, that can be used for network programming in C. It has no dependencies other than the C standard library.
@@ -16,7 +15,6 @@ There are a few optional targets, which enable specific functionality.
`debug - compile with 'debug' flag` `debug - compile with 'debug' flag`
`static - compile as statically linked library - NOT IMPLEMENTED` `static - compile as statically linked library - NOT IMPLEMENTED`
<br />
<br /> <br />
They can be used as follows: They can be used as follows:
@@ -28,7 +26,7 @@ The library can also be installed using the provided Mekfile.
``` ```
make install make install
``` ```
The `install` target supports two environment variables. `DESTDIR`, which specifies the installation directory, and `PREFIX` which specifies the installation prefix. They are used as follows: The `install` target supports two environment variables. `DESTDIR`, which specifies the installation directory, and `PREFIX` which specifies the installation prefix. They can be used as follows:
`DESTDIR=/home/user PREFIX=/usr/local make install` `DESTDIR=/home/user PREFIX=/usr/local make install`
@@ -43,5 +41,12 @@ To use the library, simply include the header file:
and link the library: and link the library:
`gcc example.c -o example -leasysock` `gcc example.c -o example -leasysock`
___ ___
### Troubleshooting
#### The library isn't loaded on Arch Linux, "cannot open shared object file: No such file or directory"
This is becacuse Arch Linux [doesn't include '/usr/local/lib'](https://libreddit.tiekoetter.com/r/archlinux/comments/ws9qty/why_is_usrlocallib_not_in_the_default_search_path/) in its library search path by default. To add it, create a file ending in '.conf' under `/etc/ld.so.conf.d/`, and add '/usr/local/lib' to it. Then, run `ldconfig` as root.
<br />
<br />
This file was written using [Dillinger](https://dillinger.io). This file was written using [Dillinger](https://dillinger.io).

Binary file not shown.

BIN
docs/man3/check_ip_ver.3.gz Normal file

Binary file not shown.

BIN
docs/man3/create_addr.3.gz Normal file

Binary file not shown.

BIN
docs/man3/create_local.3.gz Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
docs/man3/easysock.3.gz Normal file

Binary file not shown.

BIN
docs/man3/inet_to_int.3.gz Normal file

Binary file not shown.

BIN
docs/man3/int_to_inet.3.gz Normal file

Binary file not shown.

View File

@@ -1,5 +1,6 @@
#include "easysock.h" #include "easysock.h"
int create_socket(int network, char transport) { int create_socket(int network, char transport) {
int domain; int domain;
int type; int type;
@@ -25,7 +26,7 @@ int create_socket(int network, char transport) {
} }
void create_addr(int network, char* address, int port,struct sockaddr* dest) { int create_addr(int network, char* address, int port,struct sockaddr* dest) {
if (network == 4) { if (network == 4) {
struct sockaddr_in listen_address; struct sockaddr_in listen_address;
@@ -33,7 +34,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
listen_address.sin_port = htons(port); listen_address.sin_port = htons(port);
inet_pton(AF_INET,address,&listen_address.sin_addr); inet_pton(AF_INET,address,&listen_address.sin_addr);
memcpy(dest,&listen_address,sizeof(listen_address)); memcpy(dest,&listen_address,sizeof(listen_address));
return; return 0;
} else if (network == 6) { } else if (network == 6) {
struct sockaddr_in6 listen_ipv6; struct sockaddr_in6 listen_ipv6;
@@ -41,10 +42,10 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
listen_ipv6.sin6_port = htons(port); listen_ipv6.sin6_port = htons(port);
inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr); inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr);
memcpy(dest,&listen_ipv6,sizeof(listen_ipv6)); memcpy(dest,&listen_ipv6,sizeof(listen_ipv6));
return; return 0;
} else { } else {
exit(202); return -202;
} }
} }
@@ -52,7 +53,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) { int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
exit(errno); return (-1 * errno);
} }
create_addr(network,address,port,addr_struct); create_addr(network,address,port,addr_struct);
int addrlen; int addrlen;
@@ -61,7 +62,7 @@ int create_local (int network, char transport, char* address, int port,struct so
} else if (network == 6) { } else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else { } else {
exit(207); return -202;
} }
/* The value of addrlen should be the size of the 'sockaddr'. /* The value of addrlen should be the size of the 'sockaddr'.
@@ -70,7 +71,7 @@ int create_local (int network, char transport, char* address, int port,struct so
int i = bind (socket,addr_struct,(socklen_t)addrlen); int i = bind (socket,addr_struct,(socklen_t)addrlen);
if (i < 0) { if (i < 0) {
exit(errno); return (-1 * errno);
} }
return socket; return socket;
} }
@@ -93,7 +94,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
err_code = getaddrinfo(address,port_str,&hints,&results); err_code = getaddrinfo(address,port_str,&hints,&results);
if (err_code != 0) { if (err_code != 0) {
exit(err_code); return (-1 * err_code);
} }
remote_addr_struct = results->ai_addr; remote_addr_struct = results->ai_addr;
network = inet_to_int(results->ai_family); network = inet_to_int(results->ai_family);
@@ -103,7 +104,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int socket = create_socket(network,transport); int socket = create_socket(network,transport);
if (socket < 0) { if (socket < 0) {
exit(errno); return (-1 * errno);
} }
int addrlen; int addrlen;
@@ -112,7 +113,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
} else if (network == 6) { } else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else { } else {
exit(207); return (-202);
} }
/* The value of addrlen should be the size of the 'sockaddr'. /* The value of addrlen should be the size of the 'sockaddr'.
@@ -121,7 +122,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen); int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
if (i < 0) { if (i < 0) {
exit(errno); return (-1 * errno);
} }
return socket; return socket;
} }
@@ -144,7 +145,7 @@ int int_to_inet(int network) {
} else if (network == 6) { } else if (network == 6) {
return AF_INET6; return AF_INET6;
} else { } else {
exit(207); return -202;
} }
} }
@@ -154,7 +155,7 @@ int inet_to_int(int af_type) {
} else if (af_type == AF_INET6) { } else if (af_type == AF_INET6) {
return 6; return 6;
} else { } else {
exit(207); return -207;
} }
} }
@@ -164,6 +165,6 @@ int char_to_socktype(char transport) {
} else if (transport == 'U') { } else if (transport == 'U') {
return SOCK_DGRAM; return SOCK_DGRAM;
} else { } else {
exit(250); return -250;
} }
} }

View File

@@ -28,10 +28,10 @@ int create_socket(int network, char transport);
port is self-explanatory; port is self-explanatory;
and dest is a pointer to the sockaddr struct that will be filled in. and dest is a pointer to the sockaddr struct that will be filled in.
The function exits with error code 202 if the network parameter contained neither '4' The function returns with -202 if the network parameter contained neither '4'
nor '6'. */ nor '6'. */
void create_addr(int network, char* address, int port,struct sockaddr* dest); int create_addr(int network, char* address, int port,struct sockaddr* dest);
@@ -39,7 +39,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest);
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are _binds_ the addresses. It is used for local sockets (server sockets). Parameters are
same as above. same as above.
It prints the error returned by 'bind' if something went wrong, and exits with errno.*/ It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
@@ -48,7 +48,7 @@ int create_local (int network, char transport, char* address, int port,struct so
requested address. It is used for remote sockets (client sockets). The paramters are same requested address. It is used for remote sockets (client sockets). The paramters are same
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill. as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
It prints the error returned by 'connect' if something went wrong, and exits with errno.*/ If something goes wrong, this function returns with ( -1 * errno ). */
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct); int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
@@ -63,7 +63,7 @@ AF_INET6 respectively. */
int int_to_inet(int network); int int_to_inet(int network);
/* char_to_socktype - Takes a character that represents a transport-layer protocol /* char_to_socktype - Takes a character that represents a transport-layer protocol
(currently only supports 'T' for TCP or 'U' for UDP - exits with error code 250 if (currently only supports 'T' for TCP or 'U' for UDP - it returns -250 if
the given characters is neither of these) and return the appropriate SOCKTYPE value. */ the given characters is neither of these) and return the appropriate SOCKTYPE value. */
int char_to_socktype(char transport); int char_to_socktype(char transport);
@@ -72,4 +72,5 @@ int char_to_socktype(char transport);
and returns the appropriate int value. */ and returns the appropriate int value. */
int inet_to_int(int af_type); int inet_to_int(int af_type);
#endif #endif