📄 ul_l_drv_eth.c
字号:
/******************************************************************* uLan Communication - basic library ul_l_drv_eth.c - ethernet driver interface (C) Copyright 1996,1999 by Pavel Pisa The uLan driver is distributed under the Gnu General Public Licence. See file COPYING for details. *******************************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#ifndef _WIN32#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#else#include <ws2tcpip.h>#endif#include <ul_lib/ulan.h>#ifdef _WIN32 typedef SOCKET ul_fd_sock_t;#else /*_WIN32*/ typedef int ul_fd_sock_t;#endif /*_WIN32*/#define UL_ETH_BUFF_DEFAULT_SIZE 8192char ul_eth_buff[UL_ETH_BUFF_DEFAULT_SIZE];int ul_eth_buf_ptr;void ul_eth_create_header(void) { ul_eth_buff[0]='U'; ul_eth_buff[1]='L'; ul_eth_buff[2]='A'; ul_eth_buff[3]='N'; ul_eth_buff[4]=0; ul_eth_buff[5]=1; ul_eth_buff[6]=1; ul_eth_buff[7]=0; ul_eth_buf_ptr=8;}typedef struct ul_fd_eth_context_t { ul_fd_context_t context; ul_fd_sock_t sock_fd; uint32_t ipaddress; uint16_t port;} ul_fd_eth_context_t;static inlineul_fd_eth_context_t *ul_fd2eth_context(ul_fd_t ul_fd){ return UL_CONTAINEROF(ul_fd, ul_fd_eth_context_t, context);}static inlineul_fd_sock_t ulop_eth_fd2sock_fd(ul_fd_t ul_fd){ if(ul_fd==UL_FD_INVALID) return UL_FD_DIRECT_INVALID; else return ul_fd2eth_context(ul_fd)->sock_fd;}ul_fd_direct_t ulop_eth_fd2sys_fd(ul_fd_t ul_fd){ /* * This is not fully correct on Windows, there does not exist handle * type usable for both files and sockets */ return ulop_eth_fd2sock_fd(ul_fd);}ul_fd_sock_t ul_eth_init(uint32_t net_ipaddr,uint16_t net_port) { struct sockaddr_in name; ul_fd_sock_t fd; #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; #endif #ifdef _WIN32 wVersionRequested = MAKEWORD(2, 0); WSAStartup(wVersionRequested, &wsaData); #endif fd = socket(AF_INET, SOCK_STREAM, 0); name.sin_family=AF_INET; name.sin_addr.s_addr=net_ipaddr; name.sin_port=net_port; if (connect(fd, (struct sockaddr *) &name, sizeof(name)) < 0) { close(fd); return 0; } return fd;}ul_fd_ops_t ul_fd_eth_ops;static inline ul_fd_t ul_fd_direct2ul_fd(ul_fd_sock_t fd){ ul_fd_eth_context_t *eth_fd; eth_fd=malloc(sizeof(*eth_fd)); if(!eth_fd) return UL_FD_INVALID; memset(eth_fd,0,sizeof(*eth_fd)); eth_fd->sock_fd=fd; eth_fd->context.fd_ops=&ul_fd_eth_ops; return ð_fd->context;}ul_fd_t ul_eth_open(const char *dev_name, const char *options){ ul_fd_t r; char buff[20],*d; uint32_t eth_ipaddr; uint16_t eth_port; if(dev_name==NULL) dev_name=UL_DEV_NAME; else if(!strncmp(dev_name,"eth:",4)) dev_name+=4; d=strpbrk(dev_name,":"); if (!d) return UL_FD_INVALID; strncpy(buff,dev_name,d-dev_name); eth_ipaddr=inet_addr(buff); dev_name=d+1; d=strpbrk(dev_name,":"); if (!d) return UL_FD_INVALID; strncpy(buff,dev_name,d-dev_name); eth_port=htons(strtol(buff,NULL,0)); dev_name=d+1; r=ul_fd_direct2ul_fd(ul_eth_init(eth_ipaddr,eth_port)); if (r==UL_FD_INVALID) return UL_FD_INVALID; return r;}int ul_eth_close(ul_fd_t ul_fd){ ul_fd_eth_context_t *eth_fd; if(ul_fd==UL_FD_INVALID) return -1; eth_fd=ul_fd2eth_context(ul_fd); /*close connection there*/ free(eth_fd); return 0;}int ul_eth_drv_version(ul_fd_t ul_fd){ return 0;}ssize_t ul_eth_read(ul_fd_t ul_fd, void *buffer, size_t size){ return 0;}ssize_t ul_eth_write(ul_fd_t ul_fd, const void *buffer, size_t size){ send(ulop_eth_fd2sock_fd(ul_fd), NULL, 0, 0); return 0;}int ul_eth_newmsg(ul_fd_t ul_fd,const ul_msginfo *msginfo){ return 0;}int ul_eth_tailmsg(ul_fd_t ul_fd,const ul_msginfo *msginfo){ return 0;}int ul_eth_freemsg(ul_fd_t ul_fd){ return 0;}int ul_eth_acceptmsg(ul_fd_t ul_fd,ul_msginfo *msginfo){ return 0;}int ul_eth_actailmsg(ul_fd_t ul_fd,ul_msginfo *msginfo){ return 0;}int ul_eth_addfilt(ul_fd_t ul_fd,const ul_msginfo *msginfo){ return 0;}int ul_eth_abortmsg(ul_fd_t ul_fd){ return 0;}int ul_eth_rewmsg(ul_fd_t ul_fd){ return 0;}int ul_eth_inepoll(ul_fd_t ul_fd){ return 0;}int ul_eth_debflg(ul_fd_t ul_fd,int debug_msk){ return 0;}int ul_eth_fd_wait(ul_fd_t ul_fd, int wait_sec){ return 0;}int ul_eth_namematch(const char *dev_name){ int ret=0; if(!dev_name) ret=1; else if(!strncmp(dev_name,"eth:",4)) ret=100; /* 100% name match for driver */ else if(!strchr(dev_name,':')) ret=5; /* no transport selected, direct driver is reasonable for this name */ return ret;}ul_fd_ops_t ul_fd_eth_ops={ "eth", ul_eth_namematch, ul_eth_open, ul_eth_close, ul_eth_drv_version, ul_eth_read, ul_eth_write, ul_eth_newmsg, ul_eth_tailmsg, ul_eth_freemsg, ul_eth_acceptmsg, ul_eth_actailmsg, ul_eth_addfilt, ul_eth_abortmsg, ul_eth_rewmsg, ul_eth_inepoll, ul_eth_debflg, ul_eth_fd_wait, ulop_eth_fd2sys_fd};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -