~gpanders/iboot

ref: 5226a15e0116ff00092e979429413db30653c58d iboot/netpower.c -rw-r--r-- 2.3 KiB
5226a15e — Greg Anders Replace magic strings with constants 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include "config.h"

/* 4 extra bytes for the two escape characters (), the action character, and
 * the carriage return (\r) */
static const size_t BUF_SIZE = sizeof(PASSWORD) + 4;

static const char ACTION_ON[]    = "on";
static const char ACTION_OFF[]   = "off";
static const char ACTION_CYCLE[] = "cycle";
static const char ACTION_QUERY[] = "query";

enum action {
	ON      = 'n',
	OFF     = 'f',
	CYCLE   = 'c',
	QUERY   = 'q',
};

int create_socket()
{
	int sockfd;
	struct addrinfo *result, *rp;
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM,
		.ai_flags = 0,
		.ai_protocol = 0,
	};

	int s = getaddrinfo(ADDRESS, PORT, &hints, &result);
	if (s != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
		exit(EXIT_FAILURE);
	}

	for (rp = result; rp != NULL; rp = rp->ai_next) {
		sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (sockfd == -1) {
			continue;
		}

		if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) {
			break;
		}

		close(sockfd);
	}

	if (rp == NULL) {
		fprintf(stderr, "Could not connect\n");
		exit(EXIT_FAILURE);
	}

	freeaddrinfo(result);

	return sockfd;
}

void send_command(int sockfd, enum action action)
{
	char buf[BUF_SIZE];
	sprintf(buf, "%s%c\r", PASSWORD, action);
	if (write(sockfd, buf, BUF_SIZE) != BUF_SIZE) {
		fprintf(stderr, "partial/failed write\n");
		exit(EXIT_FAILURE);
	}

	memset(buf, 0, BUF_SIZE);
	ssize_t nread = read(sockfd, buf, BUF_SIZE);
	if (nread == -1) {
		perror("read");
		exit(EXIT_FAILURE);
	}

	printf("%s\n", buf);
}

enum action get_action(const char *action)
{
	if (!strcmp(action, ACTION_ON)) {
		return ON;
	}

	if (!strcmp(action, ACTION_OFF)) {
		return OFF;
	}

	if (!strcmp(action, ACTION_CYCLE)) {
		return CYCLE;
	}

	if (!strcmp(action, ACTION_QUERY)) {
		return QUERY;
	}

	return 0;
}

int main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <on|off|cycle|query>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	char action = get_action(argv[1]);
	if (!action) {
		fprintf(stderr, "Invalid action: %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	int sockfd = create_socket();
	send_command(sockfd, action);

	exit(EXIT_SUCCESS);
}