box-o-sand/socky.c

49 lines
1.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
const char *SOCKNAME = "/tmp/socky";
int sfd;
struct sockaddr_un addr;
int main(int argc, char *argv[])
{
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) {
fprintf(stderr, "FAILED TO ALLOCATE SOCKET\n");
goto error;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
fprintf(stderr, "FAILED TO BIND SOCKET TO ADDRESS\n");
goto error;
}
if (listen(sfd, 128) == -1) {
fprintf(stderr, "FAILED TO LISTEN ON ADDRESS\n");
goto error;
}
for (;;) {
fprintf(stdout, "ATTEMPTING TO WAIT FOR REQUEST ON %s\n", SOCKNAME);
if (accept(sfd, NULL, NULL) == -1) {
fprintf(stderr, "FAILED TO ACCEPT REQUEST ON ADDRESS\n");
goto error;
}
}
return 0;
error:
return 1;
}