diff options
author | Ben Brown <ben.brown@codethink.co.uk> | 2013-11-19 14:42:37 +0000 |
---|---|---|
committer | Ben Brown <ben.brown@codethink.co.uk> | 2013-11-20 12:48:25 +0000 |
commit | 9f237e103a96a95bf294c06f7774323e8cfe761b (patch) | |
tree | 201e57941f4820cd0a6a4774e468e56a2e394339 | |
parent | 0037d10f2e7f1298b376ff4819dc8d7bf19f2eb9 (diff) | |
download | tbdiff-9f237e103a96a95bf294c06f7774323e8cfe761b.tar.gz |
Added C program which creates sockets
-rw-r--r-- | yarns/sockbind.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/yarns/sockbind.c b/yarns/sockbind.c new file mode 100644 index 0000000..2f656ba --- /dev/null +++ b/yarns/sockbind.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stddef.h> +#include <string.h> +#include <stdlib.h> +#include <sys/socket.h> +#include <sys/un.h> + +/* + * There is currently no command within Busybox or otherwise to create named + * UNIX sockets; this program is used to compensate for that. + */ + +int main(int argc, char *argv[]) +{ + int sfd, len; + struct sockaddr_un sock = { .sun_family = AF_UNIX }; + + if (argc != 2) { + fprintf(stderr, "Usage: %s PATH\n", argv[0]); + return EXIT_FAILURE; + } + + len = sizeof(sock) - offsetof(struct sockaddr_un, sun_path); + strncpy(sock.sun_path, argv[1], len); + + if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + return EXIT_FAILURE; + } + + if (bind(sfd, (struct sockaddr*)&sock, sizeof(sock)) == -1) { + perror("bind"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} |