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
|
/*
* LimoX - The Linux on mobile XMPP chat client
* Copyright (C) 2022 xengineering
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
/*
* Get the domainpart of the Jabber ID (JID)
*
* See https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details.
*/
char *get_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.
*
* FIXME this just takes the first received IPv4 address (by far not compliant
* to RFC 6120!)
* FIXME This function contains a memory leak! Free the linked list servinfo!
*/
struct addrinfo *get_addrinfo(char *domain)
{
struct addrinfo hints;
struct addrinfo *servinfo;
void *addr;
char ipstr[INET6_ADDRSTRLEN];
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) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf("Got compatible IPv4 address %s from DNS.\n", ipstr);
return p;
} else if (p->ai_family == AF_INET6) {
printf("Ignored IPv6 DNS entry which is not supported.\n");
} else {
printf("Ignored unknown addrinfo address type.\n");
}
}
printf("No suitable IP address found via DNS!\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 *jid = getenv("LIMOX_USER");
char *pwd = getenv("LIMOX_PWD");
printf("Trying to connect as '%s' with '%s'.\n", jid, pwd);
char *domain = get_domainpart(jid);
printf("Domainpart is '%s'.\n", domain);
get_addrinfo(domain);
}
|