patty/examples/listen.c

114 lines
2.6 KiB
C
Raw Permalink Normal View History

2020-06-30 23:20:12 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#include <patty/ax25.h>
extern char **environ;
static void usage(int argc, char **argv, const char *message, ...) {
if (message != NULL) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
fprintf(stderr, "usage: %s /var/run/patty/patty.sock localcall\n", argv[0]);
2020-06-30 23:20:12 -04:00
exit(1);
}
int main(int argc, char **argv) {
patty_client *client;
2020-06-30 23:20:12 -04:00
patty_ax25_addr addr,
peer;
int local,
remote;
2020-06-30 23:20:12 -04:00
if (argc < 2) {
2020-06-30 23:20:12 -04:00
usage(argc, argv, "No patty socket provided");
} else if (argc < 3) {
usage(argc, argv, "No local callsign provided");
} else if (argc > 3) {
usage(argc, argv, "Too many arguments provided");
2020-06-30 23:20:12 -04:00
}
patty_ax25_pton(argv[2], &addr);
2020-06-30 23:20:12 -04:00
if ((client = patty_client_new(argv[1])) == NULL) {
2020-06-30 23:20:12 -04:00
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "patty_client_new()", argv[1], strerror(errno));
2020-06-30 23:20:12 -04:00
goto error_client_new;
2020-06-30 23:20:12 -04:00
}
if ((local = patty_client_socket(client, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_STREAM)) < 0) {
2020-06-30 23:20:12 -04:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_socket()", strerror(errno));
2020-06-30 23:20:12 -04:00
goto error_client_socket;
2020-06-30 23:20:12 -04:00
}
if (patty_client_bind(client, local, &addr) < 0) {
2020-06-30 23:20:12 -04:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_bind()", strerror(errno));
2020-06-30 23:20:12 -04:00
goto error_client_bind;
2020-06-30 23:20:12 -04:00
}
if (patty_client_listen(client, local) < 0) {
2020-06-30 23:20:12 -04:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_listen()", strerror(errno));
2020-06-30 23:20:12 -04:00
goto error_client_listen;
2020-06-30 23:20:12 -04:00
}
if ((remote = patty_client_accept(client, local, &peer)) < 0) {
2020-06-30 23:20:12 -04:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_client_accept()", strerror(errno));
2020-06-30 23:20:12 -04:00
goto error_client_accept;
2020-06-30 23:20:12 -04:00
}
if (write(remote, "hello world\n", 12) < 0) {
2020-06-30 23:20:12 -04:00
goto error_write;
}
sleep(1);
patty_client_close(client, remote);
patty_client_close(client, local);
2020-06-30 23:20:12 -04:00
patty_client_destroy(client);
2020-06-30 23:20:12 -04:00
return 0;
error_write:
(void)patty_client_close(client, remote);
2020-06-30 23:20:12 -04:00
error_client_accept:
error_client_listen:
error_client_bind:
(void)patty_client_close(client, local);
2020-06-30 23:20:12 -04:00
error_client_socket:
patty_client_destroy(client);
2020-06-30 23:20:12 -04:00
error_client_new:
2020-06-30 23:20:12 -04:00
return 1;
}