proxy-service.c (1661B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 */ 7 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <string.h> 11 #include <assert.h> 12 13 #include <sal/core/thread.h> 14 15 #include "proxy-service.h" 16 17 /* 18 * Function: _proxy_thread 19 * 20 * Purpose: 21 * Implements the proxy data loop 22 * Parameters: 23 * ctrl - proxy control structure 24 * Returns: 25 * Nothing 26 */ 27 static void 28 _proxy_thread(proxy_ctrl_t* ctrl) 29 { 30 unsigned char* data = malloc(ctrl->max_data_size); 31 assert(data); 32 assert(ctrl->input_cb); 33 34 for (;;) { 35 unsigned int len = ctrl->max_data_size; 36 memset(data, 0, len); 37 38 /* Receive packets from the given input callback */ 39 if (ctrl->input_cb(ctrl, data, &len) >= 0) { 40 /* Send it to the output callback */ 41 if (ctrl->output_cb) { 42 ctrl->output_cb(ctrl, data, &len); 43 } 44 } 45 46 if (ctrl->exit) { 47 if (ctrl->exit_cb) { 48 ctrl->exit_cb(ctrl, NULL, 0); 49 } 50 free(data); 51 ctrl->exit = 0; 52 return; 53 } 54 } 55 } 56 57 /* 58 * Function: proxy_service_start 59 * 60 * Purpose: 61 * Start a proxy thread/loop 62 * Parameters: 63 * ctrl - proxy control structure 64 * fork - indicates whether a new thread should be created. 65 * TRUE: run loop in a new thread. 66 * FALSE: Run loop (blocking) 67 * Returns: 68 * Nothing 69 */ 70 int 71 proxy_service_start(proxy_ctrl_t* ctrl, int fork) 72 { 73 if (fork) { 74 sal_thread_create("_proxy_thread", 0, 0, 75 (void (*)(void*))_proxy_thread, ctrl); 76 } else { 77 _proxy_thread(ctrl); 78 } 79 return 0; 80 }