sal_socket.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-23 ChenYong First version
  9. * 2018-11-12 ChenYong Add TLS support
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <string.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <sal_socket.h>
  19. #include <sal_netdb.h>
  20. #ifdef SAL_USING_TLS
  21. #include <sal_tls.h>
  22. #endif
  23. #include <sal_low_lvl.h>
  24. #include <netdev.h>
  25. #ifdef SAL_INTERNET_CHECK
  26. #include <ipc/workqueue.h>
  27. #endif
  28. #ifdef RT_USING_LWP
  29. #include <lwp_sys_socket.h>
  30. #endif
  31. #if defined(RT_USING_DFS_V2) && defined(SAL_USING_AF_UNIX)
  32. #include <af_unix.h>
  33. #endif
  34. /* check system workqueue stack size */
  35. #if defined(SAL_INTERNET_CHECK) && RT_SYSTEM_WORKQUEUE_STACKSIZE < 1536
  36. #error "The system workqueue stack size must more than 1536 bytes"
  37. #endif
  38. #define DBG_TAG "sal.skt"
  39. #define DBG_LVL DBG_INFO
  40. #include <rtdbg.h>
  41. #define SOCKET_TABLE_STEP_LEN 4
  42. /* the socket table used to dynamic allocate sockets */
  43. struct sal_socket_table
  44. {
  45. uint32_t max_socket;
  46. struct sal_socket **sockets;
  47. };
  48. /* record the netdev and res table*/
  49. struct sal_netdev_res_table
  50. {
  51. struct addrinfo *res;
  52. struct netdev *netdev;
  53. };
  54. struct ifconf
  55. {
  56. int ifc_len; /* Size of buffer. */
  57. union
  58. {
  59. char* ifcu_buf;
  60. struct sal_ifreq *ifcu_req;
  61. } ifc_ifcu;
  62. };
  63. #ifdef SAL_USING_TLS
  64. /* The global TLS protocol options */
  65. static struct sal_proto_tls *proto_tls;
  66. #endif
  67. /* The global socket table */
  68. static struct sal_socket_table socket_table;
  69. static struct rt_mutex sal_core_lock;
  70. static rt_bool_t init_ok = RT_FALSE;
  71. static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM];
  72. #define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \
  73. ((sock)->protocol == PROTOCOL_DTLS))
  74. #define SAL_SOCKOPS_PROTO_TLS_VALID(sock, name) (proto_tls && (proto_tls->ops->name) && IS_SOCKET_PROTO_TLS(sock))
  75. #define SAL_SOCKOPT_PROTO_TLS_EXEC(sock, name, optval, optlen) \
  76. do { \
  77. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)){ \
  78. return proto_tls->ops->name((sock)->user_data_tls, (optval), (optlen)); \
  79. } \
  80. }while(0) \
  81. #define SAL_SOCKET_OBJ_GET(sock, socket) \
  82. do { \
  83. (sock) = sal_get_socket(socket); \
  84. if ((sock) == RT_NULL) { \
  85. return -1; \
  86. } \
  87. }while(0) \
  88. #define SAL_NETDEV_IS_UP(netdev) \
  89. do { \
  90. if (!netdev_is_up(netdev)) { \
  91. return -1; \
  92. } \
  93. }while(0) \
  94. #define SAL_NETDEV_SOCKETOPS_VALID(netdev, pf, ops) \
  95. do { \
  96. (pf) = (struct sal_proto_family *) netdev->sal_user_data; \
  97. if ((pf)->skt_ops->ops == RT_NULL){ \
  98. return -1; \
  99. } \
  100. }while(0) \
  101. #define SAL_NETDEV_NETDBOPS_VALID(netdev, pf, ops) \
  102. ((netdev) && netdev_is_up(netdev) && \
  103. ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
  104. (pf)->netdb_ops->ops) \
  105. #define SAL_NETDBOPS_VALID(netdev, pf, ops) \
  106. ((netdev) && \
  107. ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \
  108. (pf)->netdb_ops->ops) \
  109. /**
  110. * SAL (Socket Abstraction Layer) initialize.
  111. *
  112. * @return result 0: initialize success
  113. * -1: initialize failed
  114. */
  115. int sal_init(void)
  116. {
  117. int cn;
  118. if (init_ok)
  119. {
  120. LOG_D("Socket Abstraction Layer is already initialized.");
  121. return 0;
  122. }
  123. /* init sal socket table */
  124. cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
  125. socket_table.max_socket = cn;
  126. socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
  127. if (socket_table.sockets == RT_NULL)
  128. {
  129. LOG_E("No memory for socket table.\n");
  130. return -1;
  131. }
  132. /*init the dev_res table */
  133. rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl));
  134. /* create sal socket lock */
  135. rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_PRIO);
  136. LOG_I("Socket Abstraction Layer initialize success.");
  137. init_ok = RT_TRUE;
  138. return 0;
  139. }
  140. INIT_COMPONENT_EXPORT(sal_init);
  141. #ifdef SAL_INTERNET_CHECK
  142. /* check SAL network interface device internet status */
  143. static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
  144. {
  145. #define SAL_INTERNET_VERSION 0x00
  146. #define SAL_INTERNET_BUFF_LEN 12
  147. #define SAL_INTERNET_TIMEOUT (2)
  148. #define SAL_INTERNET_HOST "link.rt-thread.org"
  149. #define SAL_INTERNET_PORT 8101
  150. #define SAL_INTERNET_MONTH_LEN 4
  151. #define SAL_INTERNET_DATE_LEN 16
  152. unsigned int index;
  153. int sockfd = -1, result = 0;
  154. struct sockaddr_in server_addr;
  155. struct hostent *host;
  156. struct timeval timeout;
  157. struct netdev *netdev = (struct netdev *)work_data;
  158. socklen_t addr_len = sizeof(struct sockaddr_in);
  159. char send_data[SAL_INTERNET_BUFF_LEN], recv_data = 0;
  160. const char month[][SAL_INTERNET_MONTH_LEN] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  161. char date[SAL_INTERNET_DATE_LEN];
  162. unsigned int moth_num = 0;
  163. struct sal_proto_family *pf = (struct sal_proto_family *) netdev->sal_user_data;
  164. const struct sal_socket_ops *skt_ops;
  165. if (work)
  166. {
  167. rt_free(work);
  168. }
  169. /* get network interface socket operations */
  170. if (pf == RT_NULL || pf->skt_ops == RT_NULL)
  171. {
  172. result = -RT_ERROR;
  173. goto __exit;
  174. }
  175. host = (struct hostent *) pf->netdb_ops->gethostbyname(SAL_INTERNET_HOST);
  176. if (host == RT_NULL)
  177. {
  178. result = -RT_ERROR;
  179. goto __exit;
  180. }
  181. skt_ops = pf->skt_ops;
  182. if ((sockfd = skt_ops->socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  183. {
  184. result = -RT_ERROR;
  185. goto __exit;
  186. }
  187. server_addr.sin_family = AF_INET;
  188. server_addr.sin_port = htons(SAL_INTERNET_PORT);
  189. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  190. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  191. timeout.tv_sec = SAL_INTERNET_TIMEOUT;
  192. timeout.tv_usec = 0;
  193. /* set receive and send timeout */
  194. skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout, sizeof(timeout));
  195. skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout, sizeof(timeout));
  196. /* get build moth value*/
  197. rt_memset(date, 0x00, SAL_INTERNET_DATE_LEN);
  198. rt_snprintf(date, SAL_INTERNET_DATE_LEN, "%s", __DATE__);
  199. for (index = 0; index < sizeof(month) / SAL_INTERNET_MONTH_LEN; index++)
  200. {
  201. if (rt_memcmp(date, month[index], SAL_INTERNET_MONTH_LEN - 1) == 0)
  202. {
  203. moth_num = index + 1;
  204. break;
  205. }
  206. }
  207. /* not find build month */
  208. if (moth_num == 0 || moth_num > sizeof(month) / SAL_INTERNET_MONTH_LEN)
  209. {
  210. result = -RT_ERROR;
  211. goto __exit;
  212. }
  213. rt_memset(send_data, 0x00, SAL_INTERNET_BUFF_LEN);
  214. send_data[0] = SAL_INTERNET_VERSION;
  215. for (index = 0; index < netdev->hwaddr_len; index++)
  216. {
  217. send_data[index + 1] = netdev->hwaddr[index] + moth_num;
  218. }
  219. send_data[9] = RT_VERSION_MAJOR;
  220. send_data[10] = RT_VERSION_MINOR;
  221. send_data[11] = RT_VERSION_PATCH;
  222. skt_ops->sendto(sockfd, send_data, SAL_INTERNET_BUFF_LEN, 0,
  223. (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
  224. result = skt_ops->recvfrom(sockfd, &recv_data, sizeof(recv_data), 0, (struct sockaddr *)&server_addr, &addr_len);
  225. if (result < 0)
  226. {
  227. goto __exit;
  228. }
  229. if (recv_data == RT_FALSE)
  230. {
  231. result = -RT_ERROR;
  232. goto __exit;
  233. }
  234. __exit:
  235. if (result > 0)
  236. {
  237. LOG_D("Set network interface device(%s) internet status up.", netdev->name);
  238. netdev_low_level_set_internet_status(netdev, RT_TRUE);
  239. }
  240. else
  241. {
  242. LOG_D("Set network interface device(%s) internet status down.", netdev->name);
  243. netdev_low_level_set_internet_status(netdev, RT_FALSE);
  244. }
  245. if (sockfd >= 0)
  246. {
  247. skt_ops->closesocket(sockfd);
  248. }
  249. }
  250. #endif /* SAL_INTERNET_CHECK */
  251. /**
  252. * This function will check SAL network interface device internet status.
  253. *
  254. * @param netdev the network interface device to check
  255. */
  256. int sal_check_netdev_internet_up(struct netdev *netdev)
  257. {
  258. RT_ASSERT(netdev);
  259. #ifdef SAL_INTERNET_CHECK
  260. /* workqueue for network connect */
  261. struct rt_work *net_work = RT_NULL;
  262. net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
  263. if (net_work == RT_NULL)
  264. {
  265. LOG_W("No memory for network interface device(%s) delay work.", netdev->name);
  266. return -1;
  267. }
  268. rt_work_init(net_work, check_netdev_internet_up_work, (void *)netdev);
  269. rt_work_submit(net_work, RT_TICK_PER_SECOND);
  270. #endif /* SAL_INTERNET_CHECK */
  271. return 0;
  272. }
  273. /**
  274. * This function will register TLS protocol to the global TLS protocol.
  275. *
  276. * @param pt TLS protocol object
  277. *
  278. * @return 0: TLS protocol object register success
  279. */
  280. #ifdef SAL_USING_TLS
  281. int sal_proto_tls_register(const struct sal_proto_tls *pt)
  282. {
  283. RT_ASSERT(pt);
  284. proto_tls = (struct sal_proto_tls *) pt;
  285. return 0;
  286. }
  287. #endif
  288. /**
  289. * This function will get sal socket object by sal socket descriptor.
  290. *
  291. * @param socket sal socket index
  292. *
  293. * @return sal socket object of the current sal socket index
  294. */
  295. struct sal_socket *sal_get_socket(int socket)
  296. {
  297. struct sal_socket_table *st = &socket_table;
  298. socket = socket - SAL_SOCKET_OFFSET;
  299. if (socket < 0 || socket >= (int) st->max_socket)
  300. {
  301. return RT_NULL;
  302. }
  303. /* check socket structure valid or not */
  304. RT_ASSERT(st->sockets[socket]->magic == SAL_SOCKET_MAGIC);
  305. return st->sockets[socket];
  306. }
  307. /**
  308. * This function will lock sal socket.
  309. *
  310. * @note please don't invoke it on ISR.
  311. */
  312. static void sal_lock(void)
  313. {
  314. rt_err_t result;
  315. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  316. if (result != RT_EOK)
  317. {
  318. RT_ASSERT(0);
  319. }
  320. }
  321. /**
  322. * This function will lock sal socket.
  323. *
  324. * @note please don't invoke it on ISR.
  325. */
  326. static void sal_unlock(void)
  327. {
  328. rt_mutex_release(&sal_core_lock);
  329. }
  330. /**
  331. * This function will clean the netdev.
  332. *
  333. * @note please don't invoke it on ISR.
  334. */
  335. int sal_netdev_cleanup(struct netdev *netdev)
  336. {
  337. uint32_t idx = 0;
  338. int find_dev;
  339. do
  340. {
  341. find_dev = 0;
  342. sal_lock();
  343. for (idx = 0; idx < socket_table.max_socket; idx++)
  344. {
  345. if (socket_table.sockets[idx] && socket_table.sockets[idx]->netdev == netdev)
  346. {
  347. find_dev = 1;
  348. break;
  349. }
  350. }
  351. sal_unlock();
  352. if (find_dev)
  353. {
  354. rt_thread_mdelay(100);
  355. }
  356. }
  357. while (find_dev);
  358. return 0;
  359. }
  360. /**
  361. * This function will initialize sal socket object and set socket options
  362. *
  363. * @param family protocol family
  364. * @param type socket type
  365. * @param protocol transfer Protocol
  366. * @param res sal socket object address
  367. *
  368. * @return 0 : socket initialize success
  369. * -1 : input the wrong family
  370. * -2 : input the wrong socket type
  371. * -3 : get network interface failed
  372. */
  373. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  374. {
  375. struct sal_socket *sock;
  376. struct sal_proto_family *pf;
  377. struct netdev *netdv_def = netdev_default;
  378. struct netdev *netdev = RT_NULL;
  379. rt_bool_t flag = RT_FALSE;
  380. if (family == AF_UNIX)
  381. {
  382. netdv_def = netdev_lo;
  383. }
  384. if (family < 0 || family > AF_MAX)
  385. {
  386. return -1;
  387. }
  388. if (type < 0 || type > SOCK_MAX)
  389. {
  390. return -2;
  391. }
  392. sock = *res;
  393. sock->domain = family;
  394. sock->type = type;
  395. sock->protocol = protocol;
  396. if (netdv_def && netdev_is_up(netdv_def))
  397. {
  398. /* check default network interface device protocol family */
  399. pf = (struct sal_proto_family *) netdv_def->sal_user_data;
  400. if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family))
  401. {
  402. sock->netdev = netdv_def;
  403. flag = RT_TRUE;
  404. }
  405. }
  406. if (flag == RT_FALSE)
  407. {
  408. /* get network interface device by protocol family */
  409. netdev = netdev_get_by_family(family);
  410. if (netdev == RT_NULL)
  411. {
  412. LOG_E("not find network interface device by protocol family(%d).", family);
  413. return -3;
  414. }
  415. sock->netdev = netdev;
  416. }
  417. return 0;
  418. }
  419. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  420. {
  421. int idx;
  422. /* find an empty socket entry */
  423. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  424. {
  425. if (st->sockets[idx] == RT_NULL)
  426. {
  427. break;
  428. }
  429. }
  430. /* allocate a larger sockte container */
  431. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  432. {
  433. int cnt, index;
  434. struct sal_socket **sockets;
  435. /* increase the number of socket with 4 step length */
  436. cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
  437. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  438. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  439. if (sockets == RT_NULL)
  440. goto __result; /* return st->max_socket */
  441. /* clean the new allocated fds */
  442. for (index = st->max_socket; index < cnt; index++)
  443. {
  444. sockets[index] = RT_NULL;
  445. }
  446. st->sockets = sockets;
  447. st->max_socket = cnt;
  448. }
  449. /* allocate 'struct sal_socket' */
  450. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  451. {
  452. st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
  453. if (st->sockets[idx] == RT_NULL)
  454. {
  455. idx = st->max_socket;
  456. }
  457. }
  458. __result:
  459. return idx;
  460. }
  461. static void socket_free(struct sal_socket_table *st, int idx)
  462. {
  463. struct sal_socket *sock;
  464. sock = st->sockets[idx];
  465. st->sockets[idx] = RT_NULL;
  466. rt_free(sock);
  467. }
  468. static int socket_new(void)
  469. {
  470. struct sal_socket *sock;
  471. struct sal_socket_table *st = &socket_table;
  472. int idx;
  473. sal_lock();
  474. /* find an empty sal socket entry */
  475. idx = socket_alloc(st, 0);
  476. /* can't find an empty sal socket entry */
  477. if (idx == (int) st->max_socket)
  478. {
  479. idx = -(1 + SAL_SOCKET_OFFSET);
  480. goto __result;
  481. }
  482. sock = st->sockets[idx];
  483. sock->socket = idx + SAL_SOCKET_OFFSET;
  484. sock->magic = SAL_SOCKET_MAGIC;
  485. sock->netdev = RT_NULL;
  486. sock->user_data = RT_NULL;
  487. #ifdef SAL_USING_TLS
  488. sock->user_data_tls = RT_NULL;
  489. #endif
  490. __result:
  491. sal_unlock();
  492. return idx + SAL_SOCKET_OFFSET;
  493. }
  494. static void socket_delete(int socket)
  495. {
  496. struct sal_socket *sock;
  497. struct sal_socket_table *st = &socket_table;
  498. int idx;
  499. idx = socket - SAL_SOCKET_OFFSET;
  500. if (idx < 0 || idx >= (int) st->max_socket)
  501. {
  502. return;
  503. }
  504. sal_lock();
  505. sock = sal_get_socket(socket);
  506. RT_ASSERT(sock != RT_NULL);
  507. sock->magic = 0;
  508. sock->netdev = RT_NULL;
  509. socket_free(st, idx);
  510. sal_unlock();
  511. }
  512. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  513. {
  514. int new_socket;
  515. struct sal_socket *sock;
  516. struct sal_proto_family *pf;
  517. /* get the socket object by socket descriptor */
  518. SAL_SOCKET_OBJ_GET(sock, socket);
  519. /* check the network interface is up status */
  520. SAL_NETDEV_IS_UP(sock->netdev);
  521. /* check the network interface socket operations */
  522. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, accept);
  523. new_socket = pf->skt_ops->accept((int)(size_t)sock->user_data, addr, addrlen);
  524. if (new_socket != -1)
  525. {
  526. int retval;
  527. int new_sal_socket;
  528. struct sal_socket *new_sock;
  529. /* allocate a new socket structure and registered socket options */
  530. new_sal_socket = socket_new();
  531. new_sock = sal_get_socket(new_sal_socket);
  532. if (new_sock == RT_NULL)
  533. {
  534. pf->skt_ops->closesocket(new_socket);
  535. return -1;
  536. }
  537. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  538. if (retval < 0)
  539. {
  540. pf->skt_ops->closesocket(new_socket);
  541. rt_memset(new_sock, 0x00, sizeof(struct sal_socket));
  542. /* socket init failed, delete socket */
  543. socket_delete(new_sal_socket);
  544. LOG_E("New socket registered failed, return error %d.", retval);
  545. return -1;
  546. }
  547. /* new socket create by accept should have the same netdev with server*/
  548. new_sock->netdev = sock->netdev;
  549. /* socket structure user_data used to store the acquired new socket */
  550. new_sock->user_data = (void *)(size_t)new_socket;
  551. return new_sal_socket;
  552. }
  553. return -1;
  554. }
  555. static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr)
  556. {
  557. const struct sockaddr_in *svr_addr = (const struct sockaddr_in *) name;
  558. #if NETDEV_IPV4 && NETDEV_IPV6
  559. local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr;
  560. local_ipaddr->type = IPADDR_TYPE_V4;
  561. #elif NETDEV_IPV4
  562. local_ipaddr->addr = svr_addr->sin_addr.s_addr;
  563. #elif NETDEV_IPV6
  564. #error "not only support IPV6"
  565. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  566. }
  567. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  568. {
  569. struct sal_socket *sock;
  570. struct sal_proto_family *pf;
  571. struct sockaddr_un *addr_un = RT_NULL;
  572. ip_addr_t input_ipaddr;
  573. RT_ASSERT(name);
  574. /* get the socket object by socket descriptor */
  575. SAL_SOCKET_OBJ_GET(sock, socket);
  576. addr_un = (struct sockaddr_un *)name;
  577. if ((addr_un->sa_family != AF_UNIX) && (addr_un->sa_family != AF_NETLINK))
  578. {
  579. /* bind network interface by ip address */
  580. sal_sockaddr_to_ipaddr(name, &input_ipaddr);
  581. /* check input ipaddr is default netdev ipaddr */
  582. if (!ip_addr_isany_val(input_ipaddr))
  583. {
  584. struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL;
  585. struct netdev *new_netdev = RT_NULL;
  586. new_netdev = netdev_get_by_ipaddr(&input_ipaddr);
  587. if (new_netdev == RT_NULL)
  588. {
  589. return -1;
  590. }
  591. /* get input and local ip address proto_family */
  592. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind);
  593. SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind);
  594. /* check the network interface protocol family type */
  595. if (input_pf->family != local_pf->family)
  596. {
  597. int new_socket = -1;
  598. /* protocol family is different, close old socket and create new socket by input ip address */
  599. local_pf->skt_ops->closesocket(socket);
  600. new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol);
  601. if (new_socket < 0)
  602. {
  603. return -1;
  604. }
  605. sock->netdev = new_netdev;
  606. sock->user_data = (void *)(size_t)new_socket;
  607. }
  608. }
  609. }
  610. /* check and get protocol families by the network interface device */
  611. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind);
  612. return pf->skt_ops->bind((int)(size_t)sock->user_data, name, namelen);
  613. }
  614. int sal_shutdown(int socket, int how)
  615. {
  616. struct sal_socket *sock;
  617. struct sal_proto_family *pf;
  618. int error = 0;
  619. /* get the socket object by socket descriptor */
  620. SAL_SOCKET_OBJ_GET(sock, socket);
  621. /* shutdown operation not need to check network interface status */
  622. /* check the network interface socket opreation */
  623. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, shutdown);
  624. if (pf->skt_ops->shutdown((int)(size_t)sock->user_data, how) == 0)
  625. {
  626. #ifdef SAL_USING_TLS
  627. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  628. {
  629. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  630. {
  631. return -1;
  632. }
  633. }
  634. #endif
  635. error = 0;
  636. }
  637. else
  638. {
  639. error = -1;
  640. }
  641. return error;
  642. }
  643. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  644. {
  645. struct sal_socket *sock;
  646. struct sal_proto_family *pf;
  647. /* get the socket object by socket descriptor */
  648. SAL_SOCKET_OBJ_GET(sock, socket);
  649. /* check the network interface socket opreation */
  650. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getpeername);
  651. return pf->skt_ops->getpeername((int)(size_t)sock->user_data, name, namelen);
  652. }
  653. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  654. {
  655. struct sal_socket *sock;
  656. struct sal_proto_family *pf;
  657. /* get socket object by socket descriptor */
  658. SAL_SOCKET_OBJ_GET(sock, socket);
  659. /* check the network interface socket opreation */
  660. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockname);
  661. return pf->skt_ops->getsockname((int)(size_t)sock->user_data, name, namelen);
  662. }
  663. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  664. {
  665. struct sal_socket *sock;
  666. struct sal_proto_family *pf;
  667. /* get the socket object by socket descriptor */
  668. SAL_SOCKET_OBJ_GET(sock, socket);
  669. /* check the network interface socket opreation */
  670. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockopt);
  671. return pf->skt_ops->getsockopt((int)(size_t)sock->user_data, level, optname, optval, optlen);
  672. }
  673. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  674. {
  675. struct sal_socket *sock;
  676. struct sal_proto_family *pf;
  677. /* get the socket object by socket descriptor */
  678. SAL_SOCKET_OBJ_GET(sock, socket);
  679. /* check the network interface socket opreation */
  680. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, setsockopt);
  681. #ifdef SAL_USING_TLS
  682. if (level == SOL_TLS)
  683. {
  684. switch (optname)
  685. {
  686. case TLS_CRET_LIST:
  687. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen);
  688. break;
  689. case TLS_CIPHERSUITE_LIST:
  690. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen);
  691. break;
  692. case TLS_PEER_VERIFY:
  693. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen);
  694. break;
  695. case TLS_DTLS_ROLE:
  696. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen);
  697. break;
  698. default:
  699. return -1;
  700. }
  701. return 0;
  702. }
  703. else
  704. {
  705. return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  706. }
  707. #else
  708. return pf->skt_ops->setsockopt((int)(size_t)sock->user_data, level, optname, optval, optlen);
  709. #endif /* SAL_USING_TLS */
  710. }
  711. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  712. {
  713. struct sal_socket *sock;
  714. struct sal_proto_family *pf;
  715. int ret;
  716. /* get the socket object by socket descriptor */
  717. SAL_SOCKET_OBJ_GET(sock, socket);
  718. /* check the network interface is up status */
  719. SAL_NETDEV_IS_UP(sock->netdev);
  720. /* check the network interface socket opreation */
  721. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect);
  722. ret = pf->skt_ops->connect((int)(size_t)sock->user_data, name, namelen);
  723. #ifdef SAL_USING_TLS
  724. if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect))
  725. {
  726. if (proto_tls->ops->connect(sock->user_data_tls) < 0)
  727. {
  728. return -1;
  729. }
  730. return ret;
  731. }
  732. #endif
  733. return ret;
  734. }
  735. int sal_listen(int socket, int backlog)
  736. {
  737. struct sal_socket *sock;
  738. struct sal_proto_family *pf;
  739. /* get the socket object by socket descriptor */
  740. SAL_SOCKET_OBJ_GET(sock, socket);
  741. /* check the network interface socket opreation */
  742. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, listen);
  743. return pf->skt_ops->listen((int)(size_t)sock->user_data, backlog);
  744. }
  745. int sal_sendmsg(int socket, const struct msghdr *message, int flags)
  746. {
  747. struct sal_socket *sock;
  748. struct sal_proto_family *pf;
  749. /* get the socket object by socket descriptor */
  750. SAL_SOCKET_OBJ_GET(sock, socket);
  751. /* check the network interface is up status */
  752. SAL_NETDEV_IS_UP(sock->netdev);
  753. /* check the network interface socket opreation */
  754. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendmsg);
  755. #ifdef SAL_USING_TLS
  756. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, sendmsg))
  757. {
  758. int ret;
  759. if ((ret = proto_tls->ops->sendmsg(sock->user_data_tls, message, flags)) < 0)
  760. {
  761. return -1;
  762. }
  763. return ret;
  764. }
  765. else
  766. {
  767. return pf->skt_ops->sendmsg((int)(size_t)sock->user_data, message, flags);
  768. }
  769. #else
  770. return pf->skt_ops->sendmsg((int)(size_t)sock->user_data, message, flags);
  771. #endif
  772. }
  773. int sal_recvmsg(int socket, struct msghdr *message, int flags)
  774. {
  775. struct sal_socket *sock;
  776. struct sal_proto_family *pf;
  777. /* get the socket object by socket descriptor */
  778. SAL_SOCKET_OBJ_GET(sock, socket);
  779. /* check the network interface is up status */
  780. SAL_NETDEV_IS_UP(sock->netdev);
  781. /* check the network interface socket opreation */
  782. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvmsg);
  783. #ifdef SAL_USING_TLS
  784. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recvmsg))
  785. {
  786. int ret;
  787. if ((ret = proto_tls->ops->recvmsg(sock->user_data_tls, message, flags)) < 0)
  788. {
  789. return -1;
  790. }
  791. return ret;
  792. }
  793. else
  794. {
  795. return pf->skt_ops->recvmsg((int)(size_t)sock->user_data, message, flags);
  796. }
  797. #else
  798. return pf->skt_ops->recvmsg((int)(size_t)sock->user_data, message, flags);
  799. #endif
  800. }
  801. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  802. struct sockaddr *from, socklen_t *fromlen)
  803. {
  804. struct sal_socket *sock;
  805. struct sal_proto_family *pf;
  806. /* get the socket object by socket descriptor */
  807. SAL_SOCKET_OBJ_GET(sock, socket);
  808. /* check the network interface is up status */
  809. SAL_NETDEV_IS_UP(sock->netdev);
  810. /* check the network interface socket opreation */
  811. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom);
  812. #ifdef SAL_USING_TLS
  813. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  814. {
  815. int ret;
  816. if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0)
  817. {
  818. return -1;
  819. }
  820. return ret;
  821. }
  822. else
  823. {
  824. return pf->skt_ops->recvfrom((int)(size_t)sock->user_data, mem, len, flags, from, fromlen);
  825. }
  826. #else
  827. return pf->skt_ops->recvfrom((int)(size_t)sock->user_data, mem, len, flags, from, fromlen);
  828. #endif
  829. }
  830. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  831. const struct sockaddr *to, socklen_t tolen)
  832. {
  833. struct sal_socket *sock;
  834. struct sal_proto_family *pf;
  835. /* get the socket object by socket descriptor */
  836. SAL_SOCKET_OBJ_GET(sock, socket);
  837. /* check the network interface is up status */
  838. SAL_NETDEV_IS_UP(sock->netdev);
  839. /* check the network interface socket opreation */
  840. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto);
  841. #ifdef SAL_USING_TLS
  842. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  843. {
  844. int ret;
  845. if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0)
  846. {
  847. return -1;
  848. }
  849. return ret;
  850. }
  851. else
  852. {
  853. return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  854. }
  855. #else
  856. return pf->skt_ops->sendto((int)(size_t)sock->user_data, dataptr, size, flags, to, tolen);
  857. #endif
  858. }
  859. int sal_socket(int domain, int type, int protocol)
  860. {
  861. int retval;
  862. int socket, proto_socket;
  863. struct sal_socket *sock;
  864. struct sal_proto_family *pf;
  865. /* allocate a new socket and registered socket options */
  866. socket = socket_new();
  867. if (socket < 0)
  868. {
  869. return -1;
  870. }
  871. /* get sal socket object by socket descriptor */
  872. sock = sal_get_socket(socket);
  873. if (sock == RT_NULL)
  874. {
  875. socket_delete(socket);
  876. return -1;
  877. }
  878. /* Initialize sal socket object */
  879. retval = socket_init(domain, type, protocol, &sock);
  880. if (retval < 0)
  881. {
  882. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  883. socket_delete(socket);
  884. return -1;
  885. }
  886. /* valid the network interface socket opreation */
  887. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket);
  888. proto_socket = pf->skt_ops->socket(domain, type, protocol);
  889. if (proto_socket >= 0)
  890. {
  891. #ifdef SAL_USING_TLS
  892. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket))
  893. {
  894. sock->user_data_tls = proto_tls->ops->socket(socket);
  895. if (sock->user_data_tls == RT_NULL)
  896. {
  897. socket_delete(socket);
  898. return -1;
  899. }
  900. }
  901. #endif
  902. sock->user_data = (void *)(size_t)proto_socket;
  903. return sock->socket;
  904. }
  905. socket_delete(socket);
  906. return -1;
  907. }
  908. int sal_socketpair(int domain, int type, int protocol, int *fds)
  909. {
  910. int unix_fd[2];
  911. struct sal_socket *socka;
  912. struct sal_socket *sockb;
  913. struct sal_proto_family *pf;
  914. if (domain == AF_UNIX)
  915. {
  916. /* get the socket object by socket descriptor */
  917. SAL_SOCKET_OBJ_GET(socka, fds[0]);
  918. SAL_SOCKET_OBJ_GET(sockb, fds[1]);
  919. /* valid the network interface socket opreation */
  920. SAL_NETDEV_SOCKETOPS_VALID(socka->netdev, pf, socket);
  921. unix_fd[0] = (int)(size_t)socka->user_data;
  922. unix_fd[1] = (int)(size_t)sockb->user_data;
  923. if (pf->skt_ops->socketpair)
  924. {
  925. return pf->skt_ops->socketpair(domain, type, protocol, unix_fd);
  926. }
  927. }
  928. rt_set_errno(EINVAL);
  929. return -1;
  930. }
  931. int sal_closesocket(int socket)
  932. {
  933. struct sal_socket *sock;
  934. struct sal_proto_family *pf;
  935. int error = 0;
  936. /* get the socket object by socket descriptor */
  937. SAL_SOCKET_OBJ_GET(sock, socket);
  938. /* clsoesocket operation not need to vaild network interface status */
  939. /* valid the network interface socket opreation */
  940. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, closesocket);
  941. if (pf->skt_ops->closesocket((int)(size_t)sock->user_data) == 0)
  942. {
  943. #ifdef SAL_USING_TLS
  944. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  945. {
  946. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  947. {
  948. return -1;
  949. }
  950. }
  951. #endif
  952. error = 0;
  953. }
  954. else
  955. {
  956. error = -1;
  957. }
  958. /* delete socket */
  959. socket_delete(socket);
  960. return error;
  961. }
  962. #define ARPHRD_ETHER 1 /* Ethernet 10/100Mbps. */
  963. #define ARPHRD_LOOPBACK 772 /* Loopback device. */
  964. #define IFF_UP 0x1
  965. #define IFF_RUNNING 0x40
  966. #define IFF_NOARP 0x80
  967. int sal_ioctlsocket(int socket, long cmd, void *arg)
  968. {
  969. rt_slist_t *node = RT_NULL;
  970. struct netdev *netdev = RT_NULL;
  971. struct netdev *cur_netdev_list = netdev_list;
  972. struct sal_socket *sock;
  973. struct sal_proto_family *pf;
  974. struct sockaddr_in *addr_in = RT_NULL;
  975. struct sockaddr *addr = RT_NULL;
  976. ip_addr_t input_ipaddr;
  977. /* get the socket object by socket descriptor */
  978. SAL_SOCKET_OBJ_GET(sock, socket);
  979. /* check the network interface socket opreation */
  980. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, ioctlsocket);
  981. struct sal_ifreq *ifr = (struct sal_ifreq *)arg;
  982. if((sock->domain == AF_INET)&&(sock->netdev)&&(ifr != RT_NULL))
  983. {
  984. switch (cmd)
  985. {
  986. case SIOCGIFADDR:
  987. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  988. {
  989. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
  990. #if NETDEV_IPV4 && NETDEV_IPV6
  991. addr_in->sin_addr.s_addr = sock->netdev->ip_addr.u_addr.ip4.addr;
  992. #elif NETDEV_IPV4
  993. addr_in->sin_addr.s_addr = sock->netdev->ip_addr.addr;
  994. #elif NETDEV_IPV6
  995. #error "not only support IPV6"
  996. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  997. return 0;
  998. }
  999. else
  1000. {
  1001. if (cur_netdev_list == RT_NULL)
  1002. {
  1003. LOG_E("ifconfig: network interface device list error.\n");
  1004. return -1;
  1005. }
  1006. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1007. {
  1008. netdev = rt_list_entry(node, struct netdev, list);
  1009. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1010. {
  1011. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
  1012. #if NETDEV_IPV4 && NETDEV_IPV6
  1013. addr_in->sin_addr.s_addr = netdev->ip_addr.u_addr.ip4.addr;
  1014. #elif NETDEV_IPV4
  1015. addr_in->sin_addr.s_addr = netdev->ip_addr.addr;
  1016. #elif NETDEV_IPV6
  1017. #error "Do not only support IPV6"
  1018. #endif /* NETDEV_IPV4 && NETDEV_IPV6 */
  1019. return 0;
  1020. }
  1021. }
  1022. return -1;
  1023. }
  1024. case SIOCSIFADDR:
  1025. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1026. {
  1027. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_addr);
  1028. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1029. netdev_set_ipaddr(sock->netdev, &input_ipaddr);
  1030. return 0;
  1031. }
  1032. else
  1033. {
  1034. if (cur_netdev_list == RT_NULL)
  1035. {
  1036. LOG_E("ifconfig: network interface device list error.\n");
  1037. return -1;
  1038. }
  1039. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1040. {
  1041. netdev = rt_list_entry(node, struct netdev, list);
  1042. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1043. {
  1044. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_addr);
  1045. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1046. netdev_set_ipaddr(netdev, &input_ipaddr);
  1047. return 0;
  1048. }
  1049. }
  1050. return -1;
  1051. }
  1052. case SIOCGIFNETMASK:
  1053. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1054. {
  1055. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
  1056. #if NETDEV_IPV4 && NETDEV_IPV6
  1057. addr_in->sin_addr.s_addr = sock->netdev->netmask.u_addr.ip4.addr;
  1058. #elif NETDEV_IPV4
  1059. addr_in->sin_addr.s_addr = sock->netdev->netmask.addr;
  1060. #elif NETDEV_IPV6
  1061. #error "not only support IPV6"
  1062. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  1063. return 0;
  1064. }
  1065. else
  1066. {
  1067. if (cur_netdev_list == RT_NULL)
  1068. {
  1069. LOG_E("ifconfig: network interface device list error.\n");
  1070. return -1;
  1071. }
  1072. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1073. {
  1074. netdev = rt_list_entry(node, struct netdev, list);
  1075. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1076. {
  1077. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
  1078. #if NETDEV_IPV4 && NETDEV_IPV6
  1079. addr_in->sin_addr.s_addr = netdev->netmask.u_addr.ip4.addr;
  1080. #elif NETDEV_IPV4
  1081. addr_in->sin_addr.s_addr = netdev->netmask.addr;
  1082. #elif NETDEV_IPV6
  1083. #error "not only support IPV6"
  1084. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  1085. return 0;
  1086. }
  1087. }
  1088. return -1;
  1089. }
  1090. case SIOCSIFNETMASK:
  1091. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1092. {
  1093. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_netmask);
  1094. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1095. netdev_set_netmask(sock->netdev, &input_ipaddr);
  1096. return 0;
  1097. }
  1098. else
  1099. {
  1100. if (cur_netdev_list == RT_NULL)
  1101. {
  1102. LOG_E("ifconfig: network interface device list error.\n");
  1103. return -1;
  1104. }
  1105. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1106. {
  1107. netdev = rt_list_entry(node, struct netdev, list);
  1108. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1109. {
  1110. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_netmask);
  1111. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1112. netdev_set_netmask(netdev, &input_ipaddr);
  1113. return 0;
  1114. }
  1115. }
  1116. return -1;
  1117. }
  1118. case SIOCGIFHWADDR:
  1119. if (!strcmp(ifr->ifr_ifrn.ifrn_name,sock->netdev->name))
  1120. {
  1121. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_hwaddr);
  1122. #ifdef RT_USING_LWP
  1123. if (!strcmp("lo", sock->netdev->name))
  1124. {
  1125. struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1126. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
  1127. }
  1128. else
  1129. {
  1130. struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1131. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  1132. }
  1133. #endif
  1134. rt_memcpy(addr->sa_data, sock->netdev->hwaddr, sock->netdev->hwaddr_len);
  1135. return 0;
  1136. }
  1137. else
  1138. {
  1139. if (cur_netdev_list == RT_NULL)
  1140. {
  1141. LOG_E("ifconfig: network interface device list error.\n");
  1142. return -1;
  1143. }
  1144. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1145. {
  1146. netdev = rt_list_entry(node, struct netdev, list);
  1147. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1148. {
  1149. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_hwaddr);
  1150. #ifdef RT_USING_LWP
  1151. if (!strcmp("lo", netdev->name))
  1152. {
  1153. struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1154. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
  1155. }
  1156. else
  1157. {
  1158. struct musl_ifreq * musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1159. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  1160. }
  1161. #endif
  1162. rt_memcpy(addr->sa_data, netdev->hwaddr, netdev->hwaddr_len);
  1163. return 0;
  1164. }
  1165. }
  1166. return -1;
  1167. }
  1168. case SIOCGIFMTU:
  1169. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1170. {
  1171. ifr->ifr_ifru.ifru_mtu = sock->netdev->mtu;
  1172. return 0;
  1173. }
  1174. else
  1175. {
  1176. if (cur_netdev_list == RT_NULL)
  1177. {
  1178. LOG_E("ifconfig: network interface device list error.\n");
  1179. return -1;
  1180. }
  1181. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1182. {
  1183. netdev = rt_list_entry(node, struct netdev, list);
  1184. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1185. {
  1186. ifr->ifr_ifru.ifru_mtu = netdev->mtu;
  1187. return 0;
  1188. }
  1189. }
  1190. return -1;
  1191. }
  1192. case SIOCGIFFLAGS:
  1193. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1194. {
  1195. uint16_t flags_tmp = 0;
  1196. if (sock->netdev->flags & NETDEV_FLAG_UP)
  1197. flags_tmp = flags_tmp | IFF_UP;
  1198. if (!(sock->netdev->flags & NETDEV_FLAG_ETHARP))
  1199. flags_tmp = flags_tmp | IFF_NOARP;
  1200. flags_tmp = flags_tmp | IFF_RUNNING;
  1201. ifr->ifr_ifru.ifru_flags = flags_tmp;
  1202. return 0;
  1203. }
  1204. else
  1205. {
  1206. if (cur_netdev_list == RT_NULL)
  1207. {
  1208. LOG_E("ifconfig: network interface device list error.\n");
  1209. return -1;
  1210. }
  1211. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1212. {
  1213. netdev = rt_list_entry(node, struct netdev, list);
  1214. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1215. {
  1216. uint16_t flags_tmp = 0;
  1217. if (netdev->flags & NETDEV_FLAG_UP)
  1218. flags_tmp = flags_tmp | IFF_UP;
  1219. if (!(netdev->flags & NETDEV_FLAG_ETHARP))
  1220. flags_tmp = flags_tmp | IFF_NOARP;
  1221. ifr->ifr_ifru.ifru_flags = flags_tmp;
  1222. return 0;
  1223. }
  1224. }
  1225. return -1;
  1226. }
  1227. case SIOCSIFFLAGS:
  1228. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1229. {
  1230. netdev = rt_list_entry(node, struct netdev, list);
  1231. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1232. {
  1233. if ((ifr->ifr_ifru.ifru_flags & IFF_UP) == 0)
  1234. {
  1235. netdev_set_down(netdev);
  1236. }
  1237. else
  1238. {
  1239. netdev_set_up(netdev);
  1240. }
  1241. return 0;
  1242. }
  1243. }
  1244. return -1;
  1245. case SIOCGIFCONF:
  1246. {
  1247. struct ifconf *ifconf_tmp;
  1248. ifconf_tmp = (struct ifconf *)arg;
  1249. int count_size = 0;
  1250. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1251. {
  1252. struct sal_ifreq sal_ifreq_temp;
  1253. count_size++;
  1254. netdev = rt_list_entry(node, struct netdev, list);
  1255. rt_strcpy(sal_ifreq_temp.ifr_ifrn.ifrn_name, netdev->name);
  1256. rt_memcpy(ifconf_tmp->ifc_ifcu.ifcu_buf, &sal_ifreq_temp, sizeof(struct sal_ifreq));
  1257. ifconf_tmp->ifc_ifcu.ifcu_buf += sizeof(struct sal_ifreq);
  1258. }
  1259. ifconf_tmp->ifc_len = sizeof(struct sal_ifreq) * count_size;
  1260. ifconf_tmp->ifc_ifcu.ifcu_buf = ifconf_tmp->ifc_ifcu.ifcu_buf - sizeof(struct sal_ifreq) * count_size;
  1261. return 0;
  1262. }
  1263. default:
  1264. break;
  1265. }
  1266. }
  1267. return pf->skt_ops->ioctlsocket((int)(size_t)sock->user_data, cmd, arg);
  1268. }
  1269. #ifdef SAL_USING_POSIX
  1270. int sal_poll(struct dfs_file *file, struct rt_pollreq *req)
  1271. {
  1272. struct sal_socket *sock;
  1273. struct sal_proto_family *pf;
  1274. int socket = (int)(size_t)file->vnode->data;
  1275. /* get the socket object by socket descriptor */
  1276. SAL_SOCKET_OBJ_GET(sock, socket);
  1277. /* check the network interface is up status */
  1278. SAL_NETDEV_IS_UP(sock->netdev);
  1279. /* check the network interface socket opreation */
  1280. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll);
  1281. return pf->skt_ops->poll(file, req);
  1282. }
  1283. #endif
  1284. struct hostent *sal_gethostbyname(const char *name)
  1285. {
  1286. struct netdev *netdev = netdev_default;
  1287. struct sal_proto_family *pf;
  1288. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  1289. {
  1290. return pf->netdb_ops->gethostbyname(name);
  1291. }
  1292. else
  1293. {
  1294. /* get the first network interface device with up status */
  1295. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1296. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  1297. {
  1298. return pf->netdb_ops->gethostbyname(name);
  1299. }
  1300. }
  1301. return RT_NULL;
  1302. }
  1303. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  1304. size_t buflen, struct hostent **result, int *h_errnop)
  1305. {
  1306. struct netdev *netdev = netdev_default;
  1307. struct sal_proto_family *pf;
  1308. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  1309. {
  1310. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  1311. }
  1312. else
  1313. {
  1314. /* get the first network interface device with up status */
  1315. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1316. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  1317. {
  1318. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  1319. }
  1320. }
  1321. return -1;
  1322. }
  1323. int sal_getaddrinfo(const char *nodename,
  1324. const char *servname,
  1325. const struct addrinfo *hints,
  1326. struct addrinfo **res)
  1327. {
  1328. struct netdev *netdev = netdev_default;
  1329. struct sal_proto_family *pf;
  1330. int ret = 0;
  1331. rt_uint32_t i = 0;
  1332. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  1333. {
  1334. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  1335. }
  1336. else
  1337. {
  1338. /* get the first network interface device with up status */
  1339. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1340. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  1341. {
  1342. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  1343. }
  1344. else
  1345. {
  1346. ret = -1;
  1347. }
  1348. }
  1349. if(ret == RT_EOK)
  1350. {
  1351. /*record the netdev and res*/
  1352. for(i = 0; i < SAL_SOCKETS_NUM; i++)
  1353. {
  1354. if(sal_dev_res_tbl[i].res == RT_NULL)
  1355. {
  1356. sal_dev_res_tbl[i].res = *res;
  1357. sal_dev_res_tbl[i].netdev = netdev;
  1358. break;
  1359. }
  1360. }
  1361. RT_ASSERT((i < SAL_SOCKETS_NUM));
  1362. }
  1363. return ret;
  1364. }
  1365. void sal_freeaddrinfo(struct addrinfo *ai)
  1366. {
  1367. struct netdev *netdev = RT_NULL;
  1368. struct sal_proto_family *pf = RT_NULL;
  1369. rt_uint32_t i = 0;
  1370. /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
  1371. for(i = 0; i < SAL_SOCKETS_NUM; i++)
  1372. {
  1373. if(sal_dev_res_tbl[i].res == ai)
  1374. {
  1375. netdev = sal_dev_res_tbl[i].netdev;
  1376. sal_dev_res_tbl[i].res = RT_NULL;
  1377. sal_dev_res_tbl[i].netdev = RT_NULL;
  1378. break;
  1379. }
  1380. }
  1381. RT_ASSERT((i < SAL_SOCKETS_NUM));
  1382. if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
  1383. {
  1384. pf->netdb_ops->freeaddrinfo(ai);
  1385. }
  1386. }