[asterisk-dev] Need help in ast_netsock

mawali at news.icns.com mawali at news.icns.com
Sun Jun 4 22:23:55 MST 2006


Hi
I have been trying to get netsock working, but have noot been able to. I 
have attached the app_tester.c and my callback is socket_read (just like 
chan_iax2), but in my case it never gets to socket_read. I make it bind 
to port 9999 and send data to that port but it never goes to callback. I 
have attached the source file and this is what I do

I bind using


   if (defaultsockfd < 0) {
     if (!(ns = ast_netsock_bind(netsock, io, "0.0.0.0", portno, tos, 
socket_read, NULL))) {
       ast_log(LOG_ERROR, "Unable to create network socket: %s\n", 
strerror(errno));
     } else {
       if (option_verbose > 1)
         ast_verbose(VERBOSE_PREFIX_2 "Binding SVS to default address 
0.0.0.0:%d\n", portno);
       defaultsockfd = ast_netsock_sockfd(ns);
       ast_netsock_unref(ns);
     }

And netstat shows me that it is listening on 9999

Here is my callback
static int socket_read(int *id, int fd, short events, void *cbdata)
{
   struct sockaddr_in sin;
   int res;
   unsigned char buf[4096];
   ast_log(LOG_WARNING, "at socket_read \n");
   socklen_t len = sizeof(sin);
   res = recvfrom(fd, buf, sizeof(buf), 0,(struct sockaddr *) &sin, &len);
   return res;

}

It should work, but what am I missing?

Regards
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/sched.h"
#include "asterisk/options.h" /* for verbose vars/consts */
#include "asterisk/io.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/astobj.h"
#include "asterisk/netsock.h"
#include "asterisk/config.h"

#define MSVS_DEFAULT_PORTNO 9999

static char *tdesc = "Example App";

static char *app = "tester";

static char *synopsis = "Junk";

static  struct io_context *io;
static  struct sched_context *sched;

static struct ast_netsock_list *netsock;
static int defaultsockfd = -1;
static pthread_t netthreadid = AST_PTHREADT_NULL;

static int tos = 0;


static char *descrip = 
  "  Just junk \n";

/* fancy module.h macros for counting who's using this module */
STANDARD_LOCAL_USER;
LOCAL_USER_DECL;

static int hello_exec(struct ast_channel *chan, void *data)
{
  struct localuser *u;

  LOCAL_USER_ADD(u);

  if (option_verbose > 2)
    ast_verbose(VERBOSE_PREFIX_3 "Hello Kitty!\n");

  LOCAL_USER_REMOVE(u);
  return 0;
}


static int socket_read(int *id, int fd, short events, void *cbdata)
{
  struct sockaddr_in sin;
  int res;
  unsigned char buf[4096];
  ast_log(LOG_WARNING, "at socket_read \n");
  socklen_t len = sizeof(sin);
  res = recvfrom(fd, buf, sizeof(buf), 0,(struct sockaddr *) &sin, &len);
  return res;

}

/*--- set_config: Load configuration */
static int set_config(char *config_file, int reload)
{
  struct ast_config *cfg;
  int portno = MSVS_DEFAULT_PORTNO;
  struct ast_netsock *ns;
  
  cfg = ast_config_load(config_file);
  
  if (!cfg) {
    ast_log(LOG_ERROR, "Unable to load config %s\n", config_file);
    return -1;
  }
  
  if (defaultsockfd < 0) {
    if (!(ns = ast_netsock_bind(netsock, io, "0.0.0.0", portno, tos, socket_read, NULL))) {
      ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
    } else {
      if (option_verbose > 1)
	ast_verbose(VERBOSE_PREFIX_2 "Binding SVS to default address 0.0.0.0:%d\n", portno);
      defaultsockfd = ast_netsock_sockfd(ns);
      ast_netsock_unref(ns);
    }
  }
  ast_config_destroy(cfg);
}

static void *network_thread(void *ignore)
{
ast_log(LOG_WARNING, "at network thread\n");
for(;;) {
usleep(10);
}
ast_log(LOG_WARNING, "exit network thread\n");
return NULL;
}


static int start_network_thread(void)
{
        return ast_pthread_create(&netthreadid, NULL, network_thread, NULL);
}

int load_module(void)
{
  int res;
  char *config = "netsvs.conf";
  sched = sched_context_create();


  ast_log(LOG_WARNING, "creating context\n");
  if (!sched) {
    ast_log(LOG_WARNING, "Unable to create schedule context\n");
  }
  
  io = io_context_create();
  if (!io) {
    ast_log(LOG_WARNING, "Unable to create I/O context\n");
  }
  
  netsock = ast_netsock_list_alloc();
  if (!netsock) {
    ast_log(LOG_ERROR, "Could not allocate netsock list.\n");
    return -1;
  }
  ast_netsock_init(netsock);

  set_config(config, 0);

        res = start_network_thread();
        if (!res) {
                if (option_verbose > 1)
                        ast_verbose(VERBOSE_PREFIX_2 "SVC Ready and Listening\n");
        } else {
                ast_log(LOG_ERROR, "Unable to start network thread\n");
                ast_netsock_release(netsock);
        }


    ast_verbose(VERBOSE_PREFIX_3 "Registering \n");
  res = ast_register_application(app, hello_exec, synopsis, descrip);
  return res;
}

int unload_module(void)
{
  int res;
if (netthreadid != AST_PTHREADT_NULL) {
                pthread_cancel(netthreadid);
                pthread_join(netthreadid, NULL);
        }

  ast_netsock_release(netsock);
  res = ast_unregister_application(app);
  /* if there are any channels still using this module, send
   * them soft hangup signals. */
  STANDARD_HANGUP_LOCALUSERS;
  return res;
}

char *description(void)
{
  return tdesc;
}

int usecount(void)
{
  int res;
  STANDARD_USECOUNT(res);
  return res;
}

char *key(void)
{
  return ASTERISK_GPL_KEY;
}



More information about the asterisk-dev mailing list