<commit_before>/*
 * rtp.c
 *
 * library functions for the real-time transport protocol
 *
 * <NAME>
 * Cisco Systems, Inc.
 */


#include "rtp.h"

#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#define PRINT_DEBUG    0    /* set to 1 to print out debugging data */
#define VERBOSE_DEBUG  0    /* set to 1 to print out more data      */

ssize_t
rtp_sendto(rtp_sender_t *sender, const void* msg, int len) {
  int octets_sent;
  err_status_t stat;
  int pkt_len = len + rtp_header_len;

  /* marshal data */
  strncpy(sender->message.body, msg, len);
  
  /* update header */
  sender->message.header.seq = ntohs(sender->message.header.seq) + 1;
  sender->message.header.seq = htons(sender->message.header.seq);
  sender->message.header.ts = ntohl(sender->message.header.ts) + 1;
  sender->message.header.ts = htonl(sender->message.header.ts);
  
  /* apply srtp */
  stat = srtp_protect(sender->srtp_ctx, &sender->message.header, &pkt_len);
  if (stat) {
#if PRINT_DEBUG
    fprintf(stderr, "error: srtp protection failed with code %d\n", stat);
#endif
    return -1;
  }
#if VERBOSE_DEBUG
  srtp_print_packet(&sender->message.header, pkt_len);
#endif
  octets_sent = sendto(sender->socket, (void*)&sender->message,
		       pkt_len, 0, (struct sockaddr *)&sender->addr,
		       sizeof (struct sockaddr_in));

  if (octets_sent != pkt_len) {
#if PRINT_DEBUG
    fprintf(stderr, "error: couldn't send message %s", (char *)msg);
    perror("");
#endif
  }

  return octets_sent;
}

ssize_t
rtp_recvfrom(rtp_receiver_t *receiver, void *msg, int *len) {
  int octets_recvd;
  err_status_t stat;
  
  octets_recvd = recvfrom(receiver->socket, (void *)&receiver->message,
			 *len, 0, (struct sockaddr *) NULL, 0);

  /* verify rtp header */
  if (receiver->message.header.version != 2) {
    *len = 0;
    return -1;
  }

#if PRINT_DEBUG
  fprintf(stderr, "%d octets received from SSRC %u",
	  octets_recvd, receiver->message.header.ssrc);
#endif
#if VERBOSE_DEBUG
  srtp_print_packet(&receiver->message.header, octets_recvd);
#endif

  /* apply srtp */
  stat = srtp_unprotect(receiver->srtp_ctx,
			&receiver->message.header, &octets_recvd);
  if (stat) {
#if PRINT_DEBUG
    fprintf(stderr,
	    "\nerror: srtp unprotection failed with code %d", stat);
    if (stat == err_status_replay_fail)
      fprintf(stderr, " (replay check failed)\n");
    if (stat == err_status_auth_fail)
      fprintf(stderr, " (auth check failed)\n"); 
#endif
    return -1;
  }<commit_msg>(rtp_recvfrom): Always print single line error when srtp_unprotect() fails.<commit_after>/*
 * rtp.c
 *
 * library functions for the real-time transport protocol
 *
 * <NAME>
 * Cisco Systems, Inc.
 */


#include "rtp.h"

#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#define PRINT_DEBUG    0    /* set to 1 to print out debugging data */
#define VERBOSE_DEBUG  0    /* set to 1 to print out more data      */

ssize_t
rtp_sendto(rtp_sender_t *sender, const void* msg, int len) {
  int octets_sent;
  err_status_t stat;
  int pkt_len = len + rtp_header_len;

  /* marshal data */
  strncpy(sender->message.body, msg, len);
  
  /* update header */
  sender->message.header.seq = ntohs(sender->message.header.seq) + 1;
  sender->message.header.seq = htons(sender->message.header.seq);
  sender->message.header.ts = ntohl(sender->message.header.ts) + 1;
  sender->message.header.ts = htonl(sender->message.header.ts);
  
  /* apply srtp */
  stat = srtp_protect(sender->srtp_ctx, &sender->message.header, &pkt_len);
  if (stat) {
#if PRINT_DEBUG
    fprintf(stderr, "error: srtp protection failed with code %d\n", stat);
#endif
    return -1;
  }
#if VERBOSE_DEBUG
  srtp_print_packet(&sender->message.header, pkt_len);
#endif
  octets_sent = sendto(sender->socket, (void*)&sender->message,
		       pkt_len, 0, (struct sockaddr *)&sender->addr,
		       sizeof (struct sockaddr_in));

  if (octets_sent != pkt_len) {
#if PRINT_DEBUG
    fprintf(stderr, "error: couldn't send message %s", (char *)msg);
    perror("");
#endif
  }

  return octets_sent;
}

ssize_t
rtp_recvfrom(rtp_receiver_t *receiver, void *msg, int *len) {
  int octets_recvd;
  err_status_t stat;
  
  octets_recvd = recvfrom(receiver->socket, (void *)&receiver->message,
			 *len, 0, (struct sockaddr *) NULL, 0);

  /* verify rtp header */
  if (receiver->message.header.version != 2) {
    *len = 0;
    return -1;
  }

#if PRINT_DEBUG
  fprintf(stderr, "%d octets received from SSRC %u\n",
	  octets_recvd, receiver->message.header.ssrc);
#endif
#if VERBOSE_DEBUG
  srtp_print_packet(&receiver->message.header, octets_recvd);
#endif

  /* apply srtp */
  stat = srtp_unprotect(receiver->srtp_ctx,
			&receiver->message.header, &octets_recvd);
  if (stat) {
    fprintf(stderr,
	    "error: srtp unprotection failed with code %d%s\n", stat,
	    stat == err_status_replay_fail ? " (replay check failed)" :
	    stat == err_status_auth_fail ? " (auth check failed)" : "");
    return -1;
  }
{'fraction_non_alphanumeric': 0.1439543127215439, 'fraction_numerical': 0.005513981882630957, 'mean_word_length': 3.6854243542435423, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2579693', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1894, 'n_words': 441}