repo_name
stringlengths 5
122
| path
stringlengths 3
232
| text
stringlengths 6
1.05M
|
---|---|---|
ooooo-youwillsee/leetcode | 0118-Pascals-Triangle/cpp_0118/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/6.
//
#ifndef CPP_0118_SOLUTION1_H
#define CPP_0118_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> generate(int numRows) {
if (numRows == 0) return {};
vector<vector<int>> ans;
ans.push_back({1});
for (int i = 1; i < numRows; ++i) {
vector<int> prev = ans[i - 1];
vector<int> next(prev.size() + 1, 1);
for (int j = prev.size() - 1; j > 0; --j) {
next[j] = prev[j] + prev[j - 1];
}
ans.push_back(next);
}
return ans;
}
};
#endif //CPP_0118_SOLUTION1_H
|
MihawkHu/Android_scheduler | kernel/goldfish/include/generated/compile.h | <reponame>MihawkHu/Android_scheduler<gh_stars>1-10
/* This file is auto generated, version 74 */
/* PREEMPT */
#define UTS_MACHINE "arm"
#define UTS_VERSION "#74 PREEMPT Tue May 24 16:58:10 CST 2016"
#define LINUX_COMPILE_BY "mihawk"
#define LINUX_COMPILE_HOST "GG"
#define LINUX_COMPILER "gcc version 4.9 20150123 (prerelease) (GCC) "
|
MihawkHu/Android_scheduler | psinfo/psinfo.c | /*
This program is used to print the process information.
The result will display by a chart. The meanings of each row are
process name, process pid, process scheduler policy, process priority,
process normal priority, process real time priority
To implement this program, I use the syscall that I wrote in project 1.
But this time I also get extra information included priority and policy.
Type ./psinfo in avd and the process information will display.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <string.h>
// used to get process infomation
struct prinfo {
pid_t parent_pid; /* process id of parent */
pid_t pid; /* process id */
pid_t first_child_pid; /* pid of youngest child */
pid_t next_sibling_pid; /* pid of older sibling */
long prio;
long normal_prio;
long rt_priority;
long state; /* current state of process */
long uid; /* user id of process owner */
char comm[64]; /* name of program executed */
};
int main(int argc, char const *argv[]) {
// get process infomation using system call 223
struct prinfo *buf = malloc(1000 * sizeof(struct prinfo));
int *nr = malloc(sizeof(int));
if (buf == NULL || nr == NULL) {
printf("Allocation error!\n");
exit(-1);
}
syscall(223, buf, nr); // traverse pstree and store pprcess information in buf
// print all process information
int i = 0;
printf(" name pid policy prio nor_prio rt_prio\n");
while (i < *nr) {
printf("%20s%10d%10d%10d%10d%10d\n",
buf[i].comm, buf[i].pid, sched_getscheduler(buf[i].pid), buf[i].prio,
buf[i].normal_prio, buf[i].rt_priority);
++i;
}
return 0;
}
|
MihawkHu/Android_scheduler | cas/cas.c | /*
This program is used to change the scheduler of processes.
According to different parameter, this program can change the scheduler of one
process itself or all descendants of it.
You can also appoint priority of the process.
To implement this program, I use the syscall that I write in the first project.
But this time I also get extra information included priority and policy.
The mainly function I used to change the scheduler is
sched_setscheduler();
This program has 4 input parameter.
1. The first one is the descendants option. "-one" means only change itself.
"-des" means change all descendants of it.
2. The second parameter is the name of process.
3. The third parameter is the scheduler policy that will change to.
4. The last parameter is the real time priority that will change to.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
// used to get pid by process name
struct prinfo {
pid_t parent_pid; /* process id of parent */
pid_t pid; /* process id */
pid_t first_child_pid; /* pid of youngest child */
pid_t next_sibling_pid; /* pid of older sibling */
long prio;
long normal_prio;
long rt_priority;
long state; /* current state of process */
long uid; /* user id of process owner */
char comm[64]; /* name of program executed */
};
int main(int argc, char const *argv[]) {
// check input
if (argc != 5) {
printf("Input Error 1!\n");
printf("Please input ./cas #descendants option# #process_name# #schedule_method# #priority#\n");
exit(-1);
}
// descendants option, 0: -one, 1: -des
int des_op = 0;
if (strcmp("-des", argv[1]) == 0) des_op = 1;
else if (strcmp("-one", argv[1]) == 0) des_op = 0;
else {
printf("Input Error 2!\n");
printf("The first parameter should be '-one' or '-des'\n");
printf("'-one' means only this process, '-des' means it and des its descendants\n");
exit(-1);
}
// get pid
// use pstree system_call 223 in project 1
int pid = 0;
int descendants[1000] = { 0 }; // pid of descendants
int des_num = 0; // number of descendants
struct prinfo *buf = malloc(1000 * sizeof(struct prinfo));
int *nr = malloc(sizeof(int));
if (buf == NULL || nr == NULL) {
printf("Allocation error!\n");
exit(-1);
}
syscall(223, buf, nr); // traverse pstree and store pprcess information in buf
int find = 0;
// look for this process
int i = 0;
while (i < *nr) {
++i;
if (strcmp(argv[2], buf[i].comm) == 0) {
pid = buf[i].pid;
find = 1;
break;
}
}
if (!find) {
printf("Input Error 3!\n");
printf("No such process! Please check it.\n");
exit(-1);
}
// get descendants pid
if (des_op == 1) {
int pa_index = i++;
while (buf[i].parent_pid != buf[pa_index].parent_pid) {
descendants[des_num++] = buf[i++].pid;
}
}
free(buf); free(nr);
// printf("Pid: %d\n", pid);
// get sched option
int sched_option = 0;
if (strcmp(argv[3], "fifo") == 0) sched_option = SCHED_FIFO;
else if (strcmp(argv[3], "rr") == 0) sched_option = SCHED_RR;
else {
printf("Input Error 4!\n");
printf("The third parameter should be 'fifo' or 'rr'\n");
exit(-1);
}
// set sched parameter
struct sched_param param;
int maxpri;
if (strcmp(argv[4], "-d") == 0) {
if (sched_option == SCHED_FIFO) maxpri = sched_get_priority_max(SCHED_FIFO);
else maxpri = sched_get_priority_max(SCHED_RR);
if(maxpri == -1) {
printf("Function sched_get_priority_max() failed\n");
exit(-1);
}
}
else {
maxpri = atoi(argv[4]);
}
if (maxpri < 0 || maxpri > 99) {
printf("Input Error 5!\n");
printf("The last parameter should be '-d' or any number between 0~99\n");
printf("'-d' means default\n");
exit(-1);
}
// printf("Max priority: %d\n", maxpri);
param.sched_priority = maxpri;
// set scheduler
if (des_op == 0) {
if (sched_setscheduler(pid, sched_option, ¶m) == -1) {
printf("Function sched_setscheduler() failed!\n");
exit(-1);
}
}
// descendants
if (des_op == 1) {
int i = 0;
while (i < des_num) {
if (sched_setscheduler(descendants[i], sched_option, ¶m) == -1) {
printf("Function sched_setscheduler() failed!\n");
exit(-1);
}
++i;
}
}
return 0;
}
|
MihawkHu/Android_scheduler | test/test.c | /*
This program is a easy time cost program. It is used to compete with processtest.
The print value of this program is the execution time of itself.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
int main()
{
double tt = clock();
// some calculation to cost time
double a = 1254125135;
int cnt1 = 11000000;
while (cnt1 > 0) {
cnt1--;
a = sqrt(abs(a));
}
tt = (double)(clock() - tt) / CLOCKS_PER_SEC;
printf("Cost %.3f seconds!\n", tt);
return 0;
}
|
MihawkHu/Android_scheduler | sys_mycall/sys_mycall.c | /*
This program add a new system call to the avd. It is similar with the system call
that I write in project 1. The difference is that I add more information included
process priorities and scheduler policy.
Type #insmod sys_mycall.ko# in adb shell and then you can use this system call.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/syscalls.h>
#include <linux/slab.h> //alloction function
#include <linux/uaccess.h> //kernel--user
#include <linux/list.h>
MODULE_LICENSE("Dual BSD/GPL");
#define __NR_mycall 223
static int (*oldcall)(void);
//==========================================================================================
struct prinfo {
pid_t parent_pid; /* process id of parent */
pid_t pid; /* process id */
pid_t first_child_pid; /* pid of youngest child */
pid_t next_sibling_pid; /* pid of older sibling */
long prio;
long normal_prio;
long rt_priority;
long state; /* current state of process */
long uid; /* user id of process owner */
char comm[64]; /* name of program executed */
};
// transform task_struct to prinfo struct
void taskToPrinfo(struct task_struct *task, struct prinfo *prin) {
prin->state = task->state; //state
prin->uid = task->cred->uid; //uid
prin->prio = task->prio;
prin->normal_prio = task->normal_prio;
prin->rt_priority = task->rt_priority;
get_task_comm(prin->comm, task); //comm[64]
prin->parent_pid = task->parent->pid; //parent_pid
prin->pid = task->pid; //process id
//first child pid
if (list_empty(&task->children)) {
prin->first_child_pid = 0;
} else {
prin->first_child_pid = list_entry((&task->children)->next, struct task_struct, sibling)->pid;
}
// next sibling pid
if (list_empty(&task->sibling)) {
prin->next_sibling_pid = 0;
} else {
struct list_head *list;
struct task_struct *task_tmp;
list_for_each(list, &(task->parent->children)) { //traverse siblings of it
task_tmp = list_entry(list, struct task_struct, sibling);
if (task_tmp->pid == task->pid) {
prin->next_sibling_pid = list_entry(list->next, struct task_struct, sibling)->pid;
break;
}
}
}
}
void dfs(struct task_struct *task, struct prinfo *buf, int *nr) {
struct task_struct *task_tmp;
struct list_head *list;
// operation
taskToPrinfo(task, &buf[*nr]);
*nr = *nr + 1;
list_for_each(list, &task->children) {
task_tmp = list_entry(list, struct task_struct, sibling);
dfs(task_tmp, buf, nr);
}
}
int sys_mycall(struct prinfo *buf, int *nr) {
//initialize dynamic space
struct prinfo *buf_tmp;
int *nr_tmp;
buf_tmp = kmalloc_array(1000, sizeof(*buf), GFP_KERNEL); //suppose processes less than 200
nr_tmp = kmalloc(sizeof(int), GFP_KERNEL);
if (buf_tmp == NULL || nr_tmp == NULL) {
printk("Allocation initialize error!\n");
return -EFAULT;
}
//initializition
*nr_tmp = 0;
//critical segment
read_lock(&tasklist_lock); //use tasklist_lock to avoid sleep
dfs(&init_task, buf_tmp, nr_tmp);
read_unlock(&tasklist_lock);
// transform to user
if (copy_to_user(buf, buf_tmp, 1000 * sizeof(*buf_tmp))) {
printk("Copy_to_user error!\n");
return -EFAULT;
}
if (copy_to_user(nr, nr_tmp, sizeof(int))) {
printk("Copy_to_user error!\n");
return -EFAULT;
}
kfree(buf_tmp); kfree(nr_tmp);
return *nr;
}
//==========================================================================================
static int addsyscall_init(void)
{
long *syscall = (long*)0xc000d8c4;
oldcall = (int(*)(void))(syscall[__NR_mycall]);
syscall[__NR_mycall] = (unsigned long)sys_mycall;
printk(KERN_INFO "module load!\n");
return 0;
}
static void addsyscall_exit(void)
{
long *syscall = (long*)0xc000d8c4;
syscall[__NR_mycall] = (unsigned long)oldcall;
printk(KERN_INFO "module exit!\n");
}
module_init(addsyscall_init);
module_exit(addsyscall_exit);
//
|
MihawkHu/Android_scheduler | kernel/goldfish/include/generated/autoconf.h | /*
*
* Automatically generated file; DO NOT EDIT.
* Linux/arm 3.4.67 Kernel Configuration
*
*/
#define CONFIG_RING_BUFFER 1
#define CONFIG_NF_CONNTRACK_H323 1
#define CONFIG_HAVE_ARCH_SECCOMP_FILTER 1
#define CONFIG_KERNEL_GZIP 1
#define CONFIG_INPUT_KEYBOARD 1
#define CONFIG_IP_NF_TARGET_REDIRECT 1
#define CONFIG_CRC32 1
#define CONFIG_NF_NAT_PROTO_SCTP 1
#define CONFIG_HAVE_AOUT 1
#define CONFIG_VFP 1
#define CONFIG_AEABI 1
#define CONFIG_FB_TILEBLITTING 1
#define CONFIG_HIGH_RES_TIMERS 1
#define CONFIG_BLK_DEV_DM 1
#define CONFIG_VLAN_8021Q 1
#define CONFIG_IP_MULTIPLE_TABLES 1
#define CONFIG_FLATMEM_MANUAL 1
#define CONFIG_INOTIFY_USER 1
#define CONFIG_NF_CONNTRACK_NETBIOS_NS 1
#define CONFIG_NETWORK_FILESYSTEMS 1
#define CONFIG_MODULE_FORCE_UNLOAD 1
#define CONFIG_EXPERIMENTAL 1
#define CONFIG_ARCH_SUSPEND_POSSIBLE 1
#define CONFIG_HAVE_ARCH_MMAP_RND_BITS 1
#define CONFIG_BLK_DEV_NBD 1
#define CONFIG_ARM_UNWIND 1
#define CONFIG_BINFMT_MISC 1
#define CONFIG_NETFILTER_XT_MATCH_HELPER 1
#define CONFIG_SSB_POSSIBLE 1
#define CONFIG_NETFILTER_XT_MATCH_STATISTIC 1
#define CONFIG_FSNOTIFY 1
#define CONFIG_BLK_DEV_LOOP_MIN_COUNT 8
#define CONFIG_STP 1
#define CONFIG_CRYPTO_MANAGER_DISABLE_TESTS 1
#define CONFIG_HAVE_KERNEL_LZMA 1
#define CONFIG_QEMU_PIPE 1
#define CONFIG_FIB_RULES 1
#define CONFIG_RT_GROUP_SCHED 1
#define CONFIG_KTIME_SCALAR 1
#define CONFIG_IP6_NF_MANGLE 1
#define CONFIG_IPV6 1
#define CONFIG_CRYPTO_AEAD 1
#define CONFIG_BQL 1
#define CONFIG_INPUT_MOUSEDEV_PSAUX 1
#define CONFIG_DEFAULT_TCP_CONG "cubic"
#define CONFIG_UEVENT_HELPER_PATH ""
#define CONFIG_NF_NAT_PROTO_GRE 1
#define CONFIG_ANDROID_BINDER_IPC 1
#define CONFIG_CRYPTO_PCBC 1
#define CONFIG_IP6_NF_TARGET_REJECT 1
#define CONFIG_WLAN 1
#define CONFIG_DEFAULT_MESSAGE_LOGLEVEL 4
#define CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG 1
#define CONFIG_CONNECTOR 1
#define CONFIG_CRYPTO_RNG2 1
#define CONFIG_NETFILTER_NETLINK_QUEUE 1
#define CONFIG_MSDOS_FS 1
#define CONFIG_TUN 1
#define CONFIG_TINY_PREEMPT_RCU 1
#define CONFIG_DM_CRYPT 1
#define CONFIG_HAVE_PROC_CPU 1
#define CONFIG_IOMMU_SUPPORT 1
#define CONFIG_GOLDFISH_TTY 1
#define CONFIG_NFSD 1
#define CONFIG_CRYPTO_HMAC 1
#define CONFIG_ETHERNET 1
#define CONFIG_BRANCH_PROFILE_NONE 1
#define CONFIG_DQL 1
#define CONFIG_IP_NF_ARPTABLES 1
#define CONFIG_BCMA_POSSIBLE 1
#define CONFIG_NET_VENDOR_CIRRUS 1
#define CONFIG_FORCE_MAX_ZONEORDER 11
#define CONFIG_PRINTK 1
#define CONFIG_NF_CONNTRACK_PROC_COMPAT 1
#define CONFIG_TIMERFD 1
#define CONFIG_TRACEPOINTS 1
#define CONFIG_MTD_CFI_I2 1
#define CONFIG_CRYPTO_AUTHENC 1
#define CONFIG_BOUNCE 1
#define CONFIG_SHMEM 1
#define CONFIG_MTD 1
#define CONFIG_HAVE_ARCH_JUMP_LABEL 1
#define CONFIG_MMC_BLOCK_MINORS 8
#define CONFIG_DNOTIFY 1
#define CONFIG_INPUT_MOUSEDEV 1
#define CONFIG_CRYPTO_DES 1
#define CONFIG_ENABLE_MUST_CHECK 1
#define CONFIG_NLS_CODEPAGE_437 1
#define CONFIG_EXPORTFS 1
#define CONFIG_SERIO 1
#define CONFIG_SCHEDSTATS 1
#define CONFIG_RTC_INTF_SYSFS 1
#define CONFIG_BLK_DEV_INITRD 1
#define CONFIG_NF_CONNTRACK_SANE 1
#define CONFIG_HAVE_BPF_JIT 1
#define CONFIG_NF_CT_PROTO_DCCP 1
#define CONFIG_ZLIB_INFLATE 1
#define CONFIG_CRYPTO_TWOFISH_COMMON 1
#define CONFIG_AUDITSYSCALL 1
#define CONFIG_IP_PNP 1
#define CONFIG_RTC_INTF_PROC 1
#define CONFIG_STACKTRACE_SUPPORT 1
#define CONFIG_LOCKD 1
#define CONFIG_ARM 1
#define CONFIG_ARM_L1_CACHE_SHIFT 6
#define CONFIG_NETFILTER_XT_MATCH_STRING 1
#define CONFIG_HAS_WAKELOCK 1
#define CONFIG_NET_VENDOR_BROADCOM 1
#define CONFIG_STANDALONE 1
#define CONFIG_ASHMEM 1
#define CONFIG_BLOCK 1
#define CONFIG_INIT_ENV_ARG_LIMIT 32
#define CONFIG_IP_NF_ARP_MANGLE 1
#define CONFIG_NF_CONNTRACK_PPTP 1
#define CONFIG_BUG 1
#define CONFIG_CONTEXT_SWITCH_TRACER 1
#define CONFIG_PM 1
#define CONFIG_NF_CONNTRACK_IRC 1
#define CONFIG_DEVKMEM 1
#define CONFIG_TEXTSEARCH_KMP 1
#define CONFIG_VT 1
#define CONFIG_NETFILTER_XT_TARGET_CLASSIFY 1
#define CONFIG_SPLIT_PTLOCK_CPUS 4
#define CONFIG_POWER_SUPPLY 1
#define CONFIG_CPU_CACHE_VIPT 1
#define CONFIG_NETFILTER_XT_TARGET_NFQUEUE 1
#define CONFIG_SECURITY_SELINUX_BOOTPARAM 1
#define CONFIG_NLS 1
#define CONFIG_SYN_COOKIES 1
#define CONFIG_IP_ADVANCED_ROUTER 1
#define CONFIG_ENABLE_WARN_DEPRECATED 1
#define CONFIG_IP6_NF_IPTABLES 1
#define CONFIG_ANDROID_LOW_MEMORY_KILLER_AUTODETECT_OOM_ADJ_VALUES 1
#define CONFIG_EVENT_TRACING 1
#define CONFIG_NLS_ISO8859_1 1
#define CONFIG_CRYPTO_WORKQUEUE 1
#define CONFIG_TEXTSEARCH_BM 1
#define CONFIG_NF_CONNTRACK_PROCFS 1
#define CONFIG_NETDEVICES 1
#define CONFIG_NET_KEY 1
#define CONFIG_IOSCHED_DEADLINE 1
#define CONFIG_CGROUP_FREEZER 1
#define CONFIG_CPU_TLB_V7 1
#define CONFIG_EVENTFD 1
#define CONFIG_IPV6_SIT 1
#define CONFIG_QEMU_TRACE 1
#define CONFIG_XFRM 1
#define CONFIG_DEFCONFIG_LIST "/lib/modules/$UNAME_RELEASE/.config"
#define CONFIG_IPV6_MULTIPLE_TABLES 1
#define CONFIG_IP_NF_TARGET_MASQUERADE 1
#define CONFIG_NF_CONNTRACK_BROADCAST 1
#define CONFIG_PROC_PAGE_MONITOR 1
#define CONFIG_CC_OPTIMIZE_FOR_SIZE 1
#define CONFIG_NF_NAT_PROTO_DCCP 1
#define CONFIG_ANDROID_LOW_MEMORY_KILLER 1
#define CONFIG_ARCH_HAS_CPU_IDLE_WAIT 1
#define CONFIG_NET_VENDOR_SEEQ 1
#define CONFIG_NF_DEFRAG_IPV4 1
#define CONFIG_SELECT_MEMORY_MODEL 1
#define CONFIG_HAVE_ARCH_PFN_VALID 1
#define CONFIG_CPU_COPY_V6 1
#define CONFIG_NETFILTER_ADVANCED 1
#define CONFIG_NETFILTER_NETLINK_LOG 1
#define CONFIG_HAVE_DYNAMIC_FTRACE 1
#define CONFIG_MAGIC_SYSRQ 1
#define CONFIG_MTD_GOLDFISH_NAND 1
#define CONFIG_NETFILTER_XT_MATCH_MARK 1
#define CONFIG_IP_NF_MANGLE 1
#define CONFIG_DEFAULT_CFQ 1
#define CONFIG_INET6_XFRM_MODE_TUNNEL 1
#define CONFIG_DEBUG_BUGVERBOSE 1
#define CONFIG_IP_NF_FILTER 1
#define CONFIG_NETFILTER_XT_MATCH_LENGTH 1
#define CONFIG_FAT_FS 1
#define CONFIG_TEXTSEARCH_FSM 1
#define CONFIG_HIGHMEM 1
#define CONFIG_IP6_NF_RAW 1
#define CONFIG_INET_TUNNEL 1
#define CONFIG_MMC_BLOCK_BOUNCE 1
#define CONFIG_GENERIC_CLOCKEVENTS 1
#define CONFIG_IOSCHED_CFQ 1
#define CONFIG_HAVE_KERNEL_XZ 1
#define CONFIG_CPU_CP15_MMU 1
#define CONFIG_CONSOLE_TRANSLATIONS 1
#define CONFIG_DUMMY_CONSOLE 1
#define CONFIG_MODULE_FORCE_LOAD 1
#define CONFIG_MACH_GOLDFISH_ARMV7 1
#define CONFIG_ARCH_MMAP_RND_BITS_MAX 16
#define CONFIG_TRACE_IRQFLAGS_SUPPORT 1
#define CONFIG_NETFILTER_XT_MATCH_CONNMARK 1
#define CONFIG_RD_GZIP 1
#define CONFIG_HAVE_REGS_AND_STACK_ACCESS_API 1
#define CONFIG_LBDAF 1
#define CONFIG_EXT4_FS_SECURITY 1
#define CONFIG_INET_XFRM_MODE_TRANSPORT 1
#define CONFIG_CRYPTO_MD5 1
#define CONFIG_NFSD_V3 1
#define CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE 1
#define CONFIG_HAVE_GENERIC_HARDIRQS 1
#define CONFIG_BINFMT_ELF 1
#define CONFIG_AUDIT_GENERIC 1
#define CONFIG_HOTPLUG 1
#define CONFIG_SCHED_TRACER 1
#define CONFIG_IP_PIMSM_V1 1
#define CONFIG_CPU_CP15 1
#define CONFIG_NETFILTER_XT_MARK 1
#define CONFIG_NETFILTER_XTABLES 1
#define CONFIG_RESOURCE_COUNTERS 1
#define CONFIG_SLABINFO 1
#define CONFIG_RTC_DRV_GOLDFISH 1
#define CONFIG_CRYPTO_HW 1
#define CONFIG_HARDIRQS_SW_RESEND 1
#define CONFIG_NETFILTER_XT_TARGET_TPROXY 1
#define CONFIG_CRC16 1
#define CONFIG_GENERIC_CALIBRATE_DELAY 1
#define CONFIG_CPU_HAS_PMU 1
#define CONFIG_BROKEN_ON_SMP 1
#define CONFIG_TMPFS 1
#define CONFIG_ANON_INODES 1
#define CONFIG_FUTEX 1
#define CONFIG_IP_PNP_DHCP 1
#define CONFIG_VMSPLIT_3G 1
#define CONFIG_RTC_HCTOSYS 1
#define CONFIG_SECURITY_NETWORK 1
#define CONFIG_ANDROID 1
#define CONFIG_NF_CONNTRACK_EVENTS 1
#define CONFIG_IPV6_NDISC_NODETYPE 1
#define CONFIG_CGROUP_SCHED 1
#define CONFIG_SYSVIPC 1
#define CONFIG_CRYPTO_PCOMP2 1
#define CONFIG_NF_CONNTRACK_FTP 1
#define CONFIG_MODULES 1
#define CONFIG_IP_NF_MATCH_ECN 1
#define CONFIG_CPU_HAS_ASID 1
#define CONFIG_EVENT_POWER_TRACING_DEPRECATED 1
#define CONFIG_AUDIT_WATCH 1
#define CONFIG_UNIX 1
#define CONFIG_YAFFS_YAFFS1 1
#define CONFIG_CRYPTO_HASH2 1
#define CONFIG_DEFAULT_HOSTNAME "(none)"
#define CONFIG_BLK_DEV_IO_TRACE 1
#define CONFIG_INET_ESP 1
#define CONFIG_SECURITY_SELINUX_DEVELOP 1
#define CONFIG_NF_CONNTRACK_IPV6 1
#define CONFIG_MD 1
#define CONFIG_CRYPTO_ALGAPI 1
#define CONFIG_BRIDGE 1
#define CONFIG_KEYBOARD_ATKBD 1
#define CONFIG_MTD_CFI_I1 1
#define CONFIG_NF_NAT 1
#define CONFIG_NFS_COMMON 1
#define CONFIG_FAIR_GROUP_SCHED 1
#define CONFIG_CRYPTO_HASH 1
#define CONFIG_FB_GOLDFISH 1
#define CONFIG_LOG_BUF_SHIFT 16
#define CONFIG_EXTRA_FIRMWARE ""
#define CONFIG_NET_VENDOR_8390 1
#define CONFIG_PROC_EVENTS 1
#define CONFIG_VIRT_TO_BUS 1
#define CONFIG_VFAT_FS 1
#define CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE 1
#define CONFIG_CRC32_SLICEBY8 1
#define CONFIG_BLK_DEV_LOOP 1
#define CONFIG_WAKELOCK 1
#define CONFIG_NF_NAT_IRC 1
#define CONFIG_INPUT_MISC 1
#define CONFIG_CPU_PABRT_V7 1
#define CONFIG_SUSPEND 1
#define CONFIG_CRYPTO_CBC 1
#define CONFIG_FS_MBCACHE 1
#define CONFIG_RTC_CLASS 1
#define CONFIG_HAVE_LATENCYTOP_SUPPORT 1
#define CONFIG_CPU_PM 1
#define CONFIG_GENERIC_TRACER 1
#define CONFIG_YAFFS_AUTO_YAFFS2 1
#define CONFIG_HAVE_FUNCTION_TRACER 1
#define CONFIG_NF_NAT_TFTP 1
#define CONFIG_CPU_CACHE_V7 1
#define CONFIG_CRYPTO_MANAGER2 1
#define CONFIG_GENERIC_PCI_IOMAP 1
#define CONFIG_PM_SLEEP 1
#define CONFIG_CPU_ABRT_EV7 1
#define CONFIG_VM_EVENT_COUNTERS 1
#define CONFIG_RELAY 1
#define CONFIG_BATTERY_GOLDFISH 1
#define CONFIG_ARCH_GOLDFISH 1
#define CONFIG_CRYPTO_ECB 1
#define CONFIG_NF_CONNTRACK_AMANDA 1
#define CONFIG_DEBUG_FS 1
#define CONFIG_BASE_FULL 1
#define CONFIG_FB_CFB_IMAGEBLIT 1
#define CONFIG_SUNRPC 1
#define CONFIG_YAFFS_FS 1
#define CONFIG_FW_LOADER 1
#define CONFIG_KALLSYMS 1
#define CONFIG_RTC_HCTOSYS_DEVICE "rtc0"
#define CONFIG_NETFILTER_XT_MATCH_PKTTYPE 1
#define CONFIG_MII 1
#define CONFIG_SIGNALFD 1
#define CONFIG_NET_CORE 1
#define CONFIG_IP_NF_TARGET_REJECT_SKERR 1
#define CONFIG_EXT4_FS 1
#define CONFIG_UNINLINE_SPIN_UNLOCK 1
#define CONFIG_CRYPTO_SHA1 1
#define CONFIG_ARM_DMA_MEM_BUFFERABLE 1
#define CONFIG_LOCKD_V4 1
#define CONFIG_HAS_IOMEM 1
#define CONFIG_GENERIC_IRQ_PROBE 1
#define CONFIG_IP_NF_MATCH_TTL 1
#define CONFIG_NETFILTER_XT_TARGET_TRACE 1
#define CONFIG_MTD_MAP_BANK_WIDTH_1 1
#define CONFIG_EPOLL 1
#define CONFIG_YAFFS_XATTR 1
#define CONFIG_NETFILTER_XT_MATCH_COMMENT 1
#define CONFIG_NET 1
#define CONFIG_INPUT_EVDEV 1
#define CONFIG_NETFILTER_XT_MATCH_CONNTRACK 1
#define CONFIG_VFPv3 1
#define CONFIG_PACKET 1
#define CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE 1
#define CONFIG_NETFILTER_XT_MATCH_IPRANGE 1
#define CONFIG_NF_CONNTRACK_TFTP 1
#define CONFIG_NOP_TRACER 1
#define CONFIG_INET 1
#define CONFIG_IP_PNP_BOOTP 1
#define CONFIG_PREVENT_FIRMWARE_BUILD 1
#define CONFIG_CRYPTO_TWOFISH 1
#define CONFIG_FREEZER 1
#define CONFIG_NET_VENDOR_CHELSIO 1
#define CONFIG_RTC_LIB 1
#define CONFIG_NETFILTER_XT_MATCH_POLICY 1
#define CONFIG_HAVE_KPROBES 1
#define CONFIG_CRYPTO_AES 1
#define CONFIG_EXT4_USE_FOR_EXT23 1
#define CONFIG_IP6_NF_TARGET_REJECT_SKERR 1
#define CONFIG_NF_CONNTRACK_MARK 1
#define CONFIG_NETFILTER 1
#define CONFIG_NETFILTER_XT_MATCH_HASHLIMIT 1
#define CONFIG_NET_VENDOR_SMSC 1
#define CONFIG_BLK_DEV_RAM_COUNT 16
#define CONFIG_IP_MROUTE 1
#define CONFIG_INET_XFRM_MODE_TUNNEL 1
#define CONFIG_PREEMPT_RCU 1
#define CONFIG_NF_NAT_NEEDED 1
#define CONFIG_LOCKDEP_SUPPORT 1
#define CONFIG_NO_HZ 1
#define CONFIG_MTD_BLKDEVS 1
#define CONFIG_IP6_NF_FILTER 1
#define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
#define CONFIG_NEED_DMA_MAP_STATE 1
#define CONFIG_SERIO_LIBPS2 1
#define CONFIG_NETFILTER_XT_MATCH_CONNBYTES 1
#define CONFIG_ANDROID_PARANOID_NETWORK 1
#define CONFIG_PAGE_OFFSET 0xC0000000
#define CONFIG_SMC91X 1
#define CONFIG_CPU_V7 1
#define CONFIG_PANIC_TIMEOUT 0
#define CONFIG_ZBOOT_ROM_BSS 0x0
#define CONFIG_NETFILTER_XT_MATCH_ECN 1
#define CONFIG_NEED_MACH_IO_H 1
#define CONFIG_NETFILTER_XT_MATCH_TIME 1
#define CONFIG_HAVE_KERNEL_GZIP 1
#define CONFIG_DM_UEVENT 1
#define CONFIG_NET_VENDOR_I825XX 1
#define CONFIG_NETFILTER_XT_MATCH_MAC 1
#define CONFIG_NEED_PER_CPU_KM 1
#define CONFIG_ARM_NR_BANKS 8
#define CONFIG_NETFILTER_XT_TARGET_NFLOG 1
#define CONFIG_ANDROID_TIMED_OUTPUT 1
#define CONFIG_GENERIC_IO 1
#define CONFIG_LIBCRC32C 1
#define CONFIG_ARCH_NR_GPIO 0
#define CONFIG_GENERIC_BUG 1
#define CONFIG_CRYPTO_SHA256 1
#define CONFIG_HAVE_FTRACE_MCOUNT_RECORD 1
#define CONFIG_HW_CONSOLE 1
#define CONFIG_DM_DEBUG 1
#define CONFIG_DEVMEM 1
#define CONFIG_IOSCHED_NOOP 1
#define CONFIG_NEON 1
#define CONFIG_DEBUG_KERNEL 1
#define CONFIG_COMPAT_BRK 1
#define CONFIG_LOCALVERSION ""
#define CONFIG_CRYPTO 1
#define CONFIG_SCHED_DEBUG 1
#define CONFIG_DEFAULT_MMAP_MIN_ADDR 4096
#define CONFIG_IP_NF_IPTABLES 1
#define CONFIG_CMDLINE ""
#define CONFIG_HAVE_DMA_API_DEBUG 1
#define CONFIG_IP_PIMSM_V2 1
#define CONFIG_USB_ARCH_HAS_HCD 1
#define CONFIG_GENERIC_IRQ_SHOW 1
#define CONFIG_ALIGNMENT_TRAP 1
#define CONFIG_SCSI_MOD 1
#define CONFIG_NET_VENDOR_MICREL 1
#define CONFIG_AUDIT_TREE 1
#define CONFIG_CRYPTO_CRC32C 1
#define CONFIG_FUSE_FS 1
#define CONFIG_UID16 1
#define CONFIG_EMBEDDED 1
#define CONFIG_DEBUG_PREEMPT 1
#define CONFIG_HAVE_KRETPROBES 1
#define CONFIG_NF_DEFRAG_IPV6 1
#define CONFIG_HAS_DMA 1
#define CONFIG_NF_CT_PROTO_SCTP 1
#define CONFIG_FB_CFB_FILLRECT 1
#define CONFIG_NF_NAT_PPTP 1
#define CONFIG_HID 1
#define CONFIG_VT_CONSOLE_SLEEP 1
#define CONFIG_MMC_GOLDFISH 1
#define CONFIG_JBD2 1
#define CONFIG_NET_VENDOR_MARVELL 1
#define CONFIG_NET_VENDOR_FARADAY 1
#define CONFIG_LSM_MMAP_MIN_ADDR 32768
#define CONFIG_LOCALVERSION_AUTO 1
#define CONFIG_MISC_FILESYSTEMS 1
#define CONFIG_ARCH_MMAP_RND_BITS_MIN 8
#define CONFIG_FTRACE 1
#define CONFIG_NETFILTER_XT_MATCH_CONNLIMIT 1
#define CONFIG_IP_NF_RAW 1
#define CONFIG_IP_NF_ARPFILTER 1
#define CONFIG_ARM_L1_CACHE_SHIFT_6 1
#define CONFIG_NETFILTER_XT_MATCH_SOCKET 1
#define CONFIG_NET_VENDOR_STMICRO 1
#define CONFIG_NF_NAT_H323 1
#define CONFIG_IP_NF_TARGET_NETMAP 1
#define CONFIG_ARM_CPU_SUSPEND 1
#define CONFIG_YAFFS_YAFFS2 1
#define CONFIG_NF_NAT_AMANDA 1
#define CONFIG_INET6_XFRM_MODE_TRANSPORT 1
#define CONFIG_CRYPTO_MANAGER 1
#define CONFIG_RT_MUTEXES 1
#define CONFIG_VECTORS_BASE 0xffff0000
#define CONFIG_NETFILTER_XT_TARGET_MARK 1
#define CONFIG_MMC_BLOCK 1
#define CONFIG_EXPERT 1
#define CONFIG_WIRELESS 1
#define CONFIG_PERF_USE_VMALLOC 1
#define CONFIG_FAT_DEFAULT_IOCHARSET "iso8859-1"
#define CONFIG_FRAME_WARN 1024
#define CONFIG_ARCH_MMAP_RND_BITS 16
#define CONFIG_GENERIC_HWEIGHT 1
#define CONFIG_INITRAMFS_SOURCE ""
#define CONFIG_CGROUPS 1
#define CONFIG_MMC 1
#define CONFIG_STACKTRACE 1
#define CONFIG_NETFILTER_XT_TARGET_IDLETIMER 1
#define CONFIG_HAS_IOPORT 1
#define CONFIG_CGROUP_CPUACCT 1
#define CONFIG_HZ 100
#define CONFIG_NETFILTER_XT_MATCH_U32 1
#define CONFIG_ARM_PATCH_PHYS_VIRT 1
#define CONFIG_DEFAULT_IOSCHED "cfq"
#define CONFIG_NLATTR 1
#define CONFIG_TCP_CONG_CUBIC 1
#define CONFIG_SUSPEND_FREEZER 1
#define CONFIG_NETFILTER_XT_CONNMARK 1
#define CONFIG_FIRMWARE_IN_KERNEL 1
#define CONFIG_SYSFS 1
#define CONFIG_ARM_THUMB 1
#define CONFIG_IP_NF_MATCH_AH 1
#define CONFIG_NETFILTER_XT_MATCH_LIMIT 1
#define CONFIG_FB 1
#define CONFIG_TRACING 1
#define CONFIG_CPU_32v7 1
#define CONFIG_MSDOS_PARTITION 1
#define CONFIG_HAVE_OPROFILE 1
#define CONFIG_HAVE_GENERIC_DMA_COHERENT 1
#define CONFIG_HAVE_ARCH_KGDB 1
#define CONFIG_NF_CONNTRACK_IPV4 1
#define CONFIG_ZONE_DMA_FLAG 0
#define CONFIG_NET_VENDOR_INTEL 1
#define CONFIG_MTD_MAP_BANK_WIDTH_2 1
#define CONFIG_IP_MULTICAST 1
#define CONFIG_CPU_32v6K 1
#define CONFIG_DEFAULT_SECURITY "selinux"
#define CONFIG_TICK_ONESHOT 1
#define CONFIG_NF_NAT_PROTO_UDPLITE 1
#define CONFIG_CGROUP_DEBUG 1
#define CONFIG_HW_RANDOM 1
#define CONFIG_RWSEM_GENERIC_SPINLOCK 1
#define CONFIG_HAVE_FUNCTION_GRAPH_TRACER 1
#define CONFIG_BASE_SMALL 0
#define CONFIG_CRYPTO_BLKCIPHER2 1
#define CONFIG_SECURITY_SELINUX_AVC_STATS 1
#define CONFIG_PROC_FS 1
#define CONFIG_MTD_BLOCK 1
#define CONFIG_FLATMEM 1
#define CONFIG_PAGEFLAGS_EXTENDED 1
#define CONFIG_NET_VENDOR_NATSEMI 1
#define CONFIG_IKCONFIG 1
#define CONFIG_SYSCTL 1
#define CONFIG_BRIDGE_IGMP_SNOOPING 1
#define CONFIG_HAVE_C_RECORDMCOUNT 1
#define CONFIG_HAVE_PERF_EVENTS 1
#define CONFIG_SLAB 1
#define CONFIG_AUDIT 1
#define CONFIG_SYS_SUPPORTS_APM_EMULATION 1
#define CONFIG_SECURITY 1
#define CONFIG_NETFILTER_XT_MATCH_QUOTA2 1
#define CONFIG_FAT_DEFAULT_CODEPAGE 437
#define CONFIG_BLK_DEV 1
#define CONFIG_TRACING_SUPPORT 1
#define CONFIG_UNIX98_PTYS 1
#define CONFIG_NETFILTER_XT_TARGET_CONNMARK 1
#define CONFIG_SECURITY_SELINUX 1
#define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
#define CONFIG_NETFILTER_XT_MATCH_QUOTA 1
#define CONFIG_HAVE_KERNEL_LZO 1
#define CONFIG_NF_NAT_FTP 1
#define CONFIG_NF_CT_PROTO_UDPLITE 1
#define CONFIG_IKCONFIG_PROC 1
#define CONFIG_ELF_CORE 1
#define CONFIG_TEXTSEARCH 1
#define CONFIG_USB_SUPPORT 1
#define CONFIG_NETFILTER_XT_MATCH_QTAGUID 1
#define CONFIG_STAGING 1
#define CONFIG_MTD_CHAR 1
#define CONFIG_FLAT_NODE_MEM_MAP 1
#define CONFIG_VT_CONSOLE 1
#define CONFIG_BLK_DEV_RAM 1
#define CONFIG_NETFILTER_XT_MATCH_STATE 1
#define CONFIG_INET6_XFRM_MODE_BEET 1
#define CONFIG_PREEMPT 1
#define CONFIG_FB_CFB_COPYAREA 1
#define CONFIG_BINARY_PRINTF 1
#define CONFIG_GENERIC_CLOCKEVENTS_BUILD 1
#define CONFIG_TRACER_MAX_TRACE 1
#define CONFIG_SYSVIPC_SYSCTL 1
#define CONFIG_DECOMPRESS_GZIP 1
#define CONFIG_LLC 1
#define CONFIG_CROSS_COMPILE ""
#define CONFIG_NETWORK_SECMARK 1
#define CONFIG_NETFILTER_TPROXY 1
#define CONFIG_SWAP 1
#define CONFIG_NETFILTER_NETLINK 1
#define CONFIG_MODULE_UNLOAD 1
#define CONFIG_PREEMPT_COUNT 1
#define CONFIG_BITREVERSE 1
#define CONFIG_BLK_DEV_RAM_SIZE 8192
#define CONFIG_KEYBOARD_GOLDFISH_EVENTS 1
#define CONFIG_FB_MODE_HELPERS 1
#define CONFIG_CRYPTO_BLKCIPHER 1
#define CONFIG_NF_CONNTRACK 1
#define CONFIG_FILE_LOCKING 1
#define CONFIG_AIO 1
#define CONFIG_IP_NF_TARGET_REJECT 1
#define CONFIG_GENERIC_HARDIRQS 1
#define CONFIG_RTC_INTF_DEV 1
#define CONFIG_MTD_MAP_BANK_WIDTH_4 1
#define CONFIG_HID_SUPPORT 1
#define CONFIG_EXT4_FS_XATTR 1
#define CONFIG_NET_ACTIVITY_STATS 1
#define CONFIG_NLS_DEFAULT "iso8859-1"
#define CONFIG_NF_CT_PROTO_GRE 1
#define CONFIG_NF_CT_NETLINK 1
#define CONFIG_DEFAULT_SECURITY_SELINUX 1
#define CONFIG_CRYPTO_AEAD2 1
#define CONFIG_NETFILTER_XT_MATCH_HL 1
#define CONFIG_CRYPTO_ALGAPI2 1
#define CONFIG_ZBOOT_ROM_TEXT 0x0
#define CONFIG_HAVE_MEMBLOCK 1
#define CONFIG_INPUT 1
#define CONFIG_PROC_SYSCTL 1
#define CONFIG_MMU 1
#define CONFIG_HAVE_IRQ_WORK 1
#define CONFIG_KUSER_HELPERS 1
|
jweinst1/GoldScript | test/test_gs-item.c | #include "gs-item.h"
#include <stdlib.h>
static unsigned failures = 0;
#define CHECK(cond) if(!(cond) && ++failures) \
fprintf(stderr, "FAILURE: expression '%s', line %u\n", #cond, (unsigned)__LINE__)
static void test_golds_item_new_num(void)
{
golds_item_t* i1;
i1 = golds_item_new_num(45.6);
CHECK(i1 != NULL);
golds_item_del(i1);
}
static const char* TEST_STR = "test";
static void test_golds_item_new_str(void)
{
golds_item_t* i1;
i1 = golds_item_new_str(TEST_STR);
CHECK(i1 != NULL);
CHECK(i1->type == GOLDS_ITEM_TYPE_STR);
CHECK(GOLDSCRIPT_ITEM_STR_AT(i1, 0) == 't');
CHECK(GOLDSCRIPT_ITEM_STR_AT(i1, 1) == 'e');
CHECK(GOLDSCRIPT_ITEM_STR_AT(i1, 2) == 's');
CHECK(GOLDSCRIPT_ITEM_STR_AT(i1, 3) == 't');
CHECK(GOLDSCRIPT_ITEM_STR_AT(i1, 4) == '\0');
golds_item_del(i1);
}
int main(void) {
test_golds_item_new_num();
test_golds_item_new_str();
return failures ? 3 : 0;
} |
jweinst1/GoldScript | src/gs-linked.h | #ifndef SRC_GOLDSCRIPT_LINKED_H
#define SRC_GOLDSCRIPT_LINKED_H
#include "gs-memory.h"
/**
* @file This file provides an inheritable interface of a singly linked list.
*/
#define GOLDSCRIPT_LINKED_SYMBOL __gs_link
#define GOLDSCRIPT_LINKED_HEAD \
int type; \
struct GOLDSCRIPT_LINKED_SYMBOL* next
/**
* @brief Represents the base struct used in constructing linked list
* interfaces.
*/
struct GOLDSCRIPT_LINKED_SYMBOL
{
GOLDSCRIPT_LINKED_HEAD;
};
typedef struct GOLDSCRIPT_LINKED_SYMBOL golds_linked_t;
/**
* @brief Function pointer used for generic finds on linked lists.
*/
typedef int (*golds_linked_check)(const golds_linked_t*);
typedef void (*golds_linked_op)(golds_linked_t*);
#define GOLDSCRIPT_LINKED_HAS_NEXT(node) (node->next != NULL)
#define GOLDSCRIPT_LINKED_CAST(node) ((golds_linked_t*)node)
#define GOLDSCRIPT_LINKED_CONN(n1, n2) (n1->next = n2)
/**
* @brief Macro that consumes a ptr to a \c golds_linked_t* to advance it to
* the end.
*/
#define GOLDSCRIPT_LINKED_ADV_END(node) while((node) != NULL && (node)->next != NULL) node = (node)->next
size_t golds_linked_len(golds_linked_t* lst);
void golds_linked_put(golds_linked_t* lst, golds_linked_t* item);
void golds_linked_append(golds_linked_t* lst, golds_linked_t* item);
golds_linked_t* golds_linked_find(golds_linked_t* lst, golds_linked_check fn);
void golds_linked_apply_each(golds_linked_t* lst, golds_linked_op fn);
void golds_linked_del(golds_linked_t* lst);
#endif // SRC_GOLDSCRIPT_LINKED_H |
jweinst1/GoldScript | src/gs-parse.c | <filename>src/gs-parse.c
#include "gs-parse.h"
#define CASES_WHITE_SPACE \
case ' ': \
case '\n': \
case '\t':
static golds_item_t* _golds_parse_item(golds_parser_t* prs)
{
golds_item_t* parsed = NULL;
while(!prs->stop && !prs->has_err) {
switch(*(prs->data)) {
CASES_WHITE_SPACE
prs->data++;
break;
case '\0':
prs->stop =1;
goto end_parsing;
case '(':
break;
case '{':
break;
case '[':
break;
default:
sprintf(prs->err_mes,
"Expected item, found '%c'\n",
*(prs->data));
prs->has_err = 1;
return NULL;
}
}
end_parsing:
return parsed;
}
#undef CASES_WHITE_SPACE
golds_item_t* golds_parse_string(const char* code)
{
golds_item_t* item_prsd;
golds_parser_t parser;
GOLDSCRIPT_PARSER_INIT(parser, code);
item_prsd = _golds_parse_item(&parser);
return item_prsd;
} |
jweinst1/GoldScript | src/compiler-info.h | #ifndef SRC_GOLDSCIRPT_COMPILER_INFO_H
#define SRC_GOLDSCIRPT_COMPILER_INFO_H
#if defined(__STDC__)
# if defined(__STDC_VERSION__)
# if (__STDC_VERSION__ >= 199409L)
# if (__STDC_VERSION__ >= 199901L)
# if (__STDC_VERSION__ >= 201112L)
# define GOLDSCRIPT_CVERS_11
# else // !(__STDC_VERSION__ >= 201112L)
# define GOLDSCRIPT_CVERS_99
# endif // (__STDC_VERSION__ >= 201112L)
# else // !(__STDC_VERSION__ >= 199901L)
# define GOLDSCRIPT_CVERS_94
# endif // (__STDC_VERSION__ >= 199901L)
# else // (__STDC_VERSION__ >= 199409L)
# define GOLDSCRIPT_CVERS_90
# endif // !(__STDC_VERSION__ >= 199409L)
# else // defined(__STDC_VERSION__)
# define GOLDSCRIPT_CVERS_89
# endif // !defined(__STDC_VERSION__)
#else // defined(__STDC__)
# ifdef _WIN32
# pragma message("__STDC__ is not defined, this is a non-standard C version.")
# else
# warn "__STDC__ is not defined, this is a non-standard C version."
# endif
#endif
#if defined(__unix__) || defined(__APPLE__) && defined(__MACH__)
# define GOLDSCRIPT_OS_UNIX
#endif
#ifdef _WIN32
# define GOLDSCRIPT_OS_WIN
#endif
#ifdef SAPPHIRE_STD_C_1999
# define GOLDSCRIPT_HAS_VARIADIC_ARGS
# define GOLDSCRIPT_HAS_STDINT
#endif
#endif // SRC_GOLDSCIRPT_COMPILER_INFO_H |
jweinst1/GoldScript | test/test_gs-memory.c | #include "gs-memory.h"
#include <stdlib.h>
static unsigned failures = 0;
#define CHECK(cond) if(!(cond) && ++failures) \
fprintf(stderr, "FAILURE: expression '%s', line %u\n", #cond, (unsigned)__LINE__)
static void test_golds_mem_alloc(void)
{
void* p1 = NULL;
void* p2 = NULL;
p1 = golds_mem_malloc(50);
p2 = golds_mem_calloc(30);
CHECK(p1 != NULL);
CHECK(p2 != NULL);
CHECK(*((unsigned char*)p2) == 0);
free(p1);
free(p2);
}
static void test_golds_mem_new(void)
{
void* p1;
size_t len1;
size_t cap1;
len1 = 0;
cap1 = 0;
golds_mem_new(10, &p1, &len1, &cap1);
CHECK(p1 != NULL);
CHECK(len1 == 0);
CHECK(cap1 == 10);
free(p1);
}
int main(void)
{
test_golds_mem_alloc();
test_golds_mem_new();
return failures ? 3 : 0;
}
#undef CHECK
|
jweinst1/GoldScript | src/gs-linked.c | #include "gs-linked.h"
size_t golds_linked_len(golds_linked_t* lst)
{
size_t total = 0;
while(lst != NULL) {
total++;
lst = lst->next;
}
return total;
}
void golds_linked_put(golds_linked_t* lst, golds_linked_t* item)
{
if(lst != NULL) {
if(lst->next != NULL) {
// Assumes item is a single item, not another list.
item->next = lst->next;
lst->next = item;
} else {
lst->next = item;
}
}
}
void golds_linked_append(golds_linked_t* lst, golds_linked_t* item)
{
if(lst == NULL)
return;
while(lst->next != NULL)
lst = lst->next;
lst->next = item;
}
golds_linked_t* golds_linked_find(golds_linked_t* lst, golds_linked_check fn)
{
while(lst != NULL) {
if(fn(lst)) {
return lst;
}
lst = lst->next;
}
return NULL;
}
void golds_linked_apply_each(golds_linked_t* lst, golds_linked_op fn)
{
while(lst != NULL) {
fn(lst);
lst = lst->next;
}
}
void golds_linked_del(golds_linked_t* lst)
{
while(lst != NULL) {
free(lst);
lst = lst->next;
}
} |
jweinst1/GoldScript | src/gs-parse.h | <reponame>jweinst1/GoldScript<filename>src/gs-parse.h
#ifndef SRC_GOLDSCRIPT_PARSE_H
#define SRC_GOLDSCRIPT_PARSE_H
#include "gs-item.h"
#define GOLDSCRIPT_PARSER_MAX_ERR_LEN 256
/**
* @brief Acts as a parser container and state based object.
*/
typedef struct
{
char err_mes[GOLDSCRIPT_PARSER_MAX_ERR_LEN];
const char* data;
int stop;
int has_err;
} golds_parser_t;
#define GOLDSCRIPT_PARSER_INIT(prs, code, md) \
prs.stop = 0; \
prs.has_err = 0; \
prs.err_mes[0] = '\0'; \
prs.data = code
golds_item_t* golds_parse_string(const char* code);
#endif // SRC_GOLDSCRIPT_PARSE_H |
jweinst1/GoldScript | src/gs-memory.h | <reponame>jweinst1/GoldScript
#ifndef SRC_GOLDSCRIPT_MEMORY_H
#define SRC_GOLDSCRIPT_MEMORY_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GOLDSCRIPT_MEM_ERR_EXIT 3
/**
* @brief Cross platform macro for advancing a void pointer.
*/
#define GOLDSCRIPT_MEM_ADV(ptr, amnt) ((unsigned char*)(ptr) + amnt)
void* golds_mem_malloc(size_t size);
void* golds_mem_calloc(size_t size);
void golds_mem_new(size_t size, void** ptr, size_t* len, size_t* cap);
void golds_mem_grow(size_t size, void** ptr, size_t* len, size_t* cap);
#define GOLDSCRIPT_MEM_POOL(name, capacity) \
static size_t golds_mem_pool_##name##_len = 0; \
static size_t golds_mem_pool_##name##_cap = capacity; \
static void* golds_mem_pool_##name##_data = NULL
#define GOLDSCRIPT_MEM_POOL_LEN(name) (golds_mem_pool_##name##_len)
#define GOLDSCRIPT_MEM_POOL_CAP(name) (golds_mem_pool_##name##_cap)
#define GOLDSCRIPT_MEM_POOL_DATA(name) (golds_mem_pool_##name##_data)
#endif // SRC_GOLDSCRIPT_MEMORY_H |
jweinst1/GoldScript | src/gs-item.h | <reponame>jweinst1/GoldScript
#ifndef SRC_GOLDSCRIPT_ITEM_H
#define SRC_GOLDSCRIPT_ITEM_H
#include "gs-memory.h"
#ifndef GOLDSCRIPT_MAX_STR_LEN
#define GOLDSCRIPT_MAX_STR_LEN 25
#endif
typedef enum
{
GOLDS_ITEM_TYPE_BOOL,
GOLDS_ITEM_TYPE_NUMBER,
GOLDS_ITEM_TYPE_STR,
GOLDS_ITEM_TYPE_LST_RULE,
GOLDS_ITEM_TYPE_LST_CMD,
GOLDS_ITEM_TYPE_LST_COMP
} golds_item_type_t;
#define GOLDSCRIPT_ITEM_TYPE_IS_LST(type) ((type) == GOLDS_ITEM_TYPE_LST_CMD || \
(type) == GOLDS_ITEM_TYPE_LST_COMP || \
(type) == GOLDS_ITEM_TYPE_LST_RULE)
struct __gs_item;
typedef union
{
char _string[GOLDSCRIPT_MAX_STR_LEN + 1];
int _boolean;
double _number;
struct __gs_item* _lst;
} golds_item_val_t;
struct __gs_item
{
golds_item_type_t type;
golds_item_val_t val;
struct __gs_item* next;
};
typedef struct __gs_item golds_item_t;
#define GOLDSCRIPT_ITEM_HAS_NEXT(it) (it != NULL && it->next != NULL)
#define GOLDSCRIPT_ITEM_STR_AT(it, index) (it->val._string[index])
golds_item_t* golds_item_new_bool(int boolean);
golds_item_t* golds_item_new_num(double number);
golds_item_t* golds_item_new_lst(golds_item_t* insert, golds_item_type_t kind);
golds_item_t* golds_item_new_str(const char* text);
void golds_item_del(golds_item_t* item);
/**
* @brief Creates an \c item based on a specified type.
*/
golds_item_t* golds_item_new(golds_item_type_t type, void* data);
#endif // SRC_GOLDSCRIPT_ITEM_H |
jweinst1/GoldScript | src/gs-memory.c | #include "gs-memory.h"
#define _GOLDSCRIPT_MEM_CHECK(ptr, caller) if((ptr) == NULL) { \
fprintf(stderr, "Memory Error: Got NULL on call to '%s', exiting.\n", caller);\
exit(GOLDSCRIPT_MEM_ERR_EXIT);\
}
void* golds_mem_malloc(size_t size)
{
void* ptr = malloc(size);
_GOLDSCRIPT_MEM_CHECK(ptr, "goldscript_mem_malloc")
return ptr;
}
void* golds_mem_calloc(size_t size)
{
void* ptr = calloc(1, size);
_GOLDSCRIPT_MEM_CHECK(ptr, "goldscript_mem_calloc")
return ptr;
}
void golds_mem_new(size_t size, void** ptr, size_t* len, size_t* cap)
{
*ptr = calloc(0, size);
_GOLDSCRIPT_MEM_CHECK(*ptr, "goldscript_mem_new")
*cap = size;
}
void golds_mem_grow(size_t size, void** ptr, size_t* len, size_t* cap)
{
*cap += size;
*ptr = realloc(*ptr, *cap);
_GOLDSCRIPT_MEM_CHECK(*ptr, "goldscript_mem_grow")
} |
jweinst1/GoldScript | src/gs-item.c | <reponame>jweinst1/GoldScript<gh_stars>0
#include "gs-item.h"
#include <assert.h>
golds_item_t* golds_item_new_bool(int boolean)
{
golds_item_t* item = golds_mem_calloc(sizeof(golds_item_t));
item->type = GOLDS_ITEM_TYPE_BOOL;
item->val._boolean = boolean;
item->next = NULL;
return item;
}
golds_item_t* golds_item_new_num(double number)
{
golds_item_t* item = golds_mem_calloc(sizeof(golds_item_t));
item->type = GOLDS_ITEM_TYPE_NUMBER;
item->val._number = number;
item->next = NULL;
return item;
}
golds_item_t* golds_item_new_lst(golds_item_t* insert, golds_item_type_t kind)
{
assert(GOLDSCRIPT_ITEM_TYPE_IS_LST(kind));
golds_item_t* item = golds_mem_calloc(sizeof(golds_item_t));
item->type = kind;
item->val._lst = insert;
item->next = NULL;
return item;
}
golds_item_t* golds_item_new_str(const char* text)
{
unsigned i;
golds_item_t* item = golds_mem_calloc(sizeof(golds_item_t));
item->type = GOLDS_ITEM_TYPE_STR;
item->next = NULL;
for(i=0; i <= GOLDSCRIPT_MAX_STR_LEN && text[i] != '\0'; i++) {
item->val._string[i] = text[i];
}
item->val._string[i] = '\0';
return item;
}
void golds_item_del(golds_item_t* item)
{
if(item != NULL) {
if(item->next != NULL)
golds_item_del(item->next);
if(GOLDSCRIPT_ITEM_TYPE_IS_LST(item->type))
golds_item_del(item->val._lst);
free(item);
}
}
golds_item_t* golds_item_new(golds_item_type_t type, void* data)
{
switch(type) {
case GOLDS_ITEM_TYPE_BOOL:
return golds_item_new_bool(*(int*)data);
case GOLDS_ITEM_TYPE_NUMBER:
return golds_item_new_num(*(double*)data);
case GOLDS_ITEM_TYPE_STR:
return golds_item_new_str((const char*)data);
case GOLDS_ITEM_TYPE_LST_RULE:
case GOLDS_ITEM_TYPE_LST_CMD:
case GOLDS_ITEM_TYPE_LST_COMP:
return golds_item_new_lst((golds_item_t*)data, type);
}
return NULL;
} |
zonca/aptpac | C-edition/src/main.c | <reponame>zonca/aptpac<gh_stars>0
/*********************
MIT License
Copyright (c) 2021 <NAME>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*********************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h" //VER defined in "functions.h"
int main(int argc, char **argv) {
char command[101], cmdflags[4097]="";
int LEARN=0;
struct config *conf=config_init();
//set the 'APTPAC_LEARN' env var to "0" (yes, a string) to avoid a segfault in getenv if it isn't set
//set its value to 0, and don't overwrite it if it already exists.
setenv("APTPAC_LEARN", "0", 0);
//activate learning mode if env var 'APTPAC_LEARN' = 1
char *learn_env=getenv("APTPAC_LEARN");
//load the configuration
config_load(conf);
if(!strcmp(learn_env, "1")||conf->learn==1) {
LEARN=1;
}
while(argc>1) {
if(!strcasecmp(argv[1], "--config")) {
if(argc!=4) {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m not enough options for option 'config' provided!\e[0m\n");
free(conf);
return 1;
}
if(!strcmp(argv[2], "set")) {
if(!strcmp(argv[3], "learn")) {
conf->learn=1;
} else {
fprintf(stderr, "\e[1;31mERROR: \e[0;31minvalid configuration option '%s'!\e[0m\n", argv[3]);
config_free(conf);
return 1;
}
} else if(!strcmp(argv[2], "unset")) {
if(!strcmp(argv[3], "learn")) {
conf->learn=0;
} else {
fprintf(stderr, "\e[1;31mERROR: \e[0;31minvalid configuration option '%s'!\e[0m\n", argv[3]);
config_free(conf);
return 1;
}
} else {
fprintf(stderr, "\e[1;31mERROR:\e[0;31m invalid config mode!\e[0m\n");
config_free(conf);
return 1;
}
if(config_save(conf)) {
fprintf(stderr, "\e[1;31mERROR:\e[0;31m Failed to write config changes!\e[0m\n");
free(conf);
return 1;
} else {
printf("\e[1mConfiguration '%s' %s succesfully!\e[0m\n", argv[3], argv[2]);
config_free(conf);
return 0;
}
} else if(!strcasecmp(argv[1], "install")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m '%s' option passed, but no app provided!\e[0m\n", argv[1]);
break;
}
strcpy(command, "sudo pacman -S ");
strcat(command, cmdflags);
//if learning mode is on, print the command being run using the 'learn' function
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "install-local")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'install-local' option passed, but no package(s) provided!\e[0m\n");
break;
}
strcpy(command, "sudo pacman -U ");
strcat(command, cmdflags);
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "remove") || !strcasecmp(argv[1], "uninstall")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'install' option passed, but no app provided!\e[0m\n");
break;
}
strcpy(command, "sudo pacman -Rs ");
strcat(command, cmdflags);
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "purge")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'purge' option passed, but no app provided!\e[0m\n");
break;
}
strcpy(command, "sudo pacman -Rn ");
strcat(command, cmdflags);
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "search")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'search' option passed, but no search string provided!\e[0m\n");
break;
}
strcpy(command, "pacman -Ss ");
strcat(command, cmdflags);
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "find")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'find' option passed, but no search string provided!\e[0m\n");
break;
}
strcpy(command, "pacman -F ");
strcat(command, cmdflags);
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "update")) {
learn("sudo pacman -Sy", LEARN);
system("sudo pacman -Sy");
break;
} else if(!strcasecmp(argv[1], "upgrade")) {
learn("sudo pacman -Su", LEARN);
system("sudo pacman -Su");
break;
} else if(!strcasecmp(argv[1], "full-upgrade")) {
learn("sudo pacman -Syu", LEARN);
system("sudo pacman -Syu");
break;
} else if(!strcasecmp(argv[1], "clean") || !strcasecmp(argv[1], "autoclean")) {
learn("sudo pacman -Scc", LEARN);
system("sudo pacman -Scc");
break;
} else if(!strcasecmp(argv[1], "autoremove")) {
learn("sudo pacman -Qdtq | sudo pacman -Rs -", LEARN);
system("sudo pacman -Qdtq | sudo pacman -Rs -");
break;
} else if(!strcasecmp(argv[1], "list-installed")) {
learn(command, LEARN);
system("pacman -Qqe");
break;
} else if(!strcasecmp(argv[1], "show")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
#ifdef DEBUG
debug("comdflags", cmdflags);
#endif
strcpy(command, "pacman -Qi ");
strcat(command, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'show' option passed, but no package provided \e[0m\n");
break;
}
learn(command, LEARN);
#ifdef DEBUG
debug("command", command);
#endif
system(command);
break;
} else if(!strcasecmp(argv[1], "show-all")) {
if(argv[2]) {
get_cmdargs(argc, argv, 2, cmdflags);
strcpy(command, "pacman -Si ");
strcat(command, cmdflags);
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m\e[31m 'show-all' option passed, but no package provided \e[0m\n");
break;
}
learn(command, LEARN);
system(command);
break;
} else if(!strcasecmp(argv[1], "help") || !strcasecmp(argv[1], "-help") || !strcasecmp(argv[1], "--help") || !strcasecmp(argv[1], "-h")) {
help();
break;
} else if(!strcasecmp(argv[1], "version") || !strcasecmp(argv[1], "-v") || !strcasecmp(argv[1], "--version")) {
about();
break;
} else {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m \e[31minvalid option \"%s\"!\e[0m\n", argv[1]);
break;
}
}
if(argc==1) {
fprintf(stderr, "\e[31m\e[1mERROR:\e[0m \e[31mno operation specified!\e[0m\n");
fprintf(stderr, "\e[1mrun \"%s --help\" for help\e[0m\n", CALLCOMMAND);
//about();
//echo("");
//help();
}
config_free(conf);
return 0;
}
|
CISVVC/cis208-expression-project-Sprigg-Matthew | main.c | <gh_stars>0
/*
* file: main.c
* main C program that uses assembly routine in prog.asm
* to create executable:
* gcc: gcc -m32 -o main main.c asm_main.o asm_io.o
*/
#include "cdecl.h"
#include <stdio.h>
#define A 2471
#define B 626
#define C 200
#define D 333
int PRE_CDECL asm_main( int a, int b, int c, int d) POST_CDECL;
int main()
{
int res = asm_main(A, B, C, D);
printf("%d\n",res);
return 0;
}
|
NemoChenTW/Sample-FunctionPassing | LiveMsgTool/LiveMsgTool.h | <gh_stars>0
/*
* LiveMsgTool.h
*
* Created on: 2015年12月15日
* Author: nemo
*/
#ifndef LIVEMSGTOOL_LIVEMSGTOOL_H_
#define LIVEMSGTOOL_LIVEMSGTOOL_H_
#include <string>
#include <iostream>
using namespace std;
class LiveMsgTool {
private:
string msgEntry;
string msgExit;
public:
LiveMsgTool();
virtual ~LiveMsgTool();
void EntryAlive()
{
cout << "run EntryAlive." << endl;
msgEntry = "Entry";
}
void ExitAlive()
{
cout << "run ExitAlive." << endl;
msgExit = "Exit";
}
void showMsg();
};
#endif /* LIVEMSGTOOL_LIVEMSGTOOL_H_ */
|
NemoChenTW/Sample-FunctionPassing | MiTACCSC/MiTACCSC.h | /*
* MiTACCSC.h
*
* Created on: 2015年12月15日
* Author: nemo
*/
#ifndef MITACCSC_MITACCSC_H_
#define MITACCSC_MITACCSC_H_
#include <stddef.h>
#include <iostream>
#include <functional>
using namespace std;
class MiTACCSC {
private:
function<void(void)> funPtr;
public:
MiTACCSC();
virtual ~MiTACCSC();
void setFunPtr(function<void(void)> fun);
void runFun();
};
#endif /* MITACCSC_MITACCSC_H_ */
|
ken0x0a/rust-mac-app-examples | 6-create-rust-lib-in-cocoa-app/app/BridgingHeader.h | //
// BridgingHeader.h
// RustPack
//
// Created by Delisa on 11/15/16.
// Copyright © 2016 <NAME>. All rights reserved.
//
#ifndef BridgingHeader_h
#define BridgingHeader_h
#include "cruncher.h"
#endif /* BridgingHeader_h */
|
ken0x0a/rust-mac-app-examples | 6-create-rust-lib-in-cocoa-app/src/cruncher.h | <gh_stars>100-1000
//
// cruncher.h
// RustPack
//
// Created by Delisa on 11/15/16.
// Copyright © 2016 <NAME>. All rights reserved.
//
#ifndef cruncher_h
#define cruncher_h
#include <stdint.h>
int32_t square(int32_t num);
int32_t cube(int32_t num);
#endif /* cruncher_h */
|
UsrLightmann/MsgSwap | Tweak/MsgSwapFooter.h | <filename>Tweak/MsgSwapFooter.h
#import <UIKit/UIKit.h>
// https://stackoverflow.com/a/5337804
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface IMBalloonPlugin : NSObject
@end
@interface IMBalloonPluginManager : NSObject
@property (nonatomic,retain) NSMutableDictionary * pluginsMap;
+(instancetype)sharedInstance;
@end
@interface CKBalloonPluginManager : NSObject
+(instancetype)sharedInstance;
-(void)prepareForSuspend;
@end
@interface CKBrowserPluginCell : UICollectionViewCell
@property (nonatomic,retain) IMBalloonPlugin * plugin;
-(void)setPlugin:(IMBalloonPlugin *)arg1;
@property (nonatomic,retain) UIImageView * browserImage;
-(void)setBrowserImage:(UIImageView *)arg1 ;
@end
@interface CKAppStripLayout : UICollectionViewLayout
@end
@interface CKMessageEntryContentView : UIView
@end
@interface CKBrowserSwitcherFooterView : UIView <UICollectionViewDelegate, UICollectionViewDataSource>
@property (assign,nonatomic) id delegate;
@property (assign,nonatomic) id dataSource;
@property (assign,nonatomic) BOOL isMagnified;
@property (assign,nonatomic) BOOL hideShinyStatus;
@property (assign,nonatomic) BOOL showBorders;
@property (nonatomic,retain) CKAppStripLayout * appStripLayout;
@property (assign,nonatomic) BOOL minifiesOnSelection;
@property (assign,nonatomic) BOOL isMinifyingOnTranscriptScroll;
@property (assign,nonatomic) double snapshotVerticalOffset;
@property (nonatomic,retain) CKMessageEntryContentView * contentView;
-(UICollectionView*)collectionView;
@end
@interface CKEntryViewButton : UIButton // UIView (iOS 13) || UIButton (iOS 12)
@property (nonatomic,retain) UIButton * button; // iOS 13
@end
@interface MsgSwapFooter : CKBrowserSwitcherFooterView
@property (nonatomic,retain) UICollectionView * collectionView;
@property (nonatomic,retain) CKBrowserPluginCell * cell;
@property (nonatomic,retain) CKEntryViewButton * cameraButton;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
-(CKBrowserPluginCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
-(void)deselectCell;
-(void)clickCameraButton:(UILongPressGestureRecognizer*)gesture;
@end
@interface CKMessageEntryView : UIView
@property (nonatomic,retain) CKEntryViewButton * photoButton; // camera
@property (nonatomic,retain) CKEntryViewButton * browserButton; // toggles appstrip
@property (nonatomic,retain) CKEntryViewButton * arrowButton; // caret
@property (nonatomic,retain) CKBrowserSwitcherFooterView * appStrip;
@property (nonatomic,retain) UIView * inputButtonContainerView;
@property (nonatomic,retain) CKMessageEntryContentView * contentView;
@property (nonatomic, retain) MsgSwapFooter *footer;
@property (nonatomic) BOOL caretState;
-(void)messageEntryContentViewWasTapped:(id)arg1 isLongPress:(BOOL)arg2 ;
-(void)unleashTheRabbit;
-(void)recollectTheRabbit;
-(void)secondTap;
@end |
NSBum/si7021_test | main/main.c | <filename>main/main.c
// FreeRTOS includes
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// I2C driver
#include "driver/i2c.h"
// Error library
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_system.h"
#include <stdio.h>
#include "si7021.h"
#define I2C_SDA 21 // GPIO_NUM_21
#define I2C_SCL 22 // GPIO_NUM_22
void query_sensor_task(void *pvParameter) {
while(1) {
float temp = si7021_read_temperature();
float hum = si7021_read_humidity();
printf("%0.2f degrees C, %0.2f%% RH\n", temp, hum);
vTaskDelay(5000 / portTICK_RATE_MS);
}
}
// application entry point
int app_main(void) {
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// initialize I2C driver/device
ret = si7021_init(I2C_NUM_0, I2C_SDA, I2C_SCL,
GPIO_PULLUP_DISABLE,GPIO_PULLUP_DISABLE);
ESP_ERROR_CHECK(ret);
printf("I2C driver initialized\n");
xTaskCreate(&query_sensor_task,
"sensor_task",
2048,
NULL,
5,
NULL);
return 0;
}
|
NSBum/si7021_test | components/si7021/si7021.h | <reponame>NSBum/si7021_test
/*
* HTU21D Component
*
* esp-idf component to interface with HTU21D humidity and temperature sensor
* by TE Connectivity (http://www.te.com/usa-en/product-CAT-HSC0004.html)
*
* <NAME>, www.lucadentella.it
*/
// Error library
#include "esp_err.h"
// I2C driver
#include "driver/i2c.h"
// FreeRTOS (for delay)
#include "freertos/task.h"
#ifndef __ESP_SI7021_H__
#define __ESP_SI7021_H__
// sensor address
#define SI7021_ADDR 0x40
#define SI7021_MEASRH_HOLD_CMD 0xE5
#define SI7021_MEASRH_NOHOLD_CMD 0xF5
#define SI7021_MEASTEMP_HOLD_CMD 0xE3 // TRIGGER_TEMP_MEASURE_HOLD
#define SI7021_MEASTEMP_NOHOLD_CMD 0xF3
#define SI7021_READPREVTEMP_CMD 0xE0
#define SI7021_RESET_CMD 0xFE
#define SI7021_WRITERHT_REG_CMD 0xE6
#define SI7021_READRHT_REG_CMD 0xE7
#define SI7021_WRITEHEATER_REG_CMD 0x51
#define SI7021_READHEATER_REG_CMD 0x11
#define SI7021_ID1_CMD 0xFA0F
#define SI7021_ID2_CMD 0xFCC9
#define SI7021_FIRMVERS_CMD 0x84B8
// HTU21D commands
#define TRIGGER_TEMP_MEASURE_HOLD 0xE3
#define TRIGGER_HUMD_MEASURE_HOLD 0xE5
#define TRIGGER_TEMP_MEASURE_NOHOLD 0xF3
#define TRIGGER_HUMD_MEASURE_NOHOLD 0xF5
#define WRITE_USER_REG 0xE6
#define READ_USER_REG 0xE7
#define SOFT_RESET 0xFE
// return values
#define SI7021_ERR_OK 0x00
#define SI7021_ERR_CONFIG 0x01
#define SI7021_ERR_INSTALL 0x02
#define SI7021_ERR_NOTFOUND 0x03
#define SI7021_ERR_INVALID_ARG 0x04
#define SI7021_ERR_FAIL 0x05
#define SI7021_ERR_INVALID_STATE 0x06
#define SI7021_ERR_TIMEOUT 0x07
// variables
i2c_port_t _port;
// functions
int si7021_init(i2c_port_t port, int sda_pin, int scl_pin, gpio_pullup_t sda_internal_pullup, gpio_pullup_t scl_internal_pullup);
float si7021_read_temperature();
float si7021_read_humidity();
uint8_t si7021_get_resolution();
int si7021_set_resolution(uint8_t resolution);
int si7021_soft_reset();
// helper functions
uint8_t si7021_read_user_register();
int si7021_write_user_register(uint8_t value);
uint16_t read_value(uint8_t command);
bool is_crc_valid(uint16_t value, uint8_t crc);
#endif // __ESP_SI7021_H__
|
DMDavid/DMPieChart | DMPieChart/DMPieChart/DMPieChart/DMPieChartView.h | //
// DMPieChartView.h
// DMPieChart
//
// Created by David on 16/5/22.
// Copyright © 2016年 OrangeCat. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "DMPieChartModel.h"
@class DMPieChartView;
@protocol DMPieChartViewDelegate <NSObject>
- (void)pieView:(DMPieChartView *)pieView didSelectSectionAtIndex:(NSInteger)index;
@end
@interface DMPieChartView : UIView
@property (nonatomic, weak) id<DMPieChartViewDelegate> delegate;
#pragma mark - publice methods
/**
* init
*
* @param frame frame
* @param pieChartModels 数组,类型: @[DMPieChartModel]
*
* @return 实体
*/
- (instancetype)initWithFrame:(CGRect)frame pieChartModels:(NSArray *)pieChartModels;
/**
* 重置
*/
- (void)reloadData;
#pragma mark - Layer
/**
* 设置背景图层颜色(default is green)
*
* @param backgroundColor 颜色
*/
- (void)showBackgroundLayerWithColor:(UIColor *)backgroundColor;
#pragma mark - Label
- (void)isShowCenterTipLabel:(BOOL)isShow;
- (void)isShowSubTipLabel:(BOOL)isShow;
//Center Label
- (void)setCenterTipLabelWithValue:(CGFloat)value;
- (void)setCenterTipLabelForFont:(UIFont *)font textColor:(UIColor *)textColor;
//Sub Label
- (void)setSubLabelForFont:(UIFont *)font textColor:(UIColor *)textColor;
@end
|
DMDavid/DMPieChart | DMPieChart/DMPieChart/ViewController.h | //
// ViewController.h
// DMPieChart
//
// Created by David on 16/5/22.
// Copyright © 2016年 OrangeCat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
|
DMDavid/DMPieChart | DMPieChart/DMPieChart/DMPieChart/DMPieChartModel.h | //
// DMPieChartModel.h
// DMPieChart
//
// Created by David on 16/5/22.
// Copyright © 2016年 OrangeCat. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DMPieChartModel : NSObject
@property(nonatomic, copy) NSString *name;
@property(nonatomic, strong) NSNumber *value;
@property(nonatomic, strong) UIColor *color;
@property(nonatomic, strong) NSNumber *totalValue;
#pragma mark - 计算后值
@property(nonatomic, assign) CGFloat percentage; //百分比
@property(nonatomic, assign) CGFloat startAngle; //开始角度
@property(nonatomic, assign) CGFloat endAngle;
- (instancetype)initWithName:(NSString *)name value:(NSNumber *)value color:(UIColor *)color totalValue:(NSNumber *)totalValue;
@end
|
2bbb/ofxTMP | src/ofxTMPVersionMacro.h | <filename>src/ofxTMPVersionMacro.h<gh_stars>0
//
// ofxTMPVersionMacro.h
//
// Created by ISHII 2bit on 2016/04/28.
//
//
#pragma once
#include "ofConstants.h"
#define OFX_MAKE_OF_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch)
#define OFX_OF_VERSION OFX_MAKE_OF_VERSION(OF_VERSION_MAJOR, OF_VERSION_MINOR, OF_VERSION_PATCH)
#define OFX_THIS_OF_IS_OLDER_THAN(major, minor, patch) (OFX_OF_VERSION < OFX_MAKE_OF_VERSION(major, minor, patch))
#define OFX_THIS_OF_IS_OLDER_THAN_EQ(major, minor, patch) (OFX_OF_VERSION <= OFX_MAKE_OF_VERSION(major, minor, patch))
#define OFX_THIS_OF_IS_NEWER_THAN(major, minor, patch) (OFX_MAKE_OF_VERSION(major, minor, patch) < OFX_OF_VERSION)
#define OFX_THIS_OF_IS_NEWER_THAN_EQ(major, minor, patch) (OFX_MAKE_OF_VERSION(major, minor, patch) <= OFX_OF_VERSION)
|
2bbb/ofxTMP | src/ofxTMPFunctionTraits.h | //
// ofxTMPFunctionTraits.h
// ofxTMPExample
//
// Created by ISHII 2bit on 2016/04/28.
//
//
#include <type_traits>
#include <tuple>
#include <functional>
namespace ofx {
namespace TMP {
namespace function_info {
namespace detail {
template <typename ret, typename ... arguments>
struct function_traits {
static constexpr std::size_t arity = sizeof...(arguments);
using result_type = ret;
using arguments_types_tuple = std::tuple<arguments ...>;
template <std::size_t index>
using argument_type = type_at<index, arguments ...>;
using function_type = std::function<ret(arguments ...)>;
template <typename function_t>
static constexpr function_type cast(function_t f) {
return static_cast<function_type>(f);
}
};
};
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> {};
template <typename class_type, typename ret, typename ... arguments>
struct function_traits<ret(class_type::*)(arguments ...) const>
: detail::function_traits<ret, arguments ...> {};
template <typename class_type, typename ret, typename ... arguments>
struct function_traits<ret(class_type::*)(arguments ...)>
: detail::function_traits<ret, arguments ...> {};
template <typename ret, typename ... arguments>
struct function_traits<ret(*)(arguments ...)>
: detail::function_traits<ret, arguments ...> {};
template <typename ret, typename ... arguments>
struct function_traits<ret(arguments ...)>
: detail::function_traits<ret, arguments ...> {};
template <typename ret, typename ... arguments>
struct function_traits<std::function<ret(arguments ...)>>
: detail::function_traits<ret, arguments ...> {};
template<typename T>
using result_type = typename function_traits<T>::result_type;
template<typename T>
using arguments_types_tuple = typename function_traits<T>::arguments_types_tuple;
template<typename T, std::size_t index>
using argument_type = typename function_traits<T>::template argument_type<index>;
template<typename T>
struct arity {
static constexpr std::size_t value = function_traits<T>::arity;
};
template <typename patient>
struct has_call_operator {
template <typename inner_patient, decltype(&inner_patient::operator())> struct checker {};
template <typename inner_patient> static std::true_type check(checker<inner_patient, &inner_patient::operator()> *);
template <typename> static std::false_type check(...);
using type = decltype(check<patient>(nullptr));
static constexpr bool value = type::value;
};
template <typename patient>
constexpr bool is_callable(const patient &) {
return std::is_function<patient>::value || has_call_operator<patient>::value;
};
template <typename function_t>
constexpr auto cast_lambda(function_t f)
-> typename function_traits<function_t>::function_type {
return static_cast<typename function_traits<function_t>::function_type>(f);
}
};
using namespace function_info;
};
};
|
2bbb/ofxTMP | src/ofxTMPAlias.h | //
// ofxTMPAlias.h
//
// Created by ISHII 2bit on 2016/04/29.
//
//
#pragma once
namespace ofx {
namespace TMP {
template <typename>
struct alias {};
namespace detail {
template <typename T>
struct remove_alias { using type = T; };
template <typename T>
struct remove_alias<alias<T>> { using type = T; };
template <typename T>
struct remove_aliases { using type = T; };
template <typename T>
struct remove_aliases<alias<T>> { using type = typename remove_alias<T>::type; };
};
template <typename T>
using remove_alias = get_type<TMP::detail::remove_alias<T>>;
template <typename T>
using remove_aliases = get_type<TMP::detail::remove_aliases<T>>;
};
};
|
2bbb/ofxTMP | src/ofxTMPTypeTraits.h | //
// ofxTMPTypeTraits.h
//
// Created by ISHII 2bit on 2016/04/28.
//
//
#pragma once
#include <type_traits>
#include <array>
namespace ofx {
namespace TMP {
struct default_traits {
static constexpr size_t size = 1;
static constexpr bool has_subscript_operator = false;
};
template <std::size_t size_, typename T = float>
struct array_traits {
using inner_type = T;
static constexpr std::size_t size = size_;
static constexpr bool has_subscript_operator = true;
};
template <typename T>
struct type_traits : default_traits {
using inner_type = T;
};
template <typename T>
struct type_traits<ofColor_<T>> : array_traits<4, T> {};
template <>
struct type_traits<ofVec2f> : array_traits<2> {};
template <>
struct type_traits<ofVec3f> : array_traits<3> {};
template <>
struct type_traits<ofVec4f> : array_traits<4> {};
template <>
struct type_traits<ofQuaternion> : array_traits<4> {};
template <>
struct type_traits<ofMatrix3x3> : array_traits<9> {
static constexpr bool has_subscript_operator = false; // because don't has "float operator[](int n) const"
};
template <>
struct type_traits<ofMatrix4x4> : array_traits<16> {
static constexpr bool has_subscript_operator = false;
};
template <>
struct type_traits<ofRectangle> {
using inner_type = float;
static constexpr size_t size = 4;
static constexpr bool has_subscript_operator = false;
};
template <typename T, size_t array_size>
struct type_traits<T[array_size]> {
using inner_type = T;
static constexpr size_t size = type_traits<T>::size * array_size;
static constexpr bool has_subscript_operator = true;
};
template <typename T, size_t array_size>
struct type_traits<std::array<T, array_size>> {
using inner_type = T;
static constexpr size_t size = type_traits<T>::size * array_size;
static constexpr bool has_subscript_operator = true;
};
};
};
|
2bbb/ofxTMP | src/ofxTMPSequence.h | //
// ofxTMPSequence.h
//
// Created by ISHII 2bit on 2016/04/28.
//
//
#pragma once
namespace ofx {
namespace TMP {
namespace sequences {
template <typename type, type ... ns>
struct integer_sequence {
using value_type = type;
static constexpr std::size_t size = sizeof...(ns);
};
namespace detail {
template <typename integer_type, integer_type n, integer_type ... ns>
struct make_integer_sequence {
struct sequence_wrapper { using type = integer_sequence<integer_type, ns ...>; };
using type = get_type<conditional<
n == 0,
sequence_wrapper,
detail::make_integer_sequence<integer_type, n - 1, n - 1, ns ...>
>>;
};
};
template <typename type, type n>
using make_integer_sequence = detail::make_integer_sequence<type, n>;
template <std::size_t ... ns>
using index_sequence = integer_sequence<std::size_t, ns ...>;
template <std::size_t n>
using make_index_sequence = make_integer_sequence<std::size_t, n>;
template <typename... types>
using index_sequence_for = make_index_sequence<sizeof...(types)>;
};
using namespace sequences;
};
}; |
2bbb/ofxTMP | src/ofxTMP.h | //
// ofxTMP.h
//
// Created by ISHII 2bit on 2015/11/19.
//
//
#pragma once
#include "ofxTMPUtils.h"
#include "ofxTMPVersionMacro.h"
#include "ofxTMPTypeTraits.h"
#include "ofxTMPFunctionTraits.h"
#include "ofxTMPSequence.h"
#include "ofxTMPAlias.h"
namespace ofxTMP = ofx::TMP;
|
2bbb/ofxTMP | src/ofxTMPUtils.h | //
// ofxTMPUtils.h
//
// Created by ISHII 2bit on 2016/04/28.
//
//
#pragma once
#include <type_traits>
#include <tuple>
#include <functional>
#if __cplusplus < 201103L
# error all you need is C++11 (or later)
#elif __cplusplus < 201402L
# define bbb_is_cpp11 true
# define bbb_is_cpp14 false
#else
# define bbb_is_cpp11 true
# define bbb_is_cpp14 true
#endif
namespace ofx {
namespace TMP {
constexpr bool is_cpp11() { return bbb_is_cpp11; }
constexpr bool is_cpp14() { return bbb_is_cpp14; }
template <typename T>
using get_type = typename T::type;
template <bool b, typename T = void>
using enable_if = get_type<std::enable_if<b, T>>;
template <bool b, typename T, typename F>
using conditional = get_type<std::conditional<b, T, F>>;
template <typename T, typename U>
constexpr bool is_same() { return std::is_same<T, U>::value; };
template <typename T>
constexpr bool is_const() {
return std::is_const<T>::value;
}
template <typename T>
constexpr bool is_number() {
return std::is_arithmetic<T>::value;
}
template <typename T>
constexpr bool is_integer() {
return std::is_integral<T>::value;
}
template <typename T>
constexpr bool is_float() {
return std::is_floating_point<T>::value;
}
template <typename T>
using remove_const_reference_if_number = get_type<std::conditional<
is_number<T>(),
get_type<std::remove_reference<
get_type<std::remove_const<T>>
>>,
T
>>;
template <typename T>
using add_const_reference_if_not_number = get_type<std::conditional<
!is_number<T>(),
get_type<std::add_const<
get_type<std::add_lvalue_reference<T>>
>>,
T
>>;
template <std::size_t index, typename ... arguments>
using type_at = get_type<std::tuple_element<index, std::tuple<arguments ...>>>;
template <bool condition, template <typename ...> class t, template <typename ...> class f>
struct template_conditional {
template <typename ... arguments>
using type = get_type<std::conditional<condition, t<arguments ...>, f<arguments ...>>>;
};
};
}; |
yozoe/PullToRefreshKit | PullToRefreshKit/PullToRefreshKit.h | <gh_stars>100-1000
//
// PullToRefreshKit.h
// PullToRefreshKit
//
// Created by Leo on 2017/6/30.
// Copyright © 2017年 <NAME>. All rights reserved.
//
#import <UIKit/UIKit.h>
//! Project version number for PullToRefreshKit.
FOUNDATION_EXPORT double PullToRefreshKitVersionNumber;
//! Project version string for PullToRefreshKit.
FOUNDATION_EXPORT const unsigned char PullToRefreshKitVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <PullToRefreshKit/PublicHeader.h>
|
Bennygmate/Deep-Learning | deep learning/neural_networks/Convolution.h | //Bennygmate
#include "TMatrix.h"
namespace neurons{
class Conv_1d {
private:
mutable TMatrix<> m_diff_to_w;
Shape m_input_sh;
Shape m_weights_sh;
lint m_stride;
public:
Conv_1d(const Shape & input_shape, const Shape & weights_shape, lint stride = 1);
Conv_1d();
~Conv_1d();
TMatrix<> operator () (const TMatrix<> & input, const TMatrix<> & weights, const TMatrix<> & bias);
TMatrix<> & get_diff_to_weights() const;
};
class Conv_2d {
private:
Shape m_input_sh;
Shape m_ex_in_sh;
Shape m_weights_sh;
Shape m_output_sh;
lint m_r_stride;
lint m_c_stride;
lint m_r_zero_p;
lint m_c_zero_p;
mutable TMatrix<> m_diff_to_w;
mutable TMatrix<> m_diff_to_x;
public:
Conv_2d(const Shape & input_shape, const Shape & weights_shape, lint r_stride = 1, lint c_stride = 1, lint r_zero_p = 0, lint c_zero_p = 0);
Conv_2d();
~Conv_2d();
TMatrix<> operator () (const TMatrix<> & input, const TMatrix<> & weights, const TMatrix<> & bias);
TMatrix<> & get_diff_to_weights() const;
TMatrix<> & get_diff_to_input() const;
Shape get_output_shape() const;
public:
TMatrix<> zero_padding(const TMatrix<> & input);
lint r_stride() const { return this->m_r_stride; }
lint c_stride() const { return this->m_c_stride; }
lint r_zero_p() const { return this->m_r_zero_p; }
lint c_zero_p() const { return this->m_c_zero_p; }
};
class Conv_3d {};
}
|
Bennygmate/Deep-Learning | deep learning/neural_networks/NN.h | // Bennygmate
#include "TMatrix.h"
#include "Functions.h"
#include "NN_layer.h"
#include "Dataset.h"
#include <iostream>
#include <random>
class NN {
private:
std::uniform_int_distribution<size_t> m_train_distribution;
std::uniform_int_distribution<size_t> m_test_distribution;
protected:
double m_l_rate;
double m_mmt_rate;
lint m_threads;
std::string m_model_file;
std::vector<neurons::TMatrix<>> m_train_set;
std::vector<neurons::TMatrix<>> m_train_labels;
std::vector<neurons::TMatrix<>> m_test_set;
std::vector<neurons::TMatrix<>> m_test_labels;
std::vector<std::shared_ptr<neurons::NN_layer>> m_layers;
public:
NN(double l_rate, double mmt_rate, lint threads, const std::string & model_file, const dataset::Dataset &d_set);
~NN();
NN(const NN & other) = delete;
NN(NN && other) = delete;
NN & operator = (const NN & other) = delete;
NN & operator = (NN && other) = delete;
public:
virtual void print_layers(std::ostream & os) const = 0;
virtual void save_layers_as_images() const = 0;
lint n_layers() const;
void print_train_set(std::ostream & os) const;
void print_train_label(std::ostream & os) const;
void print_test_set(std::ostream & os) const;
void print_test_label(std::ostream & os) const;
public:
void train_network(lint batch_size, lint epoch_size, lint epochs, lint epochs_between_saves,lint secs_allowed);
void test_network(lint batch_size, lint epoch_size);
std::vector<neurons::TMatrix<>> network_predict(lint batch_size, const std::vector<neurons::TMatrix<>> & inputs) const;
virtual bool load(const std::string & file_name) = 0;
virtual bool load_until(const std::string & file_name, lint layer_index) = 0;
virtual void initialize_model() = 0;
virtual void save(const std::string & file_name) const = 0;
private:
void get_batch(lint batch_size,
std::vector<std::vector<neurons::TMatrix<>>> & data_batch,
std::vector<std::vector<neurons::TMatrix<>>> & label_batch,
const std::vector<neurons::TMatrix<>> & data,
const std::vector<neurons::TMatrix<>> & label,
std::uniform_int_distribution<size_t> & distribution);
double train_step(lint batch_size,
const std::vector<std::vector<neurons::TMatrix<>>> & inputs,
const std::vector<std::vector<neurons::TMatrix<>>> & targets,
std::vector<std::vector<neurons::TMatrix<>>> & preds);
double test_step(lint batch_size,
const std::vector<std::vector<neurons::TMatrix<>>> & inputs,
const std::vector<std::vector<neurons::TMatrix<>>> & targets,
std::vector<std::vector<neurons::TMatrix<>>> & preds);
std::vector<std::vector<neurons::TMatrix<>>> predict_step(lint batch_size,
const std::vector<std::vector<neurons::TMatrix<>>> & inputs) const;
double get_accuracy(const neurons::TMatrix<> & pred, const neurons::TMatrix<> & target);
double get_accuracy(lint batch_size,
const std::vector<std::vector<neurons::TMatrix<>>> & preds,
const std::vector<std::vector<neurons::TMatrix<>>> & targets);
virtual std::vector<neurons::TMatrix<>> test(
const std::vector<neurons::TMatrix<>> & inputs,
const std::vector<neurons::TMatrix<>> & targets,
lint thread_id) = 0;
virtual std::vector<neurons::TMatrix<>> optimise(
const std::vector<neurons::TMatrix<>> & inputs,
const std::vector<neurons::TMatrix<>> & targets,
lint thread_id) = 0;
virtual std::vector<neurons::TMatrix<>> predict(
const std::vector<neurons::TMatrix<>> & inputs,
lint thread_id) const = 0;
};
std::ostream & operator << (std::ostream & os, const NN & nn);
|
Bennygmate/Deep-Learning | deep learning/neural_networks/NN_layer.h | <reponame>Bennygmate/Deep-Learning
//Bennygmate
#include "TMatrix.h"
namespace neurons {
class NN_layer_op;
class NN_layer {
public:
static const std::string NN;
static const std::string FCNN;
static const std::string CNN;
static const std::string RNN;
protected:
mutable std::vector<std::shared_ptr<NN_layer_op>> m_ops;
public:
NN_layer();
NN_layer(lint threads);
NN_layer(const NN_layer & other);
NN_layer(NN_layer && other);
NN_layer & operator = (const NN_layer & other);
NN_layer & operator = (NN_layer && other);
std::vector<std::shared_ptr<NN_layer_op>>& operation_instances() const;
virtual double commit_training();
virtual double commit_testing();
virtual Shape output_shape() const = 0;
virtual std::unique_ptr<char[]> to_binary_data(lint & data_size) const = 0;
virtual std::string nn_type() const = 0;
};
class NN_layer_op {
protected:
double m_loss;
public:
NN_layer_op();
NN_layer_op(const NN_layer_op & other);
NN_layer_op(NN_layer_op && other);
NN_layer_op & operator = (const NN_layer_op & other);
NN_layer_op & operator = (NN_layer_op && other);
public:
// Forward prop
virtual TMatrix<> forward_propagate(const TMatrix<> &input) = 0;
virtual TMatrix<> forward_propagate(const TMatrix<> &input, const TMatrix<> &target) = 0;
// Back prop
virtual TMatrix<> back_propagate(double l_rate, const TMatrix<> & E_to_y_diff) = 0;
virtual TMatrix<> back_propagate(double l_rate) = 0;
// Batch Forward prop
virtual std::vector<TMatrix<>> batch_forward_propagate(const std::vector<TMatrix<>> & inputs) = 0;
virtual std::vector<TMatrix<>> batch_forward_propagate(const std::vector<TMatrix<>> & inputs, const std::vector<TMatrix<>> & targets) = 0;
// Batch Back propg
virtual std::vector<TMatrix<>> batch_back_propagate(double l_rate, const std::vector<TMatrix<>> & E_to_y_diffs) = 0;
virtual std::vector<TMatrix<>> batch_back_propagate(double l_rate) = 0;
virtual Shape output_shape() const = 0;
double get_loss() const;
void clear_loss();
};
}
|
Bennygmate/Deep-Learning | deep learning/facial_recognition/network.h | <gh_stars>0
//Bennygmate
#include "Mnist.h"
#include "CIFAR_10.h"
#include "PGM.h"
#include "Conv_NN.h"
#include "Simple_NN.h"
#include "Multi_Layer_NN.h"
#include "Conv_Pooling_NN.h"
#include <memory>
std::string dataset_dir = "../../../../";
std::shared_ptr<dataset::Dataset> data_set;
std::string argv_dataset_type = "mnist";
lint argv_batch_size;
lint argv_threads;
lint argv_epoch_size;
std::string argv_mode;
int make_dataset(std::shared_ptr<dataset::Dataset> & data_set, std::string dataset_type) {
if ("mnist" == dataset_type) {
data_set = std::make_shared<dataset::Mnist>(
dataset_dir + "mnist/train-images-idx3-ubyte",
dataset_dir + "mnist/train-labels-idx1-ubyte",
dataset_dir + "mnist/t10k-images-idx3-ubyte",
dataset_dir + "mnist/t10k-labels-idx1-ubyte");
}
else if ("fashion-mnist" == dataset_type) {
data_set = std::make_shared<dataset::Mnist>(
dataset_dir + "fashion-mnist/train-images-idx3-ubyte",
dataset_dir + "fashion-mnist/train-labels-idx1-ubyte",
dataset_dir + "fashion-mnist/t10k-images-idx3-ubyte",
dataset_dir + "fashion-mnist/t10k-labels-idx1-ubyte");
}
else if ("cifar-10" == dataset_type){
data_set = std::make_shared<dataset::CIFAR_10>(dataset_dir + "cifar-10/");
}
else {
return 1;
}
return 0;
}
void parse_args(std::vector<std::string> argv) {
argv_batch_size = std::stoi(argv[1]);
argv_threads = std::stoi(argv[2]);
argv_epoch_size = std::stoi(argv[3]);
argv_dataset_type = argv[4];
argv_mode = argv[5];
}
int run_dnn(int argc, std::vector<std::string> argv) {
argv_batch_size = 64;
argv_threads = 4;
argv_epoch_size = 100;
std::string model_file_name = "dnn.dat";
argv_mode = "test";
if (argc == 6) {
parse_args(argv);
}
int retval = make_dataset(data_set, argv_dataset_type);
if (retval != 0) {
return retval;
}
neurons::global::global_rand_engine.seed(static_cast<unsigned int>(neurons::now_in_seconds()));
Multi_Layer_NN nn{ 0.001, 0.3, argv_threads, model_file_name, *data_set };
if ("train" == argv_mode) {
nn.train_network(argv_batch_size, argv_epoch_size, 200, 20, 7200);
}
else {
std::vector<neurons::TMatrix<>> test_inputs;
std::vector<neurons::TMatrix<>> test_labels;
data_set->get_test_set(test_inputs, test_labels, 5);
auto prediction = nn.network_predict(argv_batch_size, test_inputs);
for (size_t i = 0; i < prediction.size(); ++i) {
test_inputs[i].reshape(test_inputs[i].shape().sub_shape(0, 1));
std::cout << test_inputs[i] << '\n';
std::cout << prediction[i] << '\n';
}
for (lint i = 0; i < 100; ++i) nn.test_network(argv_batch_size, argv_epoch_size);
}
return 0;
}
int run_cnn(int argc, std::vector<std::string> argv) {
argv_batch_size = 64;
argv_threads = 4;
argv_epoch_size = 10;
std::string model_file_name = "cnn.dat";
argv_mode = "train";
if (argc == 6) {
parse_args(argv);
}
int retval = make_dataset(data_set, argv_dataset_type);
if (retval != 0) {
return retval;
}
neurons::global::global_rand_engine.seed(static_cast<unsigned int>(neurons::now_in_seconds()));
Conv_NN nn{ 0.001, 0.3, argv_threads, model_file_name, *data_set };
if ("train" == argv_mode) {
nn.train_network(argv_batch_size, argv_epoch_size, 200, 20, 7200);
}
else {
std::vector<neurons::TMatrix<>> tests;
std::vector<neurons::TMatrix<>> labels;
std::vector<neurons::TMatrix<>> test_inputs;
std::vector<neurons::TMatrix<>> test_labels;
data_set->get_test_set(tests, labels);
for (lint i = 0; i < 10; ++i) {
lint index = rand() % tests.size();
test_inputs.push_back(tests[index]);
test_labels.push_back(labels[index]);
}
auto prediction = nn.network_predict(argv_batch_size, test_inputs);
double total = 0;
double right = 0;
for (size_t i = 0; i < prediction.size(); ++i) {
test_inputs[i].reshape(test_inputs[i].shape().sub_shape(0, 1));
std::cout << test_inputs[i] << '\n';
std::cout << test_labels[i] << '\n';
std::cout << prediction[i] << '\n';
if (test_labels[i].argmax() == prediction[i].argmax()) {
right += 1;
}
total += 1;
}
std::cout << "accuracy: " << right / total << '\n';
for (lint i = 0; i < 100; ++i) nn.test_network(argv_batch_size, argv_epoch_size);
}
std::cout << "=================== end of the program =================" << "\n";
return 0;
}
int run_facial_network(
bool hitopgm,
bool test_only,
lint seed,
lint batch_size,
lint n_threads,
lint epoch_size,
lint n_epochs,
lint n_epochs_between_save,
lint label_id,
double learning_rate,
double momentum,
const std::string & network_type,
const std::string & network_file_name,
const std::string & network_from_file_name,
const std::vector<std::string> & train_list,
const std::vector<std::string> & test_1_list,
const std::vector<std::string> & test_2_list)
{
// Load dataset into memory
dataset::PGM pgm_train_dataset{ train_list, test_1_list, label_id };
dataset::PGM pgm_test_dataset{ std::vector<std::string>{}, test_2_list, label_id };
std::unique_ptr<NN> network;
if (seed < 0) {
neurons::global::global_rand_engine.seed(static_cast<unsigned int>(neurons::now_in_seconds()));
}
else {
neurons::global::global_rand_engine.seed(seed);
}
if ("cnn" == network_type) {
network = std::make_unique<Conv_NN>(
learning_rate, momentum, n_threads, "cnn_" + network_file_name, pgm_train_dataset);
if (!network_from_file_name.empty()) {
network->load_until("cnn_" + network_from_file_name, network->n_layers() - 1);
}
}
else if ("dnn" == network_type) {
network = std::make_unique<Multi_Layer_NN>(learning_rate, momentum, n_threads, "dnn_" + network_file_name, pgm_train_dataset);
if (!network_from_file_name.empty()) {
network->load_until("dnn_" + network_from_file_name, network->n_layers() - 1);
}
}
else {
std::cout << "No suitable network available, please choose cnn or dnn.\n";
return 0;
}
if (hitopgm) {
network->save_layers_as_images();
return 1;
}
if (!test_only) {
network->train_network(batch_size, epoch_size, n_epochs, n_epochs_between_save, 36000);
}
std::vector<neurons::TMatrix<>> test_input;
std::vector<neurons::TMatrix<>> test_labels;
pgm_test_dataset.get_test_set(test_input, test_labels);
lint n_tests = 0;
double n_rights = 0;
std::vector<neurons::TMatrix<>> test_pred = network->network_predict(batch_size, test_input);
for (size_t i = 0; i < test_labels.size(); ++i) {
std::cout << "Expected prediction: " << pgm_test_dataset.mat_to_name(test_labels[i]) << "\n";
std::cout << test_labels[i] << "\n";
std::cout << "Prediction: " << pgm_test_dataset.mat_to_name(test_pred[i]) << "\n";
std::cout << test_pred[i] << "\n";
n_tests += 1;
if (test_pred[i].argmax() == test_labels[i].argmax()) {
n_rights += 1;
}
}
std::cout << "Test accuracy is: " << n_rights / n_tests << "\n";
return 1;
}
|
Bennygmate/Deep-Learning | deep learning/neural_networks/CNN_layer.h | <filename>deep learning/neural_networks/CNN_layer.h<gh_stars>0
//Bennygmate
#include "Functions.h"
#include "Traditional_NN_layer.h"
#include "Convolution.h"
namespace neurons {
class CNN_layer : public Traditional_NN_layer {
private:
Conv_2d m_conv2d;
public:
static std::string from_binary_data(
char * binary_data, lint & data_size, TMatrix<> & w, TMatrix<> & b,
lint & stride, lint & padding,
std::unique_ptr<Activation> & act_func, std::unique_ptr<ErrorFunction> & err_func,
char *& residual_data, lint & residual_len
);
CNN_layer();
CNN_layer(
double mmt_rate,
lint rows,
lint cols,
lint chls,
lint filters,
lint filter_rows,
lint filter_cols,
lint stride,
lint padding,
lint threads,
neurons::Activation *act_func,
neurons::ErrorFunction *err_func = nullptr
);
CNN_layer(double mmt_rate,
lint rows, lint cols, lint chls, lint stride, lint padding, lint threads,
const TMatrix<> & w, const TMatrix<> & b,
std::unique_ptr<Activation> & act_func, std::unique_ptr<ErrorFunction> & err_func);
CNN_layer(const CNN_layer & other);
CNN_layer(CNN_layer && other);
CNN_layer & operator = (const CNN_layer & other);
CNN_layer & operator = (CNN_layer && other);
Shape output_shape() const;
virtual std::string nn_type() const { return NN_layer::CNN; }
virtual std::unique_ptr<char[]> to_binary_data(lint & data_size) const;
};
class CNN_layer_op : public Traditional_NN_layer_op {
private:
std::vector<TMatrix<>> m_conv_to_x_diffs;
std::vector<TMatrix<>> m_conv_to_w_diffs;
std::vector<TMatrix<>> m_act_diffs;
Conv_2d m_conv2d;
public:
CNN_layer_op();
CNN_layer_op(
const Conv_2d & conv2d,
const TMatrix<> &w,
const TMatrix<> &b,
const std::unique_ptr<Activation> &act_func,
const std::unique_ptr<ErrorFunction> &err_func);
CNN_layer_op(const CNN_layer_op & other);
CNN_layer_op(CNN_layer_op && other);
CNN_layer_op & operator = (const CNN_layer_op & other);
CNN_layer_op & operator = (CNN_layer_op && other);
// Forward prop
virtual std::vector<TMatrix<>> batch_forward_propagate(const std::vector<TMatrix<>> & inputs);
virtual std::vector<TMatrix<>> batch_forward_propagate(const std::vector<TMatrix<>> & inputs, const std::vector<TMatrix<>> & targets);
// Back prop
virtual std::vector<TMatrix<>> batch_back_propagate(double l_rate, const std::vector<TMatrix<>> & E_to_y_diffs);
virtual std::vector<TMatrix<>> batch_back_propagate(double l_rate);
virtual Shape output_shape() const;
};
}
|
Rasmustex/snek | src/main.c | <gh_stars>0
#include "../include/snek.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#include <time.h>
#define MOVEMENT_TIMEOUT 150
// TODO: Enable drawing a smaller box as the game window, as terminal emulators can be quite large
int main() {
srand(time(0));
setlocale( LC_ALL, "en_US.UTF-8" );
initscr(); cbreak(); noecho(); nonl(); intrflush( stdscr, FALSE ); keypad( stdscr, TRUE ); curs_set( 0 );
// box(stdscr, 0, 0);
timeout( MOVEMENT_TIMEOUT );
snek* head = init_snek( LINES/2, COLS/2, NULL, NULL );
add_apple( head );
draw_snek( head );
direction d = M_RIGHT;
direction prev_d = M_RIGHT;
refresh();
char input = 1;
while( (input = getch()) != 'q' ) {
switch( input ) {
case 5:
d = M_RIGHT;
break;
case 3:
d = M_UP;
break;
case 4:
d = M_LEFT;
break;
case 2:
d = M_DOWN;
break;
default:
break;
}
if( (d ^ prev_d) != 1 || d == prev_d ) {
prev_d = d;
if(move_snek( head, d ))
break;
} else if( move_snek( head, prev_d ))
break;
draw_snek( head );
refresh();
}
endwin();
snek* s = head->next;
int score = 0;
while( s ) {
score++;
s = s->next;
}
if( input != 'q' )
printf("Oh no! You died! :c\n");
printf("Score: %d\n", score);
clean_snek(head);
} |
Rasmustex/snek | src/snek.c | <filename>src/snek.c
#include "../include/snek.h"
#include <stdlib.h>
#include <curses.h>
#include <stdbool.h>
snek* init_snek( int y, int x, snek* next, snek* prev ) {
snek* s = (snek*)malloc( sizeof(snek) );
s->y = y;
s->x = x;
s->next = next;
s->prev = prev;
return s;
}
void clean_snek( snek* head ) {
snek* s = head;
snek* next;
while( s ) {
next = s->next;
free( s );
s = next;
}
return;
}
void grow_snek( snek* head ) {
snek* tail = head;
while( tail->next ) {
tail = tail->next;
}
tail->next = init_snek( tail->y, tail->x, NULL, tail );
return;
}
int check_collission( snek* head ) {
if( head->x < 0 || head->x >= COLS )
return 1;
if( head->y < 0 || head->y >= LINES )
return 1;
move(head->y, head->x);
char object_at_head = inch();
if( object_at_head == '#' )
return 1;
else if( object_at_head == 'O' )
return 2;
return 0;
}
int move_snek( snek* head, direction d ) {
snek* s = head;
while( s->next ) {
s = s->next;
}
move(s->y, s->x);
addch(' ');
while( s->prev ) {
s->y = s->prev->y;
s->x = s->prev->x;
s = s->prev;
}
switch( d ) {
case M_UP:
head->y--;
break;
case M_RIGHT:
head->x++;
break;
case M_DOWN:
head->y++;
break;
case M_LEFT:
head->x--;
break;
}
int col;
if((col = check_collission( head )) == 1)
return 1;
else if( col == 2 ) {
grow_snek( head );
add_apple( head );
}
return 0;
} |
Rasmustex/snek | src/draw.c | #include "../include/snek.h"
#include <curses.h>
#include <stdlib.h>
void draw_snek( snek* head ) {
move( head->y, head->x );
addch('@');
snek* s = head->next;
snek* next;
while( s ) {
next = s->next;
move( s->y, s->x );
addch('#');
s = next;
}
return;
}
void add_apple( snek* head ) {
snek* s = head;
int x, y;
bool is_in_snek = true;
while( is_in_snek ) {
is_in_snek = false;
s = head;
x = (rand() % COLS);
y = (rand() % LINES);
while( s ) {
if( y == s->y && x == s->x ) {
is_in_snek = true;
break;
}
s = s->next;
}
}
move( y, x );
addch('O');
return;
} |
Rasmustex/snek | include/snek.h | #ifndef SNEK_H
#define SNEK_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct sn snek;
struct sn {
int x, y;
snek *next, *prev;
};
typedef enum {
M_UP = 0b00,
M_DOWN = 0b01,
M_RIGHT = 0b10,
M_LEFT = 0b11
} direction;
snek* init_snek( int y, int x, snek* next, snek* prev );
void clean_snek( snek* head );
void draw_snek( snek* head );
int move_snek( snek* head, direction d );
void add_apple( snek* head );
#ifdef __cplusplus
}
#endif
#endif |
ULL-ESIT-IB-2020-2021/ib-practica10-funciones-doxygen-JonayVE-ull | src/cripto.h | /**
* Universidad de La Laguna
* Escuela Superior de Ingeniería y Tecnología
* Grado en Ingeniería Informática
* Informática Básica
*
* @author <NAME>
* @date 16.dic.2020
* @brief This file declares the "Help Text" constant and two functions
*
*/
#include <iostream>
const std::string kHelpText =
"./cripto -- Cifrado de ficheros\n\
Modo de uso: ./cripto fichero_entrada fichero_salida método password operación\n\n\
fichero_entrada: Fichero a codificar\n\
fichero_salida: Fichero codificado\n\
método: Indica el método de encriptado\n\
1: Cifrado_xor \n\
2: Cifrado_Cesar\n\
password: <PASSWORD> en el caso de método 1, Valor de K en el método 2\n\
operación: Operación a realizar en el fichero\n\
+: encriptar el fichero\n\
-: desencriptar el fichero;\n";
void Usage(int argc, char *argv[]);
bool IsFileOpen(std::string input_file);
void EncryptCesar(std::string input_file, std::string output_file, int key);
void DecryptCesar(std::string input_file, std::string output_file, int key);
void EncryptXor(std::string input_file, std::string output_file,
std::string password, const int KconstNumber);
void DecryptXor(std::string input_file, std::string output_file,
std::string password, const int KconstNumber);
int VocalCount(std::string input_file); |
esperancija/dragonpilot | panda/board/safety/safety_mitsubishi.h | // global torque limit
const int MITSUBISHI_MAX_TORQUE = 1500; // max torque cmd allowed ever
// rate based torque limit + stay within actually applied
// packet is sent at 100hz, so this limit is 1000/sec
const int MITSUBISHI_MAX_RATE_UP = 10; // ramp up slow
const int MITSUBISHI_MAX_RATE_DOWN = 25; // ramp down fast
const int MITSUBISHI_MAX_TORQUE_ERROR = 350; // max torque cmd in excess of torque motor
// real time torque limit to prevent controls spamming
// the real time limit is 1500/sec
const int MITSUBISHI_MAX_RT_DELTA = 375; // max delta torque allowed for real time checks
const uint32_t MITSUBISHI_RT_INTERVAL = 250000; // 250ms between real time checks
// longitudinal limits
const int MITSUBISHI_MAX_ACCEL = 2000; // 2.0 m/s2
const int MITSUBISHI_MIN_ACCEL = -3500; // -3.5 m/s2
const int MITSUBISHI_STANDSTILL_THRSLD = 100; // 1kph
// Roughly calculated using the offsets in openpilot +5%:
// In openpilot: ((gas1_norm + gas2_norm)/2) > 15
// gas_norm1 = ((gain_dbc*gas1) + offset1_dbc)
// gas_norm2 = ((gain_dbc*gas2) + offset2_dbc)
// In this safety: ((gas1 + gas2)/2) > THRESHOLD
const int MITSUBISHI_GAS_INTERCEPTOR_THRSLD = 845;
#define MITSUBISHI_GET_INTERCEPTOR(msg) (((GET_BYTE((msg), 0) << 8) + GET_BYTE((msg), 1) + (GET_BYTE((msg), 2) << 8) + GET_BYTE((msg), 3)) / 2) // avg between 2 tracks
const CanMsg MITSUBISHI_TX_MSGS[] = {{0x398, 0, 7}}; // interceptor
AddrCheckStruct mitsubishi_addr_checks[] = {
{.msg = {{ 0xaa, 0, 8, .check_checksum = false, .expected_timestep = 12000U}, { 0 }, { 0 }}},
{.msg = {{0x260, 0, 8, .check_checksum = true, .expected_timestep = 20000U}, { 0 }, { 0 }}},
{.msg = {{0x1D2, 0, 8, .check_checksum = true, .expected_timestep = 30000U}, { 0 }, { 0 }}},
{.msg = {{0x224, 0, 8, .check_checksum = false, .expected_timestep = 25000U},
{0x226, 0, 8, .check_checksum = false, .expected_timestep = 25000U}, { 0 }}},
};
#define MITSUBISHI_ADDR_CHECKS_LEN (sizeof(mitsubishi_addr_checks) / sizeof(mitsubishi_addr_checks[0]))
addr_checks mitsubishi_rx_checks = {mitsubishi_addr_checks, MITSUBISHI_ADDR_CHECKS_LEN};
// global actuation limit states
int mitsubishi_dbc_eps_torque_factor = 100; // conversion factor for STEER_TORQUE_EPS in %: see dbc file
//static uint8_t mitsubishi_compute_checksum(CAN_FIFOMailBox_TypeDef *to_push) {
// int addr = GET_ADDR(to_push);
// int len = GET_LEN(to_push);
// uint8_t checksum = (uint8_t)(addr) + (uint8_t)((unsigned int)(addr) >> 8U) + (uint8_t)(len);
// for (int i = 0; i < (len - 1); i++) {
// checksum += (uint8_t)GET_BYTE(to_push, i);
// }
// return checksum;
//}
//
//static uint8_t mitsubishi_get_checksum(CAN_FIFOMailBox_TypeDef *to_push) {
// int checksum_byte = GET_LEN(to_push) - 1;
// return (uint8_t)(GET_BYTE(to_push, checksum_byte));
//}
static int mitsubishi_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
UNUSED(to_push);
return true;
}
static int mitsubishi_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
int tx = 0;
int addr = GET_ADDR(to_send);
// int bus = GET_BUS(to_send);
if (addr == 0x3b6)
tx = 1;
// if (relay_malfunction) {
// tx = 0;
// }
return tx;
}
static const addr_checks* mitsubishi_init(int16_t param) {
// controls_allowed = 0;
// relay_malfunction_reset();
// gas_interceptor_detected = 0;
// mitsubishi_dbc_eps_torque_factor = param;
// return &mitsubishi_rx_checks;
UNUSED(param);
controls_allowed = true;
relay_malfunction_reset();
return &default_rx_checks;
}
static int mitsubishi_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
UNUSED(to_fwd);
UNUSED(bus_num);
return 0;
}
const safety_hooks mitsubishi_hooks = {
.init = mitsubishi_init,
.rx = mitsubishi_rx_hook,
.tx = mitsubishi_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.fwd = mitsubishi_fwd_hook,
};
|
nwpu-basketball-robot/basketball_2018 | basketball_base_serial/include/basketball_base_serial/SerialPort.h | /*
* SerialPort.h
*
* Created on: 2012-4-8
* Author: startar
*/
#ifndef SERIALPORT_H_
#define SERIALPORT_H_
#include <ros/ros.h>
#include <inttypes.h>
#include <vector>
#include <queue>
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/thread.hpp>
#include <iostream>
namespace serial {
// 串口通信选项
class SerialParams {
public:
std::string serialPort; // 串口的设备文件
unsigned int baudRate; // 波特率
unsigned int flowControl; // 流控
unsigned int parity; // 校验位
unsigned int stopBits; // 停止位
SerialParams() :
serialPort("/dev/ttyUSB0"), baudRate(115200), flowControl(0), parity(0), stopBits(1)
{
}
SerialParams(
std::string _serialPort ,
unsigned int _baudRate ,
unsigned int _flowControl = 0 ,
unsigned int _parity = 0 ,
unsigned int _stopBits = 0
) :
serialPort(_serialPort),
baudRate(_baudRate),
flowControl(_flowControl),
parity(_parity),
stopBits(_stopBits)
{
}
};
typedef std::vector<uint8_t> ByteVector;
typedef boost::shared_ptr<ByteVector> pByteVector;
class SerialPort {
private:
boost::shared_ptr<boost::asio::deadline_timer> m_ptimer; // 超时定时器
boost::shared_ptr<boost::asio::io_service> m_pios; // io_service对象
boost::shared_ptr<boost::asio::serial_port> m_pSerial; // 串口对象的指针
boost::mutex m_serialMutex; // 串口对象的互斥锁. 按照boost官方文档, serial_port对象不是线程安全的. 故需要此锁
enum {HEADER_LEN = 4};
//enum {HEADER_LEN = 3};
enum STATE {//使用了枚举类型讲每种工作状态都列举了出来
WAITING_FF, WAITING_FF2, READING_HEAD, READING_DATA, READING_CHECKSUM
} m_state; // 程序工作状态
SerialParams m_serialParams; // 串口的配置数据
int m_timeOut; // 数据报超时时间
int num ;
unsigned int checksum;
ByteVector m_tempBuf; // 数据读取的临时缓冲区
//uint8_t *current_array ;
ByteVector m_currentHeader; // 正在读取的报头(4字节)
size_t m_HeaderBytesRead; // 报头已经读取的字节数
ByteVector m_currentData; // 正在读取的报文数据
size_t m_DataBytesRead; // 数据已经读取的字节数
std::queue<pByteVector> m_writeQueue; // 待发送数据的队列
boost::mutex m_writeQueueMutex; // 队列的互斥锁
boost::function<void(ByteVector, int)> m_dataCallbackFunc; // 数据回调函数
boost::function<void()> m_errorCallbackFunc; // 错误回调函数
// 跑io_service::run()的线程
boost::thread m_thread;
// 线程的主过程, 主要是在跑io_service::run()
void mainRun();
// 为了方便写的函数
void start_a_read();
void start_a_write();
// async_read_some的Handler
void readHandler(const boost::system::error_code &ec, size_t bytesTransferred);
// async_write_some的Handler
void writeHandler(const boost::system::error_code &ec);
// 超时定时器的Handler
void timeoutHandler(const boost::system::error_code &ec);
public:
SerialPort();
virtual ~SerialPort();
void setSerialParams(const SerialParams ¶ms); // 设置串口参数
void setTimeOut(int timeout); // 设置超时时间
bool startThread(); // 启动线程
bool stopThread(); // 停止线程
// 设置收到数据之后的回调函数
void setCallbackFunc(const boost::function<void(ByteVector,int)> &func);
// 向串口中发送一个数据报文
//bool writeDatagram(const XM_msgs::XM_Datagram &datagram);
// 向串口中直接写入一串数据
bool writeRaw(const ByteVector &rawData);
};
} /* namespace XM_SerialNode */
#endif /* SERIALPORT_H_ */
|
nwpu-basketball-robot/basketball_2018 | basketball_move/include/robot_move_pkg/move_srv.h | #include "ros/ros.h"
#include "basketball_msgs/move_to_point.h"
#include "basketball_msgs/robot_rotate.h"
#include "basketball_msgs/focus_target.h"
#include "basketball_msgs/robot_state.h"
#include "geometry_msgs/Twist.h"
#include "tf/tf.h"
#include "tf/transform_listener.h"
#include "nav_msgs/Odometry.h"
#include <mutex>
struct ObsMsg{
ros::Time detectTime;
double x ;
double y ;
};
class MovetoPoint
{
public:
MovetoPoint(ros::NodeHandle &node) ;
~MovetoPoint() ;
protected:
private:
ros::ServiceServer move_to_point ;
ros::ServiceServer robot_rotate ;
ros::ServiceServer focus_target ;
ros::Publisher world_locate_puber ;
ros::Publisher robot_rotate_puber ;
ros::Publisher focus_target_puber ;
ros::Subscriber odom_subscriber ;
ros::Subscriber obs_subscriber ;
ros::NodeHandle mtp_nh ;
tf::TransformListener odom_listener ;
tf::StampedTransform odom_tf ;
nav_msgs::Odometry odom ;
ObsMsg obstacle ;
std::mutex odom_mutex ;
std::mutex chassis_mutex ;
ros::Time StartTime ;
bool AchiveHomePoint ;
bool moveable ;
bool ReturnHome ;
bool MTPServiceCallBack(basketball_msgs::move_to_point::Request&, basketball_msgs::move_to_point::Response&) ;
bool ROTServiceCallBack(basketball_msgs::robot_rotate::Request&, basketball_msgs::robot_rotate::Response&) ;
bool TGTServiceCallBack(basketball_msgs::focus_target::Request&, basketball_msgs::focus_target::Response&) ;
void ObsSubCallBack(const basketball_msgs::robot_state::ConstPtr&) ;
void OdomSubCallBack(const nav_msgs::Odometry::ConstPtr&) ;
};
|
nwpu-basketball-robot/basketball_2018 | basketball_base_serial/include/basketball_base_serial/SerialNode.h | <filename>basketball_base_serial/include/basketball_base_serial/SerialNode.h
#ifndef SERIALNODE_H
#define SERIALNODE_H
#include <ros/ros.h>
#include <basketball_msgs/robot_message.h>
#include <basketball_msgs/robot_state.h>
#include <basketball_base_serial/SerialPort.h>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <map>
using namespace serial ;
namespace exp_serial {
class ExpSerial
{
public:
ExpSerial(ros::NodeHandle node) ;
~ExpSerial() ;
void serialCall(ByteVector current_data,int id) ;
private:
std::string serial_port_ ; //串口
int baud_rate_ ; //波特率
boost::shared_ptr<SerialPort>main_serial ;
ros::NodeHandle node ;
ros::Subscriber data_sub ;
void dataCallBack(const basketball_msgs::robot_message::ConstPtr &ptr) ;
void dumpBuffer(const char *buffer, int elements) ;
std::map<u_int8_t,ros::Publisher> pub_ ;
protected:
};
}
#endif // SERIALNODE_H
|
intmian/hexo_GUI | hexo_GUI/GeneratedFiles/ui_hexo_GUI.h | <filename>hexo_GUI/GeneratedFiles/ui_hexo_GUI.h
/********************************************************************************
** Form generated from reading UI file 'hexo_GUI.ui'
**
** Created by: Qt User Interface Compiler version 5.9.6
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_HEXO_GUI_H
#define UI_HEXO_GUI_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QListView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_hexo_GUIClass
{
public:
QWidget *centralWidget;
QTabWidget *functional_area;
QWidget *blog_area;
QPushButton *up_button;
QPushButton *tsst_button;
QWidget *article_area;
QPushButton *new_blog_button;
QListView *listView;
QWidget *configure_area;
QPushButton *open_local_button;
QPushButton *theme_y_button;
QPushButton *hexo_y_button;
QPushButton *ssh_button;
QPushButton *button_to_blog;
QPushButton *button_to_article;
QPushButton *button_to_configuration;
QMenuBar *menuBar;
QMenu *menu;
QMenu *menu_2;
void setupUi(QMainWindow *hexo_GUIClass)
{
if (hexo_GUIClass->objectName().isEmpty())
hexo_GUIClass->setObjectName(QStringLiteral("hexo_GUIClass"));
hexo_GUIClass->resize(482, 470);
centralWidget = new QWidget(hexo_GUIClass);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
functional_area = new QTabWidget(centralWidget);
functional_area->setObjectName(QStringLiteral("functional_area"));
functional_area->setGeometry(QRect(130, 10, 341, 421));
blog_area = new QWidget();
blog_area->setObjectName(QStringLiteral("blog_area"));
up_button = new QPushButton(blog_area);
up_button->setObjectName(QStringLiteral("up_button"));
up_button->setGeometry(QRect(30, 60, 71, 28));
tsst_button = new QPushButton(blog_area);
tsst_button->setObjectName(QStringLiteral("tsst_button"));
tsst_button->setGeometry(QRect(30, 100, 71, 28));
functional_area->addTab(blog_area, QString());
article_area = new QWidget();
article_area->setObjectName(QStringLiteral("article_area"));
new_blog_button = new QPushButton(article_area);
new_blog_button->setObjectName(QStringLiteral("new_blog_button"));
new_blog_button->setGeometry(QRect(70, 30, 101, 28));
listView = new QListView(article_area);
listView->setObjectName(QStringLiteral("listView"));
listView->setGeometry(QRect(50, 80, 256, 301));
functional_area->addTab(article_area, QString());
configure_area = new QWidget();
configure_area->setObjectName(QStringLiteral("configure_area"));
open_local_button = new QPushButton(configure_area);
open_local_button->setObjectName(QStringLiteral("open_local_button"));
open_local_button->setGeometry(QRect(20, 80, 151, 28));
theme_y_button = new QPushButton(configure_area);
theme_y_button->setObjectName(QStringLiteral("theme_y_button"));
theme_y_button->setGeometry(QRect(40, 50, 111, 28));
hexo_y_button = new QPushButton(configure_area);
hexo_y_button->setObjectName(QStringLiteral("hexo_y_button"));
hexo_y_button->setGeometry(QRect(70, 210, 131, 28));
ssh_button = new QPushButton(configure_area);
ssh_button->setObjectName(QStringLiteral("ssh_button"));
ssh_button->setGeometry(QRect(50, 300, 93, 28));
functional_area->addTab(configure_area, QString());
button_to_blog = new QPushButton(centralWidget);
button_to_blog->setObjectName(QStringLiteral("button_to_blog"));
button_to_blog->setGeometry(QRect(20, 100, 93, 28));
button_to_article = new QPushButton(centralWidget);
button_to_article->setObjectName(QStringLiteral("button_to_article"));
button_to_article->setGeometry(QRect(20, 200, 93, 28));
button_to_configuration = new QPushButton(centralWidget);
button_to_configuration->setObjectName(QStringLiteral("button_to_configuration"));
button_to_configuration->setGeometry(QRect(20, 310, 93, 28));
hexo_GUIClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(hexo_GUIClass);
menuBar->setObjectName(QStringLiteral("menuBar"));
menuBar->setGeometry(QRect(0, 0, 482, 26));
menu = new QMenu(menuBar);
menu->setObjectName(QStringLiteral("menu"));
menu_2 = new QMenu(menuBar);
menu_2->setObjectName(QStringLiteral("menu_2"));
hexo_GUIClass->setMenuBar(menuBar);
menuBar->addAction(menu->menuAction());
menuBar->addAction(menu_2->menuAction());
retranslateUi(hexo_GUIClass);
functional_area->setCurrentIndex(2);
QMetaObject::connectSlotsByName(hexo_GUIClass);
} // setupUi
void retranslateUi(QMainWindow *hexo_GUIClass)
{
hexo_GUIClass->setWindowTitle(QApplication::translate("hexo_GUIClass", "hexo_GUI", Q_NULLPTR));
up_button->setText(QApplication::translate("hexo_GUIClass", "\344\270\212\344\274\240\345\215\232\345\256\242", Q_NULLPTR));
tsst_button->setText(QApplication::translate("hexo_GUIClass", "\346\265\213\350\257\225\345\215\232\345\256\242", Q_NULLPTR));
functional_area->setTabText(functional_area->indexOf(blog_area), QApplication::translate("hexo_GUIClass", "\345\215\232\345\256\242", Q_NULLPTR));
new_blog_button->setText(QApplication::translate("hexo_GUIClass", "\345\210\233\345\273\272\346\226\260\347\232\204\345\215\232\345\256\242", Q_NULLPTR));
functional_area->setTabText(functional_area->indexOf(article_area), QApplication::translate("hexo_GUIClass", "\345\215\232\346\226\207", Q_NULLPTR));
open_local_button->setText(QApplication::translate("hexo_GUIClass", "\346\234\254\345\234\260\346\211\223\345\274\200hexo\346\226\207\344\273\266\345\244\271", Q_NULLPTR));
theme_y_button->setText(QApplication::translate("hexo_GUIClass", "\346\237\245\347\234\213theme\346\226\207\344\273\266", Q_NULLPTR));
hexo_y_button->setText(QApplication::translate("hexo_GUIClass", "\346\237\245\347\234\213hexo\346\240\267\345\274\217\346\226\207\344\273\266", Q_NULLPTR));
ssh_button->setText(QApplication::translate("hexo_GUIClass", "\346\237\245\347\234\213SSH\345\257\206\351\222\245", Q_NULLPTR));
functional_area->setTabText(functional_area->indexOf(configure_area), QApplication::translate("hexo_GUIClass", "\351\205\215\347\275\256", Q_NULLPTR));
button_to_blog->setText(QApplication::translate("hexo_GUIClass", "\345\215\232\345\256\242", Q_NULLPTR));
button_to_article->setText(QApplication::translate("hexo_GUIClass", "\345\215\232\346\226\207", Q_NULLPTR));
button_to_configuration->setText(QApplication::translate("hexo_GUIClass", "\351\205\215\347\275\256", Q_NULLPTR));
menu->setTitle(QApplication::translate("hexo_GUIClass", "\350\256\276\347\275\256", Q_NULLPTR));
menu_2->setTitle(QApplication::translate("hexo_GUIClass", "\345\205\263\344\272\216", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class hexo_GUIClass: public Ui_hexo_GUIClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_HEXO_GUI_H
|
intmian/hexo_GUI | hexo_GUI/hexo_GUI.h | <reponame>intmian/hexo_GUI<gh_stars>0
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_hexo_GUI.h"
#include "qmessagebox.h"
#include <string>
using namespace std;
class hexo_GUI : public QMainWindow
{
Q_OBJECT
public:
hexo_GUI(QWidget *parent = Q_NULLPTR);
private:
Ui::hexo_GUIClass ui;
void ChangeTabIndex(int index);
void connect_slot_signal();
signals:
void ToChangeTab(int index); // 切换选项卡
};
|
intmian/hexo_GUI | hexo_GUI/Tool.h | <filename>hexo_GUI/Tool.h
#pragma once
#include <string>
#include "qmessagebox.h"
#include <cstdio>
enum message_Type
{
WARNING = 0,
QUESTION = 1,
ABOUT = 2,
INFORMATION = 3
};
class Tool
{
public:
private:
};
class Easy_message_box//解决中文乱码,并将按钮本土化
{
public:
/*WARNING
QUESTION
INFOMATION
ABOUT
输入得非二按钮数量全被视为1*/
static bool message_box(message_Type type, const char *title, const char *contain, int button_number = 1);//只能使用一个或两个按钮
}; |
GhostVaibhav/Todos | include/sha256.h | /*
* __ ___ __ ____ __ __
* / |/ /__ _____/ /__ / _// /_/ /
* / /|_/ / _ `/ __/ '_/_/ / / __/_/
* /_/ /_/\_,_/_/ /_/\_\/___/ \__(_)
*
* MIT License
*
* Copyright (c) 2021 <NAME>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SHA256_H
#define SHA256_H
#include <string>
class SHA256
{
protected:
typedef unsigned char uint8;
typedef unsigned int uint32;
typedef unsigned long long uint64;
const static uint32 sha256_k[];
static const unsigned int SHA224_256_BLOCK_SIZE = (512/8);
public:
void init();
void update(const unsigned char *message, unsigned int len);
void final(unsigned char *digest);
static const unsigned int DIGEST_SIZE = ( 256 / 8);
protected:
void transform(const unsigned char *message, unsigned int block_nb);
unsigned int m_tot_len;
unsigned int m_len;
unsigned char m_block[2*SHA224_256_BLOCK_SIZE];
uint32 m_h[8];
};
std::string sha256(std::string input);
#define SHA2_SHFR(x, n) (x >> n)
#define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
#define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
#define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
#define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
#define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
#define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
#define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
#define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
#define SHA2_UNPACK32(x, str) \
{ \
*((str) + 3) = (uint8) ((x) ); \
*((str) + 2) = (uint8) ((x) >> 8); \
*((str) + 1) = (uint8) ((x) >> 16); \
*((str) + 0) = (uint8) ((x) >> 24); \
}
#define SHA2_PACK32(str, x) \
{ \
*(x) = ((uint32) *((str) + 3) ) \
| ((uint32) *((str) + 2) << 8) \
| ((uint32) *((str) + 1) << 16) \
| ((uint32) *((str) + 0) << 24); \
}
#endif
|
GhostVaibhav/Todos | include/panel.h | /*
* __ ___ __ ____ __ __
* / |/ /__ _____/ /__ / _// /_/ /
* / /|_/ / _ `/ __/ '_/_/ / / __/_/
* /_/ /_/\_,_/_/ /_/\_\/___/ \__(_)
*
* MIT License
*
* Copyright (c) 2021 <NAME>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* ----------------------------------------------------------------------
* -------------------------PANELS FOR PDCurses--------------------------
* ----------------------------------------------------------------------
*/
#ifndef __PDCURSES_PANEL_H__
#define __PDCURSES_PANEL_H__ 1
#include <curses.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct panelobs
{
struct panelobs *above;
struct panel *pan;
} PANELOBS;
typedef struct panel
{
WINDOW *win;
int wstarty;
int wendy;
int wstartx;
int wendx;
struct panel *below;
struct panel *above;
const void *user;
struct panelobs *obscure;
} PANEL;
PDCEX int bottom_panel(PANEL *pan);
PDCEX int del_panel(PANEL *pan);
PDCEX int hide_panel(PANEL *pan);
PDCEX int move_panel(PANEL *pan, int starty, int startx);
PDCEX PANEL *new_panel(WINDOW *win);
PDCEX PANEL *panel_above(const PANEL *pan);
PDCEX PANEL *panel_below(const PANEL *pan);
PDCEX int panel_hidden(const PANEL *pan);
PDCEX const void *panel_userptr(const PANEL *pan);
PDCEX WINDOW *panel_window(const PANEL *pan);
PDCEX int replace_panel(PANEL *pan, WINDOW *win);
PDCEX int set_panel_userptr(PANEL *pan, const void *uptr);
PDCEX int show_panel(PANEL *pan);
PDCEX int top_panel(PANEL *pan);
PDCEX void update_panels(void);
#ifdef __cplusplus
}
#endif
#endif |
GhostVaibhav/Todos | include/structure.h | <gh_stars>1-10
/*
* __ ___ __ ____ __ __
* / |/ /__ _____/ /__ / _// /_/ /
* / /|_/ / _ `/ __/ '_/_/ / / __/_/
* /_/ /_/\_,_/_/ /_/\_\/___/ \__(_)
*
* MIT License
*
* Copyright (c) 2021 <NAME>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef STRUCTURE_H
#define STRUCTURE_H
#include "json.hpp"
// ------------------------------------------------------------------------
// ---------------------CORE STRUCTURE OF TODO USED------------------------
// ------------------------------------------------------------------------
struct todo
{
std::string name; // Storing the name of Todo
std::string desc; // Storing the description of Todo
std::string time; // Automatically generating the time for a Todo
bool isComplete; // Marking the Todo as "complete" or "not complete"
NLOHMANN_DEFINE_TYPE_INTRUSIVE(todo, name, desc, time, isComplete); // For serializing and deserializing JSON from Todo structure and vice-versa
};
#endif |
AdelardBanza/SingleTestHarness | Project1/Project1/TestLogger.h | #ifndef TEST_LOGGER_H
#define TEST_LOGGER_H
///////////////////////////////////////////////////////////////////////////////
// TestLogger.h - TestLogger class definition //
// ver 1.0 //
// Language: C++, Visual Studio 2017 //
// Platform: HP G1 800, Windows 10 //
// Application: Single Test Harness Project, CSE687 - Object Oriented Design //
// Author: <NAME>, //
// <EMAIL> //
///////////////////////////////////////////////////////////////////////////////
#include <memory>
#include <ostream>
#include <string>
/*
* Simple Logger placeholder class
*/
class TestLogger
{
public:
// Default constructor to create a root logger
TestLogger();
// Copy constructor to create a root logger
TestLogger(const TestLogger&);
// Factory method to create a test logger for running a test suite
std::unique_ptr<TestLogger> createLogger();
// Set the Pass fail status of the testing suite
void setStatus(bool);
// prints a summary of the results for running this test suite
void logSummary() const;
// Adds a informational message to the test log
void log(const std::string&) const;
// Utility method that creates a child context string from a valid context string, and a child's name
// usage string childContext = logger.getChildContext(currentContext, "child name");
const std::string getChildLoggingContext(const std::string&, const std::string&);
/*************************** Test Case logging methods **********************************************/
// Logs the beginning of a new Test Case
// usage logTestCaseBegin(contextStr);
void logTestCaseBegin(const std::string&);
// Logs an assertion from a test case
// usage logTestCaseAssertion(contextStr, "variable/assertion msg",
// boolean result);
void logTestCaseAssertion(const std::string&, const std::string&, bool);
// Logs that the current test case completed with a Success return
// usage logTestCaseSuccess(contextStr);
void logTestCaseSuccess(const std::string&);
// Logs that the current test case completed with a (general) failure return
// usage logTestCaseFail(contextStr);
void logTestCaseFail(const std::string&);
// Logs that the current test case completed with a (general) failure return
// usage logTestCaseFailWithException(contextStr, e);
void logTestCaseFailWithException(const std::string&, const std::exception&);
// Logs that the current test case completed with a (general) failure return
// usage logTestCaseFailWithException(contextStr, e);
void logTestCaseFailWithSpecificException(const std::string&, const std::string&, const std::exception&);
// Logs that the current test case failed with an unknown exception return
// usage logTestCaseFailWithUnknownException(contextStr);
void logTestCaseFailWithUnknownException(const std::string&);
/***************************** Test Suite Logging methods *************************************************/
// Logs the beginning of a new Test Suite
// usage logTestSuiteBegin(contextStr);
void logTestSuiteBegin(const std::string&);
// Logs that the current test suite completed with a success return
// usage logTestSuiteSuccess(contextStr);
void logTestSuiteSuccess(const std::string&);
// Logs that the current test case completed with a failure return
// usage logTestSuiteFail(contextStr);
void logTestSuiteFail(const std::string&);
// accessors and mutators for detail levels
void setShowDetailMessages(bool);
bool getShowDetailMessages();
void setShowTimestamp(bool);
bool getShowTimestamp();
void setShowAssertionDetails(bool);
bool getShowAssertionDetails();
private:
std::ostream& currentTimeStamp(std::ostream& out) const;
bool testResults;
bool showTimestamp;
bool showDetailMessages;
bool showAssertionDetails;
};
#endif // TEST_LOGGER_H |
AdelardBanza/SingleTestHarness | Project1/Project1/Assertion.h | <reponame>AdelardBanza/SingleTestHarness<filename>Project1/Project1/Assertion.h<gh_stars>0
#ifndef ASSERTION_H
#define ASSERTION_H
///////////////////////////////////////////////////////////////////////////////
// Assertion.h - Assertion class definition //
// ver 1.0 //
// Language: C++, Visual Studio 2017 //
// Platform: HP G1 800, Windows 10 //
// Application: Single Test Harness Project, CSE687 - Object Oriented Design //
// Author: <NAME>, //
// <EMAIL> //
///////////////////////////////////////////////////////////////////////////////
#include "TestLogger.h"
#include <memory>
#include <sstream>
#include <stack>
#include <string>
class AssertionManager;
class TestHarness;
class TestSuiteRunner;
template<typename T1, typename T2> void assertEquals(const std::string& msg, const T1 t1, const T2 t2);
template<typename T> void assertNotNull(const std::string& s, const T* const tPtr);
class AssertionManager
{
public:
static AssertionManager& getInstance();
~AssertionManager();
template<typename T1, typename T2> friend void assertEquals(const std::string& msg, const T1 t1, const T2 t2);
template<typename T> friend void assertNotNull(const std::string& s, const T* const tPtr);
friend class TestHarness;
friend class TestCaseRunner;
friend class TestSuiteRunner;
private:
AssertionManager();
void pushCntx(const std::string&, TestLogger*);
bool assertCntx(const std::string& msg, TestLogger* logger);
bool popCntx();
void logSuccess(const std::string&);
void logFail(const std::string&);
static AssertionManager *instance;
std::stack<std::pair<std::pair<std::string, bool>, TestLogger*>> ctxStack;
};
// assertEquals(lineNumberr, filename, testCaseName, expected, actual, testing condition)
template<typename T1, typename T2>
void assertEquals(const std::string& msg, const T1 t1, const T2 t2)
{
AssertionManager& assertM = AssertionManager::getInstance();
if (t2 == t1) {
std::stringstream s;
s << "assertEquals passed: " << msg << " received " << t1 << " while expecting " << t1;
assertM.logSuccess(s.str());
}
else {
std::stringstream s;
s << "assertEquals failed: " << msg << " received " << t1 << " while expecting " << t2;
assertM.logFail(s.str());
}
}
template<typename T>
void assertNotNull(const std::string& s, const T* const tPtr)
{
AssertionManager& assertM = AssertionManager::getInstance();
if (tPtr != nullptr_t) {
std::stringstream s;
s << "assertNotNull passed for " << msg;
assertM.logSuccess(s.str());
}
else {
std::stringstream s;
s << "assertNotNull failed " << msg;
assertM.logFail(s.str());
}
}
#endif // ASSERTION_H
|
AdelardBanza/SingleTestHarness | Project1/Project1/TestHarness.h | <filename>Project1/Project1/TestHarness.h
#ifndef TESTHARNESS_H
#define TESTHARNESS_H
///////////////////////////////////////////////////////////////////////////////
// TestHarness.h - TestHarness class definition //
// ver 1.0 //
// Language: C++, Visual Studio 2017 //
// Platform: HP G1 800, Windows 10 //
// Application: Single Test Harness Project, CSE687 - Object Oriented Design //
// Author: <NAME>, //
// <EMAIL> //
///////////////////////////////////////////////////////////////////////////////
#include "Assertion.h"
#include <functional>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
/*
* Typedef for our test executors. Which are function pointers, to a function
* which returns a bool (true/false) value and takes no arguments.
*/
//typedef bool (*testExecutor)(void);
typedef std::function<bool(void)> testExecutor;
/*
* Forward declaration of TestHarness classes
*/
class TestRunner;
class TestHarness;
class TestCaseRunner;
class TestSuiteRunner;
/*
* A data structure to hold the metadata and executor for a single test case.
*/
// TestRunner base class definition
class TestRunner
{
public:
TestRunner(const char* name) : runnerName(name) {};
TestRunner(const std::string& name) : runnerName(name) {};
virtual bool run(const std::string& runContext, TestLogger& testLogger) const = 0;
std::string getRunnerName() const { return this->runnerName;};
friend TestCaseRunner;
friend TestSuiteRunner;
private:
TestRunner(const TestRunner&) = delete;
TestRunner& operator=(const TestRunner&) = delete;
std::string runnerName;
};
// TestSuiteRunner class definition
class TestSuiteRunner : public TestRunner
{
public:
TestSuiteRunner(const char* name);
TestSuiteRunner(const std::string& name);
bool run(const std::string& runContext, TestLogger& testLogger) const;
friend TestHarness;
private:
TestSuiteRunner(const TestSuiteRunner&) = delete;
TestSuiteRunner& operator=(const TestSuiteRunner&) = delete;
TestSuiteRunner* addTestSuiteRunner(std::string name);
void addTestCaseRunner(std::string, testExecutor);
static std::unique_ptr<TestSuiteRunner> defaultTestSuiteRunnerFactory();
std::vector<std::unique_ptr<TestRunner>> testRunnerList;
};
// TestCaseRunner class definition
class TestCaseRunner : public TestRunner
{
public:
TestCaseRunner(const char* name, testExecutor testExecutor)
: testExecutor(testExecutor), TestRunner((const char*)name) {};
TestCaseRunner(const std::string& name, testExecutor testExecutor)
: testExecutor(testExecutor), TestRunner((const std::string&) name) {};
bool run(const std::string& runContext, TestLogger& testLogger) const ;
private:
TestCaseRunner(const TestCaseRunner&) = delete;
TestCaseRunner& operator=(const TestCaseRunner&) = delete;
testExecutor testExecutor;
};
// TestHarness class definition
class TestHarness
{
public:
static const int PASS_FAIL_ONLY = 0;
static const int SHOW_DETAIL_MESSAGES = 1;
static const int SHOW_ALL_MESSAGES = 2;
// default (no arg) constructor
TestHarness();
// creates a testing harness with a given TestLogger
TestHarness(const TestLogger&);
// no copy constructor
TestHarness(const TestHarness&) = delete;
// no copy assignment
TestHarness& operator=(const TestHarness&) = delete;
/*
* Method to run all the tests in the test harness, returns true if all test pass
*/
std::unique_ptr<TestLogger> runTests();
/*
* This method creates a test suite to run a collection of tests or other test suites. We can choose to include this
* test suit in any test suite we create using the test suite Id, and passing it in as parentTestSuiteRunnerId.
* Alternatively add it to the default test suite by passing in a 0.
*/
int createTestSuiteRunner(int parentTestSuiteRunnerId, std::string testSuiteRunnerName);
/*
* This method creates a test runner to run a single test. We can choose to include this
* test in any test suite we create using the test suite Id, and passing it in as parentTestSuiteRunnerId.
* Alternatively add it to the default test suite by passing in a 0.
*/
void createTestRunner(int parentTestSuiteRunnerId, std::string testRunnerName, testExecutor testExecutor);
void setTestReportingLevel(int level);
void setShowTimestamp(bool val);
private:
std::unique_ptr<TestLogger> testLogger;
std::vector<TestSuiteRunner*> testSuiteRunnerIndex;
std::unique_ptr<TestSuiteRunner> rootRunner;
};
#endif // TESTHARNESS_H |
LeoNavel/usrsctp | usrsctplib/netinet/sctp_usrreq.c | <filename>usrsctplib/netinet/sctp_usrreq.c
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2008-2012, by <NAME>. All rights reserved.
* Copyright (c) 2008-2012, by <NAME>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* a) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* b) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* c) Neither the name of Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(__FreeBSD__) && !defined(__Userspace__)
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#endif
#include <netinet/sctp_os.h>
#if defined(__FreeBSD__) && !defined(__Userspace__)
#include <sys/proc.h>
#endif
#include <netinet/sctp_pcb.h>
#include <netinet/sctp_header.h>
#include <netinet/sctp_var.h>
#ifdef INET6
#include <netinet6/sctp6_var.h>
#endif
#include <netinet/sctp_sysctl.h>
#include <netinet/sctp_output.h>
#include <netinet/sctp_uio.h>
#include <netinet/sctp_asconf.h>
#include <netinet/sctputil.h>
#include <netinet/sctp_indata.h>
#include <netinet/sctp_timer.h>
#include <netinet/sctp_auth.h>
#include <netinet/sctp_bsd_addr.h>
#if defined(__Userspace__)
#include <netinet/sctp_callout.h>
#else
#include <netinet/udp.h>
#endif
#if defined(__FreeBSD__) && !defined(__Userspace__)
#include <sys/eventhandler.h>
#endif
#if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
#include <netinet/sctp_peeloff.h>
#endif /* HAVE_SCTP_PEELOFF_SOCKOPT */
extern const struct sctp_cc_functions sctp_cc_functions[];
extern const struct sctp_ss_functions sctp_ss_functions[];
void
#if defined(__Userspace__)
sctp_init(uint16_t port,
int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
void (*debug_printf)(const char *format, ...), int start_threads)
#elif defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) &&!defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
sctp_init(struct protosw *pp SCTP_UNUSED, struct domain *dp SCTP_UNUSED)
#else
sctp_init(void)
#endif
{
#if !defined(__Userspace__)
u_long sb_max_adj;
#else
init_random();
#endif
/* Initialize and modify the sysctled variables */
sctp_init_sysctls();
#if defined(__Userspace__)
SCTP_BASE_SYSCTL(sctp_udp_tunneling_port) = port;
#else
#if defined(__APPLE__) && !defined(__Userspace__)
sb_max_adj = (u_long)((u_quad_t) (sb_max) * MCLBYTES / (MSIZE + MCLBYTES));
SCTP_BASE_SYSCTL(sctp_sendspace) = sb_max_adj;
#else
if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
/*
* Allow a user to take no more than 1/2 the number of clusters or
* the SB_MAX, whichever is smaller, for the send window.
*/
sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
(((uint32_t)nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
#endif
/*
* Now for the recv window, should we take the same amount? or
* should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
* now I will just copy.
*/
SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
#endif
SCTP_BASE_VAR(first_time) = 0;
SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
#if defined(__Userspace__)
#if !defined(_WIN32)
#if defined(INET) || defined(INET6)
SCTP_BASE_VAR(userspace_route) = -1;
#endif
#endif
#ifdef INET
SCTP_BASE_VAR(userspace_rawsctp) = -1;
SCTP_BASE_VAR(userspace_udpsctp) = -1;
#endif
#ifdef INET6
SCTP_BASE_VAR(userspace_rawsctp6) = -1;
SCTP_BASE_VAR(userspace_udpsctp6) = -1;
#endif
SCTP_BASE_VAR(timer_thread_should_exit) = 0;
SCTP_BASE_VAR(conn_output) = conn_output;
SCTP_BASE_VAR(debug_printf) = debug_printf;
SCTP_BASE_VAR(crc32c_offloaded) = 0;
SCTP_BASE_VAR(iterator_thread_started) = 0;
SCTP_BASE_VAR(timer_thread_started) = 0;
#endif
#if defined(__Userspace__)
sctp_pcb_init(start_threads);
if (start_threads) {
sctp_start_timer_thread();
}
#else
sctp_pcb_init();
#endif
#if defined(SCTP_PACKET_LOGGING)
SCTP_BASE_VAR(packet_log_writers) = 0;
SCTP_BASE_VAR(packet_log_end) = 0;
memset(&SCTP_BASE_VAR(packet_log_buffer), 0, SCTP_PACKET_LOG_SIZE);
#endif
#if defined(__APPLE__) && !defined(__Userspace__)
SCTP_BASE_VAR(sctp_main_timer_ticks) = 0;
sctp_start_main_timer();
timeout(sctp_delayed_startup, NULL, 1);
#endif
#if defined(__FreeBSD__) && !defined(__Userspace__)
SCTP_BASE_VAR(eh_tag) = EVENTHANDLER_REGISTER(rt_addrmsg,
sctp_addr_change_event_handler, NULL, EVENTHANDLER_PRI_FIRST);
#endif
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
#ifdef VIMAGE
static void
sctp_finish(void *unused __unused)
{
EVENTHANDLER_DEREGISTER(rt_addrmsg, SCTP_BASE_VAR(eh_tag));
sctp_pcb_finish();
}
VNET_SYSUNINIT(sctp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, sctp_finish, NULL);
#endif
#else
void
sctp_finish(void)
{
#if defined(__APPLE__) && !defined(__Userspace__)
untimeout(sctp_delayed_startup, NULL);
sctp_over_udp_stop();
sctp_address_monitor_stop();
sctp_stop_main_timer();
#endif
#if defined(__Userspace__)
#if defined(INET) || defined(INET6)
recv_thread_destroy();
#endif
sctp_stop_timer_thread();
#endif
sctp_pcb_finish();
#if defined(_WIN32) && !defined(__Userspace__)
sctp_finish_sysctls();
#endif
#if defined(__Userspace__)
finish_random();
#endif
}
#endif
void
sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
{
struct sctp_tmit_chunk *chk;
uint16_t overhead;
/* Adjust that too */
stcb->asoc.smallest_mtu = nxtsz;
/* now off to subtract IP_DF flag if needed */
overhead = IP_HDR_SIZE + sizeof(struct sctphdr);
if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
}
TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
if ((chk->send_size + overhead) > nxtsz) {
chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
}
}
TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
if ((chk->send_size + overhead) > nxtsz) {
/*
* For this guy we also mark for immediate resend
* since we sent to big of chunk
*/
chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
if (chk->sent < SCTP_DATAGRAM_RESEND) {
sctp_flight_size_decrease(chk);
sctp_total_flight_decrease(stcb, chk);
chk->sent = SCTP_DATAGRAM_RESEND;
sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
chk->rec.data.doing_fast_retransmit = 0;
if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
chk->whoTo->flight_size,
chk->book_size,
(uint32_t)(uintptr_t)chk->whoTo,
chk->rec.data.tsn);
}
/* Clear any time so NO RTT is being done */
if (chk->do_rtt == 1) {
chk->do_rtt = 0;
chk->whoTo->rto_needed = 1;
}
}
}
}
}
#ifdef INET
#if !defined(__Userspace__)
void
sctp_notify(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
struct sctp_nets *net,
uint8_t icmp_type,
uint8_t icmp_code,
uint16_t ip_len,
uint32_t next_mtu)
{
#if defined(__APPLE__) && !defined(__Userspace__)
struct socket *so;
#endif
int timer_stopped;
if (icmp_type != ICMP_UNREACH) {
/* We only care about unreachable */
SCTP_TCB_UNLOCK(stcb);
return;
}
if ((icmp_code == ICMP_UNREACH_NET) ||
(icmp_code == ICMP_UNREACH_HOST) ||
(icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
(icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
(icmp_code == ICMP_UNREACH_ISOLATED) ||
(icmp_code == ICMP_UNREACH_NET_PROHIB) ||
(icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
#if defined(__NetBSD__)
(icmp_code == ICMP_UNREACH_ADMIN_PROHIBIT)) {
#else
(icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
#endif
/* Mark the net unreachable. */
if (net->dest_state & SCTP_ADDR_REACHABLE) {
/* OK, that destination is NOT reachable. */
net->dest_state &= ~SCTP_ADDR_REACHABLE;
net->dest_state &= ~SCTP_ADDR_PF;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
stcb, 0,
(void *)net, SCTP_SO_NOT_LOCKED);
}
SCTP_TCB_UNLOCK(stcb);
} else if ((icmp_code == ICMP_UNREACH_PROTOCOL) ||
(icmp_code == ICMP_UNREACH_PORT)) {
/* Treat it like an ABORT. */
sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED);
#if defined(__APPLE__) && !defined(__Userspace__)
so = SCTP_INP_SO(inp);
atomic_add_int(&stcb->asoc.refcnt, 1);
SCTP_TCB_UNLOCK(stcb);
SCTP_SOCKET_LOCK(so, 1);
SCTP_TCB_LOCK(stcb);
atomic_subtract_int(&stcb->asoc.refcnt, 1);
#endif
(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
#if defined(__APPLE__) && !defined(__Userspace__)
SCTP_SOCKET_UNLOCK(so, 1);
/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed.*/
#endif
/* no need to unlock here, since the TCB is gone */
} else if (icmp_code == ICMP_UNREACH_NEEDFRAG) {
if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
SCTP_TCB_UNLOCK(stcb);
return;
}
/* Find the next (smaller) MTU */
if (next_mtu == 0) {
/*
* Old type router that does not tell us what the next
* MTU is.
* Rats we will have to guess (in a educated fashion
* of course).
*/
next_mtu = sctp_get_prev_mtu(ip_len);
}
/* Stop the PMTU timer. */
if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
timer_stopped = 1;
sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
} else {
timer_stopped = 0;
}
/* Update the path MTU. */
if (net->port) {
next_mtu -= sizeof(struct udphdr);
}
if (net->mtu > next_mtu) {
net->mtu = next_mtu;
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (net->port) {
sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
} else {
sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
}
#endif
}
/* Update the association MTU */
if (stcb->asoc.smallest_mtu > next_mtu) {
sctp_pathmtu_adjustment(stcb, next_mtu);
}
/* Finally, start the PMTU timer if it was running before. */
if (timer_stopped) {
sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
}
SCTP_TCB_UNLOCK(stcb);
} else {
SCTP_TCB_UNLOCK(stcb);
}
}
#endif
#if !defined(__Userspace__)
void
#if defined(__APPLE__) && !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) && !defined(APPLE_ELCAPITAN)
sctp_ctlinput(int cmd, struct sockaddr *sa, void *vip, struct ifnet *ifp SCTP_UNUSED)
#else
sctp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
#endif
{
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct ip *outer_ip;
#endif
struct ip *inner_ip;
struct sctphdr *sh;
struct icmp *icmp;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
struct sctp_nets *net;
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct sctp_init_chunk *ch;
#endif
struct sockaddr_in src, dst;
if (sa->sa_family != AF_INET ||
((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
return;
}
if (PRC_IS_REDIRECT(cmd)) {
vip = NULL;
} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
return;
}
if (vip != NULL) {
inner_ip = (struct ip *)vip;
icmp = (struct icmp *)((caddr_t)inner_ip -
(sizeof(struct icmp) - sizeof(struct ip)));
#if defined(__FreeBSD__) && !defined(__Userspace__)
outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
#endif
sh = (struct sctphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
memset(&src, 0, sizeof(struct sockaddr_in));
src.sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
src.sin_len = sizeof(struct sockaddr_in);
#endif
src.sin_port = sh->src_port;
src.sin_addr = inner_ip->ip_src;
memset(&dst, 0, sizeof(struct sockaddr_in));
dst.sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
dst.sin_len = sizeof(struct sockaddr_in);
#endif
dst.sin_port = sh->dest_port;
dst.sin_addr = inner_ip->ip_dst;
/*
* 'dst' holds the dest of the packet that failed to be sent.
* 'src' holds our local endpoint address. Thus we reverse
* the dst and the src in the lookup.
*/
inp = NULL;
net = NULL;
stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
(struct sockaddr *)&src,
&inp, &net, 1,
SCTP_DEFAULT_VRFID);
if ((stcb != NULL) &&
(net != NULL) &&
(inp != NULL)) {
/* Check the verification tag */
if (ntohl(sh->v_tag) != 0) {
/*
* This must be the verification tag used for
* sending out packets. We don't consider
* packets reflecting the verification tag.
*/
if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
SCTP_TCB_UNLOCK(stcb);
return;
}
} else {
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (ntohs(outer_ip->ip_len) >=
sizeof(struct ip) +
8 + (inner_ip->ip_hl << 2) + 20) {
/*
* In this case we can check if we
* got an INIT chunk and if the
* initiate tag matches.
*/
ch = (struct sctp_init_chunk *)(sh + 1);
if ((ch->ch.chunk_type != SCTP_INITIATION) ||
(ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
SCTP_TCB_UNLOCK(stcb);
return;
}
} else {
SCTP_TCB_UNLOCK(stcb);
return;
}
#else
SCTP_TCB_UNLOCK(stcb);
return;
#endif
}
sctp_notify(inp, stcb, net,
icmp->icmp_type,
icmp->icmp_code,
#if defined(__FreeBSD__) && !defined(__Userspace__)
ntohs(inner_ip->ip_len),
#else
inner_ip->ip_len,
#endif
(uint32_t)ntohs(icmp->icmp_nextmtu));
#if defined(__Userspace__)
if (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) &&
(stcb->sctp_socket != NULL)) {
struct socket *upcall_socket;
upcall_socket = stcb->sctp_socket;
SOCK_LOCK(upcall_socket);
soref(upcall_socket);
SOCK_UNLOCK(upcall_socket);
if ((upcall_socket->so_upcall != NULL) &&
(upcall_socket->so_error != 0)) {
(*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
}
ACCEPT_LOCK();
SOCK_LOCK(upcall_socket);
sorele(upcall_socket);
}
#endif
} else {
if ((stcb == NULL) && (inp != NULL)) {
/* reduce ref-count */
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
SCTP_INP_WUNLOCK(inp);
}
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
}
}
return;
}
#endif
#endif
#if defined(__FreeBSD__) && !defined(__Userspace__)
static int
sctp_getcred(SYSCTL_HANDLER_ARGS)
{
struct xucred xuc;
struct sockaddr_in addrs[2];
struct sctp_inpcb *inp;
struct sctp_nets *net;
struct sctp_tcb *stcb;
int error;
uint32_t vrf_id;
/* FIX, for non-bsd is this right? */
vrf_id = SCTP_DEFAULT_VRFID;
error = priv_check(req->td, PRIV_NETINET_GETCRED);
if (error)
return (error);
error = SYSCTL_IN(req, addrs, sizeof(addrs));
if (error)
return (error);
stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]),
sintosa(&addrs[0]),
&inp, &net, 1, vrf_id);
if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
if ((inp != NULL) && (stcb == NULL)) {
/* reduce ref-count */
SCTP_INP_WLOCK(inp);
SCTP_INP_DECR_REF(inp);
goto cred_can_cont;
}
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
goto out;
}
SCTP_TCB_UNLOCK(stcb);
/* We use the write lock here, only
* since in the error leg we need it.
* If we used RLOCK, then we would have
* to wlock/decr/unlock/rlock. Which
* in theory could create a hole. Better
* to use higher wlock.
*/
SCTP_INP_WLOCK(inp);
cred_can_cont:
error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
if (error) {
SCTP_INP_WUNLOCK(inp);
goto out;
}
cru2x(inp->sctp_socket->so_cred, &xuc);
SCTP_INP_WUNLOCK(inp);
error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
out:
return (error);
}
SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred,
CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
0, 0, sctp_getcred, "S,ucred",
"Get the ucred of a SCTP connection");
#endif
#ifdef INET
#if defined(_WIN32) || defined(__Userspace__)
int
#elif defined(__FreeBSD__)
static void
#else
static int
#endif
sctp_abort(struct socket *so)
{
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
#if defined(__FreeBSD__) && !defined(__Userspace__)
return;
#else
SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
#endif
}
SCTP_INP_WLOCK(inp);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 17);
#endif
if (((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0)) {
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP;
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 16);
#endif
SCTP_INP_WUNLOCK(inp);
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
SOCK_LOCK(so);
SCTP_SB_CLEAR(so->so_snd);
/* same for the rcv ones, they are only
* here for the accounting/select.
*/
SCTP_SB_CLEAR(so->so_rcv);
#if defined(__APPLE__) && !defined(__Userspace__)
so->so_usecount--;
#else
/* Now null out the reference, we are completely detached. */
so->so_pcb = NULL;
#endif
SOCK_UNLOCK(so);
} else {
SCTP_INP_WUNLOCK(inp);
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#else
return (0);
#endif
}
#if defined(__Userspace__)
int
#else
static int
#endif
#if defined(__Userspace__)
sctp_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
#elif defined(__FreeBSD__)
sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
#elif defined(_WIN32)
sctp_attach(struct socket *so, int proto SCTP_UNUSED, PKTHREAD p SCTP_UNUSED)
#else
sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct proc *p SCTP_UNUSED)
#endif
{
struct sctp_inpcb *inp;
struct user_inpcb *ip_inp;
int error;
#if !defined(__Userspace__)
uint32_t vrf_id = SCTP_DEFAULT_VRFID;
#endif
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp != NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
if (error) {
return (error);
}
}
error = sctp_inpcb_alloc(so, vrf_id);
if (error) {
return (error);
}
inp = (struct sctp_inpcb *)so->so_pcb;
SCTP_INP_WLOCK(inp);
inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
ip_inp = &inp->ip_inp.inp;
ip_inp->inp_vflag |= INP_IPV4;
ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
SCTP_INP_WUNLOCK(inp);
return (0);
}
#if defined(__Userspace__)
int
sctp_bind(struct socket *so, struct sockaddr *addr) {
void *p = NULL;
#elif defined(__FreeBSD__)
static int
sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
{
#elif defined(__APPLE__)
static int
sctp_bind(struct socket *so, struct sockaddr *addr, struct proc *p) {
#elif defined(_WIN32)
static int
sctp_bind(struct socket *so, struct sockaddr *addr, PKTHREAD p) {
#else
static int
sctp_bind(struct socket *so, struct mbuf *nam, struct proc *p)
{
struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
#endif
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (addr != NULL) {
#ifdef HAVE_SA_LEN
if ((addr->sa_family != AF_INET) ||
(addr->sa_len != sizeof(struct sockaddr_in))) {
#else
if (addr->sa_family != AF_INET) {
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
}
return (sctp_inpcb_bind(so, addr, NULL, p));
}
#endif
#if defined(__Userspace__)
int
sctpconn_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
{
struct sctp_inpcb *inp;
struct user_inpcb *ip_inp;
int error;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp != NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
if (error) {
return (error);
}
}
error = sctp_inpcb_alloc(so, vrf_id);
if (error) {
return (error);
}
inp = (struct sctp_inpcb *)so->so_pcb;
SCTP_INP_WLOCK(inp);
inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;
inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_CONN;
ip_inp = &inp->ip_inp.inp;
ip_inp->inp_vflag |= INP_CONN;
ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
SCTP_INP_WUNLOCK(inp);
return (0);
}
int
sctpconn_bind(struct socket *so, struct sockaddr *addr)
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (addr != NULL) {
#ifdef HAVE_SA_LEN
if ((addr->sa_family != AF_CONN) ||
(addr->sa_len != sizeof(struct sockaddr_conn))) {
#else
if (addr->sa_family != AF_CONN) {
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
}
return (sctp_inpcb_bind(so, addr, NULL, NULL));
}
#endif
#if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
void
sctp_close(struct socket *so)
{
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL)
return;
/* Inform all the lower layer assoc that we
* are done.
*/
SCTP_INP_WLOCK(inp);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 17);
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP;
#if defined(__Userspace__)
if (((so->so_options & SCTP_SO_LINGER) && (so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#else
if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#endif
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 13);
#endif
SCTP_INP_WUNLOCK(inp);
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
} else {
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 14);
#endif
SCTP_INP_WUNLOCK(inp);
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
}
/* The socket is now detached, no matter what
* the state of the SCTP association.
*/
SOCK_LOCK(so);
SCTP_SB_CLEAR(so->so_snd);
/* same for the rcv ones, they are only
* here for the accounting/select.
*/
SCTP_SB_CLEAR(so->so_rcv);
#if !(defined(__APPLE__) && !defined(__Userspace__))
/* Now null out the reference, we are completely detached. */
so->so_pcb = NULL;
#endif
SOCK_UNLOCK(so);
} else {
SCTP_INP_WUNLOCK(inp);
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
}
#else
int
sctp_detach(struct socket *so)
{
struct sctp_inpcb *inp;
uint32_t flags;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
#if defined(__FreeBSD__) && !defined(__Userspace__)
return;
#else
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
#endif
}
sctp_must_try_again:
flags = inp->sctp_flags;
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 17);
#endif
if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
(atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
#if defined(__Userspace__)
if (((so->so_options & SCTP_SO_LINGER) && (so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#else
if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#endif
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 13);
#endif
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
} else {
#ifdef SCTP_LOG_CLOSING
sctp_log_closing(inp, NULL, 13);
#endif
sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
}
/* The socket is now detached, no matter what
* the state of the SCTP association.
*/
SCTP_SB_CLEAR(so->so_snd);
/* same for the rcv ones, they are only
* here for the accounting/select.
*/
SCTP_SB_CLEAR(so->so_rcv);
#if !(defined(__APPLE__) && !defined(__Userspace__))
/* Now disconnect */
so->so_pcb = NULL;
#endif
} else {
flags = inp->sctp_flags;
if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
goto sctp_must_try_again;
}
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
return;
#else
return (0);
#endif
}
#endif
#if defined(__Userspace__)
/* __Userspace__ is not calling sctp_sendm */
#endif
#if !(defined(_WIN32) && !defined(__Userspace__))
int
#if defined(__FreeBSD__) && !defined(__Userspace__)
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
struct mbuf *control, struct thread *p);
#else
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
struct mbuf *control, struct proc *p);
#endif
int
#if defined(__FreeBSD__) && !defined(__Userspace__)
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
struct mbuf *control, struct thread *p)
{
#else
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
struct mbuf *control, struct proc *p)
{
#endif
struct sctp_inpcb *inp;
int error;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
if (control) {
sctp_m_freem(control);
control = NULL;
}
SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
sctp_m_freem(m);
return (EINVAL);
}
/* Got to have an to address if we are NOT a connected socket */
if ((addr == NULL) &&
((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) {
goto connected_type;
}
error = 0;
if (addr == NULL) {
SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
error = EDESTADDRREQ;
} else if (addr->sa_family != AF_INET) {
SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
error = EAFNOSUPPORT;
#if defined(HAVE_SA_LEN)
} else if (addr->sa_len != sizeof(struct sockaddr_in)) {
SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
#endif
}
if (error != 0) {
sctp_m_freem(m);
if (control) {
sctp_m_freem(control);
control = NULL;
}
return (error);
}
connected_type:
/* now what about control */
if (control) {
if (inp->control) {
sctp_m_freem(inp->control);
inp->control = NULL;
}
inp->control = control;
}
/* Place the data */
if (inp->pkt) {
SCTP_BUF_NEXT(inp->pkt_last) = m;
inp->pkt_last = m;
} else {
inp->pkt_last = inp->pkt = m;
}
if (
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
/* FreeBSD uses a flag passed */
((flags & PRUS_MORETOCOME) == 0)
#else
1 /* Open BSD does not have any "more to come"
* indication */
#endif
) {
/*
* note with the current version this code will only be used
* by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
* re-defining sosend to use the sctp_sosend. One can
* optionally switch back to this code (by changing back the
* definitions) but this is not advisable. This code is used
* by FreeBSD when sending a file with sendfile() though.
*/
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
int ret;
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
inp->pkt = NULL;
inp->control = NULL;
return (ret);
} else {
return (0);
}
}
#endif
int
sctp_disconnect(struct socket *so)
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
return (ENOTCONN);
}
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
if (LIST_EMPTY(&inp->sctp_asoc_list)) {
/* No connection */
SCTP_INP_RUNLOCK(inp);
return (0);
} else {
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
struct sctp_association *asoc;
struct sctp_tcb *stcb;
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
SCTP_INP_RUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
SCTP_TCB_LOCK(stcb);
asoc = &stcb->asoc;
if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
/* We are about to be freed, out of here */
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
return (0);
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
#if defined(__Userspace__)
if (((so->so_options & SCTP_SO_LINGER) &&
(so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#else
if (((so->so_options & SO_LINGER) &&
(so->so_linger == 0)) ||
(so->so_rcv.sb_cc > 0)) {
#endif
if (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) {
/* Left with Data unread */
struct mbuf *op_err;
op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
SCTP_STAT_INCR_COUNTER32(sctps_aborted);
}
SCTP_INP_RUNLOCK(inp);
if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
(SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
SCTP_STAT_DECR_GAUGE32(sctps_currestab);
}
(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
/* No unlock tcb assoc is gone */
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
return (0);
}
if (TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(asoc->stream_queue_cnt == 0)) {
/* there is nothing queued to send, so done */
if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
goto abort_anyway;
}
if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
(SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
/* only send SHUTDOWN 1st time thru */
struct sctp_nets *netp;
if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
(SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
SCTP_STAT_DECR_GAUGE32(sctps_currestab);
}
SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
sctp_stop_timers_for_shutdown(stcb);
if (stcb->asoc.alternate) {
netp = stcb->asoc.alternate;
} else {
netp = stcb->asoc.primary_destination;
}
sctp_send_shutdown(stcb,netp);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
stcb->sctp_ep, stcb, netp);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
stcb->sctp_ep, stcb, NULL);
sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
}
} else {
/*
* we still got (or just got) data to send,
* so set SHUTDOWN_PENDING
*/
/*
* XXX sockets draft says that SCTP_EOF
* should be sent with no data. currently,
* we will allow user data to be sent first
* and move to SHUTDOWN-PENDING
*/
SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
}
if (TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
struct mbuf *op_err;
abort_anyway:
op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
SCTP_STAT_INCR_COUNTER32(sctps_aborted);
if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
(SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
SCTP_STAT_DECR_GAUGE32(sctps_currestab);
}
SCTP_INP_RUNLOCK(inp);
(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
return (0);
} else {
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
}
}
soisdisconnecting(so);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
return (0);
}
/* not reached */
} else {
/* UDP model does not support this */
SCTP_INP_RUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
return (EOPNOTSUPP);
}
}
#if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
int
sctp_flush(struct socket *so, int how)
{
/*
* We will just clear out the values and let
* subsequent close clear out the data, if any.
* Note if the user did a shutdown(SHUT_RD) they
* will not be able to read the data, the socket
* will block that from happening.
*/
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
SCTP_INP_RLOCK(inp);
/* For the 1 to many model this does nothing */
if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
SCTP_INP_RUNLOCK(inp);
return (0);
}
SCTP_INP_RUNLOCK(inp);
if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
/* First make sure the sb will be happy, we don't
* use these except maybe the count
*/
SCTP_INP_WLOCK(inp);
SCTP_INP_READ_LOCK(inp);
inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
SCTP_INP_READ_UNLOCK(inp);
SCTP_INP_WUNLOCK(inp);
so->so_rcv.sb_cc = 0;
so->so_rcv.sb_mbcnt = 0;
so->so_rcv.sb_mb = NULL;
}
if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
/* First make sure the sb will be happy, we don't
* use these except maybe the count
*/
so->so_snd.sb_cc = 0;
so->so_snd.sb_mbcnt = 0;
so->so_snd.sb_mb = NULL;
}
return (0);
}
#endif
int
sctp_shutdown(struct socket *so)
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
SCTP_INP_RLOCK(inp);
/* For UDP model this is a invalid call */
if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
/* Restore the flags that the soshutdown took away. */
#if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
SOCKBUF_LOCK(&so->so_rcv);
so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
SOCKBUF_UNLOCK(&so->so_rcv);
#else
SOCK_LOCK(so);
so->so_state &= ~SS_CANTRCVMORE;
SOCK_UNLOCK(so);
#endif
/* This proc will wakeup for read and do nothing (I hope) */
SCTP_INP_RUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
return (EOPNOTSUPP);
} else {
/*
* Ok, if we reach here its the TCP model and it is either
* a SHUT_WR or SHUT_RDWR.
* This means we put the shutdown flag against it.
*/
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
struct sctp_tcb *stcb;
struct sctp_association *asoc;
struct sctp_nets *netp;
if ((so->so_state &
(SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) == 0) {
SCTP_INP_RUNLOCK(inp);
return (ENOTCONN);
}
socantsendmore(so);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
/*
* Ok, we hit the case that the shutdown call was
* made after an abort or something. Nothing to do
* now.
*/
SCTP_INP_RUNLOCK(inp);
return (0);
}
SCTP_TCB_LOCK(stcb);
asoc = &stcb->asoc;
if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
return (0);
}
if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
(SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_ECHOED) &&
(SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN)) {
/* If we are not in or before ESTABLISHED, there is
* no protocol action required.
*/
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
return (0);
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
if (stcb->asoc.alternate) {
netp = stcb->asoc.alternate;
} else {
netp = stcb->asoc.primary_destination;
}
if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(asoc->stream_queue_cnt == 0)) {
if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
goto abort_anyway;
}
/* there is nothing queued to send, so I'm done... */
SCTP_STAT_DECR_GAUGE32(sctps_currestab);
SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
sctp_stop_timers_for_shutdown(stcb);
sctp_send_shutdown(stcb, netp);
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
stcb->sctp_ep, stcb, netp);
} else {
/*
* We still got (or just got) data to send, so set
* SHUTDOWN_PENDING.
*/
SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
}
if (TAILQ_EMPTY(&asoc->send_queue) &&
TAILQ_EMPTY(&asoc->sent_queue) &&
(asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
struct mbuf *op_err;
abort_anyway:
op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
SCTP_INP_RUNLOCK(inp);
sctp_abort_an_association(stcb->sctp_ep, stcb,
op_err, false, SCTP_SO_LOCKED);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
return (0);
}
}
sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
/* XXX: Why do this in the case where we have still data queued? */
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
SCTP_TCB_UNLOCK(stcb);
SCTP_INP_RUNLOCK(inp);
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
return (0);
}
}
/*
* copies a "user" presentable address and removes embedded scope, etc.
* returns 0 on success, 1 on error
*/
static uint32_t
sctp_fill_user_address(struct sockaddr *dst, struct sockaddr *src)
{
#ifdef INET6
#if defined(SCTP_EMBEDDED_V6_SCOPE)
struct sockaddr_in6 lsa6;
src = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)src,
&lsa6);
#endif
#endif
#ifdef HAVE_SA_LEN
memcpy(dst, src, src->sa_len);
#else
switch (src->sa_family) {
#ifdef INET
case AF_INET:
memcpy(dst, src, sizeof(struct sockaddr_in));
break;
#endif
#ifdef INET6
case AF_INET6:
memcpy(dst, src, sizeof(struct sockaddr_in6));
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
memcpy(dst, src, sizeof(struct sockaddr_conn));
break;
#endif
default:
/* TSNH */
break;
}
#endif
return (0);
}
static size_t
sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
size_t limit,
struct sockaddr *addr,
uint32_t vrf_id)
{
struct sctp_ifn *sctp_ifn;
struct sctp_ifa *sctp_ifa;
size_t actual;
int loopback_scope;
#if defined(INET)
int ipv4_local_scope, ipv4_addr_legal;
#endif
#if defined(INET6)
int local_scope, site_scope, ipv6_addr_legal;
#endif
#if defined(__Userspace__)
int conn_addr_legal;
#endif
struct sctp_vrf *vrf;
SCTP_IPI_ADDR_LOCK_ASSERT();
actual = 0;
if (limit == 0)
return (actual);
if (stcb) {
/* Turn on all the appropriate scope */
loopback_scope = stcb->asoc.scope.loopback_scope;
#if defined(INET)
ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
#endif
#if defined(INET6)
local_scope = stcb->asoc.scope.local_scope;
site_scope = stcb->asoc.scope.site_scope;
ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
#endif
#if defined(__Userspace__)
conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
#endif
} else {
/* Use generic values for endpoints. */
loopback_scope = 1;
#if defined(INET)
ipv4_local_scope = 1;
#endif
#if defined(INET6)
local_scope = 1;
site_scope = 1;
#endif
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
#if defined(INET6)
ipv6_addr_legal = 1;
#endif
#if defined(INET)
if (SCTP_IPV6_V6ONLY(inp)) {
ipv4_addr_legal = 0;
} else {
ipv4_addr_legal = 1;
}
#endif
#if defined(__Userspace__)
conn_addr_legal = 0;
#endif
} else {
#if defined(INET6)
ipv6_addr_legal = 0;
#endif
#if defined(__Userspace__)
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
conn_addr_legal = 1;
#if defined(INET)
ipv4_addr_legal = 0;
#endif
} else {
conn_addr_legal = 0;
#if defined(INET)
ipv4_addr_legal = 1;
#endif
}
#else
#if defined(INET)
ipv4_addr_legal = 1;
#endif
#endif
}
}
vrf = sctp_find_vrf(vrf_id);
if (vrf == NULL) {
return (0);
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
if ((loopback_scope == 0) &&
SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
/* Skip loopback if loopback_scope not set */
continue;
}
LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
if (stcb) {
/*
* For the BOUND-ALL case, the list
* associated with a TCB is Always
* considered a reverse list.. i.e.
* it lists addresses that are NOT
* part of the association. If this
* is one of those we must skip it.
*/
if (sctp_is_addr_restricted(stcb,
sctp_ifa)) {
continue;
}
}
switch (sctp_ifa->address.sa.sa_family) {
#ifdef INET
case AF_INET:
if (ipv4_addr_legal) {
struct sockaddr_in *sin;
sin = &sctp_ifa->address.sin;
if (sin->sin_addr.s_addr == 0) {
/*
* we skip unspecifed
* addresses
*/
continue;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
&sin->sin_addr) != 0) {
continue;
}
#endif
if ((ipv4_local_scope == 0) &&
(IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
continue;
}
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
if (actual + sizeof(struct sockaddr_in6) > limit) {
return (actual);
}
in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)addr);
((struct sockaddr_in6 *)addr)->sin6_port = inp->sctp_lport;
addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in6));
actual += sizeof(struct sockaddr_in6);
} else {
#endif
if (actual + sizeof(struct sockaddr_in) > limit) {
return (actual);
}
memcpy(addr, sin, sizeof(struct sockaddr_in));
((struct sockaddr_in *)addr)->sin_port = inp->sctp_lport;
addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in));
actual += sizeof(struct sockaddr_in);
#ifdef INET6
}
#endif
} else {
continue;
}
break;
#endif
#ifdef INET6
case AF_INET6:
if (ipv6_addr_legal) {
struct sockaddr_in6 *sin6;
#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
struct sockaddr_in6 lsa6;
#endif
sin6 = &sctp_ifa->address.sin6;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
/*
* we skip unspecifed
* addresses
*/
continue;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
&sin6->sin6_addr) != 0) {
continue;
}
#endif
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
if (local_scope == 0)
continue;
#if defined(SCTP_EMBEDDED_V6_SCOPE)
if (sin6->sin6_scope_id == 0) {
#ifdef SCTP_KAME
if (sa6_recoverscope(sin6) != 0)
/*
* bad link
* local
* address
*/
continue;
#else
lsa6 = *sin6;
if (in6_recoverscope(&lsa6,
&lsa6.sin6_addr,
NULL))
/*
* bad link
* local
* address
*/
continue;
sin6 = &lsa6;
#endif /* SCTP_KAME */
}
#endif /* SCTP_EMBEDDED_V6_SCOPE */
}
if ((site_scope == 0) &&
(IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
continue;
}
if (actual + sizeof(struct sockaddr_in6) > limit) {
return (actual);
}
memcpy(addr, sin6, sizeof(struct sockaddr_in6));
((struct sockaddr_in6 *)addr)->sin6_port = inp->sctp_lport;
addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in6));
actual += sizeof(struct sockaddr_in6);
} else {
continue;
}
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
if (conn_addr_legal) {
if (actual + sizeof(struct sockaddr_conn) > limit) {
return (actual);
}
memcpy(addr, &sctp_ifa->address.sconn, sizeof(struct sockaddr_conn));
((struct sockaddr_conn *)addr)->sconn_port = inp->sctp_lport;
addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_conn));
actual += sizeof(struct sockaddr_conn);
} else {
continue;
}
#endif
default:
/* TSNH */
break;
}
}
}
} else {
struct sctp_laddr *laddr;
size_t sa_len;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (stcb) {
if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
continue;
}
}
#ifdef HAVE_SA_LEN
sa_len = laddr->ifa->address.sa.sa_len;
#else
switch (laddr->ifa->address.sa.sa_family) {
#ifdef INET
case AF_INET:
sa_len = sizeof(struct sockaddr_in);
break;
#endif
#ifdef INET6
case AF_INET6:
sa_len = sizeof(struct sockaddr_in6);
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
sa_len = sizeof(struct sockaddr_conn);
break;
#endif
default:
/* TSNH */
sa_len = 0;
break;
}
#endif
if (actual + sa_len > limit) {
return (actual);
}
if (sctp_fill_user_address(addr, &laddr->ifa->address.sa))
continue;
switch (laddr->ifa->address.sa.sa_family) {
#ifdef INET
case AF_INET:
((struct sockaddr_in *)addr)->sin_port = inp->sctp_lport;
break;
#endif
#ifdef INET6
case AF_INET6:
((struct sockaddr_in6 *)addr)->sin6_port = inp->sctp_lport;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
((struct sockaddr_conn *)addr)->sconn_port = inp->sctp_lport;
break;
#endif
default:
/* TSNH */
break;
}
addr = (struct sockaddr *)((caddr_t)addr + sa_len);
actual += sa_len;
}
}
return (actual);
}
static size_t
sctp_fill_up_addresses(struct sctp_inpcb *inp,
struct sctp_tcb *stcb,
size_t limit,
struct sockaddr *addr)
{
size_t size = 0;
#ifdef SCTP_MVRF
uint32_t id;
#endif
SCTP_IPI_ADDR_RLOCK();
#ifdef SCTP_MVRF
/*
* FIX ME: ?? this WILL report duplicate addresses if they appear
* in more than one VRF.
*/
/* fill up addresses for all VRFs on the endpoint */
for (id = 0; (id < inp->num_vrfs) && (size < limit); id++) {
size += sctp_fill_up_addresses_vrf(inp, stcb, limit, addr,
inp->m_vrf_ids[id]);
addr = (struct sockaddr *)((caddr_t)addr + size);
}
#else
/* fill up addresses for the endpoint's default vrf */
size = sctp_fill_up_addresses_vrf(inp, stcb, limit, addr,
inp->def_vrf_id);
#endif
SCTP_IPI_ADDR_RUNLOCK();
return (size);
}
static int
sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
{
int cnt = 0;
struct sctp_vrf *vrf = NULL;
/*
* In both sub-set bound an bound_all cases we return the MAXIMUM
* number of addresses that you COULD get. In reality the sub-set
* bound may have an exclusion list for a given TCB OR in the
* bound-all case a TCB may NOT include the loopback or other
* addresses as well.
*/
SCTP_IPI_ADDR_LOCK_ASSERT();
vrf = sctp_find_vrf(vrf_id);
if (vrf == NULL) {
return (0);
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
struct sctp_ifn *sctp_ifn;
struct sctp_ifa *sctp_ifa;
LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
/* Count them if they are the right type */
switch (sctp_ifa->address.sa.sa_family) {
#ifdef INET
case AF_INET:
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
cnt += sizeof(struct sockaddr_in6);
else
cnt += sizeof(struct sockaddr_in);
#else
cnt += sizeof(struct sockaddr_in);
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
cnt += sizeof(struct sockaddr_in6);
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
cnt += sizeof(struct sockaddr_conn);
break;
#endif
default:
break;
}
}
}
} else {
struct sctp_laddr *laddr;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
switch (laddr->ifa->address.sa.sa_family) {
#ifdef INET
case AF_INET:
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
cnt += sizeof(struct sockaddr_in6);
else
cnt += sizeof(struct sockaddr_in);
#else
cnt += sizeof(struct sockaddr_in);
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
cnt += sizeof(struct sockaddr_in6);
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
cnt += sizeof(struct sockaddr_conn);
break;
#endif
default:
break;
}
}
}
return (cnt);
}
static int
sctp_count_max_addresses(struct sctp_inpcb *inp)
{
int cnt = 0;
#ifdef SCTP_MVRF
int id;
#endif
SCTP_IPI_ADDR_RLOCK();
#ifdef SCTP_MVRF
/*
* FIX ME: ?? this WILL count duplicate addresses if they appear
* in more than one VRF.
*/
/* count addresses for all VRFs on the endpoint */
for (id = 0; id < inp->num_vrfs; id++) {
cnt += sctp_count_max_addresses_vrf(inp, inp->m_vrf_ids[id]);
}
#else
/* count addresses for the endpoint's default VRF */
cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
#endif
SCTP_IPI_ADDR_RUNLOCK();
return (cnt);
}
static int
sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
size_t optsize, void *p, int delay)
{
int error;
int creat_lock_on = 0;
struct sctp_tcb *stcb = NULL;
struct sockaddr *sa;
unsigned int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
uint32_t vrf_id;
sctp_assoc_t *a_id;
SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
/* We are already connected AND the TCP model */
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
return (EADDRINUSE);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
(sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
SCTP_INP_RUNLOCK(inp);
}
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
return (EALREADY);
}
SCTP_INP_INCR_REF(inp);
SCTP_ASOC_CREATE_LOCK(inp);
creat_lock_on = 1;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
error = EFAULT;
goto out_now;
}
totaddrp = (unsigned int *)optval;
totaddr = *totaddrp;
sa = (struct sockaddr *)(totaddrp + 1);
error = sctp_connectx_helper_find(inp, sa, totaddr, &num_v4, &num_v6, (unsigned int)(optsize - sizeof(int)));
if (error != 0) {
/* Already have or am bring up an association */
SCTP_ASOC_CREATE_UNLOCK(inp);
creat_lock_on = 0;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
goto out_now;
}
#ifdef INET6
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
(num_v6 > 0)) {
error = EINVAL;
goto out_now;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(num_v4 > 0)) {
if (SCTP_IPV6_V6ONLY(inp)) {
/*
* if IPV6_V6ONLY flag, ignore connections destined
* to a v4 addr or v4-mapped addr
*/
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
}
#endif /* INET6 */
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
SCTP_PCB_FLAGS_UNBOUND) {
/* Bind a ephemeral port */
error = sctp_inpcb_bind(so, NULL, NULL, p);
if (error) {
goto out_now;
}
}
/* FIX ME: do we want to pass in a vrf on the connect call? */
vrf_id = inp->def_vrf_id;
/* We are GOOD to go */
stcb = sctp_aloc_assoc_connected(inp, sa, &error, 0, 0, vrf_id,
inp->sctp_ep.pre_open_stream_count,
inp->sctp_ep.port,
#if defined(__FreeBSD__) && !defined(__Userspace__)
(struct thread *)p,
#elif defined(_WIN32) && !defined(__Userspace__)
(PKTHREAD)p,
#else
(struct proc *)p,
#endif
SCTP_INITIALIZE_AUTH_PARAMS);
if (stcb == NULL) {
/* Gak! no memory */
goto out_now;
}
SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
/* move to second address */
switch (sa->sa_family) {
#ifdef INET
case AF_INET:
sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
break;
#endif
#ifdef INET6
case AF_INET6:
sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
break;
#endif
default:
break;
}
error = 0;
sctp_connectx_helper_add(stcb, sa, (totaddr-1), &error);
/* Fill in the return id */
if (error) {
goto out_now;
}
a_id = (sctp_assoc_t *)optval;
*a_id = sctp_get_associd(stcb);
if (delay) {
/* doing delayed connection */
stcb->asoc.delayed_connection = 1;
sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
} else {
(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
}
SCTP_TCB_UNLOCK(stcb);
out_now:
if (creat_lock_on) {
SCTP_ASOC_CREATE_UNLOCK(inp);
}
SCTP_INP_DECR_REF(inp);
return (error);
}
#define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
SCTP_INP_RLOCK(inp); \
stcb = LIST_FIRST(&inp->sctp_asoc_list); \
if (stcb) { \
SCTP_TCB_LOCK(stcb); \
} \
SCTP_INP_RUNLOCK(inp); \
} else if (assoc_id > SCTP_ALL_ASSOC) { \
stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
if (stcb == NULL) { \
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
error = ENOENT; \
break; \
} \
} else { \
stcb = NULL; \
} \
}
#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
if (size < sizeof(type)) { \
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
error = EINVAL; \
break; \
} else { \
destp = (type *)srcp; \
} \
}
#if defined(__Userspace__)
int
#else
static int
#endif
sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
void *p) {
struct sctp_inpcb *inp = NULL;
int error, val = 0;
struct sctp_tcb *stcb = NULL;
if (optval == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return EINVAL;
}
error = 0;
switch (optname) {
case SCTP_NODELAY:
case SCTP_AUTOCLOSE:
case SCTP_EXPLICIT_EOR:
case SCTP_AUTO_ASCONF:
case SCTP_DISABLE_FRAGMENTS:
case SCTP_I_WANT_MAPPED_V4_ADDR:
case SCTP_USE_EXT_RCVINFO:
SCTP_INP_RLOCK(inp);
switch (optname) {
case SCTP_DISABLE_FRAGMENTS:
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
break;
case SCTP_I_WANT_MAPPED_V4_ADDR:
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
break;
case SCTP_AUTO_ASCONF:
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
/* only valid for bound all sockets */
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto flags_out;
}
break;
case SCTP_EXPLICIT_EOR:
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
break;
case SCTP_NODELAY:
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
break;
case SCTP_USE_EXT_RCVINFO:
val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
break;
case SCTP_AUTOCLOSE:
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
val = sctp_ticks_to_secs(inp->sctp_ep.auto_close_time);
else
val = 0;
break;
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
error = ENOPROTOOPT;
} /* end switch (sopt->sopt_name) */
if (*optsize < sizeof(val)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
flags_out:
SCTP_INP_RUNLOCK(inp);
if (error == 0) {
/* return the option value */
*(int *)optval = val;
*optsize = sizeof(val);
}
break;
case SCTP_GET_PACKET_LOG:
{
#ifdef SCTP_PACKET_LOGGING
uint8_t *target;
int ret;
SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
ret = sctp_copy_out_packet_log(target , (int)*optsize);
*optsize = ret;
#else
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
#endif
break;
}
case SCTP_REUSE_PORT:
{
uint32_t *value;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
/* Can't do this for a 1-m socket */
error = EINVAL;
break;
}
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
*optsize = sizeof(uint32_t);
break;
}
case SCTP_PARTIAL_DELIVERY_POINT:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
*value = inp->partial_delivery_point;
*optsize = sizeof(uint32_t);
break;
}
case SCTP_FRAGMENT_INTERLEAVE:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
*value = SCTP_FRAG_LEVEL_2;
} else {
*value = SCTP_FRAG_LEVEL_1;
}
} else {
*value = SCTP_FRAG_LEVEL_0;
}
*optsize = sizeof(uint32_t);
break;
}
case SCTP_INTERLEAVING_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.idata_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
if (inp->idata_supported) {
av->assoc_value = 1;
} else {
av->assoc_value = 0;
}
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_CMT_ON_OFF:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.sctp_cmt_on_off;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->sctp_cmt_on_off;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_PLUGGABLE_CC:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.congestion_control_module;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_CC_OPTION:
{
struct sctp_cc_option *cc_opt;
SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
if (stcb == NULL) {
error = EINVAL;
} else {
if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
error = ENOTSUP;
} else {
error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 0, cc_opt);
*optsize = sizeof(struct sctp_cc_option);
}
SCTP_TCB_UNLOCK(stcb);
}
break;
}
case SCTP_PLUGGABLE_SS:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.stream_scheduling_module;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_SS_VALUE:
{
struct sctp_stream_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
(stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
&av->stream_value) < 0)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
*optsize = sizeof(struct sctp_stream_value);
}
SCTP_TCB_UNLOCK(stcb);
} else {
/* Can't get stream value without association */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
}
case SCTP_GET_ADDR_LEN:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
error = EINVAL;
#ifdef INET
if (av->assoc_value == AF_INET) {
av->assoc_value = sizeof(struct sockaddr_in);
error = 0;
}
#endif
#ifdef INET6
if (av->assoc_value == AF_INET6) {
av->assoc_value = sizeof(struct sockaddr_in6);
error = 0;
}
#endif
#if defined(__Userspace__)
if (av->assoc_value == AF_CONN) {
av->assoc_value = sizeof(struct sockaddr_conn);
error = 0;
}
#endif
if (error) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_GET_ASSOC_NUMBER:
{
uint32_t *value, cnt;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
/* Can't do this for a 1-1 socket */
error = EINVAL;
SCTP_INP_RUNLOCK(inp);
break;
}
cnt = 0;
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
cnt++;
}
SCTP_INP_RUNLOCK(inp);
*value = cnt;
*optsize = sizeof(uint32_t);
break;
}
case SCTP_GET_ASSOC_ID_LIST:
{
struct sctp_assoc_ids *ids;
uint32_t at;
size_t limit;
SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
SCTP_INP_RLOCK(inp);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
/* Can't do this for a 1-1 socket */
error = EINVAL;
SCTP_INP_RUNLOCK(inp);
break;
}
at = 0;
limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
if (at < limit) {
ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
if (at == 0) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
SCTP_INP_RUNLOCK(inp);
if (error == 0) {
ids->gaids_number_of_ids = at;
*optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
}
break;
}
case SCTP_CONTEXT:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.context;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->sctp_context;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_VRF_ID:
{
uint32_t *default_vrfid;
SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
*default_vrfid = inp->def_vrf_id;
*optsize = sizeof(uint32_t);
break;
}
case SCTP_GET_ASOC_VRF:
{
struct sctp_assoc_value *id;
SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, id->assoc_id);
if (stcb == NULL) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
id->assoc_value = stcb->asoc.vrf_id;
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_GET_VRF_IDS:
{
#ifdef SCTP_MVRF
int siz_needed;
uint32_t *vrf_ids;
SCTP_CHECK_AND_CAST(vrf_ids, optval, uint32_t, *optsize);
siz_needed = inp->num_vrfs * sizeof(uint32_t);
if (*optsize < siz_needed) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
memcpy(vrf_ids, inp->m_vrf_ids, siz_needed);
*optsize = siz_needed;
}
#else
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
#endif
break;
}
case SCTP_GET_NONCE_VALUES:
{
struct sctp_get_nonce_values *gnv;
SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
if (stcb) {
gnv->gn_peers_tag = stcb->asoc.peer_vtag;
gnv->gn_local_tag = stcb->asoc.my_vtag;
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_get_nonce_values);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
error = ENOTCONN;
}
break;
}
case SCTP_DELAYED_SACK:
{
struct sctp_sack_info *sack;
SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
if (stcb) {
sack->sack_delay = stcb->asoc.delayed_ack;
sack->sack_freq = stcb->asoc.sack_freq;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(sack->sack_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
sack->sack_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_sack_info);
}
break;
}
case SCTP_GET_SNDBUF_USE:
{
struct sctp_sockstat *ss;
SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
if (stcb) {
ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
stcb->asoc.size_on_all_streams);
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_sockstat);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
error = ENOTCONN;
}
break;
}
case SCTP_MAX_BURST:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.max_burst;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->sctp_ep.max_burst;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_MAXSEG:
{
struct sctp_assoc_value *av;
int ovh;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
ovh = SCTP_MED_OVERHEAD;
} else {
ovh = SCTP_MED_V4_OVERHEAD;
}
if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
av->assoc_value = 0;
else
av->assoc_value = inp->sctp_frag_point - ovh;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_GET_STAT_LOG:
error = sctp_fill_stat_log(optval, optsize);
break;
case SCTP_EVENTS:
{
struct sctp_event_subscribe *events;
SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
memset(events, 0, sizeof(struct sctp_event_subscribe));
SCTP_INP_RLOCK(inp);
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
events->sctp_data_io_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
events->sctp_association_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
events->sctp_address_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
events->sctp_send_failure_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
events->sctp_peer_error_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
events->sctp_shutdown_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
events->sctp_partial_delivery_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
events->sctp_adaptation_layer_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
events->sctp_authentication_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
events->sctp_sender_dry_event = 1;
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
events->sctp_stream_reset_event = 1;
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(struct sctp_event_subscribe);
break;
}
case SCTP_ADAPTATION_LAYER:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
SCTP_INP_RLOCK(inp);
*value = inp->sctp_ep.adaptation_layer_indicator;
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(uint32_t);
break;
}
case SCTP_SET_INITIAL_DBG_SEQ:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
SCTP_INP_RLOCK(inp);
*value = inp->sctp_ep.initial_sequence_debug;
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(uint32_t);
break;
}
case SCTP_GET_LOCAL_ADDR_SIZE:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
SCTP_INP_RLOCK(inp);
*value = sctp_count_max_addresses(inp);
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(uint32_t);
break;
}
case SCTP_GET_REMOTE_ADDR_SIZE:
{
uint32_t *value;
size_t size;
struct sctp_nets *net;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
/* FIXME MT: change to sctp_assoc_value? */
SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) *value);
if (stcb) {
size = 0;
/* Count the sizes */
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
switch (net->ro._l_addr.sa.sa_family) {
#ifdef INET
case AF_INET:
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
size += sizeof(struct sockaddr_in6);
} else {
size += sizeof(struct sockaddr_in);
}
#else
size += sizeof(struct sockaddr_in);
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
size += sizeof(struct sockaddr_in6);
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
size += sizeof(struct sockaddr_conn);
break;
#endif
default:
break;
}
}
SCTP_TCB_UNLOCK(stcb);
*value = (uint32_t) size;
*optsize = sizeof(uint32_t);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
error = ENOTCONN;
}
break;
}
case SCTP_GET_PEER_ADDRESSES:
/*
* Get the address information, an array is passed in to
* fill up we pack it.
*/
{
size_t cpsz, left;
struct sockaddr *addr;
struct sctp_nets *net;
struct sctp_getaddresses *saddr;
SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
if (stcb) {
left = *optsize - offsetof(struct sctp_getaddresses, addr);
*optsize = offsetof(struct sctp_getaddresses, addr);
addr = &saddr->addr[0].sa;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
switch (net->ro._l_addr.sa.sa_family) {
#ifdef INET
case AF_INET:
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
cpsz = sizeof(struct sockaddr_in6);
} else {
cpsz = sizeof(struct sockaddr_in);
}
#else
cpsz = sizeof(struct sockaddr_in);
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
cpsz = sizeof(struct sockaddr_in6);
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
cpsz = sizeof(struct sockaddr_conn);
break;
#endif
default:
cpsz = 0;
break;
}
if (cpsz == 0) {
break;
}
if (left < cpsz) {
/* not enough room. */
break;
}
#if defined(INET) && defined(INET6)
if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
(net->ro._l_addr.sa.sa_family == AF_INET)) {
/* Must map the address */
in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
(struct sockaddr_in6 *)addr);
} else {
memcpy(addr, &net->ro._l_addr, cpsz);
}
#else
memcpy(addr, &net->ro._l_addr, cpsz);
#endif
((struct sockaddr_in *)addr)->sin_port = stcb->rport;
addr = (struct sockaddr *)((caddr_t)addr + cpsz);
left -= cpsz;
*optsize += cpsz;
}
SCTP_TCB_UNLOCK(stcb);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
}
break;
}
case SCTP_GET_LOCAL_ADDRESSES:
{
size_t limit, actual;
struct sctp_getaddresses *saddr;
SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
limit = *optsize - offsetof(struct sctp_getaddresses, addr);
actual = sctp_fill_up_addresses(inp, stcb, limit, &saddr->addr[0].sa);
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
*optsize = offsetof(struct sctp_getaddresses, addr) + actual;
break;
}
case SCTP_PEER_ADDR_PARAMS:
{
struct sctp_paddrparams *paddrp;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
#if defined(INET) && defined(INET6)
if (paddrp->spp_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&paddrp->spp_address;
}
} else {
addr = (struct sockaddr *)&paddrp->spp_address;
}
#else
addr = (struct sockaddr *)&paddrp->spp_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
if (stcb != NULL) {
/* Applies to the specific association */
paddrp->spp_flags = 0;
if (net != NULL) {
paddrp->spp_hbinterval = net->heart_beat_delay;
paddrp->spp_pathmaxrxt = net->failure_threshold;
paddrp->spp_pathmtu = net->mtu;
switch (net->ro._l_addr.sa.sa_family) {
#ifdef INET
case AF_INET:
paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
break;
#endif
#ifdef INET6
case AF_INET6:
paddrp->spp_pathmtu -= SCTP_MIN_OVERHEAD;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
paddrp->spp_pathmtu -= sizeof(struct sctphdr);
break;
#endif
default:
break;
}
/* get flags for HB */
if (net->dest_state & SCTP_ADDR_NOHB) {
paddrp->spp_flags |= SPP_HB_DISABLE;
} else {
paddrp->spp_flags |= SPP_HB_ENABLE;
}
/* get flags for PMTU */
if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
paddrp->spp_flags |= SPP_PMTUD_DISABLE;
} else {
paddrp->spp_flags |= SPP_PMTUD_ENABLE;
}
if (net->dscp & 0x01) {
paddrp->spp_dscp = net->dscp & 0xfc;
paddrp->spp_flags |= SPP_DSCP;
}
#ifdef INET6
if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
(net->flowlabel & 0x80000000)) {
paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
}
#endif
} else {
/*
* No destination so return default
* value
*/
paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
paddrp->spp_pathmtu = stcb->asoc.default_mtu;
if (stcb->asoc.default_dscp & 0x01) {
paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
paddrp->spp_flags |= SPP_DSCP;
}
#ifdef INET6
if (stcb->asoc.default_flowlabel & 0x80000000) {
paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
}
#endif
/* default settings should be these */
if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
paddrp->spp_flags |= SPP_HB_DISABLE;
} else {
paddrp->spp_flags |= SPP_HB_ENABLE;
}
if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
paddrp->spp_flags |= SPP_PMTUD_DISABLE;
} else {
paddrp->spp_flags |= SPP_PMTUD_ENABLE;
}
paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
}
paddrp->spp_assoc_id = sctp_get_associd(stcb);
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC))) {
/* Use endpoint defaults */
SCTP_INP_RLOCK(inp);
paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
paddrp->spp_hbinterval = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
/* get inp's default */
if (inp->sctp_ep.default_dscp & 0x01) {
paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
paddrp->spp_flags |= SPP_DSCP;
}
#ifdef INET6
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
(inp->sctp_ep.default_flowlabel & 0x80000000)) {
paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
}
#endif
paddrp->spp_pathmtu = inp->sctp_ep.default_mtu;
if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
paddrp->spp_flags |= SPP_HB_ENABLE;
} else {
paddrp->spp_flags |= SPP_HB_DISABLE;
}
if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
paddrp->spp_flags |= SPP_PMTUD_ENABLE;
} else {
paddrp->spp_flags |= SPP_PMTUD_DISABLE;
}
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_paddrparams);
}
break;
}
case SCTP_GET_PEER_ADDR_INFO:
{
struct sctp_paddrinfo *paddri;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
#if defined(INET) && defined(INET6)
if (paddri->spinfo_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&paddri->spinfo_address;
}
} else {
addr = (struct sockaddr *)&paddri->spinfo_address;
}
#else
addr = (struct sockaddr *)&paddri->spinfo_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net != NULL)) {
if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
/* It's unconfirmed */
paddri->spinfo_state = SCTP_UNCONFIRMED;
} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
/* It's active */
paddri->spinfo_state = SCTP_ACTIVE;
} else {
/* It's inactive */
paddri->spinfo_state = SCTP_INACTIVE;
}
paddri->spinfo_cwnd = net->cwnd;
paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
paddri->spinfo_rto = net->RTO;
paddri->spinfo_assoc_id = sctp_get_associd(stcb);
paddri->spinfo_mtu = net->mtu;
switch (addr->sa_family) {
#if defined(INET)
case AF_INET:
paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
break;
#endif
#if defined(INET6)
case AF_INET6:
paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
paddri->spinfo_mtu -= sizeof(struct sctphdr);
break;
#endif
default:
break;
}
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_paddrinfo);
} else {
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
}
break;
}
case SCTP_PCB_STATUS:
{
struct sctp_pcbinfo *spcb;
SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
sctp_fill_pcbinfo(spcb);
*optsize = sizeof(struct sctp_pcbinfo);
break;
}
case SCTP_STATUS:
{
struct sctp_nets *net;
struct sctp_status *sstat;
SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
if (stcb == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
sstat->sstat_assoc_id = sctp_get_associd(stcb);
sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
/*
* We can't include chunks that have been passed to
* the socket layer. Only things in queue.
*/
sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
stcb->asoc.cnt_on_all_streams);
sstat->sstat_instrms = stcb->asoc.streamincnt;
sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
net = stcb->asoc.primary_destination;
if (net != NULL) {
#ifdef HAVE_SA_LEN
memcpy(&sstat->sstat_primary.spinfo_address,
&net->ro._l_addr,
((struct sockaddr *)(&net->ro._l_addr))->sa_len);
#else
switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
#if defined(INET)
case AF_INET:
memcpy(&sstat->sstat_primary.spinfo_address,
&net->ro._l_addr,
sizeof(struct sockaddr_in));
break;
#endif
#if defined(INET6)
case AF_INET6:
memcpy(&sstat->sstat_primary.spinfo_address,
&net->ro._l_addr,
sizeof(struct sockaddr_in6));
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
memcpy(&sstat->sstat_primary.spinfo_address,
&net->ro._l_addr,
sizeof(struct sockaddr_conn));
break;
#endif
default:
break;
}
#endif
((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
/*
* Again the user can get info from sctp_constants.h
* for what the state of the network is.
*/
if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
/* It's unconfirmed */
sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
/* It's active */
sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
} else {
/* It's inactive */
sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
}
sstat->sstat_primary.spinfo_cwnd = net->cwnd;
sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
sstat->sstat_primary.spinfo_rto = net->RTO;
sstat->sstat_primary.spinfo_mtu = net->mtu;
switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
#if defined(INET)
case AF_INET:
sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
break;
#endif
#if defined(INET6)
case AF_INET6:
sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
sstat->sstat_primary.spinfo_mtu -= sizeof(struct sctphdr);
break;
#endif
default:
break;
}
} else {
memset(&sstat->sstat_primary, 0, sizeof(struct sctp_paddrinfo));
}
sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_status);
break;
}
case SCTP_RTOINFO:
{
struct sctp_rtoinfo *srto;
SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
if (stcb) {
srto->srto_initial = stcb->asoc.initial_rto;
srto->srto_max = stcb->asoc.maxrto;
srto->srto_min = stcb->asoc.minrto;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(srto->srto_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
srto->srto_initial = inp->sctp_ep.initial_rto;
srto->srto_max = inp->sctp_ep.sctp_maxrto;
srto->srto_min = inp->sctp_ep.sctp_minrto;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_rtoinfo);
}
break;
}
case SCTP_TIMEOUTS:
{
struct sctp_timeouts *stimo;
SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
if (stcb) {
stimo->stimo_init= stcb->asoc.timoinit;
stimo->stimo_data= stcb->asoc.timodata;
stimo->stimo_sack= stcb->asoc.timosack;
stimo->stimo_shutdown= stcb->asoc.timoshutdown;
stimo->stimo_heartbeat= stcb->asoc.timoheartbeat;
stimo->stimo_cookie= stcb->asoc.timocookie;
stimo->stimo_shutdownack= stcb->asoc.timoshutdownack;
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_timeouts);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
}
case SCTP_ASSOCINFO:
{
struct sctp_assocparams *sasoc;
SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
if (stcb) {
sasoc->sasoc_cookie_life = sctp_ticks_to_msecs(stcb->asoc.cookie_life);
sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
sasoc->sasoc_cookie_life = sctp_ticks_to_msecs(inp->sctp_ep.def_cookie_life);
sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
sasoc->sasoc_number_peer_destinations = 0;
sasoc->sasoc_peer_rwnd = 0;
sasoc->sasoc_local_rwnd = (uint32_t)sbspace(&inp->sctp_socket->so_rcv);
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assocparams);
}
break;
}
case SCTP_DEFAULT_SEND_PARAM:
{
struct sctp_sndrcvinfo *s_info;
SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
if (stcb) {
memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_sndrcvinfo);
}
break;
}
case SCTP_INITMSG:
{
struct sctp_initmsg *sinit;
SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
SCTP_INP_RLOCK(inp);
sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(struct sctp_initmsg);
break;
}
case SCTP_PRIMARY_ADDR:
/* we allow a "get" operation on this */
{
struct sctp_setprim *ssp;
SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
if (stcb) {
union sctp_sockstore *addr;
addr = &stcb->asoc.primary_destination->ro._l_addr;
switch (addr->sa.sa_family) {
#ifdef INET
case AF_INET:
#ifdef INET6
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
in6_sin_2_v4mapsin6(&addr->sin,
(struct sockaddr_in6 *)&ssp->ssp_addr);
} else {
memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
}
#else
memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
memcpy(&ssp->ssp_addr, &addr->sconn, sizeof(struct sockaddr_conn));
break;
#endif
default:
break;
}
SCTP_TCB_UNLOCK(stcb);
*optsize = sizeof(struct sctp_setprim);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
}
case SCTP_HMAC_IDENT:
{
struct sctp_hmacalgo *shmac;
sctp_hmaclist_t *hmaclist;
size_t size;
int i;
SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
SCTP_INP_RLOCK(inp);
hmaclist = inp->sctp_ep.local_hmacs;
if (hmaclist == NULL) {
/* no HMACs to return */
*optsize = sizeof(*shmac);
SCTP_INP_RUNLOCK(inp);
break;
}
/* is there room for all of the hmac ids? */
size = sizeof(*shmac) + (hmaclist->num_algo *
sizeof(shmac->shmac_idents[0]));
if (*optsize < size) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_INP_RUNLOCK(inp);
break;
}
/* copy in the list */
shmac->shmac_number_of_idents = hmaclist->num_algo;
for (i = 0; i < hmaclist->num_algo; i++) {
shmac->shmac_idents[i] = hmaclist->hmac[i];
}
SCTP_INP_RUNLOCK(inp);
*optsize = size;
break;
}
case SCTP_AUTH_ACTIVE_KEY:
{
struct sctp_authkeyid *scact;
SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
if (stcb) {
/* get the active key on the assoc */
scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(scact->scact_assoc_id == SCTP_FUTURE_ASSOC))) {
/* get the endpoint active key */
SCTP_INP_RLOCK(inp);
scact->scact_keynumber = inp->sctp_ep.default_keyid;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_authkeyid);
}
break;
}
case SCTP_LOCAL_AUTH_CHUNKS:
{
struct sctp_authchunks *sac;
sctp_auth_chklist_t *chklist = NULL;
size_t size = 0;
SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
if (stcb) {
/* get off the assoc */
chklist = stcb->asoc.local_auth_chunks;
/* is there enough space? */
size = sctp_auth_get_chklist_size(chklist);
if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
/* copy in the chunks */
(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
sac->gauth_number_of_chunks = (uint32_t)size;
*optsize = sizeof(struct sctp_authchunks) + size;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(sac->gauth_assoc_id == SCTP_FUTURE_ASSOC))) {
/* get off the endpoint */
SCTP_INP_RLOCK(inp);
chklist = inp->sctp_ep.local_auth_chunks;
/* is there enough space? */
size = sctp_auth_get_chklist_size(chklist);
if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
/* copy in the chunks */
(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
sac->gauth_number_of_chunks = (uint32_t)size;
*optsize = sizeof(struct sctp_authchunks) + size;
}
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_PEER_AUTH_CHUNKS:
{
struct sctp_authchunks *sac;
sctp_auth_chklist_t *chklist = NULL;
size_t size = 0;
SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
if (stcb) {
/* get off the assoc */
chklist = stcb->asoc.peer_auth_chunks;
/* is there enough space? */
size = sctp_auth_get_chklist_size(chklist);
if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
} else {
/* copy in the chunks */
(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
sac->gauth_number_of_chunks = (uint32_t)size;
*optsize = sizeof(struct sctp_authchunks) + size;
}
SCTP_TCB_UNLOCK(stcb);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
}
break;
}
#if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
case SCTP_PEELOFF:
{
struct sctp_peeloff_opt *peeloff;
SCTP_CHECK_AND_CAST(peeloff, optval, struct sctp_peeloff_opt, *optsize);
/* do the peeloff */
error = sctp_peeloff_option(p, peeloff);
if (error == 0) {
*optsize = sizeof(struct sctp_peeloff_opt);
}
}
break;
#endif /* HAVE_SCTP_PEELOFF_SOCKOPT */
case SCTP_EVENT:
{
struct sctp_event *event;
uint32_t event_type;
SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
switch (event->se_type) {
case SCTP_ASSOC_CHANGE:
event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
break;
case SCTP_PEER_ADDR_CHANGE:
event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
break;
case SCTP_REMOTE_ERROR:
event_type = SCTP_PCB_FLAGS_RECVPEERERR;
break;
case SCTP_SEND_FAILED:
event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
break;
case SCTP_SHUTDOWN_EVENT:
event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
break;
case SCTP_ADAPTATION_INDICATION:
event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
break;
case SCTP_PARTIAL_DELIVERY_EVENT:
event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
break;
case SCTP_AUTHENTICATION_EVENT:
event_type = SCTP_PCB_FLAGS_AUTHEVNT;
break;
case SCTP_STREAM_RESET_EVENT:
event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
break;
case SCTP_SENDER_DRY_EVENT:
event_type = SCTP_PCB_FLAGS_DRYEVNT;
break;
case SCTP_NOTIFICATIONS_STOPPED_EVENT:
event_type = 0;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
error = ENOTSUP;
break;
case SCTP_ASSOC_RESET_EVENT:
event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
break;
case SCTP_STREAM_CHANGE_EVENT:
event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
break;
case SCTP_SEND_FAILED_EVENT:
event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
break;
default:
event_type = 0;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if (event_type > 0) {
if (stcb) {
event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(event->se_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
event->se_on = sctp_is_feature_on(inp, event_type);
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
}
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
if (error == 0) {
*optsize = sizeof(struct sctp_event);
}
break;
}
case SCTP_RECVRCVINFO:
if (*optsize < sizeof(int)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
SCTP_INP_RLOCK(inp);
*(int *)optval = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(int);
}
break;
case SCTP_RECVNXTINFO:
if (*optsize < sizeof(int)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
SCTP_INP_RLOCK(inp);
*(int *)optval = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
SCTP_INP_RUNLOCK(inp);
*optsize = sizeof(int);
}
break;
case SCTP_DEFAULT_SNDINFO:
{
struct sctp_sndinfo *info;
SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
if (stcb) {
info->snd_sid = stcb->asoc.def_send.sinfo_stream;
info->snd_flags = stcb->asoc.def_send.sinfo_flags;
info->snd_flags &= 0xfff0;
info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
info->snd_context = stcb->asoc.def_send.sinfo_context;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(info->snd_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
info->snd_sid = inp->def_send.sinfo_stream;
info->snd_flags = inp->def_send.sinfo_flags;
info->snd_flags &= 0xfff0;
info->snd_ppid = inp->def_send.sinfo_ppid;
info->snd_context = inp->def_send.sinfo_context;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_sndinfo);
}
break;
}
case SCTP_DEFAULT_PRINFO:
{
struct sctp_default_prinfo *info;
SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
if (stcb) {
info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(info->pr_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
info->pr_value = inp->def_send.sinfo_timetolive;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_default_prinfo);
}
break;
}
case SCTP_PEER_ADDR_THLDS:
{
struct sctp_paddrthlds *thlds;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
#if defined(INET) && defined(INET6)
if (thlds->spt_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&thlds->spt_address;
}
} else {
addr = (struct sockaddr *)&thlds->spt_address;
}
#else
addr = (struct sockaddr *)&thlds->spt_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
if (stcb != NULL) {
if (net != NULL) {
thlds->spt_pathmaxrxt = net->failure_threshold;
thlds->spt_pathpfthld = net->pf_threshold;
thlds->spt_pathcpthld = 0xffff;
} else {
thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
thlds->spt_pathcpthld = 0xffff;
}
thlds->spt_assoc_id = sctp_get_associd(stcb);
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(thlds->spt_assoc_id == SCTP_FUTURE_ASSOC))) {
/* Use endpoint defaults */
SCTP_INP_RLOCK(inp);
thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
thlds->spt_pathcpthld = 0xffff;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_paddrthlds);
}
break;
}
case SCTP_REMOTE_UDP_ENCAPS_PORT:
{
struct sctp_udpencaps *encaps;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
#if defined(INET) && defined(INET6)
if (encaps->sue_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&encaps->sue_address;
}
} else {
addr = (struct sockaddr *)&encaps->sue_address;
}
#else
addr = (struct sockaddr *)&encaps->sue_address;
#endif
if (stcb) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
if (stcb != NULL) {
if (net) {
encaps->sue_port = net->port;
} else {
encaps->sue_port = stcb->asoc.port;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(encaps->sue_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
encaps->sue_port = inp->sctp_ep.port;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_udpencaps);
}
break;
}
case SCTP_ECN_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.ecn_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->ecn_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_PR_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.prsctp_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->prsctp_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_AUTH_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.auth_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->auth_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_ASCONF_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.asconf_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->asconf_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_RECONFIG_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.reconfig_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->reconfig_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_NRSACK_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.nrsack_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->nrsack_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_PKTDROP_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.pktdrop_supported;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->pktdrop_supported;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_ENABLE_STREAM_RESET:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = (uint32_t)stcb->asoc.local_strreset_support;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = (uint32_t)inp->local_strreset_support;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
case SCTP_PR_STREAM_STATUS:
{
struct sctp_prstatus *sprstat;
uint16_t sid;
uint16_t policy;
SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
sid = sprstat->sprstat_sid;
policy = sprstat->sprstat_policy;
#if defined(SCTP_DETAILED_STR_STATS)
if ((stcb != NULL) &&
(sid < stcb->asoc.streamoutcnt) &&
(policy != SCTP_PR_SCTP_NONE) &&
((policy <= SCTP_PR_SCTP_MAX) ||
(policy == SCTP_PR_SCTP_ALL))) {
if (policy == SCTP_PR_SCTP_ALL) {
sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
} else {
sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
}
#else
if ((stcb != NULL) &&
(sid < stcb->asoc.streamoutcnt) &&
(policy == SCTP_PR_SCTP_ALL)) {
sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
#endif
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
if (error == 0) {
*optsize = sizeof(struct sctp_prstatus);
}
break;
}
case SCTP_PR_ASSOC_STATUS:
{
struct sctp_prstatus *sprstat;
uint16_t policy;
SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
policy = sprstat->sprstat_policy;
if ((stcb != NULL) &&
(policy != SCTP_PR_SCTP_NONE) &&
((policy <= SCTP_PR_SCTP_MAX) ||
(policy == SCTP_PR_SCTP_ALL))) {
if (policy == SCTP_PR_SCTP_ALL) {
sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
} else {
sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
if (error == 0) {
*optsize = sizeof(struct sctp_prstatus);
}
break;
}
case SCTP_MAX_CWND:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
av->assoc_value = stcb->asoc.max_cwnd;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_RLOCK(inp);
av->assoc_value = inp->max_cwnd;
SCTP_INP_RUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
if (error == 0) {
*optsize = sizeof(struct sctp_assoc_value);
}
break;
}
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
error = ENOPROTOOPT;
break;
} /* end switch (sopt->sopt_name) */
if (error) {
*optsize = 0;
}
return (error);
}
#if defined(__Userspace__)
int
#else
static int
#endif
sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
void *p)
{
int error, set_opt;
uint32_t *mopt;
struct sctp_tcb *stcb = NULL;
struct sctp_inpcb *inp = NULL;
uint32_t vrf_id;
if (optval == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
vrf_id = inp->def_vrf_id;
error = 0;
switch (optname) {
case SCTP_NODELAY:
case SCTP_AUTOCLOSE:
case SCTP_AUTO_ASCONF:
case SCTP_EXPLICIT_EOR:
case SCTP_DISABLE_FRAGMENTS:
case SCTP_USE_EXT_RCVINFO:
case SCTP_I_WANT_MAPPED_V4_ADDR:
/* copy in the option value */
SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
set_opt = 0;
if (error)
break;
switch (optname) {
case SCTP_DISABLE_FRAGMENTS:
set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
break;
case SCTP_AUTO_ASCONF:
/*
* NOTE: we don't really support this flag
*/
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
/* only valid for bound all sockets */
if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
(*mopt != 0)) {
/* forbidden by admin */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
return (EPERM);
}
set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
break;
case SCTP_EXPLICIT_EOR:
set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
break;
case SCTP_USE_EXT_RCVINFO:
set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
break;
case SCTP_I_WANT_MAPPED_V4_ADDR:
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
break;
case SCTP_NODELAY:
set_opt = SCTP_PCB_FLAGS_NODELAY;
break;
case SCTP_AUTOCLOSE:
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
/*
* The value is in ticks. Note this does not effect
* old associations, only new ones.
*/
inp->sctp_ep.auto_close_time = sctp_secs_to_ticks(*mopt);
break;
}
SCTP_INP_WLOCK(inp);
if (*mopt != 0) {
sctp_feature_on(inp, set_opt);
} else {
sctp_feature_off(inp, set_opt);
}
SCTP_INP_WUNLOCK(inp);
break;
case SCTP_REUSE_PORT:
{
SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
/* Can't set it after we are bound */
error = EINVAL;
break;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
/* Can't do this for a 1-m socket */
error = EINVAL;
break;
}
if (optval)
sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
else
sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
break;
}
case SCTP_PARTIAL_DELIVERY_POINT:
{
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
if (*value > SCTP_SB_LIMIT_RCV(so)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
inp->partial_delivery_point = *value;
break;
}
case SCTP_FRAGMENT_INTERLEAVE:
/* not yet until we re-write sctp_recvmsg() */
{
uint32_t *level;
SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
if (*level == SCTP_FRAG_LEVEL_2) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
} else if (*level == SCTP_FRAG_LEVEL_1) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
} else if (*level == SCTP_FRAG_LEVEL_0) {
sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
}
case SCTP_INTERLEAVING_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->idata_supported = 0;
} else {
if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
(sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
inp->idata_supported = 1;
} else {
/* Must have Frag interleave and stream interleave on */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_CMT_ON_OFF:
if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
if (av->assoc_value > SCTP_CMT_MAX) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.sctp_cmt_on_off = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->sctp_cmt_on_off = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.sctp_cmt_on_off = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
error = ENOPROTOOPT;
}
break;
case SCTP_PLUGGABLE_CC:
{
struct sctp_assoc_value *av;
struct sctp_nets *net;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
if ((av->assoc_value != SCTP_CC_RFC2581) &&
(av->assoc_value != SCTP_CC_HSTCP) &&
(av->assoc_value != SCTP_CC_HTCP) &&
(av->assoc_value != SCTP_CC_RTCC)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
stcb->asoc.congestion_control_module = av->assoc_value;
if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
}
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
stcb->asoc.congestion_control_module = av->assoc_value;
if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
}
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_CC_OPTION:
{
struct sctp_cc_option *cc_opt;
SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
if (stcb == NULL) {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC)) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
(*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 1, cc_opt);
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
} else {
error = EINVAL;
}
} else {
if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
error = ENOTSUP;
} else {
error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 1,
cc_opt);
}
SCTP_TCB_UNLOCK(stcb);
}
break;
}
case SCTP_PLUGGABLE_SS:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
if ((av->assoc_value != SCTP_SS_DEFAULT) &&
(av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
(av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
(av->assoc_value != SCTP_SS_PRIORITY) &&
(av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
(av->assoc_value != SCTP_SS_FIRST_COME)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_TCB_SEND_LOCK(stcb);
stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, true);
stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
stcb->asoc.stream_scheduling_module = av->assoc_value;
stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
SCTP_TCB_SEND_UNLOCK(stcb);
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
SCTP_TCB_SEND_LOCK(stcb);
stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, true);
stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
stcb->asoc.stream_scheduling_module = av->assoc_value;
stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
SCTP_TCB_SEND_UNLOCK(stcb);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_SS_VALUE:
{
struct sctp_stream_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
(stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
av->stream_value) < 0)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_CURRENT_ASSOC)) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (av->stream_id < stcb->asoc.streamoutcnt) {
stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
&stcb->asoc,
&stcb->asoc.strmout[av->stream_id],
av->stream_value);
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
} else {
/* Can't set stream value without association */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_CLR_STAT_LOG:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
break;
case SCTP_CONTEXT:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.context = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->sctp_context = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.context = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_VRF_ID:
{
uint32_t *default_vrfid;
#ifdef SCTP_MVRF
int i;
#endif
SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
if (*default_vrfid > SCTP_MAX_VRF_ID) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#ifdef SCTP_MVRF
for (i = 0; i < inp->num_vrfs; i++) {
/* The VRF must be in the VRF list */
if (*default_vrfid == inp->m_vrf_ids[i]) {
SCTP_INP_WLOCK(inp);
inp->def_vrf_id = *default_vrfid;
SCTP_INP_WUNLOCK(inp);
goto sctp_done;
}
}
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
#else
inp->def_vrf_id = *default_vrfid;
#endif
#ifdef SCTP_MVRF
sctp_done:
#endif
break;
}
case SCTP_DEL_VRF_ID:
{
#ifdef SCTP_MVRF
uint32_t *del_vrfid;
int i, fnd = 0;
SCTP_CHECK_AND_CAST(del_vrfid, optval, uint32_t, optsize);
if (*del_vrfid > SCTP_MAX_VRF_ID) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if (inp->num_vrfs == 1) {
/* Can't delete last one */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
/* Can't add more once you are bound */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_INP_WLOCK(inp);
for (i = 0; i < inp->num_vrfs; i++) {
if (*del_vrfid == inp->m_vrf_ids[i]) {
fnd = 1;
break;
}
}
if (!fnd) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if (i != (inp->num_vrfs - 1)) {
/* Take bottom one and move to this slot */
inp->m_vrf_ids[i] = inp->m_vrf_ids[(inp->num_vrfs-1)];
}
if (*del_vrfid == inp->def_vrf_id) {
/* Take the first one as the new default */
inp->def_vrf_id = inp->m_vrf_ids[0];
}
/* Drop the number by one killing last one */
inp->num_vrfs--;
#else
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
#endif
break;
}
case SCTP_ADD_VRF_ID:
{
#ifdef SCTP_MVRF
uint32_t *add_vrfid;
int i;
SCTP_CHECK_AND_CAST(add_vrfid, optval, uint32_t, optsize);
if (*add_vrfid > SCTP_MAX_VRF_ID) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
/* Can't add more once you are bound */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_INP_WLOCK(inp);
/* Verify its not already here */
for (i = 0; i < inp->num_vrfs; i++) {
if (*add_vrfid == inp->m_vrf_ids[i]) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
SCTP_INP_WUNLOCK(inp);
break;
}
}
if ((inp->num_vrfs + 1) > inp->vrf_size) {
/* need to grow array */
uint32_t *tarray;
SCTP_MALLOC(tarray, uint32_t *,
(sizeof(uint32_t) * (inp->vrf_size + SCTP_DEFAULT_VRF_SIZE)),
SCTP_M_MVRF);
if (tarray == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_INP_WUNLOCK(inp);
break;
}
memcpy(tarray, inp->m_vrf_ids, (sizeof(uint32_t) * inp->vrf_size));
SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
inp->m_vrf_ids = tarray;
inp->vrf_size += SCTP_DEFAULT_VRF_SIZE;
}
inp->m_vrf_ids[inp->num_vrfs] = *add_vrfid;
inp->num_vrfs++;
SCTP_INP_WUNLOCK(inp);
#else
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
#endif
break;
}
case SCTP_DELAYED_SACK:
{
struct sctp_sack_info *sack;
SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
if (sack->sack_delay) {
if (sack->sack_delay > SCTP_MAX_SACK_DELAY) {
error = EINVAL;
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
break;
}
}
if (stcb) {
if (sack->sack_delay) {
stcb->asoc.delayed_ack = sack->sack_delay;
}
if (sack->sack_freq) {
stcb->asoc.sack_freq = sack->sack_freq;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
(sack->sack_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
if (sack->sack_delay) {
inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(sack->sack_delay);
}
if (sack->sack_freq) {
inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
}
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
(sack->sack_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (sack->sack_delay) {
stcb->asoc.delayed_ack = sack->sack_delay;
}
if (sack->sack_freq) {
stcb->asoc.sack_freq = sack->sack_freq;
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_AUTH_CHUNK:
{
struct sctp_authchunk *sauth;
SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
SCTP_INP_WLOCK(inp);
if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
inp->auth_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
break;
}
case SCTP_AUTH_KEY:
{
struct sctp_authkey *sca;
struct sctp_keyhead *shared_keys;
sctp_sharedkey_t *shared_key;
sctp_key_t *key = NULL;
size_t size;
SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
if (sca->sca_keylength == 0) {
size = optsize - sizeof(struct sctp_authkey);
} else {
if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
size = sca->sca_keylength;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
}
SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
if (stcb) {
shared_keys = &stcb->asoc.shared_keys;
/* clear the cached keys for this key id */
sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
/*
* create the new shared key and
* insert/replace it
*/
if (size > 0) {
key = sctp_set_key(sca->sca_key, (uint32_t) size);
if (key == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_TCB_UNLOCK(stcb);
break;
}
}
shared_key = sctp_alloc_sharedkey();
if (shared_key == NULL) {
sctp_free_key(key);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_TCB_UNLOCK(stcb);
break;
}
shared_key->key = key;
shared_key->keyid = sca->sca_keynumber;
error = sctp_insert_sharedkey(shared_keys, shared_key);
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
(sca->sca_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
shared_keys = &inp->sctp_ep.shared_keys;
/*
* clear the cached keys on all assocs for
* this key id
*/
sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
/*
* create the new shared key and
* insert/replace it
*/
if (size > 0) {
key = sctp_set_key(sca->sca_key, (uint32_t) size);
if (key == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_INP_WUNLOCK(inp);
break;
}
}
shared_key = sctp_alloc_sharedkey();
if (shared_key == NULL) {
sctp_free_key(key);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_INP_WUNLOCK(inp);
break;
}
shared_key->key = key;
shared_key->keyid = sca->sca_keynumber;
error = sctp_insert_sharedkey(shared_keys, shared_key);
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
(sca->sca_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
shared_keys = &stcb->asoc.shared_keys;
/* clear the cached keys for this key id */
sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
/*
* create the new shared key and
* insert/replace it
*/
if (size > 0) {
key = sctp_set_key(sca->sca_key, (uint32_t) size);
if (key == NULL) {
SCTP_TCB_UNLOCK(stcb);
continue;
}
}
shared_key = sctp_alloc_sharedkey();
if (shared_key == NULL) {
sctp_free_key(key);
SCTP_TCB_UNLOCK(stcb);
continue;
}
shared_key->key = key;
shared_key->keyid = sca->sca_keynumber;
error = sctp_insert_sharedkey(shared_keys, shared_key);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_HMAC_IDENT:
{
struct sctp_hmacalgo *shmac;
sctp_hmaclist_t *hmaclist;
uint16_t hmacid;
uint32_t i;
SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
(shmac->shmac_number_of_idents > 0xffff)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
hmaclist = sctp_alloc_hmaclist((uint16_t)shmac->shmac_number_of_idents);
if (hmaclist == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
break;
}
for (i = 0; i < shmac->shmac_number_of_idents; i++) {
hmacid = shmac->shmac_idents[i];
if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
/* invalid HMACs were found */;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
sctp_free_hmaclist(hmaclist);
goto sctp_set_hmac_done;
}
}
for (i = 0; i < hmaclist->num_algo; i++) {
if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
/* already in list */
break;
}
}
if (i == hmaclist->num_algo) {
/* not found in list */
sctp_free_hmaclist(hmaclist);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
/* set it on the endpoint */
SCTP_INP_WLOCK(inp);
if (inp->sctp_ep.local_hmacs)
sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
inp->sctp_ep.local_hmacs = hmaclist;
SCTP_INP_WUNLOCK(inp);
sctp_set_hmac_done:
break;
}
case SCTP_AUTH_ACTIVE_KEY:
{
struct sctp_authkeyid *scact;
SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
/* set the active key on the right place */
if (stcb) {
/* set the active key on the assoc */
if (sctp_auth_setactivekey(stcb,
scact->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
SCTP_FROM_SCTP_USRREQ,
EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
(scact->scact_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
(scact->scact_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_auth_setactivekey(stcb, scact->scact_keynumber);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_AUTH_DELETE_KEY:
{
struct sctp_authkeyid *scdel;
SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
/* delete the key from the right place */
if (stcb) {
if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
(scdel->scact_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
(scdel->scact_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_AUTH_DEACTIVATE_KEY:
{
struct sctp_authkeyid *keyid;
SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
/* deactivate the key from the right place */
if (stcb) {
if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
(keyid->scact_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
(keyid->scact_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_ENABLE_STREAM_RESET:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->local_strreset_support = (uint8_t)av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_RESET_STREAMS:
{
struct sctp_reset_streams *strrst;
int i, send_out = 0;
int send_in = 0;
SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
if (stcb == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
break;
}
if (stcb->asoc.reconfig_supported == 0) {
/*
* Peer does not support the chunk type.
*/
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (sizeof(struct sctp_reset_streams) +
strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
send_in = 1;
if (stcb->asoc.stream_reset_outstanding) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
SCTP_TCB_UNLOCK(stcb);
break;
}
}
if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
send_out = 1;
}
if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
error = ENOMEM;
SCTP_TCB_UNLOCK(stcb);
break;
}
if ((send_in == 0) && (send_out == 0)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
break;
}
for (i = 0; i < strrst->srs_number_streams; i++) {
if ((send_in) &&
(strrst->srs_stream_list[i] >= stcb->asoc.streamincnt)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if ((send_out) &&
(strrst->srs_stream_list[i] >= stcb->asoc.streamoutcnt)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
}
if (error) {
SCTP_TCB_UNLOCK(stcb);
break;
}
if (send_out) {
int cnt;
uint16_t strm;
if (strrst->srs_number_streams) {
for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
strm = strrst->srs_stream_list[i];
if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
cnt++;
}
}
} else {
/* Its all */
for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
cnt++;
}
}
}
}
if (send_in) {
error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
strrst->srs_stream_list,
send_in, 0, 0, 0, 0, 0);
} else {
error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
}
if (error == 0) {
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
} else {
/*
* For outgoing streams don't report any problems in
* sending the request to the application.
* XXX: Double check resetting incoming streams.
*/
error = 0;
}
SCTP_TCB_UNLOCK(stcb);
break;
}
case SCTP_ADD_STREAMS:
{
struct sctp_add_streams *stradd;
uint8_t addstream = 0;
uint16_t add_o_strmcnt = 0;
uint16_t add_i_strmcnt = 0;
SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
if (stcb == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
break;
}
if (stcb->asoc.reconfig_supported == 0) {
/*
* Peer does not support the chunk type.
*/
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (stcb->asoc.stream_reset_outstanding) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
SCTP_TCB_UNLOCK(stcb);
break;
}
if ((stradd->sas_outstrms == 0) &&
(stradd->sas_instrms == 0)) {
error = EINVAL;
goto skip_stuff;
}
if (stradd->sas_outstrms) {
addstream = 1;
/* We allocate here */
add_o_strmcnt = stradd->sas_outstrms;
if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
/* You can't have more than 64k */
error = EINVAL;
goto skip_stuff;
}
}
if (stradd->sas_instrms) {
int cnt;
addstream |= 2;
/* We allocate inside sctp_send_str_reset_req() */
add_i_strmcnt = stradd->sas_instrms;
cnt = add_i_strmcnt;
cnt += stcb->asoc.streamincnt;
if (cnt > 0x0000ffff) {
/* You can't have more than 64k */
error = EINVAL;
goto skip_stuff;
}
if (cnt > (int)stcb->asoc.max_inbound_streams) {
/* More than you are allowed */
error = EINVAL;
goto skip_stuff;
}
}
error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
skip_stuff:
SCTP_TCB_UNLOCK(stcb);
break;
}
case SCTP_RESET_ASSOC:
{
int i;
uint32_t *value;
SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) *value);
if (stcb == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
break;
}
if (stcb->asoc.reconfig_supported == 0) {
/*
* Peer does not support the chunk type.
*/
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
error = EOPNOTSUPP;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
break;
}
if (stcb->asoc.stream_reset_outstanding) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
SCTP_TCB_UNLOCK(stcb);
break;
}
/* Is there any data pending in the send or sent queues? */
if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
busy_out:
error = EBUSY;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
SCTP_TCB_UNLOCK(stcb);
break;
}
/* Do any streams have data queued? */
for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
goto busy_out;
}
}
error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
SCTP_TCB_UNLOCK(stcb);
break;
}
case SCTP_CONNECT_X:
if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
break;
case SCTP_CONNECT_X_DELAYED:
if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
break;
case SCTP_CONNECT_X_COMPLETE:
{
struct sockaddr *sa;
/* FIXME MT: check correct? */
SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
/* find tcb */
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb) {
SCTP_TCB_LOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if (stcb == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
error = ENOENT;
break;
}
if (stcb->asoc.delayed_connection == 1) {
stcb->asoc.delayed_connection = 0;
(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
stcb->asoc.primary_destination,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
} else {
/*
* already expired or did not use delayed
* connectx
*/
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
}
SCTP_TCB_UNLOCK(stcb);
break;
}
case SCTP_MAX_BURST:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.max_burst = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_FUTURE_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->sctp_ep.max_burst = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((av->assoc_id == SCTP_CURRENT_ASSOC) ||
(av->assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.max_burst = av->assoc_value;
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_MAXSEG:
{
struct sctp_assoc_value *av;
int ovh;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
ovh = SCTP_MED_OVERHEAD;
} else {
ovh = SCTP_MED_V4_OVERHEAD;
}
if (stcb) {
if (av->assoc_value) {
stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
} else {
stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
/* FIXME MT: I think this is not in tune with the API ID */
if (av->assoc_value) {
inp->sctp_frag_point = (av->assoc_value + ovh);
} else {
inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_EVENTS:
{
struct sctp_event_subscribe *events;
SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
SCTP_INP_WLOCK(inp);
if (events->sctp_data_io_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
}
if (events->sctp_association_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
}
if (events->sctp_address_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
}
if (events->sctp_send_failure_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
}
if (events->sctp_peer_error_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
}
if (events->sctp_shutdown_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
}
if (events->sctp_partial_delivery_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
}
if (events->sctp_adaptation_layer_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
}
if (events->sctp_authentication_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
}
if (events->sctp_sender_dry_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
}
if (events->sctp_stream_reset_event) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
}
SCTP_INP_WUNLOCK(inp);
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (events->sctp_association_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
}
if (events->sctp_address_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
}
if (events->sctp_send_failure_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
}
if (events->sctp_peer_error_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
}
if (events->sctp_shutdown_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
}
if (events->sctp_partial_delivery_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
}
if (events->sctp_adaptation_layer_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
}
if (events->sctp_authentication_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
}
if (events->sctp_sender_dry_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
}
if (events->sctp_stream_reset_event) {
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
} else {
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
}
SCTP_TCB_UNLOCK(stcb);
}
/* Send up the sender dry event only for 1-to-1 style sockets. */
if (events->sctp_sender_dry_event) {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb) {
SCTP_TCB_LOCK(stcb);
if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
(stcb->asoc.stream_queue_cnt == 0)) {
sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
}
SCTP_TCB_UNLOCK(stcb);
}
}
}
SCTP_INP_RUNLOCK(inp);
break;
}
case SCTP_ADAPTATION_LAYER:
{
struct sctp_setadaptation *adap_bits;
SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
SCTP_INP_WLOCK(inp);
inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
inp->sctp_ep.adaptation_layer_indicator_provided = 1;
SCTP_INP_WUNLOCK(inp);
break;
}
#ifdef SCTP_DEBUG
case SCTP_SET_INITIAL_DBG_SEQ:
{
uint32_t *vvv;
SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
SCTP_INP_WLOCK(inp);
inp->sctp_ep.initial_sequence_debug = *vvv;
SCTP_INP_WUNLOCK(inp);
break;
}
#endif
case SCTP_DEFAULT_SEND_PARAM:
{
struct sctp_sndrcvinfo *s_info;
SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
if (stcb) {
if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
(s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
(s_info->sinfo_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_PEER_ADDR_PARAMS:
{
struct sctp_paddrparams *paddrp;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
#if defined(INET) && defined(INET6)
if (paddrp->spp_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&paddrp->spp_address;
}
} else {
addr = (struct sockaddr *)&paddrp->spp_address;
}
#else
addr = (struct sockaddr *)&paddrp->spp_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr,
&net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
/* sanity checks */
if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
if (stcb)
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
if (stcb)
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) &&
(paddrp->spp_pathmtu > 0) &&
((paddrp->spp_pathmtu < SCTP_SMALLEST_PMTU) ||
(paddrp->spp_pathmtu > SCTP_LARGEST_PMTU))) {
if (stcb)
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
if (stcb != NULL) {
/************************TCB SPECIFIC SET ******************/
if (net != NULL) {
/************************NET SPECIFIC SET ******************/
if (paddrp->spp_flags & SPP_HB_DISABLE) {
if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
!(net->dest_state & SCTP_ADDR_NOHB)) {
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
}
net->dest_state |= SCTP_ADDR_NOHB;
}
if (paddrp->spp_flags & SPP_HB_ENABLE) {
if (paddrp->spp_hbinterval) {
net->heart_beat_delay = paddrp->spp_hbinterval;
} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
net->heart_beat_delay = 0;
}
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
net->dest_state &= ~SCTP_ADDR_NOHB;
}
if (paddrp->spp_flags & SPP_HB_DEMAND) {
if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
}
}
if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
}
net->dest_state |= SCTP_ADDR_NO_PMTUD;
if (paddrp->spp_pathmtu > 0) {
net->mtu = paddrp->spp_pathmtu;
switch (net->ro._l_addr.sa.sa_family) {
#ifdef INET
case AF_INET:
net->mtu += SCTP_MIN_V4_OVERHEAD;
break;
#endif
#ifdef INET6
case AF_INET6:
net->mtu += SCTP_MIN_OVERHEAD;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
net->mtu += sizeof(struct sctphdr);
break;
#endif
default:
break;
}
if (net->mtu < stcb->asoc.smallest_mtu) {
sctp_pathmtu_adjustment(stcb, net->mtu);
}
}
}
if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
}
net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
}
if (paddrp->spp_pathmaxrxt > 0) {
if (net->dest_state & SCTP_ADDR_PF) {
if (net->error_count > paddrp->spp_pathmaxrxt) {
net->dest_state &= ~SCTP_ADDR_PF;
}
} else {
if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
(net->error_count > net->pf_threshold)) {
net->dest_state |= SCTP_ADDR_PF;
sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
stcb->sctp_ep, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
}
}
if (net->dest_state & SCTP_ADDR_REACHABLE) {
if (net->error_count > paddrp->spp_pathmaxrxt) {
net->dest_state &= ~SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
}
} else {
if (net->error_count <= paddrp->spp_pathmaxrxt) {
net->dest_state |= SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
}
}
net->failure_threshold = paddrp->spp_pathmaxrxt;
}
if (paddrp->spp_flags & SPP_DSCP) {
net->dscp = paddrp->spp_dscp & 0xfc;
net->dscp |= 0x01;
}
#ifdef INET6
if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
if (net->ro._l_addr.sa.sa_family == AF_INET6) {
net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
net->flowlabel |= 0x80000000;
}
}
#endif
} else {
/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
if (paddrp->spp_pathmaxrxt > 0) {
stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (net->dest_state & SCTP_ADDR_PF) {
if (net->error_count > paddrp->spp_pathmaxrxt) {
net->dest_state &= ~SCTP_ADDR_PF;
}
} else {
if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
(net->error_count > net->pf_threshold)) {
net->dest_state |= SCTP_ADDR_PF;
sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
stcb->sctp_ep, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
}
}
if (net->dest_state & SCTP_ADDR_REACHABLE) {
if (net->error_count > paddrp->spp_pathmaxrxt) {
net->dest_state &= ~SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
}
} else {
if (net->error_count <= paddrp->spp_pathmaxrxt) {
net->dest_state |= SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
}
}
net->failure_threshold = paddrp->spp_pathmaxrxt;
}
}
if (paddrp->spp_flags & SPP_HB_ENABLE) {
if (paddrp->spp_hbinterval != 0) {
stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
stcb->asoc.heart_beat_delay = 0;
}
/* Turn back on the timer */
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (paddrp->spp_hbinterval != 0) {
net->heart_beat_delay = paddrp->spp_hbinterval;
} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
net->heart_beat_delay = 0;
}
if (net->dest_state & SCTP_ADDR_NOHB) {
net->dest_state &= ~SCTP_ADDR_NOHB;
}
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
}
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
}
if (paddrp->spp_flags & SPP_HB_DISABLE) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (!(net->dest_state & SCTP_ADDR_NOHB)) {
net->dest_state |= SCTP_ADDR_NOHB;
if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
}
}
}
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
}
if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
}
net->dest_state |= SCTP_ADDR_NO_PMTUD;
if (paddrp->spp_pathmtu > 0) {
net->mtu = paddrp->spp_pathmtu;
switch (net->ro._l_addr.sa.sa_family) {
#ifdef INET
case AF_INET:
net->mtu += SCTP_MIN_V4_OVERHEAD;
break;
#endif
#ifdef INET6
case AF_INET6:
net->mtu += SCTP_MIN_OVERHEAD;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
net->mtu += sizeof(struct sctphdr);
break;
#endif
default:
break;
}
if (net->mtu < stcb->asoc.smallest_mtu) {
sctp_pathmtu_adjustment(stcb, net->mtu);
}
}
}
if (paddrp->spp_pathmtu > 0) {
stcb->asoc.default_mtu = paddrp->spp_pathmtu;
}
sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
}
if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
}
net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
}
stcb->asoc.default_mtu = 0;
sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
}
if (paddrp->spp_flags & SPP_DSCP) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
net->dscp = paddrp->spp_dscp & 0xfc;
net->dscp |= 0x01;
}
stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
stcb->asoc.default_dscp |= 0x01;
}
#ifdef INET6
if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if (net->ro._l_addr.sa.sa_family == AF_INET6) {
net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
net->flowlabel |= 0x80000000;
}
}
stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
stcb->asoc.default_flowlabel |= 0x80000000;
}
#endif
}
SCTP_TCB_UNLOCK(stcb);
} else {
/************************NO TCB, SET TO default stuff ******************/
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
/*
* For the TOS/FLOWLABEL stuff you set it
* with the options on the socket
*/
if (paddrp->spp_pathmaxrxt > 0) {
inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
}
if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
else if (paddrp->spp_hbinterval != 0) {
if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
paddrp->spp_hbinterval= SCTP_MAX_HB_INTERVAL;
inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(paddrp->spp_hbinterval);
}
if (paddrp->spp_flags & SPP_HB_ENABLE) {
if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
} else if (paddrp->spp_hbinterval) {
inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(paddrp->spp_hbinterval);
}
sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
}
if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
inp->sctp_ep.default_mtu = 0;
sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
if (paddrp->spp_pathmtu > 0) {
inp->sctp_ep.default_mtu = paddrp->spp_pathmtu;
}
sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
}
if (paddrp->spp_flags & SPP_DSCP) {
inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
inp->sctp_ep.default_dscp |= 0x01;
}
#ifdef INET6
if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
inp->sctp_ep.default_flowlabel |= 0x80000000;
}
}
#endif
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_RTOINFO:
{
struct sctp_rtoinfo *srto;
uint32_t new_init, new_min, new_max;
SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
if (stcb) {
if (srto->srto_initial)
new_init = srto->srto_initial;
else
new_init = stcb->asoc.initial_rto;
if (srto->srto_max)
new_max = srto->srto_max;
else
new_max = stcb->asoc.maxrto;
if (srto->srto_min)
new_min = srto->srto_min;
else
new_min = stcb->asoc.minrto;
if ((new_min <= new_init) && (new_init <= new_max)) {
stcb->asoc.initial_rto = new_init;
stcb->asoc.maxrto = new_max;
stcb->asoc.minrto = new_min;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(srto->srto_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (srto->srto_initial)
new_init = srto->srto_initial;
else
new_init = inp->sctp_ep.initial_rto;
if (srto->srto_max)
new_max = srto->srto_max;
else
new_max = inp->sctp_ep.sctp_maxrto;
if (srto->srto_min)
new_min = srto->srto_min;
else
new_min = inp->sctp_ep.sctp_minrto;
if ((new_min <= new_init) && (new_init <= new_max)) {
inp->sctp_ep.initial_rto = new_init;
inp->sctp_ep.sctp_maxrto = new_max;
inp->sctp_ep.sctp_minrto = new_min;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_ASSOCINFO:
{
struct sctp_assocparams *sasoc;
SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
if (sasoc->sasoc_cookie_life > 0) {
/* boundary check the cookie life */
if (sasoc->sasoc_cookie_life < SCTP_MIN_COOKIE_LIFE) {
sasoc->sasoc_cookie_life = SCTP_MIN_COOKIE_LIFE;
}
if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
}
}
if (stcb) {
if (sasoc->sasoc_asocmaxrxt > 0) {
stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
}
if (sasoc->sasoc_cookie_life > 0) {
stcb->asoc.cookie_life = sctp_msecs_to_ticks(sasoc->sasoc_cookie_life);
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (sasoc->sasoc_asocmaxrxt > 0) {
inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
}
if (sasoc->sasoc_cookie_life > 0) {
inp->sctp_ep.def_cookie_life = sctp_msecs_to_ticks(sasoc->sasoc_cookie_life);
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_INITMSG:
{
struct sctp_initmsg *sinit;
SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
SCTP_INP_WLOCK(inp);
if (sinit->sinit_num_ostreams)
inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
if (sinit->sinit_max_instreams)
inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
if (sinit->sinit_max_attempts)
inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
if (sinit->sinit_max_init_timeo)
inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
SCTP_INP_WUNLOCK(inp);
break;
}
case SCTP_PRIMARY_ADDR:
{
struct sctp_setprim *spa;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
#if defined(INET) && defined(INET6)
if (spa->ssp_addr.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&spa->ssp_addr;
}
} else {
addr = (struct sockaddr *)&spa->ssp_addr;
}
#else
addr = (struct sockaddr *)&spa->ssp_addr;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr,
&net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net != NULL)) {
if (net != stcb->asoc.primary_destination) {
if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
/* Ok we need to set it */
if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
if ((stcb->asoc.alternate) &&
(!(net->dest_state & SCTP_ADDR_PF)) &&
(net->dest_state & SCTP_ADDR_REACHABLE)) {
sctp_free_remote_addr(stcb->asoc.alternate);
stcb->asoc.alternate = NULL;
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
break;
}
case SCTP_SET_DYNAMIC_PRIMARY:
{
union sctp_sockstore *ss;
#ifdef SCTP_MVRF
int i, fnd = 0;
#endif
#if !defined(_WIN32) && !defined(__Userspace__)
#if defined(__APPLE__)
struct proc *proc;
#endif
#if defined(__FreeBSD__)
error = priv_check(curthread,
PRIV_NETINET_RESERVEDPORT);
#elif defined(__APPLE__)
proc = (struct proc *)p;
if (p) {
error = suser(proc->p_ucred, &proc->p_acflag);
} else {
break;
}
#else
error = suser(p, 0);
#endif
if (error)
break;
#endif
SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
/* SUPER USER CHECK? */
#ifdef SCTP_MVRF
for (i = 0; i < inp->num_vrfs; i++) {
if (vrf_id == inp->m_vrf_ids[i]) {
fnd = 1;
break;
}
}
if (!fnd) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#endif
error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
break;
}
case SCTP_SET_PEER_PRIMARY_ADDR:
{
struct sctp_setpeerprim *sspp;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
if (stcb != NULL) {
struct sctp_ifa *ifa;
#if defined(INET) && defined(INET6)
if (sspp->sspp_addr.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&sspp->sspp_addr;
}
} else {
addr = (struct sockaddr *)&sspp->sspp_addr;
}
#else
addr = (struct sockaddr *)&sspp->sspp_addr;
#endif
ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
if (ifa == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_of_it;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
/* Must validate the ifa found is in our ep */
struct sctp_laddr *laddr;
int found = 0;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa == NULL) {
SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
__func__);
continue;
}
if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
(!sctp_is_addr_pending(stcb, laddr->ifa))) {
continue;
}
if (laddr->ifa == ifa) {
found = 1;
break;
}
}
if (!found) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_of_it;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
} else {
switch (addr->sa_family) {
#ifdef INET
case AF_INET:
{
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
&sin->sin_addr) != 0) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_of_it;
}
break;
}
#endif
#ifdef INET6
case AF_INET6:
{
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
&sin6->sin6_addr) != 0) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_of_it;
}
break;
}
#endif
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_of_it;
}
#endif
}
if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
out_of_it:
SCTP_TCB_UNLOCK(stcb);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
}
case SCTP_BINDX_ADD_ADDR:
{
struct sockaddr *sa;
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct thread *td;
td = (struct thread *)p;
#endif
SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
#ifdef INET
if (sa->sa_family == AF_INET) {
if (optsize < sizeof(struct sockaddr_in)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (td != NULL &&
(error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)sa)->sin_addr)))) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
#endif
} else
#endif
#ifdef INET6
if (sa->sa_family == AF_INET6) {
if (optsize < sizeof(struct sockaddr_in6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (td != NULL &&
(error = prison_local_ip6(td->td_ucred,
&(((struct sockaddr_in6 *)sa)->sin6_addr),
(SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
#endif
} else
#endif
{
error = EAFNOSUPPORT;
break;
}
sctp_bindx_add_address(so, inp, sa, vrf_id, &error, p);
break;
}
case SCTP_BINDX_REM_ADDR:
{
struct sockaddr *sa;
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct thread *td;
td = (struct thread *)p;
#endif
SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
#ifdef INET
if (sa->sa_family == AF_INET) {
if (optsize < sizeof(struct sockaddr_in)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (td != NULL &&
(error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)sa)->sin_addr)))) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
#endif
} else
#endif
#ifdef INET6
if (sa->sa_family == AF_INET6) {
if (optsize < sizeof(struct sockaddr_in6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if (td != NULL &&
(error = prison_local_ip6(td->td_ucred,
&(((struct sockaddr_in6 *)sa)->sin6_addr),
(SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
#endif
} else
#endif
{
error = EAFNOSUPPORT;
break;
}
sctp_bindx_delete_address(inp, sa, vrf_id, &error);
break;
}
#if defined(__APPLE__) && !defined(__Userspace__)
case SCTP_LISTEN_FIX:
/* only applies to one-to-many sockets */
if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
/* make sure the ACCEPTCONN flag is OFF */
so->so_options &= ~SO_ACCEPTCONN;
} else {
/* otherwise, not allowed */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
break;
#endif
case SCTP_EVENT:
{
struct sctp_event *event;
uint32_t event_type;
SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
switch (event->se_type) {
case SCTP_ASSOC_CHANGE:
event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
break;
case SCTP_PEER_ADDR_CHANGE:
event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
break;
case SCTP_REMOTE_ERROR:
event_type = SCTP_PCB_FLAGS_RECVPEERERR;
break;
case SCTP_SEND_FAILED:
event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
break;
case SCTP_SHUTDOWN_EVENT:
event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
break;
case SCTP_ADAPTATION_INDICATION:
event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
break;
case SCTP_PARTIAL_DELIVERY_EVENT:
event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
break;
case SCTP_AUTHENTICATION_EVENT:
event_type = SCTP_PCB_FLAGS_AUTHEVNT;
break;
case SCTP_STREAM_RESET_EVENT:
event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
break;
case SCTP_SENDER_DRY_EVENT:
event_type = SCTP_PCB_FLAGS_DRYEVNT;
break;
case SCTP_NOTIFICATIONS_STOPPED_EVENT:
event_type = 0;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
error = ENOTSUP;
break;
case SCTP_ASSOC_RESET_EVENT:
event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
break;
case SCTP_STREAM_CHANGE_EVENT:
event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
break;
case SCTP_SEND_FAILED_EVENT:
event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
break;
default:
event_type = 0;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if (event_type > 0) {
if (stcb) {
if (event->se_on) {
sctp_stcb_feature_on(inp, stcb, event_type);
if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
(stcb->asoc.stream_queue_cnt == 0)) {
sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
}
}
} else {
sctp_stcb_feature_off(inp, stcb, event_type);
}
SCTP_TCB_UNLOCK(stcb);
} else {
/*
* We don't want to send up a storm of events,
* so return an error for sender dry events
*/
if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((event->se_assoc_id == SCTP_ALL_ASSOC) ||
(event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
error = ENOTSUP;
break;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
(event->se_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
if (event->se_on) {
sctp_feature_on(inp, event_type);
} else {
sctp_feature_off(inp, event_type);
}
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
(event->se_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (event->se_on) {
sctp_stcb_feature_on(inp, stcb, event_type);
} else {
sctp_stcb_feature_off(inp, stcb, event_type);
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
} else {
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
}
break;
}
case SCTP_RECVRCVINFO:
{
int *onoff;
SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
SCTP_INP_WLOCK(inp);
if (*onoff != 0) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
}
SCTP_INP_WUNLOCK(inp);
break;
}
case SCTP_RECVNXTINFO:
{
int *onoff;
SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
SCTP_INP_WLOCK(inp);
if (*onoff != 0) {
sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
} else {
sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
}
SCTP_INP_WUNLOCK(inp);
break;
}
case SCTP_DEFAULT_SNDINFO:
{
struct sctp_sndinfo *info;
uint16_t policy;
SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
if (stcb) {
if (info->snd_sid < stcb->asoc.streamoutcnt) {
stcb->asoc.def_send.sinfo_stream = info->snd_sid;
policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
stcb->asoc.def_send.sinfo_flags = info->snd_flags;
stcb->asoc.def_send.sinfo_flags |= policy;
stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
stcb->asoc.def_send.sinfo_context = info->snd_context;
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
(info->snd_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->def_send.sinfo_stream = info->snd_sid;
policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
inp->def_send.sinfo_flags = info->snd_flags;
inp->def_send.sinfo_flags |= policy;
inp->def_send.sinfo_ppid = info->snd_ppid;
inp->def_send.sinfo_context = info->snd_context;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
(info->snd_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
if (info->snd_sid < stcb->asoc.streamoutcnt) {
stcb->asoc.def_send.sinfo_stream = info->snd_sid;
policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
stcb->asoc.def_send.sinfo_flags = info->snd_flags;
stcb->asoc.def_send.sinfo_flags |= policy;
stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
stcb->asoc.def_send.sinfo_context = info->snd_context;
}
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_DEFAULT_PRINFO:
{
struct sctp_default_prinfo *info;
SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
if (info->pr_policy > SCTP_PR_SCTP_MAX) {
if (stcb) {
SCTP_TCB_UNLOCK(stcb);
}
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
break;
}
if (stcb) {
stcb->asoc.def_send.sinfo_flags &= 0xfff0;
stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
(info->pr_assoc_id == SCTP_ALL_ASSOC)))) {
SCTP_INP_WLOCK(inp);
inp->def_send.sinfo_flags &= 0xfff0;
inp->def_send.sinfo_flags |= info->pr_policy;
inp->def_send.sinfo_timetolive = info->pr_value;
SCTP_INP_WUNLOCK(inp);
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
(info->pr_assoc_id == SCTP_ALL_ASSOC))) {
SCTP_INP_RLOCK(inp);
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
SCTP_TCB_LOCK(stcb);
stcb->asoc.def_send.sinfo_flags &= 0xfff0;
stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
SCTP_TCB_UNLOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
}
}
break;
}
case SCTP_PEER_ADDR_THLDS:
/* Applies to the specific association */
{
struct sctp_paddrthlds *thlds;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
#if defined(INET) && defined(INET6)
if (thlds->spt_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&thlds->spt_address;
}
} else {
addr = (struct sockaddr *)&thlds->spt_address;
}
#else
addr = (struct sockaddr *)&thlds->spt_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr,
&net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
if (thlds->spt_pathcpthld != 0xffff) {
if (stcb != NULL) {
SCTP_TCB_UNLOCK(stcb);
}
error = EINVAL;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
if (stcb != NULL) {
if (net != NULL) {
net->failure_threshold = thlds->spt_pathmaxrxt;
net->pf_threshold = thlds->spt_pathpfthld;
if (net->dest_state & SCTP_ADDR_PF) {
if ((net->error_count > net->failure_threshold) ||
(net->error_count <= net->pf_threshold)) {
net->dest_state &= ~SCTP_ADDR_PF;
}
} else {
if ((net->error_count > net->pf_threshold) &&
(net->error_count <= net->failure_threshold)) {
net->dest_state |= SCTP_ADDR_PF;
sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
stcb->sctp_ep, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
}
}
if (net->dest_state & SCTP_ADDR_REACHABLE) {
if (net->error_count > net->failure_threshold) {
net->dest_state &= ~SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
}
} else {
if (net->error_count <= net->failure_threshold) {
net->dest_state |= SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
}
}
} else {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
net->failure_threshold = thlds->spt_pathmaxrxt;
net->pf_threshold = thlds->spt_pathpfthld;
if (net->dest_state & SCTP_ADDR_PF) {
if ((net->error_count > net->failure_threshold) ||
(net->error_count <= net->pf_threshold)) {
net->dest_state &= ~SCTP_ADDR_PF;
}
} else {
if ((net->error_count > net->pf_threshold) &&
(net->error_count <= net->failure_threshold)) {
net->dest_state |= SCTP_ADDR_PF;
sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
stcb->sctp_ep, stcb, net,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
}
}
if (net->dest_state & SCTP_ADDR_REACHABLE) {
if (net->error_count > net->failure_threshold) {
net->dest_state &= ~SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
}
} else {
if (net->error_count <= net->failure_threshold) {
net->dest_state |= SCTP_ADDR_REACHABLE;
sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
}
}
}
stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(thlds->spt_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_REMOTE_UDP_ENCAPS_PORT:
{
struct sctp_udpencaps *encaps;
struct sctp_nets *net;
struct sockaddr *addr;
#if defined(INET) && defined(INET6)
struct sockaddr_in sin_store;
#endif
SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
#if defined(INET) && defined(INET6)
if (encaps->sue_address.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
in6_sin6_2_sin(&sin_store, sin6);
addr = (struct sockaddr *)&sin_store;
} else {
addr = (struct sockaddr *)&encaps->sue_address;
}
} else {
addr = (struct sockaddr *)&encaps->sue_address;
}
#else
addr = (struct sockaddr *)&encaps->sue_address;
#endif
if (stcb != NULL) {
net = sctp_findnet(stcb, addr);
} else {
/* We increment here since sctp_findassociation_ep_addr() wil
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
net = NULL;
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
}
}
if ((stcb != NULL) && (net == NULL)) {
#ifdef INET
if (addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = (struct sockaddr_in *)addr;
if (sin->sin_addr.s_addr != INADDR_ANY) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#ifdef INET6
if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = (struct sockaddr_in6 *)addr;
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
#if defined(__Userspace__)
if (addr->sa_family == AF_CONN) {
struct sockaddr_conn *sconn;
sconn = (struct sockaddr_conn *)addr;
if (sconn->sconn_addr != NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
SCTP_TCB_UNLOCK(stcb);
error = EINVAL;
break;
}
} else
#endif
{
error = EAFNOSUPPORT;
SCTP_TCB_UNLOCK(stcb);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
break;
}
}
if (stcb != NULL) {
if (net != NULL) {
net->port = encaps->sue_port;
} else {
stcb->asoc.port = encaps->sue_port;
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(encaps->sue_assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
inp->sctp_ep.port = encaps->sue_port;
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_ECN_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->ecn_supported = 0;
} else {
inp->ecn_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_PR_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->prsctp_supported = 0;
} else {
inp->prsctp_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_AUTH_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
if ((av->assoc_value == 0) &&
(inp->asconf_supported == 1)) {
/* AUTH is required for ASCONF */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->auth_supported = 0;
} else {
inp->auth_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_ASCONF_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
if ((av->assoc_value != 0) &&
(inp->auth_supported == 0)) {
/* AUTH is required for ASCONF */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
} else {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->asconf_supported = 0;
sctp_auth_delete_chunk(SCTP_ASCONF,
inp->sctp_ep.local_auth_chunks);
sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
inp->sctp_ep.local_auth_chunks);
} else {
inp->asconf_supported = 1;
sctp_auth_add_chunk(SCTP_ASCONF,
inp->sctp_ep.local_auth_chunks);
sctp_auth_add_chunk(SCTP_ASCONF_ACK,
inp->sctp_ep.local_auth_chunks);
}
SCTP_INP_WUNLOCK(inp);
}
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_RECONFIG_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->reconfig_supported = 0;
} else {
inp->reconfig_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_NRSACK_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->nrsack_supported = 0;
} else {
inp->nrsack_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_PKTDROP_SUPPORTED:
{
struct sctp_assoc_value *av;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
if (av->assoc_value == 0) {
inp->pktdrop_supported = 0;
} else {
inp->pktdrop_supported = 1;
}
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
case SCTP_MAX_CWND:
{
struct sctp_assoc_value *av;
struct sctp_nets *net;
SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
SCTP_FIND_STCB(inp, stcb, av->assoc_id);
if (stcb) {
stcb->asoc.max_cwnd = av->assoc_value;
if (stcb->asoc.max_cwnd > 0) {
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
if ((net->cwnd > stcb->asoc.max_cwnd) &&
(net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
net->cwnd = stcb->asoc.max_cwnd;
if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
net->cwnd = net->mtu - sizeof(struct sctphdr);
}
}
}
}
SCTP_TCB_UNLOCK(stcb);
} else {
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
(av->assoc_id == SCTP_FUTURE_ASSOC))) {
SCTP_INP_WLOCK(inp);
inp->max_cwnd = av->assoc_value;
SCTP_INP_WUNLOCK(inp);
} else {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
}
break;
}
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
error = ENOPROTOOPT;
break;
} /* end switch (opt) */
return (error);
}
#if !defined(__Userspace__)
int
sctp_ctloutput(struct socket *so, struct sockopt *sopt)
{
#if defined(__FreeBSD__)
struct epoch_tracker et;
struct sctp_inpcb *inp;
#endif
void *optval = NULL;
void *p;
size_t optsize = 0;
int error = 0;
#if defined(__FreeBSD__)
if ((sopt->sopt_level == SOL_SOCKET) &&
(sopt->sopt_name == SO_SETFIB)) {
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
return (EINVAL);
}
SCTP_INP_WLOCK(inp);
inp->fibnum = so->so_fibnum;
SCTP_INP_WUNLOCK(inp);
return (0);
}
#endif
if (sopt->sopt_level != IPPROTO_SCTP) {
/* wrong proto level... send back up to IP */
#ifdef INET6
if (INP_CHECK_SOCKAF(so, AF_INET6))
error = ip6_ctloutput(so, sopt);
#endif /* INET6 */
#if defined(INET) && defined(INET6)
else
#endif
#ifdef INET
error = ip_ctloutput(so, sopt);
#endif
return (error);
}
optsize = sopt->sopt_valsize;
if (optsize > SCTP_SOCKET_OPTION_LIMIT) {
SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
return (ENOBUFS);
}
if (optsize) {
SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
if (optval == NULL) {
SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
return (ENOBUFS);
}
error = sooptcopyin(sopt, optval, optsize, optsize);
if (error) {
SCTP_FREE(optval, SCTP_M_SOCKOPT);
goto out;
}
}
#if defined(__FreeBSD__) || defined(_WIN32)
p = (void *)sopt->sopt_td;
#else
p = (void *)sopt->sopt_p;
#endif
if (sopt->sopt_dir == SOPT_SET) {
#if defined(__FreeBSD__)
NET_EPOCH_ENTER(et);
#endif
error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
#if defined(__FreeBSD__)
NET_EPOCH_EXIT(et);
#endif
} else if (sopt->sopt_dir == SOPT_GET) {
error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
} else {
SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
}
if ((error == 0) && (optval != NULL)) {
error = sooptcopyout(sopt, optval, optsize);
SCTP_FREE(optval, SCTP_M_SOCKOPT);
} else if (optval != NULL) {
SCTP_FREE(optval, SCTP_M_SOCKOPT);
}
out:
return (error);
}
#endif
#ifdef INET
#if defined(__Userspace__)
int
sctp_connect(struct socket *so, struct sockaddr *addr)
{
void *p = NULL;
#elif defined(__FreeBSD__)
static int
sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
{
#elif defined(__APPLE__)
static int
sctp_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
{
#elif defined(_WIN32)
static int
sctp_connect(struct socket *so, struct sockaddr *addr, PKTHREAD p)
{
#else
static int
sctp_connect(struct socket *so, struct mbuf *nam, struct proc *p)
{
struct sockaddr *addr = mtod(nam, struct sockaddr *);
#endif
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct epoch_tracker et;
#endif
#ifdef SCTP_MVRF
int i, fnd = 0;
#endif
int error = 0;
int create_lock_on = 0;
uint32_t vrf_id;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb = NULL;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
/* I made the same as TCP since we are not setup? */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
if (addr == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return EINVAL;
}
#if defined(__Userspace__)
/* TODO __Userspace__ falls into this code for IPv6 stuff at the moment... */
#endif
#if !defined(_WIN32) && !defined(__linux__) && !defined(__EMSCRIPTEN__)
switch (addr->sa_family) {
#ifdef INET6
case AF_INET6:
{
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct sockaddr_in6 *sin6;
#endif
if (addr->sa_len != sizeof(struct sockaddr_in6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
sin6 = (struct sockaddr_in6 *)addr;
if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6->sin6_addr)) != 0) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
return (error);
}
#endif
break;
}
#endif
#ifdef INET
case AF_INET:
{
#if defined(__FreeBSD__) && !defined(__Userspace__)
struct sockaddr_in *sin;
#endif
#if !defined(_WIN32)
if (addr->sa_len != sizeof(struct sockaddr_in)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
#endif
#if defined(__FreeBSD__) && !defined(__Userspace__)
sin = (struct sockaddr_in *)addr;
if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
return (error);
}
#endif
break;
}
#endif
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
return (EAFNOSUPPORT);
}
#endif
SCTP_INP_INCR_REF(inp);
SCTP_ASOC_CREATE_LOCK(inp);
create_lock_on = 1;
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_ENTER(et);
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
/* Should I really unlock ? */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
error = EFAULT;
goto out_now;
}
#ifdef INET6
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
(addr->sa_family == AF_INET6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
#endif
#if defined(__Userspace__)
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) &&
(addr->sa_family != AF_CONN)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
SCTP_PCB_FLAGS_UNBOUND) {
/* Bind a ephemeral port */
error = sctp_inpcb_bind(so, NULL, NULL, p);
if (error) {
goto out_now;
}
}
/* Now do we connect? */
if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
(sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
/* We are already connected AND the TCP model */
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
error = EADDRINUSE;
goto out_now;
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
SCTP_INP_RUNLOCK(inp);
} else {
/* We increment here since sctp_findassociation_ep_addr() will
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
} else {
SCTP_TCB_UNLOCK(stcb);
}
}
if (stcb != NULL) {
/* Already have or am bring up an association */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
goto out_now;
}
vrf_id = inp->def_vrf_id;
#ifdef SCTP_MVRF
for (i = 0; i < inp->num_vrfs; i++) {
if (vrf_id == inp->m_vrf_ids[i]) {
fnd = 1;
break;
}
}
if (!fnd) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
#endif
/* We are GOOD to go */
stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
inp->sctp_ep.pre_open_stream_count,
inp->sctp_ep.port, p,
SCTP_INITIALIZE_AUTH_PARAMS);
if (stcb == NULL) {
/* Gak! no memory */
goto out_now;
}
SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
SCTP_TCB_UNLOCK(stcb);
out_now:
#if defined(__FreeBSD__) && !defined(__Userspace__)
NET_EPOCH_EXIT(et);
#endif
if (create_lock_on) {
SCTP_ASOC_CREATE_UNLOCK(inp);
}
SCTP_INP_DECR_REF(inp);
return (error);
}
#endif
#if defined(__Userspace__)
int
sctpconn_connect(struct socket *so, struct sockaddr *addr)
{
#ifdef SCTP_MVRF
int i, fnd = 0;
#endif
void *p = NULL;
int error = 0;
int create_lock_on = 0;
uint32_t vrf_id;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb = NULL;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
/* I made the same as TCP since we are not setup? */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
if (addr == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return EINVAL;
}
switch (addr->sa_family) {
#ifdef INET
case AF_INET:
#ifdef HAVE_SA_LEN
if (addr->sa_len != sizeof(struct sockaddr_in)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
#endif
break;
#endif
#ifdef INET6
case AF_INET6:
#ifdef HAVE_SA_LEN
if (addr->sa_len != sizeof(struct sockaddr_in6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
#endif
break;
#endif
case AF_CONN:
#ifdef HAVE_SA_LEN
if (addr->sa_len != sizeof(struct sockaddr_conn)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
#endif
break;
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
return (EAFNOSUPPORT);
}
SCTP_INP_INCR_REF(inp);
SCTP_ASOC_CREATE_LOCK(inp);
create_lock_on = 1;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
(inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
/* Should I really unlock ? */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
error = EFAULT;
goto out_now;
}
#ifdef INET6
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
(addr->sa_family == AF_INET6)) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == SCTP_PCB_FLAGS_UNBOUND) {
/* Bind a ephemeral port */
error = sctp_inpcb_bind(so, NULL, NULL, p);
if (error) {
goto out_now;
}
}
/* Now do we connect? */
if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
(sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
/* We are already connected AND the TCP model */
SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
error = EADDRINUSE;
goto out_now;
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
SCTP_INP_RUNLOCK(inp);
} else {
/* We increment here since sctp_findassociation_ep_addr() will
* do a decrement if it finds the stcb as long as the locked
* tcb (last argument) is NOT a TCB.. aka NULL.
*/
SCTP_INP_INCR_REF(inp);
stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
if (stcb == NULL) {
SCTP_INP_DECR_REF(inp);
} else {
SCTP_TCB_UNLOCK(stcb);
}
}
if (stcb != NULL) {
/* Already have or am bring up an association */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
error = EALREADY;
goto out_now;
}
vrf_id = inp->def_vrf_id;
#ifdef SCTP_MVRF
for (i = 0; i < inp->num_vrfs; i++) {
if (vrf_id == inp->m_vrf_ids[i]) {
fnd = 1;
break;
}
}
if (!fnd) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
error = EINVAL;
goto out_now;
}
#endif
/* We are GOOD to go */
stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
inp->sctp_ep.pre_open_stream_count,
inp->sctp_ep.port, p,
SCTP_INITIALIZE_AUTH_PARAMS);
if (stcb == NULL) {
/* Gak! no memory */
goto out_now;
}
SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
SCTP_TCB_UNLOCK(stcb);
out_now:
if (create_lock_on) {
SCTP_ASOC_CREATE_UNLOCK(inp);
}
SCTP_INP_DECR_REF(inp);
return (error);
}
#endif
int
#if defined(__Userspace__)
sctp_listen(struct socket *so, int backlog, struct proc *p)
#elif defined(__FreeBSD__)
sctp_listen(struct socket *so, int backlog, struct thread *p)
#elif defined(_WIN32)
sctp_listen(struct socket *so, int backlog, PKTHREAD p)
#else
sctp_listen(struct socket *so, struct proc *p)
#endif
{
/*
* Note this module depends on the protocol processing being called
* AFTER any socket level flags and backlog are applied to the
* socket. The traditional way that the socket flags are applied is
* AFTER protocol processing. We have made a change to the
* sys/kern/uipc_socket.c module to reverse this but this MUST be in
* place if the socket API for SCTP is to work properly.
*/
int error = 0;
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
/* I made the same as TCP since we are not setup? */
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
/* See if we have a listener */
struct sctp_inpcb *tinp;
union sctp_sockstore store;
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
/* not bound all */
struct sctp_laddr *laddr;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
memcpy(&store, &laddr->ifa->address, sizeof(store));
switch (store.sa.sa_family) {
#ifdef INET
case AF_INET:
store.sin.sin_port = inp->sctp_lport;
break;
#endif
#ifdef INET6
case AF_INET6:
store.sin6.sin6_port = inp->sctp_lport;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
store.sconn.sconn_port = inp->sctp_lport;
break;
#endif
default:
break;
}
tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
if (tinp && (tinp != inp) &&
((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
(SCTP_IS_LISTENING(tinp))) {
/* we have a listener already and its not this inp. */
SCTP_INP_DECR_REF(tinp);
return (EADDRINUSE);
} else if (tinp) {
SCTP_INP_DECR_REF(tinp);
}
}
} else {
/* Setup a local addr bound all */
memset(&store, 0, sizeof(store));
#ifdef INET6
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
store.sa.sa_family = AF_INET6;
#ifdef HAVE_SA_LEN
store.sa.sa_len = sizeof(struct sockaddr_in6);
#endif
}
#endif
#if defined(__Userspace__)
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
store.sa.sa_family = AF_CONN;
#ifdef HAVE_SA_LEN
store.sa.sa_len = sizeof(struct sockaddr_conn);
#endif
}
#endif
#ifdef INET
#if defined(__Userspace__)
if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
#else
if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
#endif
store.sa.sa_family = AF_INET;
#ifdef HAVE_SA_LEN
store.sa.sa_len = sizeof(struct sockaddr_in);
#endif
}
#endif
switch (store.sa.sa_family) {
#ifdef INET
case AF_INET:
store.sin.sin_port = inp->sctp_lport;
break;
#endif
#ifdef INET6
case AF_INET6:
store.sin6.sin6_port = inp->sctp_lport;
break;
#endif
#if defined(__Userspace__)
case AF_CONN:
store.sconn.sconn_port = inp->sctp_lport;
break;
#endif
default:
break;
}
tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
if (tinp && (tinp != inp) &&
((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
(SCTP_IS_LISTENING(tinp))) {
/* we have a listener already and its not this inp. */
SCTP_INP_DECR_REF(tinp);
return (EADDRINUSE);
} else if (tinp) {
SCTP_INP_DECR_REF(tinp);
}
}
}
SCTP_INP_INFO_WLOCK();
SCTP_INP_WLOCK(inp);
#ifdef SCTP_LOCK_LOGGING
if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
}
#endif
if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
/* The unlucky case
* - We are in the tcp pool with this guy.
* - Someone else is in the main inp slot.
* - We must move this guy (the listener) to the main slot
* - We must then move the guy that was listener to the TCP Pool.
*/
if (sctp_swap_inpcb_for_listen(inp)) {
error = EADDRINUSE;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
goto out;
}
}
#if defined(__FreeBSD__) || defined(__Userspace__)
SOCK_LOCK(so);
error = solisten_proto_check(so);
if (error) {
SOCK_UNLOCK(so);
goto out;
}
#endif
if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
(inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
SOCK_UNLOCK(so);
#if defined(__FreeBSD__) && !defined(__Userspace__)
solisten_proto_abort(so);
#endif
error = EADDRINUSE;
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
goto out;
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
if ((error = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
SOCK_UNLOCK(so);
#if defined(__FreeBSD__) && !defined(__Userspace__)
solisten_proto_abort(so);
#endif
/* bind error, probably perm */
goto out;
}
}
#if defined(__FreeBSD__) && !defined(__Userspace__)
if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) {
solisten_proto(so, backlog);
SOCK_UNLOCK(so);
inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
} else {
solisten_proto_abort(so);
SOCK_UNLOCK(so);
if (backlog > 0) {
inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
} else {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
}
}
#elif defined(_WIN32) || defined(__Userspace__)
solisten_proto(so, backlog);
#endif
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
/* remove the ACCEPTCONN flag for one-to-many sockets */
#if defined(__Userspace__)
so->so_options &= ~SCTP_SO_ACCEPTCONN;
#else
so->so_options &= ~SO_ACCEPTCONN;
#endif
}
SOCK_UNLOCK(so);
if (backlog > 0) {
inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
} else {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
}
#endif
out:
SCTP_INP_WUNLOCK(inp);
SCTP_INP_INFO_WUNLOCK();
return (error);
}
static int sctp_defered_wakeup_cnt = 0;
int
sctp_accept(struct socket *so, struct sockaddr **addr)
{
struct sctp_tcb *stcb;
struct sctp_inpcb *inp;
union sctp_sockstore store;
#ifdef INET6
#if defined(SCTP_KAME) && defined(SCTP_EMBEDDED_V6_SCOPE)
int error;
#endif
#endif
inp = (struct sctp_inpcb *)so->so_pcb;
if (inp == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
SCTP_INP_WLOCK(inp);
if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
SCTP_INP_WUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
return (EOPNOTSUPP);
}
if (so->so_state & SS_ISDISCONNECTED) {
SCTP_INP_WUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
return (ECONNABORTED);
}
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
SCTP_INP_WUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
SCTP_TCB_LOCK(stcb);
store = stcb->asoc.primary_destination->ro._l_addr;
SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
/* Wake any delayed sleep action */
if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
if (sowriteable(inp->sctp_socket)) {
#if defined(__Userspace__)
/*__Userspace__ calling sowwakup_locked because of SOCKBUF_LOCK above. */
#endif
#if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
sowwakeup_locked(inp->sctp_socket);
#else
#if defined(__APPLE__)
/* socket is locked */
#endif
sowwakeup(inp->sctp_socket);
#endif
} else {
SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
}
}
if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
if (soreadable(inp->sctp_socket)) {
sctp_defered_wakeup_cnt++;
#if defined(__Userspace__)
/*__Userspace__ calling sorwakup_locked because of SOCKBUF_LOCK above */
#endif
#if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
sorwakeup_locked(inp->sctp_socket);
#else
#if defined(__APPLE__)
/* socket is locked */
#endif
sorwakeup(inp->sctp_socket);
#endif
} else {
SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
}
}
}
SCTP_INP_WUNLOCK(inp);
if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
} else {
SCTP_TCB_UNLOCK(stcb);
}
switch (store.sa.sa_family) {
#ifdef INET
case AF_INET:
{
struct sockaddr_in *sin;
SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
if (sin == NULL)
return (ENOMEM);
sin->sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
sin->sin_len = sizeof(*sin);
#endif
sin->sin_port = store.sin.sin_port;
sin->sin_addr = store.sin.sin_addr;
*addr = (struct sockaddr *)sin;
break;
}
#endif
#ifdef INET6
case AF_INET6:
{
struct sockaddr_in6 *sin6;
SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
if (sin6 == NULL)
return (ENOMEM);
sin6->sin6_family = AF_INET6;
#ifdef HAVE_SIN6_LEN
sin6->sin6_len = sizeof(*sin6);
#endif
sin6->sin6_port = store.sin6.sin6_port;
sin6->sin6_addr = store.sin6.sin6_addr;
#if defined(SCTP_EMBEDDED_V6_SCOPE)
#ifdef SCTP_KAME
if ((error = sa6_recoverscope(sin6)) != 0) {
SCTP_FREE_SONAME(sin6);
return (error);
}
#else
if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
/*
* sin6->sin6_scope_id =
* ntohs(sin6->sin6_addr.s6_addr16[1]);
*/
in6_recoverscope(sin6, &sin6->sin6_addr, NULL); /* skip ifp check */
else
sin6->sin6_scope_id = 0; /* XXX */
#endif /* SCTP_KAME */
#endif /* SCTP_EMBEDDED_V6_SCOPE */
*addr = (struct sockaddr *)sin6;
break;
}
#endif
#if defined(__Userspace__)
case AF_CONN:
{
struct sockaddr_conn *sconn;
SCTP_MALLOC_SONAME(sconn, struct sockaddr_conn *, sizeof(struct sockaddr_conn));
if (sconn == NULL) {
return (ENOMEM);
}
sconn->sconn_family = AF_CONN;
#ifdef HAVE_SCONN_LEN
sconn->sconn_len = sizeof(struct sockaddr_conn);
#endif
sconn->sconn_port = store.sconn.sconn_port;
sconn->sconn_addr = store.sconn.sconn_addr;
*addr = (struct sockaddr *)sconn;
break;
}
#endif
default:
/* TSNH */
break;
}
return (0);
}
#ifdef INET
int
#if !defined(__Userspace__)
sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
{
struct sockaddr_in *sin;
#else
sctp_ingetaddr(struct socket *so, struct mbuf *nam)
{
struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
#endif
uint32_t vrf_id;
struct sctp_inpcb *inp;
struct sctp_ifa *sctp_ifa;
/*
* Do the malloc first in case it blocks.
*/
#if !defined(__Userspace__)
SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
if (sin == NULL)
return (ENOMEM);
#else
SCTP_BUF_LEN(nam) = sizeof(*sin);
memset(sin, 0, sizeof(*sin));
#endif
sin->sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
sin->sin_len = sizeof(*sin);
#endif
inp = (struct sctp_inpcb *)so->so_pcb;
if (!inp) {
#if !defined(__Userspace__)
SCTP_FREE_SONAME(sin);
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
SCTP_INP_RLOCK(inp);
sin->sin_port = inp->sctp_lport;
if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
struct sctp_tcb *stcb;
struct sockaddr_in *sin_a;
struct sctp_nets *net;
int fnd;
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb == NULL) {
goto notConn;
}
fnd = 0;
sin_a = NULL;
SCTP_TCB_LOCK(stcb);
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
sin_a = (struct sockaddr_in *)&net->ro._l_addr;
if (sin_a == NULL)
/* this will make coverity happy */
continue;
if (sin_a->sin_family == AF_INET) {
fnd = 1;
break;
}
}
if ((!fnd) || (sin_a == NULL)) {
/* punt */
SCTP_TCB_UNLOCK(stcb);
goto notConn;
}
vrf_id = inp->def_vrf_id;
sctp_ifa = sctp_source_address_selection(inp,
stcb,
(sctp_route_t *)&net->ro,
net, 0, vrf_id);
if (sctp_ifa) {
sin->sin_addr = sctp_ifa->address.sin.sin_addr;
sctp_free_ifa(sctp_ifa);
}
SCTP_TCB_UNLOCK(stcb);
} else {
/* For the bound all case you get back 0 */
notConn:
sin->sin_addr.s_addr = 0;
}
} else {
/* Take the first IPv4 address in the list */
struct sctp_laddr *laddr;
int fnd = 0;
LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
if (laddr->ifa->address.sa.sa_family == AF_INET) {
struct sockaddr_in *sin_a;
sin_a = &laddr->ifa->address.sin;
sin->sin_addr = sin_a->sin_addr;
fnd = 1;
break;
}
}
if (!fnd) {
#if !defined(__Userspace__)
SCTP_FREE_SONAME(sin);
#endif
SCTP_INP_RUNLOCK(inp);
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
return (ENOENT);
}
}
SCTP_INP_RUNLOCK(inp);
#if !defined(__Userspace__)
(*addr) = (struct sockaddr *)sin;
#endif
return (0);
}
int
#if !defined(__Userspace__)
sctp_peeraddr(struct socket *so, struct sockaddr **addr)
{
struct sockaddr_in *sin;
#else
sctp_peeraddr(struct socket *so, struct mbuf *nam)
{
struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
#endif
int fnd;
struct sockaddr_in *sin_a;
struct sctp_inpcb *inp;
struct sctp_tcb *stcb;
struct sctp_nets *net;
/* Do the malloc first in case it blocks. */
#if !defined(__Userspace__)
SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
if (sin == NULL)
return (ENOMEM);
#else
SCTP_BUF_LEN(nam) = sizeof(*sin);
memset(sin, 0, sizeof(*sin));
#endif
sin->sin_family = AF_INET;
#ifdef HAVE_SIN_LEN
sin->sin_len = sizeof(*sin);
#endif
inp = (struct sctp_inpcb *)so->so_pcb;
if ((inp == NULL) ||
((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
/* UDP type and listeners will drop out here */
#if !defined(__Userspace__)
SCTP_FREE_SONAME(sin);
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
return (ENOTCONN);
}
SCTP_INP_RLOCK(inp);
stcb = LIST_FIRST(&inp->sctp_asoc_list);
if (stcb) {
SCTP_TCB_LOCK(stcb);
}
SCTP_INP_RUNLOCK(inp);
if (stcb == NULL) {
#if !defined(__Userspace__)
SCTP_FREE_SONAME(sin);
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (ECONNRESET);
}
fnd = 0;
TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
sin_a = (struct sockaddr_in *)&net->ro._l_addr;
if (sin_a->sin_family == AF_INET) {
fnd = 1;
sin->sin_port = stcb->rport;
sin->sin_addr = sin_a->sin_addr;
break;
}
}
SCTP_TCB_UNLOCK(stcb);
if (!fnd) {
/* No IPv4 address */
#if !defined(__Userspace__)
SCTP_FREE_SONAME(sin);
#endif
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
return (ENOENT);
}
#if !defined(__Userspace__)
(*addr) = (struct sockaddr *)sin;
#endif
return (0);
}
#if !defined(__Userspace__)
struct pr_usrreqs sctp_usrreqs = {
#if defined(__FreeBSD__)
.pru_abort = sctp_abort,
.pru_accept = sctp_accept,
.pru_attach = sctp_attach,
.pru_bind = sctp_bind,
.pru_connect = sctp_connect,
.pru_control = in_control,
.pru_close = sctp_close,
.pru_detach = sctp_close,
.pru_sopoll = sopoll_generic,
.pru_flush = sctp_flush,
.pru_disconnect = sctp_disconnect,
.pru_listen = sctp_listen,
.pru_peeraddr = sctp_peeraddr,
.pru_send = sctp_sendm,
.pru_shutdown = sctp_shutdown,
.pru_sockaddr = sctp_ingetaddr,
.pru_sosend = sctp_sosend,
.pru_soreceive = sctp_soreceive
#elif defined(__APPLE__)
.pru_abort = sctp_abort,
.pru_accept = sctp_accept,
.pru_attach = sctp_attach,
.pru_bind = sctp_bind,
.pru_connect = sctp_connect,
.pru_connect2 = pru_connect2_notsupp,
.pru_control = in_control,
.pru_detach = sctp_detach,
.pru_disconnect = sctp_disconnect,
.pru_listen = sctp_listen,
.pru_peeraddr = sctp_peeraddr,
.pru_rcvd = NULL,
.pru_rcvoob = pru_rcvoob_notsupp,
.pru_send = sctp_sendm,
.pru_sense = pru_sense_null,
.pru_shutdown = sctp_shutdown,
.pru_sockaddr = sctp_ingetaddr,
.pru_sosend = sctp_sosend,
.pru_soreceive = sctp_soreceive,
.pru_sopoll = sopoll
#elif defined(_WIN32) && !defined(__Userspace__)
sctp_abort,
sctp_accept,
sctp_attach,
sctp_bind,
sctp_connect,
pru_connect2_notsupp,
NULL,
NULL,
sctp_disconnect,
sctp_listen,
sctp_peeraddr,
NULL,
pru_rcvoob_notsupp,
NULL,
pru_sense_null,
sctp_shutdown,
sctp_flush,
sctp_ingetaddr,
sctp_sosend,
sctp_soreceive,
sopoll_generic,
NULL,
sctp_close
#endif
};
#elif !defined(__Userspace__)
int
sctp_usrreq(so, req, m, nam, control)
struct socket *so;
int req;
struct mbuf *m, *nam, *control;
{
struct proc *p = curproc;
int error;
int family;
struct sctp_inpcb *inp = (struct sctp_inpcb *)so->so_pcb;
error = 0;
family = so->so_proto->pr_domain->dom_family;
if (req == PRU_CONTROL) {
switch (family) {
case PF_INET:
error = in_control(so, (long)m, (caddr_t)nam,
(struct ifnet *)control);
break;
#ifdef INET6
case PF_INET6:
error = in6_control(so, (long)m, (caddr_t)nam,
(struct ifnet *)control, p);
break;
#endif
default:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
error = EAFNOSUPPORT;
}
return (error);
}
switch (req) {
case PRU_ATTACH:
error = sctp_attach(so, family, p);
break;
case PRU_DETACH:
error = sctp_detach(so);
break;
case PRU_BIND:
if (nam == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
error = sctp_bind(so, nam, p);
break;
case PRU_LISTEN:
error = sctp_listen(so, p);
break;
case PRU_CONNECT:
if (nam == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
error = sctp_connect(so, nam, p);
break;
case PRU_DISCONNECT:
error = sctp_disconnect(so);
break;
case PRU_ACCEPT:
if (nam == NULL) {
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
return (EINVAL);
}
error = sctp_accept(so, nam);
break;
case PRU_SHUTDOWN:
error = sctp_shutdown(so);
break;
case PRU_RCVD:
/*
* For Open and Net BSD, this is real ugly. The mbuf *nam
* that is passed (by soreceive()) is the int flags c ast as
* a (mbuf *) yuck!
*/
break;
case PRU_SEND:
/* Flags are ignored */
{
struct sockaddr *addr;
if (nam == NULL)
addr = NULL;
else
addr = mtod(nam, struct sockaddr *);
error = sctp_sendm(so, 0, m, addr, control, p);
}
break;
case PRU_ABORT:
error = sctp_abort(so);
break;
case PRU_SENSE:
error = 0;
break;
case PRU_RCVOOB:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
error = EAFNOSUPPORT;
break;
case PRU_SENDOOB:
SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
error = EAFNOSUPPORT;
break;
case PRU_PEERADDR:
error = sctp_peeraddr(so, nam);
break;
case PRU_SOCKADDR:
error = sctp_ingetaddr(so, nam);
break;
case PRU_SLOWTIMO:
error = 0;
break;
default:
break;
}
return (error);
}
#endif
#endif
#if defined(__Userspace__)
int
register_recv_cb(struct socket *so,
int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data,
size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info))
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *) so->so_pcb;
if (inp == NULL) {
return (0);
}
SCTP_INP_WLOCK(inp);
inp->recv_callback = receive_cb;
SCTP_INP_WUNLOCK(inp);
return (1);
}
int
register_send_cb(struct socket *so, uint32_t sb_threshold, int (*send_cb)(struct socket *sock, uint32_t sb_free, void *ulp_info))
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *) so->so_pcb;
if (inp == NULL) {
return (0);
}
SCTP_INP_WLOCK(inp);
inp->send_callback = send_cb;
inp->send_sb_threshold = sb_threshold;
SCTP_INP_WUNLOCK(inp);
/* FIXME change to current amount free. This will be the full buffer
* the first time this is registered but it could be only a portion
* of the send buffer if this is called a second time e.g. if the
* threshold changes.
*/
return (1);
}
int
register_ulp_info (struct socket *so, void *ulp_info)
{
struct sctp_inpcb *inp;
inp = (struct sctp_inpcb *) so->so_pcb;
if (inp == NULL) {
return (0);
}
SCTP_INP_WLOCK(inp);
inp->ulp_info = ulp_info;
SCTP_INP_WUNLOCK(inp);
return (1);
}
int
retrieve_ulp_info (struct socket *so, void **pulp_info)
{
struct sctp_inpcb *inp;
if (pulp_info == NULL) {
return (0);
}
inp = (struct sctp_inpcb *) so->so_pcb;
if (inp == NULL) {
return (0);
}
SCTP_INP_RLOCK(inp);
*pulp_info = inp->ulp_info;
SCTP_INP_RUNLOCK(inp);
return (1);
}
#endif
|
kgn/BBlock | Categories/UIKit/UIKit+BBlock.h | //
// UIKit+BBlock.h
// BBlock
//
// Created by <NAME> on 10/20/13.
// Copyright 2013 <NAME>. All rights reserved.
//
#import "UIActionSheet+BBlock.h"
#import "UIAlertView+BBlock.h"
#import "UIControl+BBlock.h"
#import "UIGestureRecognizer+BBlock.h"
#import "UIImage+BBlock.h"
#import "UITextField+BBlock.h"
|
kgn/BBlock | Categories/UIKit/UIAlertView+BBlock.h | //
// UIAlertView+BBlock.h
// BBlock
//
// Created by <NAME> on 5/14/12.
// Updated by <NAME> on 6/4/12.
//
#import <UIKit/UIKit.h>
@interface UIAlertView(BBlock)
typedef void (^UIAlertViewBBlock)(NSInteger buttonIndex, UIAlertView *alertView);
- (void)setCompletionBlock:(UIAlertViewBBlock)block;
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelTitle
otherButtonTitle:(NSString *)otherButtonTitle completionBlock:(UIAlertViewBBlock)block;
@end
|
kgn/BBlock | Categories/UIKit/CADisplayLink+BBlock.h | //
// CADisplayLink+BBlock.h
// BBlock
//
// Created by <NAME> on 4/23/14.
// Copyright (c) 2014 <NAME>. All rights reserved.
//
@import QuartzCore;
@interface CADisplayLink(BBlock)
typedef void (^BBlockCADisplayLinkBlock)(CADisplayLink *displayLink);
+ (instancetype)displayLinkWithBlock:(BBlockCADisplayLinkBlock)block;
@end
|
kgn/BBlock | Categories/StoreKit/SKProductsRequest+BBlock.h | //
// SKProductsRequest+BBlock.h
// BBlock
//
// Created by <NAME> on 8/7/12.
// Copyright (c) 2012 <NAME>. All rights reserved.
//
#import <StoreKit/StoreKit.h>
@interface SKProductsRequest(BBlock)
typedef void (^SKProductsRequestBBlock)(SKProductsResponse *response, NSError *error);
/// Request a StoreKit response for a set of product identifiers
+ (instancetype)requestWithProductIdentifiers:(NSSet *)productIdentifiers andBlock:(SKProductsRequestBBlock)block;
- (instancetype)initWithProductIdentifiers:(NSSet *)productIdentifiers andBlock:(SKProductsRequestBBlock)block;
@end
|
kgn/BBlock | Categories/StoreKit/SKStoreProductViewController+BBlock.h | <filename>Categories/StoreKit/SKStoreProductViewController+BBlock.h
//
// SKStoreProductViewController+BBlock.h
// BBlock
//
// Created by <NAME> on 5/23/13.
// Copyright (c) 2012 <NAME>. All rights reserved.
//
#import <StoreKit/StoreKit.h>
@interface SKStoreProductViewController(BBlock)
typedef void (^SKStoreProductViewControllerBBlock)(SKStoreProductViewController *productViewController);
+ (id)productViewControllerWithDidFinishBlock:(SKStoreProductViewControllerBBlock)block;
- (id)initWithProductViewControllerWithDidFinishBlock:(SKStoreProductViewControllerBBlock)block;
@end
|
kgn/BBlock | Categories/StoreKit/StoreKit+BBlock.h | <gh_stars>10-100
//
// StoreKit+BBlock.h
// BBlock
//
// Created by <NAME> on 10/20/13.
// Copyright 2013 <NAME>. All rights reserved.
//
#import "SKProductsRequest+BBlock.h"
#import "SKStoreProductViewController+BBlock.h"
|
kgn/BBlock | Categories/UIKit/UITextField+BBlock.h | <reponame>kgn/BBlock
//
// UITextField+BBlock.h
// SignNow
//
// Created by <NAME> on 11/16/12.
// Copyright (c) 2012 SignNow. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITextField(BBlock)
typedef BOOL (^UITextFieldShouldReturnBBlock)(UITextField *textField);
- (void)textFieldShouldReturnWithBlock:(UITextFieldShouldReturnBBlock)block;
@end
|
kgn/BBlock | Categories/UIKit/UIGestureRecognizer+BBlock.h | //
// UIGestureRecognizer+BBlock.h
// BBlock
//
// Created by <NAME> on 12/29/11.
// Copyright (c) 2011-12 <NAME>. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIGestureRecognizer(BBlock)
typedef void (^UIGestureRecognizerBBlock)(id gestureRecognizer);
- (instancetype)initWithBlock:(UIGestureRecognizerBBlock)block;
+ (instancetype)gestureRecognizerWithBlock:(UIGestureRecognizerBBlock)block;
@end
@interface UISwipeGestureRecognizer(BBlock)
- (instancetype)initWithDirection:(UISwipeGestureRecognizerDirection)direction
andBlock:(UIGestureRecognizerBBlock)block;
+ (instancetype)gestureRecognizerWithDirection:(UISwipeGestureRecognizerDirection)direction
andBlock:(UIGestureRecognizerBBlock)block;
@end
|
kgn/BBlock | Categories/UIKit/UIImage+BBlock.h | //
// UIImage+BBlock.h
// BBlock
//
// Created by <NAME> on 3/21/12.
// Copyright (c) 2012 <NAME>. All rights reserved.
//
#import <UIKit/UIKit.h>
// Helper method for creating unique image identifiers
#define BBlockImageIdentifier(fmt, ...) [NSString stringWithFormat:(@"%@%@" fmt), \
NSStringFromClass([self class]), NSStringFromSelector(_cmd), ##__VA_ARGS__]
@interface UIImage(BBlock)
/** Returns a `UIImage` rendered with the drawing code in the block.
This method does not cache the image object. */
+ (UIImage *)imageForSize:(CGSize)size withDrawingBlock:(void(^)())drawingBlock;
+ (UIImage *)imageForSize:(CGSize)size opaque:(BOOL)opaque withDrawingBlock:(void(^)())drawingBlock;
/** Returns a cached `UIImage` rendered with the drawing code in the block.
The `UIImage` is cached in an `NSCache` with the identifier provided. */
+ (UIImage *)imageWithIdentifier:(NSString *)identifier forSize:(CGSize)size andDrawingBlock:(void(^)())drawingBlock;
+ (UIImage *)imageWithIdentifier:(NSString *)identifier opaque:(BOOL)opaque forSize:(CGSize)size andDrawingBlock:(void(^)())drawingBlock;
/** Return the cached image for the identifier, or nil if there is no cached image. */
+ (UIImage *)imageWithIdentifier:(NSString *)identifier;
/** Remove the cached image for the identifier. */
+ (void)removeImageWithIdentifier:(NSString *)identifier;
/** Remove all cached images. */
+ (void)removeAllImages;
@end
|
kgn/BBlock | Categories/UIKit/UIActionSheet+BBlock.h | //
// UIActionSheet+BBlock.h
// BBlock
//
// Created by <NAME> on 6/4/12.
// Copyright (c) 2012 <NAME>. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIActionSheet(BBlock)
typedef void (^UIActionSheetBBlock)(NSInteger buttonIndex, UIActionSheet *actionSheet);
- (void)setCompletionBlock:(UIActionSheetBBlock)block;
- (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle
otherButtonTitle:(NSString *)otherTitle completionBlock:(UIActionSheetBBlock)block;
@end
|
kgn/BBlock | Categories/UIKit/UIControl+BBlock.h | //
// UIControl+BBlock.h
// BBlock
//
// Created by <NAME> on 7/16/12.
// Copyright (c) 2012 <NAME>. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIControl(BBlock)
// WARNING: this category is under developement and is not yet suitable for production
typedef void (^BBlockUIControlBlock)(id control, UIEvent *event);
- (void)addActionForControlEvents:(UIControlEvents)events withBlock:(BBlockUIControlBlock)block;
@end
|
EdisonCat/CCvoltageR | CCVRonVisualStudio/LiquidCrystal.h | <gh_stars>1-10
/*
This is not the real LiquidCrystal.h, it is used to ensure that you can successfully debug on Visual Studio
*/
#ifndef LCD_H
#define LCD_H
#include <iostream>
using namespace std;
class LiquidCrystal {
public:
void print(string message) {
}
void print(float message) {
}
void begin(int length, int width) {
}
void setCursor(int length, int width) {
}
LiquidCrystal(const int pinLCDRS, const int pinLCDE, const int pinLCDD4, const int pinLCDD5, const int pinLCDD6, const int pinLCDD7) {
}
};
#endif |
EdisonCat/CCvoltageR | CCVRonVisualStudio/Arduino.h | /*
This is not the real Arduino.h, it is used to ensure that you can successfully debug on Visual Studio
*/
#ifndef ARDUINO_H
#define ARDUINO_H
void pinMode(int, bool);
void digitalWrite(int, bool);
int digitalRead(int);
int analogRead(int);
void analogWrite(int, bool);
void delay(int);
void delayMicroseconds(int);
float map(int,int,int,int,int);
void setup();
void loop();
const bool INPUT = false;
const bool OUTPUT = true;
const bool HIGH = true;
const bool LOW = false;
const int A5 = 500;
#endif |
EdisonCat/CCvoltageR | CCVRonVisualStudio/chopping-controlled_voltage_regulator.h | <gh_stars>1-10
#ifndef CHOPPING_CONTROLLED_VOLTAGE_REGULATOR_H
#define CHOPPING_CONTROLLED_VOLTAGE_REGULATOR_H
#include "LiquidCrystal.h"
#include "Arduino.h"
/*
Set up your pins here
*/
const int pinFlag = 10;
const int pinCurrentV = A5;
const int pinSwitch1 = 8;
const int pinSwitch2 = 9;
const int pinLCDVCC = 1;
const int pinLCDGND = 1;
const int pinLCDE = 11;
const int pinLCDRS = 12;
const int pinLCDRW = 1;
const int pinLCDD7 = 2;
const int pinLCDD6 = 3;
const int pinLCDD5 = 4;
const int pinLCDD4 = 5;
const int btnNone = 5;
const int btnSelect = 4;
const int btnLeft = 3;
const int btnDown = 2;
const int btnUp = 1;
const int btnRight = 0;
class Voltage {
public:
bool flag = false;
int flagValue;
int openTime = 1000;//time for method delay() or delayMicroseconds()
float voltage_set;
float voltage_current;
int button;
int lcd_key;
LiquidCrystal *lcd = new LiquidCrystal(pinLCDRS, pinLCDE, pinLCDD4, pinLCDD5, pinLCDD6, pinLCDD7);
Voltage() {
voltage_set = 15;
voltage_current = remap(analogRead(pinCurrentV), 1023, 5);
}
/*
Check if voltage_current equals to voltage_set
*/
inline int checkVoltage() {
if (Serial.available()) {
this->voltage_set = Serial.readString().toFloat();
Serial.println(this->voltage_set);
}
this->voltage_current = remap(analogRead(pinCurrentV), 1023, 220);
if (this->voltage_current > this->voltage_set) {
this->openTime--;
return this->openTime;
}
else if (this->voltage_current < this->voltage_set) {
this->openTime++;
return this->openTime;
}
else
return this->openTime;
}
/*
Switch mosfet on or off
*/
inline void switchOn(int openTime) {
if (openTime > 2000) {
this->openTime = 2000;
digitalWrite(pinSwitch1, HIGH);
delayMicroseconds(this->openTime);
}
else if (openTime < 0) {
this->openTime = 0;
digitalWrite(pinSwitch1, LOW);
delayMicroseconds(2000);
}
else {
digitalWrite(pinSwitch1, HIGH);
delayMicroseconds(this->openTime);
digitalWrite(pinSwitch1, LOW);
delayMicroseconds(2000 - this->openTime);
}
}
/*
To check if the buttons are pressed.
voltage_set varies according to the status of the buttons
*/
inline int readButton() {
button = analogRead(0);
if (button > 1000) return btnNone;
if (button < 50) return btnNone;
if (button < 250) return btnUp;
if (button < 350) return btnDown;
if (button < 650) return btnNone;
if (button < 850) return btnNone;
return btnNone;
}
inline void checkAndPrint(float voltage_set, float voltage_current) {
lcd->setCursor(5, 0);
this->lcd_key = this->readButton();
if (this->lcd_key != btnNone) {
this->flag = false;
}
switch (this->lcd_key) {
case btnUp: {
this->voltage_set += 0.1;
if (this->voltage_set > 220) {
lcd->setCursor(5, 0);
lcd->print(" ");
lcd->setCursor(5, 0);
lcd->print("limit");
this->voltage_set -= 0.1;
delay(500);
lcd->setCursor(5, 0);
lcd->print(" ");
lcd->setCursor(5, 0);
lcd->print(this->voltage_set);
}
else {
lcd->print(this->voltage_set);
delay(100);
}
break;
}
case btnDown: {
this->voltage_set -= 0.1;
if (this->voltage_set <0) {
lcd->print("limit");
this->voltage_set = 0;
delay(500);
lcd->setCursor(5, 0);
lcd->print(" ");
lcd->setCursor(5, 0);
lcd->print(this->voltage_set);
}
else {
lcd->print(this->voltage_set);
delay(100);
}
break;
}
case btnNone: {
if (!this->flag) {
Serial.println(this->voltage_set);
this->flag = true;
}
else {
}
break;
}
}
this->voltage_current = remap(analogRead(pinCurrentV), 1023, 220);
lcd->setCursor(9, 1);
lcd->print(this->voltage_current);
}
inline float remap(float analogData, float originalMax, float afterMax) {
float result = (analogData * afterMax / originalMax);
return result;
}
inline void startRegulating() {
this->checkAndPrint(this->voltage_set, this->voltage_current);//Check the status of the buttons and return current voltage_set
this->switchOn(this->checkVoltage());//Switch the mosfet on according to the return value of method checkVoltage()
}
inline void initiateLCD() {
this->lcd->begin(16, 2);
this->lcd->setCursor(0, 0);
this->lcd->print("Set: ");
this->lcd->print(this->voltage_set);
this->lcd->setCursor(0, 1);
this->lcd->print("Current: ");
this->lcd->print(this->voltage_current);
}
};
#endif |
aleks-dimoski/ASCII_graphics | ASCII_graphics/imageLoader.h | <reponame>aleks-dimoski/ASCII_graphics<filename>ASCII_graphics/imageLoader.h
#pragma once
#include <string>
using namespace std;
class imageLoader
{
private:
public:
imageLoader();
int getImage(string filepath);
int sendImage(string filepath);
}; |
aleks-dimoski/ASCII_graphics | ASCII_graphics/ASCII_graphics.h | #pragma once
class ASCII_graphics
{
private:
public:
ASCII_graphics();
int run();
char determineChar(double);
}; |
JHG777000/builder | examples/example5/foo2/include/foo2.h | <filename>examples/example5/foo2/include/foo2.h
void foo2( void ) ; |
JHG777000/builder | examples/example5/foo2/src/foo2.c | <filename>examples/example5/foo2/src/foo2.c
#include <stdio.h>
#include <stdlib.h>
#include <foo2.h>
void foo2( void ) {
printf("Hello World!!!!, from foo2.\n") ;
} |
wu0607/2020-Spring-ME759-FinalProject | cpu/md5.h | #include <cstring>
#include <iostream>
class MD5 {
public:
MD5(const std::string& text);
void pipeline(const unsigned char *buf, int length);
void pipeline(const char *buf, int length);
std::string hex2String() const;
private:
void processBlock(const unsigned char block[64]);
static void padding(unsigned int output[], const unsigned char input[], int len);
static void encode(unsigned char output[], const unsigned int input[], int len);
bool done;
unsigned int count[2]; // 64bit counter for number of bits (lo, hi)
unsigned int state[4]; // digest so far
unsigned char digest[16]; // the result
unsigned char buffer[64]; // bytes that didn't fit in last 64 byte chunk
};
// helper function
std::string md5(const std::string str);
|
wu0607/2020-Spring-ME759-FinalProject | cpu/util.h | <reponame>wu0607/2020-Spring-ME759-FinalProject<filename>cpu/util.h
#include <iostream>
#include <vector>
#define PASSWORD_LEN 5
#define CONST_CHARSET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
#define CONST_CHARSET_LENGTH (sizeof(CONST_CHARSET) - 1)
using namespace std;
// variable
extern vector<char> alphabet;
// function
void showHelper();
string customToString(long long val);
|
SXDgit/ZBTools | ZBTools/Classes/ZBTestModule/ZBTest.h | <gh_stars>0
//
// ZBTest.h
// ZBTest
//
// Created by Zuobian on 2019/9/14.
// Copyright © 2019 admin. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZBTest : NSObject
- (NSString *)getTestString;
@end
|
SXDgit/ZBTools | Example/Pods/Target Support Files/ZBTools/ZBTools-umbrella.h | #ifdef __OBJC__
#import <UIKit/UIKit.h>
#else
#ifndef FOUNDATION_EXPORT
#if defined(__cplusplus)
#define FOUNDATION_EXPORT extern "C"
#else
#define FOUNDATION_EXPORT extern
#endif
#endif
#endif
#import "Manager.h"
#import "ZBTest.h"
FOUNDATION_EXPORT double ZBToolsVersionNumber;
FOUNDATION_EXPORT const unsigned char ZBToolsVersionString[];
|
SXDgit/ZBTools | Example/ZBTools/ZBAppDelegate.h | //
// ZBAppDelegate.h
// ZBTools
//
// Created by SXDgit on 07/02/2020.
// Copyright (c) 2020 SXDgit. All rights reserved.
//
@import UIKit;
@interface ZBAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
|
SXDgit/ZBTools | Example/ZBTools/ZBViewController.h | <filename>Example/ZBTools/ZBViewController.h<gh_stars>0
//
// ZBViewController.h
// ZBTools
//
// Created by SXDgit on 07/02/2020.
// Copyright (c) 2020 SXDgit. All rights reserved.
//
@import UIKit;
@interface ZBViewController : UIViewController
@end
|
sh1r4s3/mic | src/microcode.h | #include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "misc.h"
#include "mir.h"
struct microcode
{
struct mir *cmd;
int n_cmds;
};
struct microcode *microcode_create(int n_cmds);
void microcode_free(struct microcode *mcode);
struct microcode *microcode_load(const char *fname);
struct mir *process_cmd(struct microcode *mcode, struct mir *cmd);
|
sh1r4s3/mic | src/shift.h | <gh_stars>0
void shift_sll8(char *line, int size);
void shift_sra1(char *line, int size);
|
sh1r4s3/mic | src/alu.c | #include "alu.h"
struct ALU *alu_create(unsigned short units)
{
struct ALU *alu = (struct ALU *)malloc(sizeof(struct ALU));
if (!alu)
ERR("Can't allocate memory for ALU");
memset(alu, 0, sizeof(struct ALU));
alu->n_units = units;
alu->unit = (struct ALU_unit *)calloc(units, sizeof(struct ALU_unit));
if (!alu->unit)
ERR("Can't allocate memory for ALU units");
memset(alu->unit, 0, sizeof(struct ALU_unit)*units);
return alu;
}
void alu_free(struct ALU *alu)
{
free(alu->unit);
free(alu);
}
int alu_process(struct ALU *alu)
{
// Basic sanity check
if (!alu || !alu->unit)
return -1;
/*
* Let us try to simulate ALU as close to the circuit as possible.
* It is not an optimal way of course, but, it is WICKED FUN! Isn't it?
* operator(7) could make some help!
*/
char inva = alu->inva & 0x1;
char ena = alu->ena & 0x1;
char enb = alu->enb & 0x1;
char f0 = alu->f0 & 0x1;
char f1 = alu->f1 & 0x1;
char inc = alu->inc & 0x1;
for (int i = 0; i < alu->n_units; ++i)
{
// Decoder
char decoder = ( !f0 & !f1 |
!f0 & f1 << 1 |
f0 & !f1 << 2 |
f0 & f1 << 3 );
// Logic
char a = alu->unit[i].a & 0x1;
char b = alu->unit[i].b & 0x1;
char logic_in = a & ena ^ inva | b & enb << 1;
char logic_out = ( logic_in & 0x1 & (logic_in & 0x2 >> 1) & decoder & 0x1 |
(logic_in & 0x1 | logic_in & 0x2 >> 1) & (decoder & 0x2 >> 1) |
!(logic_in & 0x2 >> 1) & (decoder & 0x4 >> 2) );
// Full adder
char carry_in = i != 0 ? alu->unit[i - 1].carry_out & 0x1 : inc;
char carry_out = ( logic_in & 0x1 & (logic_in & 0x2 >> 1) & (decoder & 0x8 >> 3) |
carry_in & (logic_in & 0x1 ^ (logic_in & 0x2 >> 1)) & (decoder & 0x8 >> 3) );
char sum = (logic_in & 0x1 ^ (logic_in & 0x2 >> 1) ^ carry_in) & (decoder & 0x8 >> 3);
alu->unit[i].carry_out = carry_out;
alu->unit[i].output = sum;
}
return 0;
}
char alu_overflow(struct ALU *alu)
{
// Basic sanity check
if (!alu || !alu->unit)
return -1;
return alu->unit[alu->n_units - 1].carry_out;
}
void alu_set_line(struct ALU *alu, char ab, char *line)
{
// Basic sanity check
if (!alu || !alu->unit || !line)
return;
for (int i = 0; i < alu->n_units; ++i)
{
switch (ab)
{
case 'a':
alu->unit[i].a = line[i];
break;
case 'b':
alu->unit[i].b = line[i];
break;
}
}
}
void alu_get_line(struct ALU *alu, char *line)
{
// Basic sanity check
if (!alu || !alu->unit || !line)
return;
for (int i = 0; i < alu->n_units; ++i)
{
line[i] = alu->unit[i].output;
}
}
|
sh1r4s3/mic | src/shift.c | #include "shift.h"
void shift_sll8(char *line, int size)
{
if (!line)
return;
if (size > 8)
{
for (int i = size - 9; i >= 0; --i)
{
line[i + 8] = line[i];
line[i] = 0;
}
}
else
{
for (int i = 0; i < size; ++i)
{
line[i] = 0;
}
}
}
void shift_sra1(char *line, int size)
{
if (!line)
return;
for (int i = 0; i < size - 1; ++i)
{
line[i] = line[i + 1];
}
}
|
sh1r4s3/mic | src/register.h | #include <stdlib.h>
#include <string.h>
#include "misc.h"
struct reg
{
char *data;
const char *name;
int bits;
};
struct reg *reg_create(int bits, const char *name);
void reg_free(struct reg *r);
|
sh1r4s3/mic | src/alu.h | <reponame>sh1r4s3/mic<filename>src/alu.h
#include <stdlib.h>
#include <string.h>
#include "misc.h"
/*
* This structures describes a state of a simple ALU module.
* docs/ALU.md describes the circuit.
*/
struct ALU_unit {
// Input lines
char a, b;
// Output lines
char output;
char carry_out;
};
struct ALU {
// Input parallel lines
char inva;
char ena, enb;
char f0, f1;
char inc;
// ALU units description
struct ALU_unit *unit;
unsigned short n_units;
};
struct ALU *alu_create(unsigned short units);
void alu_free(struct ALU *alu);
int alu_process(struct ALU *alu);
// Helpers
char alu_overflow(struct ALU *alu);
void alu_set_line(struct ALU *alu, char ab, char *line);
void alu_get_line(struct ALU *alu, char *line);
|
sh1r4s3/mic | src/memory.c | <reponame>sh1r4s3/mic
#include "memory.h"
struct memory *memory_create(int words)
{
struct memory *m = (struct memory *)malloc(sizeof(struct memory));
if (!m)
ERR("Can't allocate memory for struct memory");
m->data = (int8_t *)calloc(words, sizeof(int32_t));
if (!m->data)
ERR("Can't allocate %d words (32 bit) for memory", words);
m->addr = 0;
m->in = 0;
m->size = words;
}
void memory_free(struct memory *m)
{
free(m->data);
free(m);
}
|
sh1r4s3/mic | src/microcode.c | #include "microcode.h"
struct microcode *microcode_create(int n_cmds)
{
struct microcode *mcode = (struct microcode *)malloc(sizeof(struct microcode));
if (!mcode)
ERR("Can't allocate memory for microcode");
mcode->cmd = (struct mir *)calloc(n_cmds, sizeof(struct mir));
if (!mcode->cmd)
ERR("Can't allocate memory for %d commands in microcode", n_cmds);
mcode->n_cmds = n_cmds;
return mcode;
}
void microcode_free(struct microcode *mcode)
{
free(mcode->cmd);
free(mcode);
}
struct microcode *microcode_load(const char *fname)
{
struct stat s;
int fd = open(fname, O_RDONLY);
if (fd < 0)
ERR("Can't open file %s / errno=%d / %s", fname, errno, strerror(errno));
if (fstat(fd, &s) < 0)
ERR("Can't get size of the file %s / errno=%d / %s", fname, errno, strerror(errno));
// Basic check
if (s.st_size % sizeof(struct mir))
ERR("It is seems that the file is corrupted");
int cmds = s.st_size/sizeof(struct mir);
struct microcode *mcode = microcode_create(cmds);
ssize_t sz = read(fd, mcode, sizeof(struct mir)*cmds);
if (sz != sizeof(struct mir)*cmds)
ERR("Can't read microcode from the file (read %li)", sz);
close(fd);
}
struct mir *process_cmd(struct microcode *mcode, struct mir *cmd)
{
return mcode->cmd + cmd->next_address/sizeof(struct mir);
}
|
sh1r4s3/mic | src/register.c | #include "register.h"
struct reg *reg_create(int bits, const char *name)
{
struct reg *r = (struct reg *)malloc(sizeof(struct reg));
if (!r)
ERR("Can't allocate memory for struct reg %s", name);
r->data = (char *)calloc(bits, sizeof(char));
if (!r->data)
ERR("Can't allocate memory for reg %s", name);
memset(r->data, 0, sizeof(char)*bits);
r->name = name;
r->bits = bits;
return r;
}
void reg_free(struct reg *r)
{
free(r->data);
free(r);
}
|
sh1r4s3/mic | src/misc.h | <gh_stars>0
#include <stdio.h>
// Emit log message
#define ERR(format, ...) \
{ \
fprintf(stderr, __FILE__ ":%d / " format "\n", __LINE__, ##__VA_ARGS__); \
exit(-1); \
}
|
sh1r4s3/mic | src/memory.h | <filename>src/memory.h
#include <stdlib.h>
#include <string.h>
#include "misc.h"
struct memory
{
char in; // rd, wr, fetch
int addr;
int size;
int8_t *data;
};
enum memory_instruction {rd = 0, wr = 1, fetch = 2};
struct memory *memory_create(int words);
void memory_free(struct memory *m);
int memory_get_word();
int memory_set_word();
char memory_get_byte();
|
sh1r4s3/mic | src/mir.h | <filename>src/mir.h
struct mir
{
int next_address;
char jam;
char alu;
int c;
char mem;
int b;
};
|
scoder/acora | acora/acora_defs.h | #ifndef HAS_ACORA_DEFS_H
#define HAS_ACORA_DEFS_H
#if PY_VERSION_HEX <= 0x03030000 && !(defined(CYTHON_PEP393_ENABLED) && CYTHON_PEP393_ENABLED)
#define PyUnicode_IS_READY(op) (0)
#define PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u)
#define PyUnicode_KIND(u) (sizeof(Py_UNICODE))
#define PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u))
#define PyUnicode_WCHAR_KIND 0
#define PyUnicode_READ(kind, data, index) \
(((void)kind), (Py_UCS4) ((Py_UNICODE*)data)[index])
#endif
#endif /* HAS_ACORA_DEFS_H */
|
jlmonge/cs153-xv6 | l2-inher.c | <filename>l2-inher.c
#include "types.h"
#include "stat.h"
#include "user.h"
#include "stddef.h"
int
main(int argc, char *argv[])
{
if (argc != 2) {
printf(1, "Usage: l2-inher <priority>\n");
}
else {
int pid = getpid();
int priority = atoi(argv[1]);
int status;
printf(1, "~~~ BEFORE ~~~\n");
ps();
printf(1, "~~~ AFTER ~~~\n");
modpr(pid, priority);
pid = fork();
if (pid < 0) {
printf(1, "ERROR: FORK");
exitWithStatus(-1);
}
if (pid == 0) {;
printf(1, "child %d has a priority of %d (expected %d)\n", getpid(), getpr(), priority);
exitWithStatus(0);
}
else {
printf(1,"parent %d of child %d has a priority of %d (expected %d)\n", getpid(), pid, getpr(), priority);
pid = wait(&status);
printf(1, "child %d done, parent %d exiting\n", pid, getpid());
}
}
exit();
return 0;
} |
Subsets and Splits