Compare commits
15 Commits
master
...
b2dc9c794f
Author | SHA1 | Date | |
---|---|---|---|
b2dc9c794f | |||
2928a673e5 | |||
d3f62f5e8d | |||
3c10661b4e | |||
6fc6262d98 | |||
2e93a4a1ef | |||
465b9f8a00 | |||
8f46a3518a | |||
a0e4bc6814 | |||
aa954570aa | |||
b91a2e5996 | |||
b5dc08e072 | |||
d9ca0819a1 | |||
5b97a27a8e | |||
590b2ffbc5 |
5
Makefile
5
Makefile
@@ -40,11 +40,9 @@ install: $(LIB_FILE)
|
||||
install -d $(DESTDIR)$(PREFIX)/include
|
||||
install -m 644 easysock.h $(DESTDIR)$(PREFIX)/include
|
||||
|
||||
install -m 644 docs/man3/*.gz /usr/share/man/man3/
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm easysock.o
|
||||
rm *.o
|
||||
rm $(LIB_FILE)
|
||||
|
||||
|
||||
@@ -52,4 +50,3 @@ clean:
|
||||
uninstall:
|
||||
rm $(DESTDIR)$(PREFIX)/lib/$(LIB_FILE)
|
||||
rm $(DESTDIR)$(PREFIX)/include/easysock.h
|
||||
|
||||
|
13
README.md
13
README.md
@@ -1,3 +1,4 @@
|
||||
# Easysock
|
||||
## 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.
|
||||
@@ -15,6 +16,7 @@ There are a few optional targets, which enable specific functionality.
|
||||
`debug - compile with 'debug' flag`
|
||||
`static - compile as statically linked library - NOT IMPLEMENTED`
|
||||
|
||||
<br />
|
||||
<br />
|
||||
|
||||
They can be used as follows:
|
||||
@@ -26,7 +28,7 @@ The library can also be installed using the provided Mekfile.
|
||||
```
|
||||
make install
|
||||
```
|
||||
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:
|
||||
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:
|
||||
|
||||
`DESTDIR=/home/user PREFIX=/usr/local make install`
|
||||
|
||||
@@ -39,14 +41,7 @@ To use the library, simply include the header file:
|
||||
...
|
||||
```
|
||||
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).
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
29
easysock.c
29
easysock.c
@@ -1,6 +1,5 @@
|
||||
#include "easysock.h"
|
||||
|
||||
|
||||
int create_socket(int network, char transport) {
|
||||
int domain;
|
||||
int type;
|
||||
@@ -26,7 +25,7 @@ int create_socket(int network, char transport) {
|
||||
}
|
||||
|
||||
|
||||
int create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
||||
void create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
||||
if (network == 4) {
|
||||
struct sockaddr_in listen_address;
|
||||
|
||||
@@ -34,7 +33,7 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
||||
listen_address.sin_port = htons(port);
|
||||
inet_pton(AF_INET,address,&listen_address.sin_addr);
|
||||
memcpy(dest,&listen_address,sizeof(listen_address));
|
||||
return 0;
|
||||
return;
|
||||
|
||||
} else if (network == 6) {
|
||||
struct sockaddr_in6 listen_ipv6;
|
||||
@@ -42,10 +41,10 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
||||
listen_ipv6.sin6_port = htons(port);
|
||||
inet_pton(AF_INET6,address,&listen_ipv6.sin6_addr);
|
||||
memcpy(dest,&listen_ipv6,sizeof(listen_ipv6));
|
||||
return 0;
|
||||
return;
|
||||
|
||||
} else {
|
||||
return -202;
|
||||
exit(202);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,7 +52,7 @@ int 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 socket = create_socket(network,transport);
|
||||
if (socket < 0) {
|
||||
return (-1 * errno);
|
||||
exit(errno);
|
||||
}
|
||||
create_addr(network,address,port,addr_struct);
|
||||
int addrlen;
|
||||
@@ -62,7 +61,7 @@ int create_local (int network, char transport, char* address, int port,struct so
|
||||
} else if (network == 6) {
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
return -202;
|
||||
exit(207);
|
||||
}
|
||||
|
||||
/* The value of addrlen should be the size of the 'sockaddr'.
|
||||
@@ -71,7 +70,7 @@ int create_local (int network, char transport, char* address, int port,struct so
|
||||
|
||||
int i = bind (socket,addr_struct,(socklen_t)addrlen);
|
||||
if (i < 0) {
|
||||
return (-1 * errno);
|
||||
exit(errno);
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
@@ -94,7 +93,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
|
||||
|
||||
err_code = getaddrinfo(address,port_str,&hints,&results);
|
||||
if (err_code != 0) {
|
||||
return (-1 * err_code);
|
||||
exit(err_code);
|
||||
}
|
||||
remote_addr_struct = results->ai_addr;
|
||||
network = inet_to_int(results->ai_family);
|
||||
@@ -104,7 +103,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
|
||||
|
||||
int socket = create_socket(network,transport);
|
||||
if (socket < 0) {
|
||||
return (-1 * errno);
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
int addrlen;
|
||||
@@ -113,7 +112,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
|
||||
} else if (network == 6) {
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
return (-202);
|
||||
exit(207);
|
||||
}
|
||||
|
||||
/* The value of addrlen should be the size of the 'sockaddr'.
|
||||
@@ -122,7 +121,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
|
||||
|
||||
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
|
||||
if (i < 0) {
|
||||
return (-1 * errno);
|
||||
exit(errno);
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
@@ -145,7 +144,7 @@ int int_to_inet(int network) {
|
||||
} else if (network == 6) {
|
||||
return AF_INET6;
|
||||
} else {
|
||||
return -202;
|
||||
exit(207);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +154,7 @@ int inet_to_int(int af_type) {
|
||||
} else if (af_type == AF_INET6) {
|
||||
return 6;
|
||||
} else {
|
||||
return -207;
|
||||
exit(207);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +164,6 @@ int char_to_socktype(char transport) {
|
||||
} else if (transport == 'U') {
|
||||
return SOCK_DGRAM;
|
||||
} else {
|
||||
return -250;
|
||||
exit(250);
|
||||
}
|
||||
}
|
||||
|
11
easysock.h
11
easysock.h
@@ -28,10 +28,10 @@ int create_socket(int network, char transport);
|
||||
port is self-explanatory;
|
||||
and dest is a pointer to the sockaddr struct that will be filled in.
|
||||
|
||||
The function returns with -202 if the network parameter contained neither '4'
|
||||
The function exits with error code 202 if the network parameter contained neither '4'
|
||||
nor '6'. */
|
||||
|
||||
int create_addr(int network, char* address, int port,struct sockaddr* dest);
|
||||
void create_addr(int network, char* address, int port,struct sockaddr* dest);
|
||||
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest);
|
||||
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are
|
||||
same as above.
|
||||
|
||||
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
|
||||
It prints the error returned by 'bind' if something went wrong, and exits with errno.*/
|
||||
|
||||
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
|
||||
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
|
||||
|
||||
If something goes wrong, this function returns with ( -1 * errno ). */
|
||||
It prints the error returned by 'connect' if something went wrong, and exits with errno.*/
|
||||
|
||||
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);
|
||||
|
||||
/* char_to_socktype - Takes a character that represents a transport-layer protocol
|
||||
(currently only supports 'T' for TCP or 'U' for UDP - it returns -250 if
|
||||
(currently only supports 'T' for TCP or 'U' for UDP - exits with error code 250 if
|
||||
the given characters is neither of these) and return the appropriate SOCKTYPE value. */
|
||||
|
||||
int char_to_socktype(char transport);
|
||||
@@ -72,5 +72,4 @@ int char_to_socktype(char transport);
|
||||
and returns the appropriate int value. */
|
||||
int inet_to_int(int af_type);
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user