summaryrefslogtreecommitdiff
path: root/xmpp.c
blob: e1ab985072fa0ec64b50e29374506cbd1caeaf4a (plain)
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


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>


/*
 * Get the domainpart of the Jabber ID (JID).
 * See https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details.
 */
char *domainpart(char *jid)
{
	int start = 0;           // inclusive
	int stop = strlen(jid);  // exclusive

	for(int i=0; i<strlen(jid); i++) {
		if (jid[i] == '/') {
			stop = i;
			break;
		}
	}

	for(int i=0; i<strlen(jid); i++) {
		if (jid[i] == '@') {
			start = i + 1;
			break;
		}
	}

	char *retval = (char *)malloc((stop-start+1) * sizeof(char));
	memcpy(retval, jid+start, (stop-start) * sizeof(char));
	retval[stop] = '\0';

	return retval;
}

/*
 * Return the preferred struct addrinfo * or NULL
 *
 * This handles DNS resolution. It returns the first valid addrinfo which is
 * returned by getaddrinfo. Mind that the addrinfo could use IPv6 instead of
 * IPv4.
 */
struct addrinfo *get_addrinfo(char *domain)
{
	struct addrinfo hints;
	struct addrinfo *servinfo;
	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	if (getaddrinfo(domain, "xmpp-client", &hints, &servinfo) != 0) {
		printf("Failed to resolve hostname '%s'.\n", domain);
		return NULL;
	}
	struct addrinfo *p;
	for(p=servinfo; p!=NULL; p=p->ai_next) {
		if (p->ai_family == AF_INET) {
			printf("an IPv4!\n");  // TODO

		} else if (p->ai_family == AF_INET6) {
			printf("an IPv6!\n");  // TODO
		} else {
			printf("Unknown addrinfo address type.\n");
		}
	}
	return NULL;
}

/*
 * Initialize the network connection to the XMPP server
 *
 * TODO: Error handling is missing.
 */
void xmpp_connect(void)
{
	printf("net_init()\n");

	char* user_str = getenv("LIMOX_USER");
	char* pwd_str = getenv("LIMOX_PWD");
	char *domain = domainpart(user_str);
	printf("Trying to connect as '%s' with '%s'.\n", user_str, pwd_str);
	printf("Domainpart is '%s'.\n", domain);

	get_addrinfo(domain);
}