|
| 1 | +/* |
| 2 | +Copyright (c) 2025 Roger Light <roger@atchoo.org> |
| 3 | +
|
| 4 | +All rights reserved. This program and the accompanying materials |
| 5 | +are made available under the terms of the Eclipse Public License 2.0 |
| 6 | +and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | +
|
| 8 | +The Eclipse Public License is available at |
| 9 | + https://www.eclipse.org/legal/epl-2.0/ |
| 10 | +and the Eclipse Distribution License is available at |
| 11 | + http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | +
|
| 13 | +SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 | +
|
| 15 | +Contributors: |
| 16 | + Roger Light - initial implementation and documentation. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <cstdio> |
| 20 | +#include <cstdint> |
| 21 | +#include <cstdlib> |
| 22 | +#include <cstring> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +static const uint8_t *packet_data = NULL; |
| 27 | +static int packet_data_pos = 0; |
| 28 | +static int packet_data_remaining = 0; |
| 29 | + |
| 30 | +extern "C" { |
| 31 | +#include "mosquitto_broker_internal.h" |
| 32 | + |
| 33 | +ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count) |
| 34 | +{ |
| 35 | + int res = count < packet_data_remaining?count:packet_data_remaining; |
| 36 | + memcpy(buf, &packet_data[packet_data_pos], res); |
| 37 | + packet_data_remaining -= res; |
| 38 | + return res; |
| 39 | +} |
| 40 | + |
| 41 | +int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len, uint16_t *remote_port) |
| 42 | +{ |
| 43 | + snprintf(buf, len, "localhost"); |
| 44 | + *remote_port = 1883; |
| 45 | + return MOSQ_ERR_SUCCESS; |
| 46 | +} |
| 47 | + |
| 48 | +int http__context_init(struct mosquitto *context) |
| 49 | +{ |
| 50 | + context->transport = mosq_t_http; |
| 51 | + |
| 52 | + return MOSQ_ERR_SUCCESS; |
| 53 | +} |
| 54 | + |
| 55 | +int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) |
| 56 | +{ |
| 57 | + return MOSQ_ERR_SUCCESS; |
| 58 | +} |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
| 64 | +{ |
| 65 | + struct mosquitto context{}; |
| 66 | + struct mosquitto__listener listener{}; |
| 67 | + |
| 68 | + packet_data = data; |
| 69 | + packet_data_pos = 0; |
| 70 | + packet_data_remaining = size; |
| 71 | + |
| 72 | + context.listener = &listener; |
| 73 | + context.proxy.cmd = -1; |
| 74 | + context.transport = mosq_t_proxy_v1; |
| 75 | + |
| 76 | + while(packet_data_remaining > 0 && context.transport != mosq_t_tcp){ |
| 77 | + int rc = proxy_v1__read(&context); |
| 78 | + if(rc){ |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + free(context.address); |
| 83 | + free(context.proxy.buf); |
| 84 | + free(context.proxy.tls_version); |
| 85 | + free(context.proxy.cipher); |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments