udp.c

来自「libosip2-3版本的osip源代码」· C语言 代码 · 共 1,949 行 · 第 1/5 页

C
1,949
字号
		    OSIP_TRACE (osip_trace				(__FILE__, __LINE__, OSIP_INFO1, NULL,				 "ACK restransmission sent.\r\n")); 		  }	      }	    osip_event_free (evt);	    return;	  }	if (jc!=NULL)	  {	    /* match answer with dialog */	    osip_dialog_t *dlg;	    osip_transaction_t *last_tr;	    int i;	    	    /* we match an existing dialog: send a retransmission of ACK */	    i = osip_dialog_init_as_uac (&dlg, evt->sip);	    if (i != 0 || dlg==NULL)	      {		OSIP_TRACE (osip_trace			    (__FILE__, __LINE__, OSIP_ERROR, NULL,			     "Cannot build dialog for 200ok.\r\n"));		osip_event_free (evt);		return;	      }	    OSIP_TRACE (osip_trace			(__FILE__, __LINE__, OSIP_INFO1, NULL,			 "sending ACK for 2xx out of transaction.\r\n"));	    {	      char *transport;	      osip_message_t *ack;	      osip_message_t *bye;	      transport = NULL;	      transport = _eXosip_transport_protocol (evt->sip);	      if (transport == NULL)		i = _eXosip_build_request_within_dialog (&ack, "ACK", dlg, "UDP");	      else		i = _eXosip_build_request_within_dialog (&ack, "ACK", dlg, transport);	      if (i != 0)		{		  osip_dialog_free(dlg);		  osip_event_free (evt);		  return;		}	      /* copy all credentials from INVITE! */	      last_tr = jc->c_out_tr;	      if (last_tr!=NULL)		{		  int pos = 0;		  int i;		  osip_proxy_authorization_t *pa = NULL;		  		  i = osip_message_get_proxy_authorization (last_tr->orig_request, pos, &pa);		  while (i == 0 && pa != NULL)		    {		      osip_proxy_authorization_t *pa2;		      		      i = osip_proxy_authorization_clone (pa, &pa2);		      if (i != 0)			{			  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,						  "Error in credential from INVITE\n"));			  break;			}		      osip_list_add (&ack->proxy_authorizations, pa2, -1);		      pa = NULL;		      pos++;		      i = osip_message_get_proxy_authorization (last_tr->orig_request, pos, &pa);		    }		}	      cb_snd_message (NULL, ack, NULL,0, -1);	      osip_message_free(ack);	      	      /* ready to send a BYE */	      transport = NULL;	      if (last_tr != NULL && last_tr->orig_request != NULL)		transport = _eXosip_transport_protocol (last_tr->orig_request);	      if (transport == NULL)		i = generating_bye (&bye, dlg, "UDP");	      else		i = generating_bye (&bye, dlg, "UDP");	      cb_snd_message (NULL, bye, NULL,0, -1);	      osip_message_free(bye);	    }	    	    osip_dialog_free(dlg);	    osip_event_free (evt);	    return;	  }		/* we don't match any existing dialog: send a ACK & send a BYE */	osip_event_free (evt);}static int _eXosip_handle_incoming_message (char *buf, size_t len, int socket,                                            char *host, int port);static int_eXosip_handle_incoming_message (char *buf, size_t len, int socket,                                 char *host, int port){  osip_transaction_t *transaction = NULL;  osip_event_t *sipevent;  int i;  sipevent = osip_parse (buf, len);  transaction = NULL;  if (sipevent != NULL && sipevent->sip != NULL)    {  } else    {      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_ERROR, NULL,                   "Could not parse SIP message\n"));      osip_event_free (sipevent);      return -1;    }  OSIP_TRACE (osip_trace              (__FILE__, __LINE__, OSIP_INFO1, NULL,               "Message received from: %s:%i\n", host, port));  osip_message_fix_last_via_header (sipevent->sip, host, port);  i = osip_find_transaction_and_add_event (eXosip.j_osip, sipevent);  if (i != 0)    {      /* this event has no transaction, */      OSIP_TRACE (osip_trace                  (__FILE__, __LINE__, OSIP_INFO1, NULL,                   "This is a request\n", buf));      eXosip_lock ();      if (MSG_IS_REQUEST (sipevent->sip))        eXosip_process_newrequest (sipevent, socket);      else if (MSG_IS_RESPONSE (sipevent->sip))        eXosip_process_response_out_of_transaction (sipevent);      eXosip_unlock ();  } else    {      /* handled by oSIP ! */      return 0;    }  return 0;}#if defined (WIN32) || defined (_WIN32_WCE)#define eXFD_SET(A, B)   FD_SET((unsigned int) A, B)#else#define eXFD_SET(A, B)   FD_SET(A, B)#endif/* if second==-1 && useconds==-1  -> wait for ever   if max_message_nb<=0  -> infinite loop....  */inteXosip_read_message (int max_message_nb, int sec_max, int usec_max){  fd_set osip_fdset;  struct timeval tv;  char *buf;  tv.tv_sec = sec_max;  tv.tv_usec = usec_max;  buf = (char *) osip_malloc (SIP_MESSAGE_MAX_LENGTH * sizeof (char) + 1);  while (max_message_nb != 0 && eXosip.j_stop_ua == 0)    {      int i;      int max=0;      int wakeup_socket = jpipe_get_read_descr (eXosip.j_socketctl);      FD_ZERO (&osip_fdset);      if (eXosip.net_interfaces[0].net_socket > 0)        {          eXFD_SET (eXosip.net_interfaces[0].net_socket, &osip_fdset);          max = eXosip.net_interfaces[0].net_socket;        }      if (eXosip.net_interfaces[1].net_socket > 0)        {          int pos;          struct eXosip_net *net = &eXosip.net_interfaces[1];          eXFD_SET (net->net_socket, &osip_fdset);          if (net->net_socket > max)            max = net->net_socket;          for (pos = 0; pos < EXOSIP_MAX_SOCKETS; pos++)            {              if (net->net_socket_tab[pos].socket != 0)                {                  eXFD_SET (net->net_socket_tab[pos].socket, &osip_fdset);                  if (net->net_socket_tab[pos].socket > max)                    max = net->net_socket_tab[pos].socket;                }            }        }      if (eXosip.net_interfaces[2].net_socket > 0)        {          eXFD_SET (eXosip.net_interfaces[2].net_socket, &osip_fdset);          if (eXosip.net_interfaces[2].net_socket > max)            max = eXosip.net_interfaces[2].net_socket;        }      eXFD_SET (wakeup_socket, &osip_fdset);      if (wakeup_socket > max)        max = wakeup_socket;      if ((sec_max == -1) || (usec_max == -1))        i = select (max + 1, &osip_fdset, NULL, NULL, NULL);      else        i = select (max + 1, &osip_fdset, NULL, NULL, &tv);#if defined (_WIN32_WCE)      /* TODO: fix me for wince */      /* if (i == -1)	 continue; */#else      if ((i == -1) && (errno == EINTR || errno == EAGAIN))        continue;#endif      if ((i > 0) && FD_ISSET (wakeup_socket, &osip_fdset))        {          char buf2[500];          jpipe_read (eXosip.j_socketctl, buf2, 499);        }      if (0 == i || eXosip.j_stop_ua != 0)        {      } else if (-1 == i)        {#if !defined (_WIN32_WCE)       /* TODO: fix me for wince */          osip_free (buf);          return -2;            /* error */#endif      } else if (FD_ISSET (eXosip.net_interfaces[1].net_socket, &osip_fdset))        {          /* accept incoming connection */          char src6host[NI_MAXHOST];          int recvport = 0;          struct sockaddr_storage sa;          int sock;          int i;          int pos;#ifdef __linux          socklen_t slen;#else          int slen;#endif          if (ipv6_enable == 0)            slen = sizeof (struct sockaddr_in);          else            slen = sizeof (struct sockaddr_in6);          for (pos = 0; pos < EXOSIP_MAX_SOCKETS; pos++)            {              if (eXosip.net_interfaces[1].net_socket_tab[pos].socket == 0)                break;            }          OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO3, NULL,                                  "creating TCP socket at index: %i\n", pos));          sock =            accept (eXosip.net_interfaces[1].net_socket,                    (struct sockaddr *) &sa, &slen);          if (sock < 0)            {              OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,                                      "Error accepting TCP socket\n"));              break;            }          eXosip.net_interfaces[1].net_socket_tab[pos].socket = sock;          OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,                                  "New TCP connection accepted\n"));          memset (src6host, 0, sizeof (src6host));          if (ipv6_enable == 0)            recvport = ntohs (((struct sockaddr_in *) &sa)->sin_port);          else            recvport = ntohs (((struct sockaddr_in6 *) &sa)->sin6_port);          i = getnameinfo ((struct sockaddr *) &sa, slen,                           src6host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);          if (i != 0)            {              OSIP_TRACE (osip_trace                          (__FILE__, __LINE__, OSIP_ERROR, NULL,                           "Message received from: %s:%i Error with getnameinfo\n",                           src6host, recvport));          } else            {              OSIP_TRACE (osip_trace                          (__FILE__, __LINE__, OSIP_INFO1, NULL,                           "Message received from: %s:%i\n", src6host, recvport));              osip_strncpy (eXosip.net_interfaces[1].net_socket_tab[pos].                            remote_ip, src6host,                            sizeof (eXosip.net_interfaces[1].                                    net_socket_tab[pos].remote_ip));              eXosip.net_interfaces[1].net_socket_tab[pos].remote_port = recvport;            }      } else if (FD_ISSET (eXosip.net_interfaces[0].net_socket, &osip_fdset))        {          /*AMDstruct sockaddr_in sa; */          struct sockaddr_storage sa;#ifdef __linux          socklen_t slen;#else          int slen;#endif          if (ipv6_enable == 0)            slen = sizeof (struct sockaddr_in);          else            slen = sizeof (struct sockaddr_in6);          i =            _eXosip_recvfrom (eXosip.net_interfaces[0].net_socket, buf,                              SIP_MESSAGE_MAX_LENGTH, 0,                              (struct sockaddr *) &sa, &slen);          if (i > 5)            /* we expect at least one byte, otherwise there's no doubt that it is not a sip message ! */            {              /* Message might not end with a "\0" but we know the number of */              /* char received! */              osip_transaction_t *transaction = NULL;              osip_event_t *sipevent;              osip_strncpy (buf + i, "\0", 1);              OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,                                      "Received message: \n%s\n", buf));#ifdef WIN32              if (strlen (buf) > 412)                {                  OSIP_TRACE (osip_trace                              (__FILE__, __LINE__, OSIP_INFO1, NULL,                               "Message suite: \n%s\n", buf + 412));                }#endif              sipevent = osip_parse (buf, i);              transaction = NULL;              if (sipevent != NULL && sipevent->sip != NULL)                {                  if (!eXosip.http_port)                    {                      char src6host[NI_MAXHOST];                      char src6buf[NI_MAXSERV];                      int recvport = 0;                      memset (src6host, 0, sizeof (src6host));                      memset (src6buf, 0, sizeof (src6buf));                      if (ipv6_enable == 0)                        recvport = ntohs (((struct sockaddr_in *) &sa)->sin_port);                      else                        recvport =                          ntohs (((struct sockaddr_in6 *) &sa)->sin6_port);                      i = getnameinfo ((struct sockaddr *) &sa, slen,                                       src6host, NI_MAXHOST,                                       NULL, 0, NI_NUMERICHOST);                      if (i != 0)                        {                          OSIP_TRACE (osip_trace                                      (__FILE__, __LINE__, OSIP_ERROR, NULL,                                       "Message received from: %s:%i (serv=%s) Error with getnameinfo\n",                                       src6host, recvport, src6buf));                      } else                        {                          OSIP_TRACE (osip_trace                                      (__FILE__, __LINE__, OSIP_INFO1, NULL,                                       "Message received from: %s:%i (serv=%s)\n",                                       src6host, recvport, src6buf));                        }                      OSIP_TRACE (osip_trace                                  (__FILE__, __LINE__, OSIP_INFO1, NULL,                                   "Message received from: %s:%i (serv=%s)\n",                                   src6host, recvport, src6buf));             

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?