Good day.
I am currently writing a plugin that will be the agent for controlling the TeamSpeak application. It uses the Plugin SDK API 22
Unfortunately, my job was to try to run a socket in the middle of the plugin. It looks more like this:
Code:
int ts3plugin_init() {
char appPath[PATH_BUFSIZE];
char resourcesPath[PATH_BUFSIZE];
char configPath[PATH_BUFSIZE];
char pluginPath[PATH_BUFSIZE];
/*
Socket
*/
int socket_desc;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("ERROR: Could not create socket\n");
} else {
printf("Socket create succesfull\n");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 2322 );
//Bind
if (bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) == -1) {
puts("ERROR: bind failed\n");
} else {
puts("bind done\n");
}
/*
End Socket
*/
/* Your plugin init code here */
printf("PLUGIN: init\n");
/* Example on how to query application, resources and configuration paths from client */
/* Note: Console client returns empty string for app and resources path */
ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE, pluginID);
printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
return 0; /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
/* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
* the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
* For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
}
Of course all the required libraries I've defined earlier. After trying to run the plugin it gets the following messages:
Code:
Socket create succesfull
ERROR: bind failed
Of course I tried to fire an identical socket as a separate application and it worked without problems
Here is the code application
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 3490
#define BACKLOG 10
int main() {
struct sockaddr_in server;
struct sockaddr_in dest;
int status,socket_fd, client_fd,num;
socklen_t size;
char buffer[10241];
char *buff;
// memset(buffer,0,sizeof(buffer));
int yes =1;
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket failure!!\n");
exit(1);
}
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
memset(&server, 0, sizeof(server));
memset(&dest,0,sizeof(dest));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1) { //sizeof(struct sockaddr)
fprintf(stderr, "Binding Failure\n");
} else {
fprintf(stderr, "Binding succesfull\n");
}
}
I check and the port is not blocked in any way, or user by other applications
Why I can't bind ports for plugin socket?
//EDIT
Sory i have bug in my code:
It is new code with support thread:
Code:
void *socket_start();
int ts3plugin_init() {
char appPath[PATH_BUFSIZE];
char resourcesPath[PATH_BUFSIZE];
char configPath[PATH_BUFSIZE];
char pluginPath[PATH_BUFSIZE];
pthread_t thread1;
pthread_create( &thread1, NULL, &socket_start, NULL);
unsigned int error;
char* version;
error = ts3client_getClientLibVersion(&version);
if(error != ERROR_ok) {
printf("Error querying clientlib version: %d\n", error);
return;
}
printf("Client library version: %s\n", version); /* Print version */
ts3client_freeMemory(version); /* Release string */
ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE, pluginID);
printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
return 0; /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
/* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
* the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
* For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
}
void *socket_start(){
/*
Socket
*/
struct sockaddr_in server, client;
struct sockaddr_in dest;
int status,socket_fd, client_fd,num, read_size;
socklen_t size;
char client_message[2000];
// memset(buffer,0,sizeof(buffer));
int yes =1;
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket failure!!\n");
exit(1);
}
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
memset(&server, 0, sizeof(server));
memset(&dest,0,sizeof(dest));
server.sin_family = AF_INET;
server.sin_port = htons(3490);
server.sin_addr.s_addr = INADDR_ANY;
if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1) { //sizeof(struct sockaddr)
fprintf(stderr, "Binding Failure\n");
return 1;
} else {
fprintf(stderr, "Binding succesfull\n");
}
//Listen
listen(socket_fd , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
int c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
int client_sock = accept(socket_fd, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
}