APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
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
/*
Copyright (c) 2016-2019 Roger Light <roger@atchoo.org>
 
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
 
The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.
 
Contributors:
   Roger Light - initial implementation and documentation.
*/
 
#include "config.h"
 
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "mosquitto_broker.h"
#include "memory_mosq.h"
 
#ifdef WITH_TLS
#  include <openssl/ssl.h>
#endif
 
const char *mosquitto_client_address(const struct mosquitto *client)
{
    return client->address;
}
 
 
bool mosquitto_client_clean_session(const struct mosquitto *client)
{
    return client->clean_start;
}
 
 
const char *mosquitto_client_id(const struct mosquitto *client)
{
    return client->id;
}
 
 
int mosquitto_client_keepalive(const struct mosquitto *client)
{
    return client->keepalive;
}
 
 
void *mosquitto_client_certificate(const struct mosquitto *client)
{
#ifdef WITH_TLS
    if(client->ssl){
        return SSL_get_peer_certificate(client->ssl);
    }else{
        return NULL;
    }
#else
    return NULL;
#endif
}
 
 
int mosquitto_client_protocol(const struct mosquitto *client)
{
    return client->protocol;
}
 
 
int mosquitto_client_sub_count(const struct mosquitto *client)
{
    return client->sub_count;
}
 
 
const char *mosquitto_client_username(const struct mosquitto *context)
{
#ifdef WITH_BRIDGE
    if(context->bridge){
        return context->bridge->local_username;
    }else
#endif
    {
        return context->username;
    }
}
 
int mosquitto_set_username(struct mosquitto *client, const char *username)
{
    char *u_dup;
    char *old;
    int rc;
 
    if(!client) return MOSQ_ERR_INVAL;
 
    if(username){
        u_dup = mosquitto__strdup(username);
        if(!u_dup) return MOSQ_ERR_NOMEM;
    }else{
        u_dup = NULL;
    }
 
    old = client->username;
    client->username = u_dup;
 
    rc = acl__find_acls(mosquitto__get_db(), client);
    if(rc){
        client->username = old;
        mosquitto__free(u_dup);
        return rc;
    }else{
        mosquitto__free(old);
        return MOSQ_ERR_SUCCESS;
    }
}