Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@feelfreelinux
Last active February 10, 2017 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save feelfreelinux/883950670b7b95b3491c05cbda0f463e to your computer and use it in GitHub Desktop.
Save feelfreelinux/883950670b7b95b3491c05cbda0f463e to your computer and use it in GitHub Desktop.
Crappy IRC bouncer, esp32 test
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <errno.h>
using namespace std;
int ircSock, bncSock;
bool connected = false;
err_t err;
esp_err_t event_handler(void *ctx, system_event_t *event)
{
if(event->event_id == SYSTEM_EVENT_STA_CONNECTED){
connected = true;
printf("connected!\n");
}
return ESP_OK;
}
// Sends IRC command
int sendCMD( string command, int socket ){
return( send( socket, (void*)command.c_str(), command.length(), 0 ) );
}
// Tests, is data available on socket
int isData( int socket ){
int count;
int res = ioctl( socket, FIONREAD, &count );
return count;
}
void client_task( void *pvParameter )
{
int clientSock = 0;
bool clientConnected = false;
while( !connected );
vTaskDelay( 1000/ portTICK_RATE_MS );
printf( "Starting IRC client...\n" );
struct sockaddr_in ircAddr, bncAddr, clientAddr;
char buffer[500];
// TODO Replace this IP with gethostbyname() DNS
ircSock = socket( AF_INET, SOCK_STREAM, 0 );
bncSock = socket( AF_INET, SOCK_STREAM, 0 );
ircAddr.sin_addr.s_addr = inet_addr("149.56.134.238");
ircAddr.sin_family = AF_INET;
ircAddr.sin_port = htons( 6667 );
bncAddr.sin_addr.s_addr = INADDR_ANY;
bncAddr.sin_family = AF_INET;
bncAddr.sin_port = htons( 1337 );
bzero( &(bncAddr.sin_zero), 8 ); // empty it
int clientSCK = 0;
bind( bncSock, (sockaddr *)&bncAddr, sizeof(struct sockaddr) );
listen( bncSock, 1 );
// We don't want to block server socket
fcntl( bncSock, F_SETFL, O_NONBLOCK );
connect( ircSock, (sockaddr*)&ircAddr, sizeof( ircAddr ) );
// empty buffer, and send needed auth messages
bzero( buffer, 500 );
sendCMD( "USER esp32-test 8 * ESP32 test\n", ircSock );
sendCMD( "NICK esp32-test\n", ircSock );
vTaskDelay( 300/ portTICK_RATE_MS );
while(1){
// Client socket have data.
if(isData(ircSock)){
bzero( buffer, 500 );
recv( ircSock, buffer, sizeof(buffer), 0 );
string line = buffer;
// handle PING command
if(line.substr(0, 4)=="PING"){
line[1] ='O';
sendCMD( line, ircSock );
}
if( clientConnected ){
sendCMD( line, clientSCK );
//printf("Sending message to client, ID %i \n", clientSCK);
}
// TODO Add logging, basic bouncer functionality
}
socklen_t socklen = sizeof(struct sockaddr_in);
if( (clientSock = accept( bncSock, (sockaddr *)&clientAddr, &socklen ) ) > 0){
clientConnected = true;
clientSCK = clientSock;
printf("Client connected :) %i \n", clientSCK);
sendCMD( "HI! \n", clientSCK );
}
if(clientConnected){
if(isData(clientSCK)){
bzero( buffer, 500 );
recv( clientSCK, buffer, sizeof(buffer), 0 );
string line = buffer;
sendCMD( line, ircSock );
//printf("Got message from client, sending it to freenode. Whoo!\n");
}
//if(clientSock<1){
// clientConnected = false;
//}
}
}
}
extern "C" void app_main()
{
nvs_flash_init();
system_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config;
sprintf((char*)sta_config.sta.ssid, "SSID");
sprintf((char*)sta_config.sta.password, "PASS");
sta_config.sta.bssid_set = false;
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
xTaskCreate(&client_task, "client_task", 2048, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment